fix assert.throws

v0.7.4-release
Oleg Slobodskoi 2010-12-21 18:42:52 +01:00 committed by Ryan Dahl
parent d793fcaabd
commit 23cf938e4f
3 changed files with 24 additions and 9 deletions

View File

@ -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"

View File

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

View File

@ -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;
}
});