Use shouldThrow helper for test asserts

This commit is contained in:
kpdecker
2013-12-23 00:35:14 -06:00
parent 3daef9d308
commit 0cf5657c43
7 changed files with 53 additions and 34 deletions
+5 -4
View File
@@ -1,3 +1,4 @@
/*global CompilerContext, Handlebars, beforeEach, shouldCompileTo */
global.handlebarsEnv = null;
beforeEach(function() {
@@ -160,9 +161,9 @@ describe("basic context", function() {
it("this keyword nested inside path", function() {
var string = "{{#hellos}}{{text/this/foo}}{{/hellos}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("this keyword in helpers", function() {
@@ -181,8 +182,8 @@ describe("basic context", function() {
it("this keyword nested inside helpers param", function() {
var string = "{{#hellos}}{{foo text/this/foo}}{{/hellos}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
});
+2 -2
View File
@@ -42,9 +42,9 @@ describe('blocks', function() {
it("block with complex lookup using nested context", function() {
var string = "{{#goodbyes}}{{text}} cruel {{foo/../name}}! {{/goodbyes}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("block with deep nested complex lookup", function() {
+7 -7
View File
@@ -1,4 +1,4 @@
/*global CompilerContext */
/*global CompilerContext, Handlebars, handlebarsEnv, shouldThrow */
describe('data', function() {
it("passing in data to a compiled function that expects data - works with helpers", function() {
var template = CompilerContext.compile("{{hello}}", {data: true});
@@ -88,25 +88,25 @@ describe('data', function() {
it("parameter data throws when using this scope references", function() {
var string = "{{#goodbyes}}{{text}} cruel {{@./name}}! {{/goodbyes}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("parameter data throws when using parent scope references", function() {
var string = "{{#goodbyes}}{{text}} cruel {{@../name}}! {{/goodbyes}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("parameter data throws when using complex scope references", function() {
var string = "{{#goodbyes}}{{text}} cruel {{@foo/../name}}! {{/goodbyes}}";
(function() {
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("data is inherited downstream", function() {
+18
View File
@@ -26,3 +26,21 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
global.equals = global.equal = function(a, b, msg) {
a.should.equal(b, msg || '');
};
global.shouldThrow = function(callback, type, msg) {
var failed;
try {
callback();
failed = true;
} catch (err) {
if (type && !(err instanceof type)) {
throw new Error('Type failure');
}
if (msg && !(msg.test ? msg.test(err.message) : msg === err.message)) {
throw new Error('Message failure');
}
}
if (failed) {
throw new Error('It failed to throw');
}
};
+9 -9
View File
@@ -1,4 +1,4 @@
/*global CompilerContext, shouldCompileTo, shouldCompileToWithPartials */
/*global CompilerContext, Handlebars, shouldCompileTo, shouldCompileToWithPartials, shouldThrow, handlebarsEnv */
describe('helpers', function() {
it("helper with complex lookup$", function() {
var string = "{{#goodbyes}}{{{link ../prefix}}}{{/goodbyes}}";
@@ -212,10 +212,10 @@ describe('helpers', function() {
});
it("using a quote in the middle of a parameter raises an error", function() {
(function() {
var string = 'Message: {{hello wo"rld"}}';
shouldThrow(function() {
CompilerContext.compile(string);
}).should.throw(Error);
}, Error);
});
it("escaping a String is possible", function(){
@@ -351,10 +351,10 @@ describe('helpers', function() {
describe("helperMissing", function() {
it("if a context is not found, helperMissing is used", function() {
(function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{hello}} {{link_to world}}");
template({});
}).should.throw(/Missing helper: 'link_to'/);
}, undefined, /Missing helper: 'link_to'/);
});
it("if a context is not found, custom helperMissing is used", function() {
@@ -417,9 +417,9 @@ describe('helpers', function() {
equal(result, "bar", "'bar' should === '" + result);
});
it("Unknown helper call in knownHelpers only mode should throw", function() {
(function() {
shouldThrow(function() {
CompilerContext.compile("{{typeof hello}}", {knownHelpersOnly: true});
}).should.throw(Error);
}, Error);
});
});
@@ -491,7 +491,7 @@ describe('helpers', function() {
cruel: function(world) {
return "cruel " + world.toUpperCase();
},
}
};
var context = {
@@ -513,7 +513,7 @@ describe('helpers', function() {
cruel: function(world) {
return "cruel " + world.toUpperCase();
},
}
};
var context = {
+4 -4
View File
@@ -32,17 +32,17 @@ describe('partials', function() {
});
it("rendering undefined partial throws an exception", function() {
(function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
}).should.throw(Handlebars.Exception, 'The partial whatever could not be found');
}, Handlebars.Exception, 'The partial whatever could not be found');
});
it("rendering template partial in vm mode throws an exception", function() {
(function() {
shouldThrow(function() {
var template = CompilerContext.compile("{{> whatever}}");
template();
}).should.throw(Handlebars.Exception, 'The partial whatever could not be found');
}, Handlebars.Exception, 'The partial whatever could not be found');
});
it("rendering function partial in vm mode", function() {
+7 -7
View File
@@ -1,4 +1,4 @@
/*global CompilerContext, shouldCompileTo */
/*global CompilerContext, Handlebars, shouldCompileTo, shouldThrow */
describe('Regressions', function() {
it("GH-94: Cannot read property of undefined", function() {
var data = {"books":[{"title":"The origin of species","author":{"name":"Charles Darwin"}},{"title":"Lazarillo de Tormes"}]};
@@ -85,12 +85,12 @@ describe('Regressions', function() {
});
it('GH-437: Matching escaping', function() {
(function() {
shouldThrow(function() {
CompilerContext.compile('{{{a}}');
}).should.throw(Error);
(function() {
}, Error);
shouldThrow(function() {
CompilerContext.compile('{{a}}}');
}).should.throw(Error);
}, Error);
});
it("Mustache man page", function() {
@@ -106,9 +106,9 @@ describe('Regressions', function() {
});
it("Passing falsy values to Handlebars.compile throws an error", function() {
(function() {
shouldThrow(function() {
CompilerContext.compile(null);
}).should.throw("You must pass a string or Handlebars AST to Handlebars.precompile. You passed null");
}, Error, 'You must pass a string or Handlebars AST to Handlebars.precompile. You passed null');
});
if (Handlebars.AST) {