DRY up some sopping wet code

This commit is contained in:
JR Boyens
2013-07-19 18:51:05 -07:00
parent 1b0b4f22a0
commit 03ffe5ce6a

View File

@@ -6,7 +6,9 @@ getJasmineRequireObj().toThrowError = function(j$) {
thrown, thrown,
errorType, errorType,
message, message,
regexp; regexp,
name,
constructorName;
if (typeof actual != "function") { if (typeof actual != "function") {
throw new Error("Actual is not a Function"); throw new Error("Actual is not a Function");
@@ -33,10 +35,12 @@ getJasmineRequireObj().toThrowError = function(j$) {
return pass("Expected function not to throw an Error, but it threw " + thrown + "."); return pass("Expected function not to throw an Error, but it threw " + thrown + ".");
} }
if (errorType && message) { if (errorType) {
var name = errorType.name || errorType.toString().match(/^\s*function\s*(\w*)\s*\(/)[1]; name = fnNameFor(errorType);
var constructorName = thrown.constructor.name || thrown.constructor.toString().match(/^\s*function\s*(\w*)\s*\(/)[1]; constructorName = fnNameFor(thrown.constructor);
}
if (errorType && message) {
if (thrown.constructor == errorType && util.equals(thrown.message, message)) { if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
return pass("Expected function not to throw " + name + " with message \"" + message + "\"."); return pass("Expected function not to throw " + name + " with message \"" + message + "\".");
} else { } else {
@@ -46,9 +50,6 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
if (errorType && regexp) { if (errorType && regexp) {
var name = errorType.name || errorType.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
var constructorName = thrown.constructor.name || thrown.constructor.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
if (thrown.constructor == errorType && regexp.test(thrown.message)) { if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw " + name + " with message matching " + regexp + "."); return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");
} else { } else {
@@ -58,10 +59,6 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
if (errorType) { if (errorType) {
var name = errorType.name || errorType.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
var constructorName = thrown.constructor.name || thrown.constructor.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
if (thrown.constructor == errorType) { if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + name + "."); return pass("Expected function not to throw " + name + ".");
} else { } else {
@@ -87,6 +84,10 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
} }
function fnNameFor(func) {
return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
}
function pass(notMessage) { function pass(notMessage) {
return { return {
pass: true, pass: true,
@@ -151,4 +152,4 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
return toThrowError; return toThrowError;
}; };