Revert removal of compare nesting
Since we want the user to be able to pass a negative comparison function, the extra layer of wrapping is now needed
This commit is contained in:
@@ -1,150 +1,152 @@
|
||||
getJasmineRequireObj().toThrowError = function(j$) {
|
||||
function toThrowError (util) {
|
||||
return function(actual) {
|
||||
var threw = false,
|
||||
thrown,
|
||||
errorType,
|
||||
message,
|
||||
regexp,
|
||||
name,
|
||||
constructorName;
|
||||
return {
|
||||
compare: function(actual) {
|
||||
var threw = false,
|
||||
thrown,
|
||||
errorType,
|
||||
message,
|
||||
regexp,
|
||||
name,
|
||||
constructorName;
|
||||
|
||||
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 " + fnNameFor(thrown) + ".");
|
||||
}
|
||||
|
||||
if (errorType) {
|
||||
name = fnNameFor(errorType);
|
||||
constructorName = fnNameFor(thrown.constructor);
|
||||
}
|
||||
|
||||
if (errorType && message) {
|
||||
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
|
||||
return pass("Expected function not to throw " + name + " with message \"" + message + "\".");
|
||||
} else {
|
||||
return fail("Expected function to throw " + name + " with message \"" + message +
|
||||
"\", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
|
||||
if (typeof actual != "function") {
|
||||
throw new Error("Actual is not a Function");
|
||||
}
|
||||
}
|
||||
|
||||
if (errorType && regexp) {
|
||||
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
|
||||
return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw " + name + " with message matching " + regexp +
|
||||
", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
|
||||
extractExpectedParams.apply(null, arguments);
|
||||
|
||||
try {
|
||||
actual();
|
||||
} catch (e) {
|
||||
threw = true;
|
||||
thrown = e;
|
||||
}
|
||||
}
|
||||
|
||||
if (errorType) {
|
||||
if (thrown.constructor == errorType) {
|
||||
return pass("Expected function not to throw " + name + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw " + name + ", but it threw " + constructorName + ".");
|
||||
if (!threw) {
|
||||
return fail("Expected function to throw an Error.");
|
||||
}
|
||||
}
|
||||
|
||||
if (message) {
|
||||
if (thrown.message == message) {
|
||||
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw an exception with message " + j$.pp(message) +
|
||||
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
|
||||
if (!(thrown instanceof Error)) {
|
||||
return fail("Expected function to throw an Error, but it threw " + thrown + ".");
|
||||
}
|
||||
}
|
||||
|
||||
if (regexp) {
|
||||
if (regexp.test(thrown.message)) {
|
||||
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
|
||||
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
|
||||
}
|
||||
}
|
||||
|
||||
function fnNameFor(func) {
|
||||
return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
|
||||
}
|
||||
|
||||
function pass(notMessage) {
|
||||
return {
|
||||
pass: true,
|
||||
message: notMessage
|
||||
};
|
||||
}
|
||||
|
||||
function fail(message) {
|
||||
return {
|
||||
pass: false,
|
||||
message: message
|
||||
};
|
||||
}
|
||||
|
||||
function extractExpectedParams() {
|
||||
if (arguments.length == 1) {
|
||||
return;
|
||||
return pass("Expected function not to throw an Error, but it threw " + fnNameFor(thrown) + ".");
|
||||
}
|
||||
|
||||
if (arguments.length == 2) {
|
||||
var expected = arguments[1];
|
||||
if (errorType) {
|
||||
name = fnNameFor(errorType);
|
||||
constructorName = fnNameFor(thrown.constructor);
|
||||
}
|
||||
|
||||
if (expected instanceof RegExp) {
|
||||
regexp = expected;
|
||||
} else if (typeof expected == "string") {
|
||||
message = expected;
|
||||
} else if (checkForAnErrorType(expected)) {
|
||||
errorType = expected;
|
||||
}
|
||||
|
||||
if (!(errorType || message || regexp)) {
|
||||
throw new Error("Expected is not an Error, string, or RegExp.");
|
||||
}
|
||||
} else {
|
||||
if (checkForAnErrorType(arguments[1])) {
|
||||
errorType = arguments[1];
|
||||
if (errorType && message) {
|
||||
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
|
||||
return pass("Expected function not to throw " + name + " with message \"" + message + "\".");
|
||||
} else {
|
||||
throw new Error("Expected error type is not an Error.");
|
||||
return fail("Expected function to throw " + name + " with message \"" + message +
|
||||
"\", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
|
||||
}
|
||||
}
|
||||
|
||||
if (arguments[2] instanceof RegExp) {
|
||||
regexp = arguments[2];
|
||||
} else if (typeof arguments[2] == "string") {
|
||||
message = arguments[2];
|
||||
if (errorType && regexp) {
|
||||
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
|
||||
return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");
|
||||
} else {
|
||||
throw new Error("Expected error message is not a string or RegExp.");
|
||||
return fail("Expected function to throw " + name + " with message matching " + regexp +
|
||||
", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkForAnErrorType(type) {
|
||||
if (typeof type !== "function") {
|
||||
return false;
|
||||
if (errorType) {
|
||||
if (thrown.constructor == errorType) {
|
||||
return pass("Expected function not to throw " + name + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw " + name + ", but it threw " + constructorName + ".");
|
||||
}
|
||||
}
|
||||
|
||||
var Surrogate = function() {};
|
||||
Surrogate.prototype = type.prototype;
|
||||
return (new Surrogate()) instanceof Error;
|
||||
if (message) {
|
||||
if (thrown.message == message) {
|
||||
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw an exception with message " + j$.pp(message) +
|
||||
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
|
||||
}
|
||||
}
|
||||
|
||||
if (regexp) {
|
||||
if (regexp.test(thrown.message)) {
|
||||
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
|
||||
} else {
|
||||
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
|
||||
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
|
||||
}
|
||||
}
|
||||
|
||||
function fnNameFor(func) {
|
||||
return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
|
||||
}
|
||||
|
||||
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 (checkForAnErrorType(expected)) {
|
||||
errorType = expected;
|
||||
}
|
||||
|
||||
if (!(errorType || message || regexp)) {
|
||||
throw new Error("Expected is not an Error, string, or RegExp.");
|
||||
}
|
||||
} else {
|
||||
if (checkForAnErrorType(arguments[1])) {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkForAnErrorType(type) {
|
||||
if (typeof type !== "function") {
|
||||
return false;
|
||||
}
|
||||
|
||||
var Surrogate = function() {};
|
||||
Surrogate.prototype = type.prototype;
|
||||
return (new Surrogate()) instanceof Error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user