diff options
author | Kevin Decker <kpdecker@gmail.com> | 2012-08-21 10:51:14 -0700 |
---|---|---|
committer | Kevin Decker <kpdecker@gmail.com> | 2012-08-21 10:51:14 -0700 |
commit | 89f5ab8aaf8c6017d4a78b2bce719be0d99f82bd (patch) | |
tree | 8ef9806dbd558719dad3f90a344230bb416a70a2 | |
parent | 6761d4c6d16893f1a347d69956f926f56124e841 (diff) | |
parent | dc0426d836027b611b61ebdf0a491e9e6ab889db (diff) | |
download | handlebars.js-89f5ab8aaf8c6017d4a78b2bce719be0d99f82bd.zip handlebars.js-89f5ab8aaf8c6017d4a78b2bce719be0d99f82bd.tar.gz handlebars.js-89f5ab8aaf8c6017d4a78b2bce719be0d99f82bd.tar.bz2 |
Merge pull request #277 from kpdecker/mocha-tests
Mocha tests
Add and fix node tests running qunit_spec under mocha and revs the node package to the latest.
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | .jshintrc | 2 | ||||
-rw-r--r-- | lib/handlebars/base.js | 6 | ||||
-rw-r--r-- | package.json | 14 | ||||
-rw-r--r-- | spec/acceptance_spec.rb | 2 | ||||
-rw-r--r-- | spec/qunit_spec.js | 123 |
6 files changed, 90 insertions, 59 deletions
@@ -4,3 +4,5 @@ vendor .DS_Store lib/handlebars/compiler/parser.js node_modules +*.sublime-project +*.sublime-workspace @@ -10,7 +10,9 @@ "ember_deprecate", "ember_deprecateFunc", "require", + "suite", "equal", + "equals", "test", "testBoth", "raises", diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 659295c..ca4b159 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -3,7 +3,7 @@ /*jshint eqnull:true*/ this.Handlebars = {}; -(function() { +(function(Handlebars) { Handlebars.VERSION = "1.0.rc.1"; @@ -111,9 +111,9 @@ Handlebars.registerHelper('log', function(context) { Handlebars.log(context); }); -}()); +}(this.Handlebars)); // END(BROWSER) -module.exports = Handlebars; +module.exports = this.Handlebars; diff --git a/package.json b/package.json index 321f3ca..d838894 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "handlebars", "description": "Extension of the Mustache logicless template language", - "version": "1.0.5beta", + "version": "1.0.6beta", "homepage": "http://www.handlebarsjs.com/", "keywords": [ "handlebars mustache template html" @@ -17,9 +17,15 @@ "optimist": "~0.3", "uglify-js": "~1.2" }, - "devDependencies": {}, + "devDependencies": { + "mocha": "*" + }, "main": "lib/handlebars.js", "bin": { "handlebars": "bin/handlebars" - } -}
\ No newline at end of file + }, + "scripts": { + "test": "node_modules/.bin/mocha -u qunit spec/qunit_spec.js" + }, + "optionalDependencies": {} +} diff --git a/spec/acceptance_spec.rb b/spec/acceptance_spec.rb index 9d98ab4..03b23c5 100644 --- a/spec/acceptance_spec.rb +++ b/spec/acceptance_spec.rb @@ -77,7 +77,7 @@ Module.new do js_context["equal"] = js_context["equals"] - js_context["module"] = proc do |this, name| + js_context["suite"] = proc do |this, name| test_context.module(name) end diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 3860c7f..cbbc138 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -1,4 +1,28 @@ -module("basic context"); +var Handlebars; +if (!Handlebars) { + // Setup for Node package testing + Handlebars = require('../lib/handlebars'); + + var assert = require("assert"), + + equal = assert.equal, + equals = assert.equal, + ok = assert.ok; + + // Note that this doesn't have the same context separation as the rspec test. + // Both should be run for full acceptance of the two libary modes. + var CompilerContext = { + compile: function(template, options) { + var templateSpec = Handlebars.precompile(template, options); + return Handlebars.template(eval('(' + templateSpec + ')')); + }, + compileWithPartial: function(template, options) { + return Handlebars.compile(template, options); + } + }; +} + +suite("basic context"); Handlebars.registerHelper('helperMissing', function(helper, context) { if(helper === "link_to") { @@ -6,13 +30,13 @@ Handlebars.registerHelper('helperMissing', function(helper, context) { } }); -var shouldCompileTo = function(string, hashOrArray, expected, message) { +function shouldCompileTo(string, hashOrArray, expected, message) { shouldCompileToWithPartials(string, hashOrArray, false, expected, message); -}; -var shouldCompileToWithPartials = function(string, hashOrArray, partials, expected, message) { +} +function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) { var template = CompilerContext[partials ? 'compileWithPartial' : 'compile'](string), ary; if(Object.prototype.toString.call(hashOrArray) === "[object Array]") { - helpers = hashOrArray[1]; + var helpers = hashOrArray[1]; if(helpers) { for(var prop in Handlebars.helpers) { @@ -27,11 +51,11 @@ var shouldCompileToWithPartials = function(string, hashOrArray, partials, expect ary = [hashOrArray]; } - result = template.apply(this, ary); + var result = template.apply(this, ary); equal(result, expected, "'" + expected + "' should === '" + result + "': " + message); -}; +} -var shouldThrow = function(fn, exception, message) { +function shouldThrow(fn, exception, message) { var caught = false; try { fn(); @@ -158,7 +182,7 @@ test("this keyword in paths", function() { shouldCompileTo(string, hash, "goodbyeGoodbyeGOODBYE", "This keyword in paths evaluates to current context"); - string = "{{#hellos}}{{this/text}}{{/hellos}}" + string = "{{#hellos}}{{this/text}}{{/hellos}}"; hash = {hellos: [{text: "hello"}, {text: "Hello"}, {text: "HELLO"}]}; shouldCompileTo(string, hash, "helloHelloHELLO", "This keyword evaluates in more complex paths"); }); @@ -177,7 +201,7 @@ test("this keyword in helpers", function() { shouldCompileTo(string, [hash, helpers], "bar hellobar Hellobar HELLO", "This keyword evaluates in more complex paths"); }); -module("inverted sections"); +suite("inverted sections"); test("inverted sections with unset value", function() { var string = "{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}"; @@ -197,10 +221,10 @@ test("inverted section with empty set", function() { shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when value is empty set."); }); -module("blocks"); +suite("blocks"); test("array", function() { - var string = "{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!" + var string = "{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!"; var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!", "Arrays iterate over the contents when not empty"); @@ -211,7 +235,7 @@ test("array", function() { }); test("empty block", function() { - var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!" + var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!"; var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; shouldCompileTo(string, hash, "cruel world!", "Arrays iterate over the contents when not empty"); @@ -284,7 +308,7 @@ test("block helper", function() { var string = "{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!"; var template = CompilerContext.compile(string); - result = template({world: "world"}, { helpers: {goodbyes: function(options) { return options.fn({text: "GOODBYE"}); }}}); + var result = template({world: "world"}, { helpers: {goodbyes: function(options) { return options.fn({text: "GOODBYE"}); }}}); equal(result, "GOODBYE! cruel world!", "Block helper executed"); }); @@ -292,7 +316,7 @@ test("block helper staying in the same context", function() { var string = "{{#form}}<p>{{name}}</p>{{/form}}"; var template = CompilerContext.compile(string); - result = template({name: "Yehuda"}, {helpers: {form: function(options) { return "<form>" + options.fn(this) + "</form>"; } }}); + var result = template({name: "Yehuda"}, {helpers: {form: function(options) { return "<form>" + options.fn(this) + "</form>"; } }}); equal(result, "<form><p>Yehuda</p></form>", "Block helper executed with current context"); }); @@ -317,7 +341,7 @@ test("block helper passing a new context", function() { var string = "{{#form yehuda}}<p>{{name}}</p>{{/form}}"; var template = CompilerContext.compile(string); - result = template({yehuda: {name: "Yehuda"}}, { helpers: {form: function(context, options) { return "<form>" + options.fn(context) + "</form>"; }}}); + var result = template({yehuda: {name: "Yehuda"}}, { helpers: {form: function(context, options) { return "<form>" + options.fn(context) + "</form>"; }}}); equal(result, "<form><p>Yehuda</p></form>", "Context variable resolved"); }); @@ -325,7 +349,7 @@ test("block helper passing a complex path context", function() { var string = "{{#form yehuda/cat}}<p>{{name}}</p>{{/form}}"; var template = CompilerContext.compile(string); - result = template({yehuda: {name: "Yehuda", cat: {name: "Harold"}}}, { helpers: {form: function(context, options) { return "<form>" + options.fn(context) + "</form>"; }}}); + var result = template({yehuda: {name: "Yehuda", cat: {name: "Harold"}}}, { helpers: {form: function(context, options) { return "<form>" + options.fn(context) + "</form>"; }}}); equal(result, "<form><p>Harold</p></form>", "Complex path variable resolved"); }); @@ -333,7 +357,7 @@ test("nested block helpers", function() { var string = "{{#form yehuda}}<p>{{name}}</p>{{#link}}Hello{{/link}}{{/form}}"; var template = CompilerContext.compile(string); - result = template({ + var result = template({ yehuda: {name: "Yehuda" } }, { helpers: { @@ -387,7 +411,7 @@ test("block helper inverted sections", function() { shouldCompileTo(messageString, [rootMessage, { list: list }], "<p>Nobody's here</p>", "the context of an inverse is the parent of the block"); }); -module("helpers hash"); +suite("helpers hash"); test("providing a helpers hash", function() { shouldCompileTo("Goodbye {{cruel}} {{world}}!", [{cruel: "cruel"}, {world: function() { return "world"; }}], "Goodbye cruel world!", @@ -405,7 +429,7 @@ test("the helpers hash is available is nested contexts", function() { }); -module("partials"); +suite("partials"); test("basic partials", function() { var string = "Dudes: {{#dudes}}{{> dude}}{{/dudes}}"; @@ -441,9 +465,6 @@ test("rendering undefined partial throws an exception", function() { test("rendering template partial in vm mode throws an exception", function() { shouldThrow(function() { var template = CompilerContext.compile("{{> whatever}}"); - var string = "Dudes: {{>dude}} {{another_dude}}"; - var dude = "{{name}}"; - var hash = {name:"Jeepers", another_dude:"Creepers"}; template(); }, Handlebars.Exception, "Should throw exception"); }); @@ -472,7 +493,7 @@ test("Partials with literal paths", function() { shouldCompileToWithPartials(string, [hash, {}, {dude:dude}], true, "Dudes: Jeepers", "Partials can use literal paths"); }); -module("String literal parameters"); +suite("String literal parameters"); test("simple literals work", function() { var string = 'Message: {{hello "world" 12 true false}}'; @@ -482,7 +503,7 @@ test("simple literals work", function() { if(typeof bool1 !== 'boolean') { bool1 = "NaB"; } if(typeof bool2 !== 'boolean') { bool2 = "NaB"; } return "Hello " + param + " " + times + " times: " + bool1 + " " + bool2; - }} + }}; shouldCompileTo(string, [hash, helpers], "Message: Hello world 12 times: true false", "template with a simple String literal"); }); @@ -495,8 +516,8 @@ test("using a quote in the middle of a parameter raises an error", function() { test("escaping a String is possible", function(){ var string = 'Message: {{{hello "\\"world\\""}}}'; - var hash = {} - var helpers = {hello: function(param) { return "Hello " + param; }} + var hash = {}; + var helpers = {hello: function(param) { return "Hello " + param; }}; shouldCompileTo(string, [hash, helpers], "Message: Hello \"world\"", "template with an escaped String literal"); }); @@ -507,7 +528,7 @@ test("it works with ' marks", function() { shouldCompileTo(string, [hash, helpers], "Message: Hello Alan's world", "template with a ' mark"); }); -module("multiple parameters"); +suite("multiple parameters"); test("simple multi-params work", function() { var string = 'Message: {{goodbye cruel world}}'; @@ -525,7 +546,7 @@ test("block multi-params work", function() { shouldCompileTo(string, [hash, helpers], "Message: Goodbye cruel world", "block helpers with multiple params"); }); -module("safestring"); +suite("safestring"); test("constructing a safestring from a string and checking its type", function() { var safe = new Handlebars.SafeString("testing 1, 2, 3"); @@ -533,7 +554,7 @@ test("constructing a safestring from a string and checking its type", function() equal(safe, "testing 1, 2, 3", "SafeString is equivalent to its underlying string"); }); -module("helperMissing"); +suite("helperMissing"); test("if a context is not found, helperMissing is used", function() { var string = "{{hello}} {{link_to world}}"; @@ -542,47 +563,47 @@ test("if a context is not found, helperMissing is used", function() { shouldCompileTo(string, context, "Hello <a>world</a>"); }); -module("knownHelpers"); +suite("knownHelpers"); test("Known helper should render helper", function() { - var template = CompilerContext.compile("{{hello}}", {knownHelpers: {"hello" : true}}) + var template = CompilerContext.compile("{{hello}}", {knownHelpers: {"hello" : true}}); var result = template({}, {helpers: {hello: function() { return "foo"; }}}); equal(result, "foo", "'foo' should === '" + result); }); test("Unknown helper in knownHelpers only mode should be passed as undefined", function() { - var template = CompilerContext.compile("{{typeof hello}}", {knownHelpers: {'typeof': true}, knownHelpersOnly: true}) + var template = CompilerContext.compile("{{typeof hello}}", {knownHelpers: {'typeof': true}, knownHelpersOnly: true}); var result = template({}, {helpers: {'typeof': function(arg) { return typeof arg; }, hello: function() { return "foo"; }}}); equal(result, "undefined", "'undefined' should === '" + result); }); test("Builtin helpers available in knownHelpers only mode", function() { - var template = CompilerContext.compile("{{#unless foo}}bar{{/unless}}", {knownHelpersOnly: true}) + var template = CompilerContext.compile("{{#unless foo}}bar{{/unless}}", {knownHelpersOnly: true}); var result = template({}); equal(result, "bar", "'bar' should === '" + result); }); test("Field lookup works in knownHelpers only mode", function() { - var template = CompilerContext.compile("{{foo}}", {knownHelpersOnly: true}) + var template = CompilerContext.compile("{{foo}}", {knownHelpersOnly: true}); var result = template({foo: 'bar'}); equal(result, "bar", "'bar' should === '" + result); }); test("Conditional blocks work in knownHelpers only mode", function() { - var template = CompilerContext.compile("{{#foo}}bar{{/foo}}", {knownHelpersOnly: true}) + var template = CompilerContext.compile("{{#foo}}bar{{/foo}}", {knownHelpersOnly: true}); var result = template({foo: 'baz'}); equal(result, "bar", "'bar' should === '" + result); }); test("Invert blocks work in knownHelpers only mode", function() { - var template = CompilerContext.compile("{{^foo}}bar{{/foo}}", {knownHelpersOnly: true}) + var template = CompilerContext.compile("{{^foo}}bar{{/foo}}", {knownHelpersOnly: true}); var result = template({foo: false}); equal(result, "bar", "'bar' should === '" + result); }); -module("blockHelperMissing"); +suite("blockHelperMissing"); test("lambdas are resolved by blockHelperMissing, not handlebars proper", function() { var string = "{{#truthy}}yep{{/truthy}}"; @@ -591,7 +612,7 @@ test("lambdas are resolved by blockHelperMissing, not handlebars proper", functi }); var teardown; -module("built-in helpers", { +suite("built-in helpers", { setup: function(){ teardown = null; }, teardown: function(){ if (teardown) { teardown(); } } }); @@ -619,13 +640,13 @@ test("if", function() { test("if with function argument", function() { var string = "{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!"; - shouldCompileTo(string, {goodbye: function() {return true}, world: "world"}, "GOODBYE cruel world!", + shouldCompileTo(string, {goodbye: function() {return true;}, world: "world"}, "GOODBYE cruel world!", "if with function shows the contents when function returns true"); - shouldCompileTo(string, {goodbye: function() {return this.world}, world: "world"}, "GOODBYE cruel world!", + shouldCompileTo(string, {goodbye: function() {return this.world;}, world: "world"}, "GOODBYE cruel world!", "if with function shows the contents when function returns string"); - shouldCompileTo(string, {goodbye: function() {return false}, world: "world"}, "cruel world!", + shouldCompileTo(string, {goodbye: function() {return false;}, world: "world"}, "cruel world!", "if with function does not show the contents when returns false"); - shouldCompileTo(string, {goodbye: function() {return this.foo}, world: "world"}, "cruel world!", + shouldCompileTo(string, {goodbye: function() {return this.foo;}, world: "world"}, "cruel world!", "if with function does not show the contents when returns undefined"); }); @@ -649,13 +670,13 @@ test("each with @index", function() { }); test("log", function() { - var string = "{{log blah}}" + var string = "{{log blah}}"; var hash = { blah: "whee" }; var logArg; var originalLog = Handlebars.log; - Handlebars.log = function(arg){ logArg = arg; } - teardown = function(){ Handlebars.log = originalLog; } + Handlebars.log = function(arg){ logArg = arg; }; + teardown = function(){ Handlebars.log = originalLog; }; shouldCompileTo(string, hash, "", "log should not display"); equals("whee", logArg, "should call log with 'whee'"); @@ -1025,8 +1046,8 @@ test("block helpers can take an optional hash with booleans", function() { var result = template({}, {helpers: helpers}); equals(result, "GOODBYE CRUEL world", "Boolean hash parameter honored"); - var template = CompilerContext.compile('{{#goodbye cruel="CRUEL" print=false}}world{{/goodbye}}'); - var result = template({}, {helpers: helpers}); + template = CompilerContext.compile('{{#goodbye cruel="CRUEL" print=false}}world{{/goodbye}}'); + result = template({}, {helpers: helpers}); equals(result, "NOT PRINTING", "Boolean hash parameter honored"); }); @@ -1107,7 +1128,7 @@ test("when inside a block in String mode, .. passes the appropriate context in t equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke wot", "Proper context variable output"); }); -module("Regressions") +suite("Regressions"); test("GH-94: Cannot read property of undefined", function() { var data = {"books":[{"title":"The origin of species","author":{"name":"Charles Darwin"}},{"title":"Lazarillo de Tormes"}]}; @@ -1126,13 +1147,13 @@ test("GH-150: Inverted sections print when they shouldn't", function() { }); test("Mustache man page", function() { - var string = "Hello {{name}}. You have just won ${{value}}!{{#in_ca}} Well, ${{taxed_value}}, after taxes.{{/in_ca}}" + var string = "Hello {{name}}. You have just won ${{value}}!{{#in_ca}} Well, ${{taxed_value}}, after taxes.{{/in_ca}}"; var data = { "name": "Chris", "value": 10000, "taxed_value": 10000 - (10000 * 0.4), "in_ca": true - } + }; shouldCompileTo(string, data, "Hello Chris. You have just won $10000! Well, $6000, after taxes.", "the hello world mustache example works"); }); |