Remove an extra layer of wrapping for matchers/custom matchers

Helps reduce how nested custom matchers have to be for users as well as
Jasmine internal matchers

[#59161378]
This commit is contained in:
Sheel Choksi
2013-10-19 22:46:19 -07:00
parent 5f429fcb37
commit 39d7ebf28e
37 changed files with 437 additions and 494 deletions

View File

@@ -1,142 +1,142 @@
describe("toThrowError", function() {
it("throws an error when the actual is not a function", function() {
var matcher = j$.matchers.toThrowError();
var matcherComparator = j$.matchers.toThrowError();
expect(function() {
matcher.compare({});
matcherComparator({});
}).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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
};
expect(function() {
matcher.compare(fn, 1);
matcherComparator(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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
};
expect(function() {
matcher.compare(fn, void 0, "foo");
matcherComparator(fn, void 0, "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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
};
expect(function() {
matcher.compare(fn, Error, 1);
matcherComparator(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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
return true;
},
result;
result = matcher.compare(fn);
result = matcherComparator(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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw 4;
},
result;
result = matcher.compare(fn);
result = matcherComparator(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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw undefined;
},
result;
result = matcher.compare(fn);
result = matcherComparator(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 a type of Error, but there is no expected error", function() {
var matcher = j$.matchers.toThrowError(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new TypeError();
},
result;
result = matcher.compare(fn);
result = matcherComparator(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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
},
result;
result = matcher.compare(fn, "foo");
result = matcherComparator(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception with message 'foo'.");
});
it("fails if thrown is an Error and the expected is not the same message", function() {
var matcher = j$.matchers.toThrowError(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
},
result;
result = matcher.compare(fn, "bar");
result = matcherComparator(fn, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'.");
});
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
var matcher = j$.matchers.toThrowError(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("a long message");
},
result;
result = matcher.compare(fn, /long/);
result = matcherComparator(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception 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(),
var matcherComparator = j$.matchers.toThrowError(),
fn = function() {
throw new Error("a long message");
},
result;
result = matcher.compare(fn, /foo/);
result = matcherComparator(fn, /foo/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'.");
@@ -146,13 +146,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new Error();
},
result;
result = matcher.compare(fn, Error);
result = matcherComparator(fn, Error);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw Error.");
@@ -162,7 +162,7 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
CustomError = function CustomError(arg) { arg.x },
fn = function() {
throw new CustomError({ x: 1 });
@@ -172,7 +172,7 @@ describe("toThrowError", function() {
CustomError.prototype = new Error();
CustomError.prototype.constructor = CustomError;
result = matcher.compare(fn, CustomError);
result = matcherComparator(fn, CustomError);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw CustomError.");
@@ -182,13 +182,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new Error();
},
result;
result = matcher.compare(fn, TypeError);
result = matcherComparator(fn, TypeError);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError, but it threw Error.");
@@ -198,13 +198,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, TypeError, "foo");
result = matcherComparator(fn, TypeError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw TypeError with message \"foo\".");
@@ -214,7 +214,7 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
CustomError = function CustomError(arg) { this.message = arg.message },
fn = function() {
throw new CustomError({message: "foo"});
@@ -224,7 +224,7 @@ describe("toThrowError", function() {
CustomError.prototype = new Error();
CustomError.prototype.constructor = CustomError;
result = matcher.compare(fn, CustomError, "foo");
result = matcherComparator(fn, CustomError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw CustomError with message \"foo\".");
@@ -234,13 +234,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, TypeError, "bar");
result = matcherComparator(fn, TypeError, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError with message \"bar\", but it threw TypeError with message \"foo\".");
@@ -250,13 +250,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, TypeError, /foo/);
result = matcherComparator(fn, TypeError, /foo/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw TypeError with message matching /foo/.");
@@ -266,13 +266,13 @@ describe("toThrowError", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
},
matcher = j$.matchers.toThrowError(util),
matcherComparator = j$.matchers.toThrowError(util),
fn = function() {
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, TypeError, /bar/);
result = matcherComparator(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError with message matching /bar/, but it threw TypeError with message \"foo\".");