summaryrefslogtreecommitdiffstats
path: root/spec/qunit_spec.js
diff options
context:
space:
mode:
authorTommy Messbauer <tommy@vast.com>2012-11-26 10:26:15 -0600
committerTommy Messbauer <tommy@vast.com>2012-11-26 10:26:15 -0600
commitdb975b42a0ae295b6716700900a4724d97f1d9c6 (patch)
tree9d33e5c1ab8c279698cb58894711ac6f1282ec67 /spec/qunit_spec.js
parent79632184953284603b64a3179d087ebcda9d0ab6 (diff)
parentbd0490145438e8f9df05abd2f4c25687bac81326 (diff)
downloadhandlebars.js-db975b42a0ae295b6716700900a4724d97f1d9c6.zip
handlebars.js-db975b42a0ae295b6716700900a4724d97f1d9c6.tar.gz
handlebars.js-db975b42a0ae295b6716700900a4724d97f1d9c6.tar.bz2
Merged upstream master and ran unit tests
Diffstat (limited to 'spec/qunit_spec.js')
-rw-r--r--spec/qunit_spec.js75
1 files changed, 68 insertions, 7 deletions
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js
index cbbc138..5c94253 100644
--- a/spec/qunit_spec.js
+++ b/spec/qunit_spec.js
@@ -33,7 +33,13 @@ Handlebars.registerHelper('helperMissing', function(helper, context) {
function shouldCompileTo(string, hashOrArray, expected, message) {
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
}
+
function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) {
+ var result = compileWithPartials(string, hashOrArray, partials);
+ equal(result, expected, "'" + expected + "' should === '" + result + "': " + message);
+}
+
+function compileWithPartials(string, hashOrArray, partials) {
var template = CompilerContext[partials ? 'compileWithPartial' : 'compile'](string), ary;
if(Object.prototype.toString.call(hashOrArray) === "[object Array]") {
var helpers = hashOrArray[1];
@@ -51,18 +57,31 @@ function shouldCompileToWithPartials(string, hashOrArray, partials, expected, me
ary = [hashOrArray];
}
- var result = template.apply(this, ary);
- equal(result, expected, "'" + expected + "' should === '" + result + "': " + message);
+ return template.apply(this, ary);
}
function shouldThrow(fn, exception, message) {
- var caught = false;
+ var caught = false,
+ exType, exMessage;
+
+ if (exception instanceof Array) {
+ exType = exception[0];
+ exMessage = exception[1];
+ } else if (typeof exception === 'string') {
+ exType = Error;
+ exMessage = exception;
+ } else {
+ exType = exception;
+ }
+
try {
fn();
}
catch (e) {
- if (e instanceof exception) {
- caught = true;
+ if (e instanceof exType) {
+ if (!exMessage || e.message === exMessage) {
+ caught = true;
+ }
}
}
@@ -123,6 +142,8 @@ test("escaping expressions", function() {
shouldCompileTo("{{awesome}}", {awesome: "&\"'`\\<>"}, '&amp;&quot;&#x27;&#x60;\\&lt;&gt;',
"by default expressions should be escaped");
+ shouldCompileTo("{{awesome}}", {awesome: "Escaped, <b> looks like: &lt;b&gt;"}, 'Escaped, &lt;b&gt; looks like: &amp;lt;b&amp;gt;',
+ "escaping should properly handle amperstands");
});
test("functions returning safestrings shouldn't be escaped", function() {
@@ -134,10 +155,14 @@ test("functions returning safestrings shouldn't be escaped", function() {
test("functions", function() {
shouldCompileTo("{{awesome}}", {awesome: function() { return "Awesome"; }}, "Awesome",
"functions are called and render their output");
+ shouldCompileTo("{{awesome}}", {awesome: function() { return this.more; }, more: "More awesome"}, "More awesome",
+ "functions are bound to the context");
});
test("paths with hyphens", function() {
shouldCompileTo("{{foo-bar}}", {"foo-bar": "baz"}, "baz", "Paths can contain hyphens (-)");
+ shouldCompileTo("{{foo.foo-bar}}", {foo: {"foo-bar": "baz"}}, "baz", "Paths can contain hyphens (-)");
+ shouldCompileTo("{{foo/foo-bar}}", {foo: {"foo-bar": "baz"}}, "baz", "Paths can contain hyphens (-)");
});
test("nested paths", function() {
@@ -234,6 +259,16 @@ test("array", function() {
});
+test("array with @index", function() {
+ var string = "{{#goodbyes}}{{@index}}. {{text}}! {{/goodbyes}}cruel {{world}}!";
+ var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
+
+ var template = CompilerContext.compile(string);
+ var result = template(hash);
+
+ equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
+});
+
test("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
@@ -459,14 +494,14 @@ test("rendering undefined partial throws an exception", function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
- }, Handlebars.Exception, "Should throw exception");
+ }, [Handlebars.Exception, 'The partial whatever could not be found'], "Should throw exception");
});
test("rendering template partial in vm mode throws an exception", function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
- }, Handlebars.Exception, "Should throw exception");
+ }, [Handlebars.Exception, 'The partial whatever could not be found'], "Should throw exception");
});
test("rendering function partial in vm mode", function() {
@@ -602,6 +637,11 @@ test("Invert blocks work in knownHelpers only mode", function() {
var result = template({foo: false});
equal(result, "bar", "'bar' should === '" + result);
});
+test("Functions are bound to the context in knownHelpers only mode", function() {
+ var template = CompilerContext.compile("{{foo}}", {knownHelpersOnly: true});
+ var result = template({foo: function() { return this.bar; }, bar: 'bar'});
+ equal(result, "bar", "'bar' should === '" + result);
+});
suite("blockHelperMissing");
@@ -610,6 +650,11 @@ test("lambdas are resolved by blockHelperMissing, not handlebars proper", functi
var data = { truthy: function() { return true; } };
shouldCompileTo(string, data, "yep");
});
+test("lambdas resolved by blockHelperMissing are bound to the context", function() {
+ var string = "{{#truthy}}yep{{/truthy}}";
+ var boundData = { truthy: function() { return this.truthiness(); }, truthiness: function() { return false; } };
+ shouldCompileTo(string, boundData, "");
+});
var teardown;
suite("built-in helpers", {
@@ -659,6 +704,22 @@ test("each", function() {
"each with array argument ignores the contents when empty");
});
+test("each with an object and @key", function() {
+ var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!";
+ var hash = {goodbyes: {"<b>#1</b>": {text: "goodbye"}, 2: {text: "GOODBYE"}}, world: "world"};
+
+ // Object property iteration order is undefined according to ECMA spec,
+ // so we need to check both possible orders
+ // @see http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop
+ var actual = compileWithPartials(string, hash);
+ var expected1 = "&lt;b&gt;#1&lt;/b&gt;. goodbye! 2. GOODBYE! cruel world!";
+ var expected2 = "2. GOODBYE! &lt;b&gt;#1&lt;/b&gt;. goodbye! cruel world!";
+
+ ok(actual === expected1 || actual === expected2, "each with object argument iterates over the contents when not empty");
+ shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!",
+ "each with object argument ignores the contents when empty");
+});
+
test("each with @index", function() {
var string = "{{#each goodbyes}}{{@index}}. {{text}}! {{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};