From 0cf5657c43791817776477b727b30ad7f6275707 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Mon, 23 Dec 2013 00:35:14 -0600 Subject: [PATCH] Use shouldThrow helper for test asserts --- spec/basic.js | 9 +++++---- spec/blocks.js | 4 ++-- spec/data.js | 14 +++++++------- spec/env/common.js | 18 ++++++++++++++++++ spec/helpers.js | 20 ++++++++++---------- spec/partials.js | 8 ++++---- spec/regressions.js | 14 +++++++------- 7 files changed, 53 insertions(+), 34 deletions(-) diff --git a/spec/basic.js b/spec/basic.js index ee154a16..245022c6 100644 --- a/spec/basic.js +++ b/spec/basic.js @@ -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); }); }); diff --git a/spec/blocks.js b/spec/blocks.js index 1880eb58..d72a44ec 100644 --- a/spec/blocks.js +++ b/spec/blocks.js @@ -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() { diff --git a/spec/data.js b/spec/data.js index cf9424e9..d84eae72 100644 --- a/spec/data.js +++ b/spec/data.js @@ -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() { diff --git a/spec/env/common.js b/spec/env/common.js index 53ddd61a..7dfe16ea 100644 --- a/spec/env/common.js +++ b/spec/env/common.js @@ -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'); + } +}; diff --git a/spec/helpers.js b/spec/helpers.js index c5ea574b..b0eb91e6 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -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"}}'; + 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 = { diff --git a/spec/partials.js b/spec/partials.js index 7ff9d3c2..3f46c377 100644 --- a/spec/partials.js +++ b/spec/partials.js @@ -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() { diff --git a/spec/regressions.js b/spec/regressions.js index eec5a490..23fcbc0b 100644 --- a/spec/regressions.js +++ b/spec/regressions.js @@ -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) {