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. 9
      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");
},
function(err) {
if ( !(err instanceof Error) || !/value/.test(err) ) {
return false;
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
}
},
"unexpected error"

9
lib/assert.js

@ -246,13 +246,14 @@ function expectedException(actual, expected) {
}
if (expected instanceof RegExp) {
if (expected.test(actual)) {
return expected.test(actual);
} else if (actual instanceof expected) {
return true;
}
} else if (actual instanceof expected ||
expected.call({}, actual) !== false) {
} else if ( expected.call({}, actual) === true ) {
return true;
}
return false;
}
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()});
// 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
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;
if ( (err instanceof TypeError) && /test/.test(err)) {
return true;
}
});

Loading…
Cancel
Save