mirror of https://github.com/nodejs/node.git
fix assert.throws
parent
d793fcaabd
commit
23cf938e4f
|
@ -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"
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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…
Reference in New Issue