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:
@@ -74,7 +74,8 @@ getJasmineRequireObj().requireMatchers = function(jRequire) {
|
||||
"toHaveBeenCalled",
|
||||
"toHaveBeenCalledWith",
|
||||
"toMatch",
|
||||
"toThrow"
|
||||
"toThrow",
|
||||
"toThrowError"
|
||||
],
|
||||
matchers = {};
|
||||
|
||||
@@ -1653,7 +1654,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).
|
||||
@@ -2023,63 +2026,44 @@ getJasmineRequireObj().toMatch = function() {
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -2087,6 +2071,136 @@ getJasmineRequireObj().toThrow = function() {
|
||||
return toThrow;
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
getJasmineRequireObj().version = function() {
|
||||
return "2.0.0-alpha";
|
||||
};
|
||||
@@ -58,6 +58,14 @@ describe("matchersUtil", function() {
|
||||
expect(j$.matchersUtil.equals([1, 2], [1, 2, 3])).toBe(false);
|
||||
});
|
||||
|
||||
it("passes for Errors that are the same type and have the same message", function() {
|
||||
expect(j$.matchersUtil.equals(new Error("foo"), new Error("foo"))).toBe(true);
|
||||
});
|
||||
|
||||
it("fails for Errors that are the same type and have different messages", function() {
|
||||
expect(j$.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
|
||||
});
|
||||
|
||||
it("passes for Objects that are equivalent (simple case)", function() {
|
||||
expect(j$.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
|
||||
});
|
||||
|
||||
240
spec/core/matchers/toThrowErrorSpec.js
Normal file
240
spec/core/matchers/toThrowErrorSpec.js
Normal file
@@ -0,0 +1,240 @@
|
||||
describe("toThrowError", function() {
|
||||
it("throws an error when the actual is not a function", function() {
|
||||
var matcher = j$.matchers.toThrowError();
|
||||
|
||||
expect(function() {
|
||||
matcher.compare({});
|
||||
}).toThrow(new Error("Actual is not a Function")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected is not an Error, string, or RegExp", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, 1);
|
||||
}).toThrow(new Error("Expected is not an Error, string, or RegExp.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected error type is not an Error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, "string", "foo");
|
||||
}).toThrow(new Error("Expected error type is not an Error.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected error message is not a string or RegExp", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, Error, 1);
|
||||
}).toThrow(new Error("Expected error message is not a string or RegExp.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("fails if actual does not throw at all", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
return true;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an Error.");
|
||||
});
|
||||
|
||||
it("fails if thrown is not an instanceof Error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw 4;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an Error, but it threw 4.");
|
||||
});
|
||||
|
||||
it("fails with the correct message if thrown is a falsy value", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw undefined;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an Error, but it threw undefined.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error, but there is no expected error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new TypeError();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an Error, but it threw TypeError.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected is the same message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an execption with message 'foo'.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is not the same message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "bar");
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an execption with message 'bar'.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("a long message");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /long/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an execption with a message matching /long/.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a RegExp that does not match the message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("a long message");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /foo/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an execption with a message matching /foo/.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected the same Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, TypeError);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw TypeError.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and it is equal to the expected Error and message", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error with message \"foo\".");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, "bar");
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw Error with message \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and has the same type as the expected Error and the message matches the exepcted message", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, /foo/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error with message matching /foo/.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, /bar/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw Error with message matching /bar/.");
|
||||
});
|
||||
});
|
||||
@@ -1,41 +1,16 @@
|
||||
describe("toThrow", function() {
|
||||
it("throw an error when the acutal is not a function ", function() {
|
||||
it("throws an error when the actual is not a function", function() {
|
||||
var matcher = j$.matchers.toThrow();
|
||||
|
||||
expect(function() {
|
||||
matcher.compare({});
|
||||
}).toThrow(new Error("Actual is not a Function"));
|
||||
}).toThrow(new Error("Actual is not a Function")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected can't be turned into an exception", function() {
|
||||
it("fails if actual does not throw", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, 1);
|
||||
}).toThrow(new Error("Expected cannot be treated as an exception."));
|
||||
});
|
||||
|
||||
it("passes if the actual throws any exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
});
|
||||
|
||||
it("fails if the actual does not throw an exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
return 0;
|
||||
return true;
|
||||
},
|
||||
result;
|
||||
|
||||
@@ -45,107 +20,79 @@ describe("toThrow", function() {
|
||||
expect(result.message).toEqual("Expected function to throw an exception.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with the expected message", function() {
|
||||
it("passes if it throws but there is no expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw.");
|
||||
});
|
||||
|
||||
it("passes even if what is thrown is falsy", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
throw undefined;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw.");
|
||||
});
|
||||
|
||||
it("passes if what is thrown is equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, 5);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw 5.");
|
||||
});
|
||||
|
||||
it("fails if what is thrown is not equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw 'foo'.");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception with a different message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
it("fails if what is thrown is not equivalent to undefined", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "bar");
|
||||
result = matcher.compare(fn, void 0);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception and matches the message of the expected exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, new Error("foo"));
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception and it does not match the message of the expected exception with a custom message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, new Error("bar"));
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception and the message matches the expected regular expression", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "a long message";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /long/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception matching /long/.");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception and the message does not match the expected regular expression", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "a long message";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /short/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception matching /short/.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with an undefined message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw void 0;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with an empty message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
expect(result.message).toEqual("Expected function to throw undefined.");
|
||||
});
|
||||
});
|
||||
@@ -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).
|
||||
|
||||
@@ -15,7 +15,8 @@ getJasmineRequireObj().requireMatchers = function(jRequire) {
|
||||
"toHaveBeenCalled",
|
||||
"toHaveBeenCalledWith",
|
||||
"toMatch",
|
||||
"toThrow"
|
||||
"toThrow",
|
||||
"toThrowError"
|
||||
],
|
||||
matchers = {};
|
||||
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
130
src/core/matchers/toThrowError.js
Normal file
130
src/core/matchers/toThrowError.js
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user