Browse files

Merge branch 'jxson'

Conflicts:
	lib/core.js
  • Loading branch information...
2 parents 692b4e1 + 92b6b77 commit 2883644a2be80c2f3d6677f5dc4e8300a64689bb @caolan committed Feb 12, 2012
Showing with 61 additions and 36 deletions.
  1. +36 −31 lib/assert.js
  2. +21 −1 test/test-base.js
  3. +4 −4 test/test-runmodule.js
View
67 lib/assert.js
@@ -286,47 +286,52 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
}
};
-function _throws (shouldThrow, block, err, message) {
- var exception = null,
- threw = false,
- typematters = true;
-
- message = message || "";
-
- //handle optional arguments
- if (arguments.length == 3) {
- if (typeof(err) == "string") {
- message = err;
- typematters = false;
- }
- } else if (arguments.length == 2) {
- typematters = false;
+function expectedException(actual, expected) {
+ if (!actual || !expected) {
+ return false;
+ }
+
+ if (expected instanceof RegExp) {
+ return expected.test(actual.message || actual);
+ } else if (actual instanceof expected) {
+ return true;
+ } else if (expected.call({}, actual) === true) {
+ return true;
+ }
+
+ return false;
+}
+
+function _throws(shouldThrow, block, expected, message) {
+ var actual;
+
+ if (typeof expected === 'string') {
+ message = expected;
+ expected = null;
}
try {
block();
} catch (e) {
- threw = true;
- exception = e;
+ actual = e;
}
- if (shouldThrow && !threw) {
- fail( "Missing expected exception"
- + (err && err.name ? " ("+err.name+")." : '.')
- + (message ? " " + message : "")
- );
+ message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
+ (message ? ' ' + message : '.');
+
+ if (shouldThrow && !actual) {
+ fail('Missing expected exception' + message);
}
- if (!shouldThrow && threw && typematters && exception instanceof err) {
- fail( "Got unwanted exception"
- + (err && err.name ? " ("+err.name+")." : '.')
- + (message ? " " + message : "")
- );
+
+ if (!shouldThrow && expectedException(actual, expected)) {
+ fail('Got unwanted exception' + message);
}
- if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
- (!shouldThrow && threw)) {
- throw exception;
+
+ if ((shouldThrow && actual && expected &&
+ !expectedException(actual, expected)) || (!shouldThrow && actual)) {
+ throw actual;
}
-};
+}
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
View
22 test/test-base.js
@@ -130,6 +130,26 @@ exports.testThrows = makeTest('throws',
return;
}]
);
+exports.testThrowsWithReGex = makeTest('throws',
+ [function () {
+ throw new Error('test');
+ }, /test/],
+ [function () {
+ throw new Error('test');
+ }, /fail/]
+);
+exports.testThrowsWithErrorValidation = makeTest('throws',
+ [function () {
+ throw new Error('test');
+ }, function(err) {
+ return true;
+ }],
+ [function () {
+ throw new Error('test');
+ }, function(err) {
+ return false;
+ }]
+);
exports.testDoesNotThrows = makeTest('doesNotThrow',
[function () {
return;
@@ -205,7 +225,7 @@ exports.testExpect = function (test) {
assert.ok(test2_called);
assert.ok(test3_called);
tests_called.expect = true;
- }, 500, test.done);
+ }, 1000, test.done);
};
View
8 test/test-runmodule.js
@@ -91,15 +91,15 @@ exports.testRunModuleTestSpec = function (test) {
},
testStart: function (name) {
call_order.push('testStart');
- test.ok(
- name.toString() === 'test2',
+ test.equals(
+ name,'test2',
'testStart called with test name '
);
},
testDone: function (name, assertions) {
call_order.push('testDone');
- test.ok(
- name.toString() === 'test2',
+ test.equal(
+ name, 'test2',
'testDone called with test name'
);
},

0 comments on commit 2883644

Please sign in to comment.