Simplifying toThrow:

- It still supports no expected, which means that something was thrown
- Expected value is now tested via equality in order to pass

Adding toThrowError:
- toThrowError() passes if an Error type was thrown
- toThrowError(String) & toThrowError(RegExp) compare Expected to the Error message
- toThrowError(Error constructor) compares Expected to the constructor of what was thrown
- toThrowError(Error constructor, String) & toThrowError(Error constructor, RegExp) compares both the Error and the message

Also, equality now handles Errors, enforcing the message as part of the equality.
This commit is contained in:
Davis W. Frank
2013-06-03 09:24:43 -07:00
parent 9e31201f1a
commit 0ac497db6b
8 changed files with 611 additions and 188 deletions

View File

@@ -79,7 +79,9 @@ getJasmineRequireObj().matchersUtil = function(j$) {
}
}
if (a instanceof Error && b instanceof Error) {
return a.message == b.message;
}
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).

View File

@@ -15,7 +15,8 @@ getJasmineRequireObj().requireMatchers = function(jRequire) {
"toHaveBeenCalled",
"toHaveBeenCalledWith",
"toMatch",
"toThrow"
"toThrow",
"toThrowError"
],
matchers = {};

View File

@@ -1,62 +1,43 @@
getJasmineRequireObj().toThrow = function() {
function toThrow() {
function toThrow(util) {
return {
compare: function(actual, expected) {
var result = { pass: false },
exception;
threw = false,
thrown;
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
if (expectedCannotBeTreatedAsException()) {
throw new Error("Expected cannot be treated as an exception.");
}
try {
actual();
} catch (e) {
exception = new Error(e);
threw = true;
thrown = e;
}
if (!exception) {
if (!threw) {
result.message = "Expected function to throw an exception.";
return result;
}
if (void 0 == expected) {
if (arguments.length == 1) {
result.pass = true;
result.message = "Expected function not to throw an exception.";
} else if (exception.message == expected) {
result.message = "Expected function not to throw.";
return result;
}
if (util.equals(thrown, expected)) {
result.pass = true;
result.message = "Expected function not to throw an exception \"" + expected + "\".";
} else if (exception.message == expected.message) {
result.pass = true;
result.message = "Expected function not to throw an exception \"" + expected.message + "\".";
} else if (expected instanceof RegExp) {
if (expected.test(exception.message)) {
result.pass = true;
result.message = "Expected function not to throw an exception matching " + expected + ".";
} else {
result.pass = false;
result.message = "Expected function to throw an exception matching " + expected + ".";
}
result.message = "Expected function not to throw " + j$.pp(expected) + ".";
} else {
result.pass = false;
result.message = "Expected function to throw an exception \"" + (expected.message || expected) + "\".";
result.message = "Expected function to throw " + j$.pp(expected) + ".";
}
return result;
function expectedCannotBeTreatedAsException() {
return !(
(void 0 == expected) ||
(expected instanceof Error) ||
(typeof expected == "string") ||
(expected instanceof RegExp)
);
}
}
};
}

View File

@@ -0,0 +1,130 @@
getJasmineRequireObj().toThrowError = function() {
function toThrowError (util) {
return {
compare: function(actual) {
var threw = false,
thrown,
errorType,
message,
regexp;
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
extractExpectedParams.apply(null, arguments);
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
if (!threw) {
return fail("Expected function to throw an Error.");
}
if (!(thrown instanceof Error)) {
return fail("Expected function to throw an Error, but it threw " + thrown + ".");
}
if (arguments.length == 1) {
return pass("Expected function not to throw an Error, but it threw " + thrown + ".");
}
if (errorType && message) {
if (util.equals(thrown, new errorType(message))) {
return pass("Expected function not to throw Error with message \"" + message + "\".");
} else {
return fail("Expected function to throw Error with message \"" + message + "\".");
}
}
if (errorType && regexp) {
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw Error with message matching " + regexp + ".");
} else {
return fail("Expected function to throw Error with message matching " + regexp + ".");
}
}
if (errorType) {
if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + errorType.name + ".");
} else {
return fail("Expected function to throw " + errorType.name + ".");
}
}
if (message) {
if (thrown.message == message) {
return pass("Expected function not to throw an execption with message " + j$.pp(message) + ".");
} else {
return fail("Expected function to throw an execption with message " + j$.pp(message) + ".");
}
}
if (regexp) {
if (regexp.test(thrown.message)) {
return pass("Expected function not to throw an execption with a message matching " + j$.pp(regexp) + ".");
} else {
return fail("Expected function to throw an execption with a message matching " + j$.pp(regexp) + ".");
}
}
function pass(notMessage) {
return {
pass: true,
message: notMessage
};
}
function fail(message) {
return {
pass: false,
message: message
};
}
function extractExpectedParams() {
if (arguments.length == 1) {
return;
}
if (arguments.length == 2) {
var expected = arguments[1];
if (expected instanceof RegExp) {
regexp = expected;
} else if (typeof expected == "string") {
message = expected;
} else if (typeof expected == "function" && new expected() instanceof Error) {
errorType = expected;
}
if (!(errorType || message || regexp)) {
throw new Error("Expected is not an Error, string, or RegExp.");
}
} else {
if (typeof arguments[1] == "function" && new arguments[1]() instanceof Error) {
errorType = arguments[1];
} else {
throw new Error("Expected error type is not an Error.");
}
if (arguments[2] instanceof RegExp) {
regexp = arguments[2];
} else if (typeof arguments[2] == "string") {
message = arguments[2];
} else {
throw new Error("Expected error message is not a string or RegExp.");
}
}
}
}
};
}
return toThrowError;
};