From 68e8281b3940309ac609e72e8af0c97b16d49a81 Mon Sep 17 00:00:00 2001 From: Jason Campbell Date: Wed, 14 Sep 2011 15:48:42 -0700 Subject: [PATCH] fixes #100 --- lib/assert.js | 67 +++++++++++++++++++++++++---------------------- test/test-base.js | 24 +++++++++++++++++ 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 8d9e0e3cf..f3974336e 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -259,47 +259,52 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) { } }; -function _throws (shouldThrow, block, err, message) { - var exception = null, - threw = false, - typematters = true; - - message = message || ""; - - //handle optional arguments - if (arguments.length == 3) { - if (typeof(err) == "string") { - message = err; - typematters = false; - } - } else if (arguments.length == 2) { - typematters = false; +function expectedException(actual, expected) { + if (!actual || !expected) { + return false; + } + + if (expected instanceof RegExp) { + return expected.test(actual); + } else if (actual instanceof expected) { + return true; + } else if (expected.call({}, actual) === true) { + return true; + } + + return false; +} + +function _throws(shouldThrow, block, expected, message) { + var actual; + + if (typeof expected === 'string') { + message = expected; + expected = null; } try { block(); } catch (e) { - threw = true; - exception = e; + actual = e; } - if (shouldThrow && !threw) { - fail( "Missing expected exception" - + (err && err.name ? " ("+err.name+")." : '.') - + (message ? " " + message : "") - ); + message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + + (message ? ' ' + message : '.'); + + if (shouldThrow && !actual) { + fail('Missing expected exception' + message); } - if (!shouldThrow && threw && typematters && exception instanceof err) { - fail( "Got unwanted exception" - + (err && err.name ? " ("+err.name+")." : '.') - + (message ? " " + message : "") - ); + + if (!shouldThrow && expectedException(actual, expected)) { + fail('Got unwanted exception' + message); } - if ((shouldThrow && threw && typematters && !(exception instanceof err)) || - (!shouldThrow && threw)) { - throw exception; + + if ((shouldThrow && actual && expected && + !expectedException(actual, expected)) || (!shouldThrow && actual)) { + throw actual; } -}; +} // 11. Expected to throw an error: // assert.throws(block, Error_opt, message_opt); diff --git a/test/test-base.js b/test/test-base.js index 64b8c8bbd..94b0f1480 100644 --- a/test/test-base.js +++ b/test/test-base.js @@ -130,6 +130,30 @@ exports.testThrows = makeTest('throws', return; }] ); +exports.testThrowsWithReGex = makeTest('throws', + [function () { + throw new Error('test'); + }, /test/], + [function () { + throw new Error('test'); + }, /fail/] +); +exports.testThrowsWithErrorValidation = makeTest('throws', + [function () { + throw new Error('test'); + }, function(err) { + if ( (err instanceof Error) && /test/.test(err) ) { + return true; + } + }], + [function () { + throw new Error('test'); + }, function(err) { + if ( (err instanceof Error) && /fail/.test(err) ) { + return true; + } + }] +); exports.testDoesNotThrows = makeTest('doesNotThrow', [function () { return;