Browse Source

fix assert.throws

v0.7.4-release
Oleg Slobodskoi 14 years ago
committed by Ryan Dahl
parent
commit
23cf938e4f
  1. 4
      doc/api/assert.markdown
  2. 11
      lib/assert.js
  3. 18
      test/simple/test-assert.js

4
doc/api/assert.markdown

@ -65,8 +65,8 @@ Custom error validation:
throw new Error("Wrong value"); throw new Error("Wrong value");
}, },
function(err) { function(err) {
if ( !(err instanceof Error) || !/value/.test(err) ) { if ( (err instanceof Error) && /value/.test(err) ) {
return false; return true;
} }
}, },
"unexpected error" "unexpected error"

11
lib/assert.js

@ -246,13 +246,14 @@ function expectedException(actual, expected) {
} }
if (expected instanceof RegExp) { if (expected instanceof RegExp) {
if (expected.test(actual)) { return expected.test(actual);
return true; } else if (actual instanceof expected) {
} return true;
} else if (actual instanceof expected || } else if ( expected.call({}, actual) === true ) {
expected.call({}, actual) !== false) {
return true; return true;
} }
return false;
} }
function _throws(shouldThrow, block, expected, message) { function _throws(shouldThrow, block, expected, message) {

18
test/simple/test-assert.js

@ -165,12 +165,26 @@ 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()});
// make sure that validating using constructor really works
threw = false;
try {
assert.throws(
function() {
throw {};
},
Array
);
} catch(e) {
threw = true;
}
assert.ok(threw, "wrong constructor validation");
// use a RegExp to validate error message // use a RegExp to validate error message
a.throws(makeBlock(thrower, TypeError), /test/); a.throws(makeBlock(thrower, TypeError), /test/);
// use a fn to validate error object // use a fn to validate error object
a.throws(makeBlock(thrower, TypeError), function(err) { a.throws(makeBlock(thrower, TypeError), function(err) {
if (!(err instanceof TypeError) || !/test/.test(err)) { if ( (err instanceof TypeError) && /test/.test(err)) {
return false; return true;
} }
}); });

Loading…
Cancel
Save