Browse Source

assert.throws can now accept as RegExp

makes validation of errors more flexible
v0.7.4-release
Oleg Slobodskoi 14 years ago
committed by Ryan Dahl
parent
commit
02083412eb
  1. 63
      lib/assert.js
  2. 10
      test/simple/test-assert.js

63
lib/assert.js

@ -239,47 +239,50 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
} }
}; };
function _throws (shouldThrow, block, err, message) { function expectedException(actual, expected) {
var exception = null, if (!actual || !expected) {
threw = false, return false;
typematters = true; }
message = message || ""; if (expected instanceof RegExp) {
if (expected.test(actual)) {
//handle optional arguments return true;
if (arguments.length == 3) {
if (typeof(err) == "string") {
message = err;
typematters = false;
} }
} else if (arguments.length == 2) { } else if (actual instanceof expected || expected.call({}, actual) !== false) {
typematters = false; return true;
}
}
function _throws (shouldThrow, block, expected, message) {
var actual;
if (typeof expected === "string") {
message = expected;
expected = null;
} }
try { try {
block(); block();
} catch (e) { } catch (e) {
threw = true; actual = e;
exception = e;
} }
if (shouldThrow && !threw) { message = (expected && expected.name ? " (" + expected.name + ")." : ".")
fail( "Missing expected exception" + (message ? " " + message : ".");
+ (err && err.name ? " ("+err.name+")." : '.')
+ (message ? " " + message : "") if (shouldThrow && !actual) {
); fail("Missing expected exception" + message);
} }
if (!shouldThrow && threw && typematters && exception instanceof err) {
fail( "Got unwanted exception" if (!shouldThrow && expectedException(actual, expected)) {
+ (err && err.name ? " ("+err.name+")." : '.') fail("Got unwanted exception" + message);
+ (message ? " " + message : "")
);
} }
if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
(!shouldThrow && threw)) { if ((shouldThrow && actual && expected && !expectedException(actual, expected)) ||
throw exception; (!shouldThrow && actual)) {
throw actual;
} }
}; }
// 11. Expected to throw an error: // 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt); // assert.throws(block, Error_opt, message_opt);

10
test/simple/test-assert.js

@ -158,3 +158,13 @@ assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');
assert.throws(function () {assert.ifError(new Error('test error'))}); assert.throws(function () {assert.ifError(new Error('test error'))});
assert.doesNotThrow(function(){assert.ifError(null)}); assert.doesNotThrow(function(){assert.ifError(null)});
assert.doesNotThrow(function(){assert.ifError()}); assert.doesNotThrow(function(){assert.ifError()});
// use a RegExp to validate error message
a.throws(makeBlock(thrower, TypeError), /test/ );
// use a fn to validate error object
a.throws(makeBlock(thrower, TypeError), function(err) {
if (!(err instanceof TypeError) || !/test/.test(err)) {
return false;
}
});

Loading…
Cancel
Save