Merge branch 'custom-object-formatters' into cof-merge-candidate

This commit is contained in:
Steve Gravrock
2020-02-11 11:44:38 -08:00
84 changed files with 3047 additions and 1017 deletions

View File

@@ -1,4 +1,11 @@
describe("matchersUtil", function() {
it("exposes the injected pretty-printer as .pp", function() {
var pp = function() {},
matchersUtil = new jasmineUnderTest.MatchersUtil({pp: pp});
expect(matchersUtil.pp).toBe(pp);
});
describe("equals", function() {
describe('Properties', function() {
var fc;
@@ -72,178 +79,210 @@ describe("matchersUtil", function() {
});
it("passes for literals that are triple-equal", function() {
expect(jasmineUnderTest.matchersUtil.equals(null, null)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(void 0, void 0)).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(null, null)).toBe(true);
expect(matchersUtil.equals(void 0, void 0)).toBe(true);
});
it("fails for things that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, 1)).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({a: "foo"}, 1)).toBe(false);
});
it("passes for Strings that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals("foo", "foo")).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals("foo", "foo")).toBe(true);
});
it("fails for Strings that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals("foo", "bar")).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals("foo", "bar")).toBe(false);
});
it("passes for Numbers that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(123, 123)).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(123, 123)).toBe(true);
});
it("fails for Numbers that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(123, 456)).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(123, 456)).toBe(false);
});
it("passes for Dates that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(new Date("Jan 1, 1970"), new Date("Jan 1, 1970"))).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(new Date("Jan 1, 1970"), new Date("Jan 1, 1970"))).toBe(true);
});
it("fails for Dates that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(new Date("Jan 1, 1970"), new Date("Feb 3, 1991"))).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(new Date("Jan 1, 1970"), new Date("Feb 3, 1991"))).toBe(false);
});
it("passes for Booleans that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(true, true)).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(true, true)).toBe(true);
});
it("fails for Booleans that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(true, false)).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(true, false)).toBe(false);
});
it("passes for RegExps that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(/foo/, /foo/)).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(/foo/, /foo/)).toBe(true);
});
it("fails for RegExps that are not equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals(/foo/, /bar/)).toBe(false);
expect(jasmineUnderTest.matchersUtil.equals(new RegExp("foo", "i"), new RegExp("foo"))).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(/foo/, /bar/)).toBe(false);
expect(matchersUtil.equals(new RegExp("foo", "i"), new RegExp("foo"))).toBe(false);
});
it("passes for Arrays that are equivalent", function() {
expect(jasmineUnderTest.matchersUtil.equals([1, 2], [1, 2])).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals([1, 2], [1, 2])).toBe(true);
});
it("passes for Arrays that are equivalent, with elements added by changing length", function() {
var foo = [];
var foo = [],
matchersUtil = new jasmineUnderTest.MatchersUtil();
foo.length = 1;
expect(jasmineUnderTest.matchersUtil.equals(foo, [undefined])).toBe(true);
expect(matchersUtil.equals(foo, [undefined])).toBe(true);
});
it("fails for Arrays that have different lengths", function() {
expect(jasmineUnderTest.matchersUtil.equals([1, 2], [1, 2, 3])).toBe(false);
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals([1, 2], [1, 2, 3])).toBe(false);
});
it("fails for Arrays that have different elements", function() {
expect(jasmineUnderTest.matchersUtil.equals([1, 2, 3], [1, 5, 3])).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals([1, 2, 3], [1, 5, 3])).toBe(false);
});
it("fails for Arrays whose contents are equivalent, but have differing properties", function() {
var one = [1,2,3],
two = [1,2,3];
two = [1,2,3],
matchersUtil = new jasmineUnderTest.MatchersUtil();
one.foo = 'bar';
two.foo = 'baz';
expect(jasmineUnderTest.matchersUtil.equals(one, two)).toBe(false);
expect(matchersUtil.equals(one, two)).toBe(false);
});
it("passes for Arrays with equivalent contents and properties", function() {
var one = [1,2,3],
two = [1,2,3];
two = [1,2,3],
matchersUtil = new jasmineUnderTest.MatchersUtil();
one.foo = 'bar';
two.foo = 'bar';
expect(jasmineUnderTest.matchersUtil.equals(one, two)).toBe(true);
expect(matchersUtil.equals(one, two)).toBe(true);
});
it("passes for Errors that are the same type and have the same message", function() {
expect(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("foo"))).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(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(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
});
it("fails for objects with different constructors", function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil();
function One() {}
function Two() {}
expect(jasmineUnderTest.matchersUtil.equals(new One(), new Two())).toBe(false);
expect(matchersUtil.equals(new One(), new Two())).toBe(false);
});
it("passes for Objects that are equivalent (simple case)", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
});
it("fails for Objects that are not equivalent (simple case)", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "bar"})).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({a: "foo"}, {a: "bar"})).toBe(false);
});
it("passes for Objects that are equivalent (deep case)", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo", b: { c: "bar"}}, {a: "foo", b: { c: "bar"}})).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({a: "foo", b: { c: "bar"}}, {a: "foo", b: { c: "bar"}})).toBe(true);
});
it("fails for Objects that are not equivalent (deep case)", function() {
expect(jasmineUnderTest.matchersUtil.equals({a: "foo", b: { c: "baz"}}, {a: "foo", b: { c: "bar"}})).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({a: "foo", b: { c: "baz"}}, {a: "foo", b: { c: "bar"}})).toBe(false);
});
it("passes for Objects that are equivalent (with cycles)", function() {
var actual = { a: "foo" },
expected = { a: "foo" };
expected = { a: "foo" },
matchersUtil = new jasmineUnderTest.MatchersUtil();
actual.b = actual;
expected.b = actual;
expect(jasmineUnderTest.matchersUtil.equals(actual, expected)).toBe(true);
expect(matchersUtil.equals(actual, expected)).toBe(true);
});
it("fails for Objects that are not equivalent (with cycles)", function() {
var actual = { a: "foo" },
expected = { a: "bar" };
expected = { a: "bar" },
matchersUtil = new jasmineUnderTest.MatchersUtil();
actual.b = actual;
expected.b = actual;
expect(jasmineUnderTest.matchersUtil.equals(actual, expected)).toBe(false);
expect(matchersUtil.equals(actual, expected)).toBe(false);
});
it("fails for Objects that have the same number of keys, but different keys/values", function () {
var expected = { a: undefined },
actual = { b: 1 };
actual = { b: 1 },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(actual, expected)).toBe(false);
expect(matchersUtil.equals(actual, expected)).toBe(false);
});
it("fails when comparing an empty object to an empty array (issue #114)", function() {
var emptyObject = {},
emptyArray = [];
emptyArray = [],
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(emptyObject, emptyArray)).toBe(false);
expect(jasmineUnderTest.matchersUtil.equals(emptyArray, emptyObject)).toBe(false);
expect(matchersUtil.equals(emptyObject, emptyArray)).toBe(false);
expect(matchersUtil.equals(emptyArray, emptyObject)).toBe(false);
});
it("passes for equivalent frozen objects (GitHub issue #266)", function() {
var a = { foo: 1 },
b = {foo: 1 };
b = {foo: 1 },
matchersUtil = new jasmineUnderTest.MatchersUtil();
Object.freeze(a);
Object.freeze(b);
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
expect(matchersUtil.equals(a,b)).toBe(true);
});
it("passes for equivalent Promises (GitHub issue #1314)", function() {
if (typeof Promise === 'undefined') { return; }
var p1 = new Promise(function () {}),
p2 = new Promise(function () {});
p2 = new Promise(function () {}),
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(p1, p1)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(p1, p2)).toBe(false);
expect(matchersUtil.equals(p1, p1)).toBe(true);
expect(matchersUtil.equals(p1, p2)).toBe(false);
});
describe("when running in a browser", function() {
@@ -256,6 +295,8 @@ describe("matchersUtil", function() {
return;
}
var a = document.createElement("div");
var matchersUtil = new jasmineUnderTest.MatchersUtil();
a.setAttribute("test-attr", "attr-value");
a.appendChild(document.createTextNode('test'));
@@ -263,17 +304,18 @@ describe("matchersUtil", function() {
b.setAttribute("test-attr", "attr-value");
b.appendChild(document.createTextNode('test'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
expect(matchersUtil.equals(a,b)).toBe(true);
});
it("passes for equivalent objects from different frames", function() {
if (isNotRunningInBrowser()) {
return;
}
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.eval('window.testObject = {}');
expect(jasmineUnderTest.matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true);
expect(matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true);
document.body.removeChild(iframe);
});
@@ -281,6 +323,7 @@ describe("matchersUtil", function() {
if (isNotRunningInBrowser()) {
return;
}
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var a = document.createElement("div");
a.setAttribute("test-attr", "attr-value");
a.appendChild(document.createTextNode('test'));
@@ -289,16 +332,16 @@ describe("matchersUtil", function() {
b.setAttribute("test-attr", "attr-value2");
b.appendChild(document.createTextNode('test'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(false);
expect(matchersUtil.equals(a,b)).toBe(false);
b.setAttribute("test-attr", "attr-value");
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
expect(matchersUtil.equals(a,b)).toBe(true);
b.appendChild(document.createTextNode('2'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(false);
expect(matchersUtil.equals(a,b)).toBe(false);
a.appendChild(document.createTextNode('2'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
expect(matchersUtil.equals(a,b)).toBe(true);
});
});
@@ -311,43 +354,47 @@ describe("matchersUtil", function() {
if (isNotRunningInNode()) {
return;
}
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var vm = require('vm');
var sandbox = {
obj: null
};
vm.runInNewContext('obj = {a: 1, b: 2}', sandbox);
expect(jasmineUnderTest.matchersUtil.equals(sandbox.obj, {a: 1, b: 2})).toBe(true);
expect(matchersUtil.equals(sandbox.obj, {a: 1, b: 2})).toBe(true);
});
it("passes for equivalent arrays from different vm contexts", function() {
if (isNotRunningInNode()) {
return;
}
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var vm = require('vm');
var sandbox = {
arr: null
};
vm.runInNewContext('arr = [1, 2]', sandbox);
expect(jasmineUnderTest.matchersUtil.equals(sandbox.arr, [1, 2])).toBe(true);
expect(matchersUtil.equals(sandbox.arr, [1, 2])).toBe(true);
});
});
it("passes when Any is used", function() {
var number = 3,
anyNumber = new jasmineUnderTest.Any(Number);
anyNumber = new jasmineUnderTest.Any(Number),
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(number, anyNumber)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(anyNumber, number)).toBe(true);
expect(matchersUtil.equals(number, anyNumber)).toBe(true);
expect(matchersUtil.equals(anyNumber, number)).toBe(true);
});
it("fails when Any is compared to something unexpected", function() {
var number = 3,
anyString = new jasmineUnderTest.Any(String);
anyString = new jasmineUnderTest.Any(String),
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(number, anyString)).toBe(false);
expect(jasmineUnderTest.matchersUtil.equals(anyString, number)).toBe(false);
expect(matchersUtil.equals(number, anyString)).toBe(false);
expect(matchersUtil.equals(anyString, number)).toBe(false);
});
it("passes when ObjectContaining is used", function() {
@@ -355,135 +402,209 @@ describe("matchersUtil", function() {
foo: 3,
bar: 7
},
containing = new jasmineUnderTest.ObjectContaining({foo: 3});
containing = new jasmineUnderTest.ObjectContaining({foo: 3}),
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(obj, containing)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
expect(matchersUtil.equals(obj, containing)).toBe(true);
expect(matchersUtil.equals(containing, obj)).toBe(true);
});
it("passes when MapContaining is used", function() {
jasmine.getEnv().requireFunctioningMaps();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var obj = new Map();
obj.set(1, 2);
obj.set('foo', 'bar');
var containing = new jasmineUnderTest.MapContaining(new Map());
containing.sample.set('foo', 'bar');
expect(jasmineUnderTest.matchersUtil.equals(obj, containing)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
expect(matchersUtil.equals(obj, containing)).toBe(true);
expect(matchersUtil.equals(containing, obj)).toBe(true);
});
it("passes when SetContaining is used", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var obj = new Set();
obj.add(1);
obj.add('foo');
var containing = new jasmineUnderTest.SetContaining(new Set());
containing.sample.add(1);
expect(jasmineUnderTest.matchersUtil.equals(obj, containing)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
expect(matchersUtil.equals(obj, containing)).toBe(true);
expect(matchersUtil.equals(containing, obj)).toBe(true);
});
it("passes when an asymmetric equality tester returns true", function() {
var tester = { asymmetricMatch: function(other) { return true; } };
var tester = { asymmetricMatch: function(other) { return true; } },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(false, tester)).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(tester, false)).toBe(true);
expect(matchersUtil.equals(false, tester)).toBe(true);
expect(matchersUtil.equals(tester, false)).toBe(true);
});
it("fails when an asymmetric equality tester returns false", function() {
var tester = { asymmetricMatch: function(other) { return false; } };
var tester = { asymmetricMatch: function(other) { return false; } },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(true, tester)).toBe(false);
expect(jasmineUnderTest.matchersUtil.equals(tester, true)).toBe(false);
expect(matchersUtil.equals(true, tester)).toBe(false);
expect(matchersUtil.equals(tester, true)).toBe(false);
});
it("passes when ArrayContaining is used", function() {
var arr = ["foo", "bar"];
var arr = ["foo", "bar"],
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(arr, new jasmineUnderTest.ArrayContaining(["bar"]))).toBe(true);
expect(matchersUtil.equals(arr, new jasmineUnderTest.ArrayContaining(["bar"]))).toBe(true);
});
it("passes when a custom equality matcher returns true", function() {
var tester = function(a, b) { return true; };
it("passes when a custom equality matcher passed to equals returns true", function() {
// TODO: remove this in the next major release.
var tester = function(a, b) { return true; },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(1, 2, [tester])).toBe(true);
expect(matchersUtil.equals(1, 2, [tester])).toBe(true);
});
it("passes when a custom equality matcher passed to the constructor returns true", function() {
var tester = function(a, b) { return true; },
matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester], pp: function() {}});
expect(matchersUtil.equals(1, 2)).toBe(true);
});
it("passes for two empty Objects", function () {
expect(jasmineUnderTest.matchersUtil.equals({}, {})).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({}, {})).toBe(true);
});
describe("when a custom equality matcher is installed that returns 'undefined'", function () {
describe("when a custom equality matcher is passed to equals that returns 'undefined'", function () {
// TODO: remove this in the next major release.
var tester = function(a, b) { return jasmine.undefined; };
it("passes for two empty Objects", function () {
expect(jasmineUnderTest.matchersUtil.equals({}, {}, [tester])).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals({}, {}, [tester])).toBe(true);
});
});
it("fails for equivalents when a custom equality matcher returns false", function() {
var tester = function(a, b) { return false; };
describe("when a custom equality matcher is passed to the constructor that returns 'undefined'", function () {
var tester = function(a, b) { return jasmine.undefined; };
expect(jasmineUnderTest.matchersUtil.equals(1, 1, [tester])).toBe(false);
it("passes for two empty Objects", function () {
var matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester], pp: function() {}});
expect(matchersUtil.equals({}, {})).toBe(true);
});
});
it("passes for an asymmetric equality tester that returns true when a custom equality tester return false", function() {
it("fails for equivalents when a custom equality matcher passed to equals returns false", function() {
// TODO: remove this in the next major release.
var tester = function(a, b) { return false; },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(1, 1, [tester])).toBe(false);
});
it("fails for equivalents when a custom equality matcher passed to the constructor returns false", function() {
var tester = function(a, b) { return false; },
matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester], pp: function() {}});
expect(matchersUtil.equals(1, 1)).toBe(false);
});
it("passes for an asymmetric equality tester that returns true when a custom equality tester passed to equals return false", function() {
// TODO: remove this in the next major release.
var asymmetricTester = { asymmetricMatch: function(other) { return true; } },
symmetricTester = function(a, b) { return false; };
symmetricTester = function(a, b) { return false; },
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(asymmetricTester, true, [symmetricTester])).toBe(true);
expect(jasmineUnderTest.matchersUtil.equals(true, asymmetricTester, [symmetricTester])).toBe(true);
expect(matchersUtil.equals(asymmetricTester, true, [symmetricTester])).toBe(true);
expect(matchersUtil.equals(true, asymmetricTester, [symmetricTester])).toBe(true);
});
it("passes custom equality matchers to asymmetric equality testers", function() {
var tester = function(a, b) {};
var asymmetricTester = { asymmetricMatch: jasmine.createSpy('asymmetricMatch') };
asymmetricTester.asymmetricMatch.and.returnValue(true);
var other = {};
it("passes for an asymmetric equality tester that returns true when a custom equality tester passed to the constructor return false", function() {
var asymmetricTester = { asymmetricMatch: function(other) { return true; } },
symmetricTester = function(a, b) { return false; },
matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [symmetricTester()], pp: function() {}});
expect(jasmineUnderTest.matchersUtil.equals(asymmetricTester, other, [tester])).toBe(true);
expect(asymmetricTester.asymmetricMatch).toHaveBeenCalledWith(other, [tester]);
expect(matchersUtil.equals(asymmetricTester, true)).toBe(true);
expect(matchersUtil.equals(true, asymmetricTester)).toBe(true);
});
describe("The compatibility shim passed to asymmetric equality testers", function() {
describe("When equals is called with custom equality testers", function() {
it("is both a matchersUtil and the custom equality testers passed to equals", function() {
var asymmetricTester = jasmine.createSpyObj('tester', ['asymmetricMatch']),
symmetricTester = function() { } ,
matchersUtil = new jasmineUnderTest.MatchersUtil(),
shim;
matchersUtil.equals(true, asymmetricTester, [symmetricTester]);
shim = asymmetricTester.asymmetricMatch.calls.argsFor(0)[1];
expect(shim).toEqual(jasmine.any(jasmineUnderTest.MatchersUtil));
expect(shim.length).toEqual(1);
expect(shim[0]).toEqual(symmetricTester);
});
});
describe("When equals is called with custom equality testers", function() {
it("is both a matchersUtil and the custom equality testers passed to the constructor", function() {
var asymmetricTester = jasmine.createSpyObj('tester', ['asymmetricMatch']),
symmetricTester = function() { } ,
matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [symmetricTester], pp: function() {}}),
shim;
matchersUtil.equals(true, asymmetricTester);
shim = asymmetricTester.asymmetricMatch.calls.argsFor(0)[1];
expect(shim).toEqual(jasmine.any(jasmineUnderTest.MatchersUtil));
expect(shim.length).toEqual(1);
expect(shim[0]).toEqual(symmetricTester);
});
});
});
it("passes when an Any is compared to an Any that checks for the same type", function() {
var any1 = new jasmineUnderTest.Any(Function),
any2 = new jasmineUnderTest.Any(Function);
any2 = new jasmineUnderTest.Any(Function),
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.equals(any1, any2)).toBe(true);
expect(matchersUtil.equals(any1, any2)).toBe(true);
});
it("passes for null prototype objects with same properties", function () {
var objA = Object.create(null),
objB = Object.create(null);
objB = Object.create(null),
matchersUtil = new jasmineUnderTest.MatchersUtil();
objA.name = 'test';
objB.name = 'test';
expect(jasmineUnderTest.matchersUtil.equals(objA, objB)).toBe(true);
expect(matchersUtil.equals(objA, objB)).toBe(true);
});
it("fails for null prototype objects with different properties", function () {
var objA = Object.create(null),
objB = Object.create(null);
objB = Object.create(null),
matchersUtil = new jasmineUnderTest.MatchersUtil();
objA.name = 'test';
objB.test = 'name';
expect(jasmineUnderTest.matchersUtil.equals(objA, objB)).toBe(false);
expect(matchersUtil.equals(objA, objB)).toBe(false);
});
it("passes when comparing two empty sets", function() {
jasmine.getEnv().requireFunctioningSets();
expect(jasmineUnderTest.matchersUtil.equals(new Set(), new Set())).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(new Set(), new Set())).toBe(true);
});
it("passes when comparing identical sets", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA = new Set();
setA.add(6);
setA.add(5);
@@ -491,12 +612,13 @@ describe("matchersUtil", function() {
setB.add(6);
setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
expect(matchersUtil.equals(setA, setB)).toBe(true);
});
it("passes when comparing identical sets with different insertion order and simple elements", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA = new Set();
setA.add(3);
setA.add(6);
@@ -504,12 +626,13 @@ describe("matchersUtil", function() {
setB.add(6);
setB.add(3);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
expect(matchersUtil.equals(setA, setB)).toBe(true);
});
it("passes when comparing identical sets with different insertion order and complex elements 1", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA1 = new Set();
setA1.add(['a',3]);
setA1.add([6,1]);
@@ -531,12 +654,13 @@ describe("matchersUtil", function() {
setB.add(setB1);
setB.add(setB2);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
expect(matchersUtil.equals(setA, setB)).toBe(true);
});
it("passes when comparing identical sets with different insertion order and complex elements 2", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA = new Set();
setA.add([[1,2], [3,4]]);
setA.add([[5,6], [7,8]]);
@@ -544,11 +668,12 @@ describe("matchersUtil", function() {
setB.add([[5,6], [7,8]]);
setB.add([[1,2], [3,4]]);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
expect(matchersUtil.equals(setA, setB)).toBe(true);
});
it("fails for sets with different elements", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA = new Set();
setA.add(6);
setA.add(3);
@@ -558,11 +683,12 @@ describe("matchersUtil", function() {
setB.add(4);
setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
expect(matchersUtil.equals(setA, setB)).toBe(false);
});
it("fails for sets of different size", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setA = new Set();
setA.add(6);
setA.add(3);
@@ -571,36 +697,40 @@ describe("matchersUtil", function() {
setB.add(4);
setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
expect(matchersUtil.equals(setA, setB)).toBe(false);
});
it("passes when comparing two empty maps", function() {
jasmine.getEnv().requireFunctioningMaps();
expect(jasmineUnderTest.matchersUtil.equals(new Map(), new Map())).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.equals(new Map(), new Map())).toBe(true);
});
it("passes when comparing identical maps", function() {
jasmine.getEnv().requireFunctioningMaps();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var mapA = new Map();
mapA.set(6, 5);
var mapB = new Map();
mapB.set(6, 5);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(true);
expect(matchersUtil.equals(mapA, mapB)).toBe(true);
});
it("passes when comparing identical maps with different insertion order", function() {
jasmine.getEnv().requireFunctioningMaps();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var mapA = new Map();
mapA.set("a", 3);
mapA.set(6, 1);
var mapB = new Map();
mapB.set(6, 1);
mapB.set("a", 3);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(true);
expect(matchersUtil.equals(mapA, mapB)).toBe(true);
});
it("fails for maps with different elements", function() {
jasmine.getEnv().requireFunctioningMaps();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var mapA = new Map();
mapA.set(6, 3);
mapA.set(5, 1);
@@ -608,17 +738,18 @@ describe("matchersUtil", function() {
mapB.set(6, 4);
mapB.set(5, 1);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
expect(matchersUtil.equals(mapA, mapB)).toBe(false);
});
it("fails for maps of different size", function() {
jasmine.getEnv().requireFunctioningMaps();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var mapA = new Map();
mapA.set(6, 3);
var mapB = new Map();
mapB.set(6, 4);
mapB.set(5, 1);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
expect(matchersUtil.equals(mapA, mapB)).toBe(false);
});
describe("when running in an environment with array polyfills", function() {
@@ -673,105 +804,160 @@ describe("matchersUtil", function() {
return {
self: 'asymmetric tester value',
other: 'other value'
}
};
}
},
diffBuilder = jasmine.createSpyObj('diffBuilder', ['record', 'withPath']);
diffBuilder.withPath.and.callFake(function(p, block) { block() });
jasmineUnderTest.matchersUtil.equals({x: 42}, {x: tester}, [], diffBuilder);
actual = {x: 42},
expected = {x: tester},
diffBuilder = jasmine.createSpyObj('diffBuilder', ['recordMismatch', 'withPath', 'setRoots']);
diffBuilder.withPath.and.callFake(function(p, block) { block(); });
jasmineUnderTest.matchersUtil.equals(actual, expected, [], diffBuilder);
expect(diffBuilder.setRoots).toHaveBeenCalledWith(actual, expected);
expect(diffBuilder.withPath).toHaveBeenCalledWith('x', jasmine.any(Function));
expect(diffBuilder.record). toHaveBeenCalledWith(
'other value', 'asymmetric tester value'
);
expect(diffBuilder.recordMismatch). toHaveBeenCalledWith();
});
it("records both objects when the tester does not implement valuesForDiff", function() {
var tester = {
asymmetricMatch: function() { return false; },
},
diffBuilder = jasmine.createSpyObj('diffBuilder', ['record', 'withPath']);
diffBuilder.withPath.and.callFake(function(p, block) { block() });
jasmineUnderTest.matchersUtil.equals({x: 42}, {x: tester}, [], diffBuilder);
actual = {x: 42},
expected = {x: tester},
diffBuilder = jasmine.createSpyObj('diffBuilder', ['recordMismatch', 'withPath', 'setRoots']);
diffBuilder.withPath.and.callFake(function(p, block) { block(); });
jasmineUnderTest.matchersUtil.equals(actual, expected, [], diffBuilder);
expect(diffBuilder.setRoots).toHaveBeenCalledWith(actual, expected);
expect(diffBuilder.withPath).toHaveBeenCalledWith('x', jasmine.any(Function));
expect(diffBuilder.record). toHaveBeenCalledWith(42, tester);
expect(diffBuilder.recordMismatch). toHaveBeenCalledWith();
});
});
it('uses a diffBuilder if one is provided as the fourth argument', function() {
// TODO: remove this in the next major release.
var diffBuilder = new jasmineUnderTest.DiffBuilder(),
matchersUtil = new jasmineUnderTest.MatchersUtil();
spyOn(diffBuilder, 'recordMismatch');
spyOn(diffBuilder, 'withPath').and.callThrough();
matchersUtil.equals([1], [2], [], diffBuilder);
expect(diffBuilder.withPath).toHaveBeenCalledWith('length', jasmine.any(Function));
expect(diffBuilder.withPath).toHaveBeenCalledWith(0, jasmine.any(Function));
expect(diffBuilder.recordMismatch).toHaveBeenCalledWith();
});
it('uses a diffBuilder if one is provided as the third argument', function() {
var diffBuilder = new jasmineUnderTest.DiffBuilder(),
matchersUtil = new jasmineUnderTest.MatchersUtil();
spyOn(diffBuilder, 'recordMismatch');
spyOn(diffBuilder, 'withPath').and.callThrough();
matchersUtil.equals([1], [2], diffBuilder);
expect(diffBuilder.withPath).toHaveBeenCalledWith('length', jasmine.any(Function));
expect(diffBuilder.withPath).toHaveBeenCalledWith(0, jasmine.any(Function));
expect(diffBuilder.recordMismatch).toHaveBeenCalled();
});
});
describe("contains", function() {
it("passes when expected is a substring of actual", function() {
expect(jasmineUnderTest.matchersUtil.contains("ABC", "BC")).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains("ABC", "BC")).toBe(true);
});
it("fails when expected is a not substring of actual", function() {
expect(jasmineUnderTest.matchersUtil.contains("ABC", "X")).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains("ABC", "X")).toBe(false);
});
it("passes when expected is an element in an actual array", function() {
expect(jasmineUnderTest.matchersUtil.contains(['foo', 'bar'], 'foo')).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains(['foo', 'bar'], 'foo')).toBe(true);
});
it("fails when expected is not an element in an actual array", function() {
expect(jasmineUnderTest.matchersUtil.contains(['foo', 'bar'], 'baz')).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains(['foo', 'bar'], 'baz')).toBe(false);
});
it("passes with mixed-element arrays", function() {
expect(jasmineUnderTest.matchersUtil.contains(["foo", {some: "bar"}], "foo")).toBe(true);
expect(jasmineUnderTest.matchersUtil.contains(["foo", {some: "bar"}], {some: "bar"})).toBe(true);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains(["foo", {some: "bar"}], "foo")).toBe(true);
expect(matchersUtil.contains(["foo", {some: "bar"}], {some: "bar"})).toBe(true);
});
it("uses custom equality testers if passed in and actual is an Array", function() {
var customTester = function(a, b) {return true;};
it("uses custom equality testers if passed to contains and actual is an Array", function() {
// TODO: remove this in the next major release.
var customTester = function(a, b) {return true;},
matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(jasmineUnderTest.matchersUtil.contains([1, 2], 3, [customTester])).toBe(true);
expect(matchersUtil.contains([1, 2], 3, [customTester])).toBe(true);
});
it("uses custom equality testers if passed to the constructor and actual is an Array", function() {
var customTester = function(a, b) {return true;},
matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [customTester], pp: function() {}});
expect(matchersUtil.contains([1, 2], 3)).toBe(true);
});
it("fails when actual is undefined", function() {
expect(jasmineUnderTest.matchersUtil.contains(undefined, 'A')).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains(undefined, 'A')).toBe(false);
});
it("fails when actual is null", function() {
expect(jasmineUnderTest.matchersUtil.contains(null, 'A')).toBe(false);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(matchersUtil.contains(null, 'A')).toBe(false);
});
it("passes with array-like objects", function() {
var capturedArgs = null;
var capturedArgs = null,
matchersUtil = new jasmineUnderTest.MatchersUtil();
function testFunction(){
capturedArgs = arguments;
}
testFunction('foo', 'bar');
expect(jasmineUnderTest.matchersUtil.contains(capturedArgs, 'bar')).toBe(true);
expect(matchersUtil.contains(capturedArgs, 'bar')).toBe(true);
});
it("passes for set members", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var setItem = {'foo': 'bar'};
var set = new Set();
set.add(setItem);
expect(jasmineUnderTest.matchersUtil.contains(set, setItem)).toBe(true);
expect(matchersUtil.contains(set, setItem)).toBe(true);
});
// documenting current behavior
it("fails (!) for objects that equal to a set member", function() {
jasmine.getEnv().requireFunctioningSets();
var matchersUtil = new jasmineUnderTest.MatchersUtil();
var set = new Set();
set.add({'foo': 'bar'});
expect(jasmineUnderTest.matchersUtil.contains(set, {'foo': 'bar'})).toBe(false);
expect(matchersUtil.contains(set, {'foo': 'bar'})).toBe(false);
});
});
describe("buildMessage", function() {
describe("buildFailureMessage", function() {
it("builds an English sentence for a failure case", function() {
var actual = "foo",
name = "toBar",
message = jasmineUnderTest.matchersUtil.buildFailureMessage(name, false, actual);
pp = jasmineUnderTest.makePrettyPrinter(),
matchersUtil = new jasmineUnderTest.MatchersUtil({pp: pp}),
message = matchersUtil.buildFailureMessage(name, false, actual);
expect(message).toEqual("Expected 'foo' to bar.");
});
@@ -780,7 +966,9 @@ describe("matchersUtil", function() {
var actual = "foo",
name = "toBar",
isNot = true,
message = message = jasmineUnderTest.matchersUtil.buildFailureMessage(name, isNot, actual);
pp = jasmineUnderTest.makePrettyPrinter(),
matchersUtil = new jasmineUnderTest.MatchersUtil({pp: pp}),
message = message = matchersUtil.buildFailureMessage(name, isNot, actual);
expect(message).toEqual("Expected 'foo' not to bar.");
});
@@ -788,9 +976,24 @@ describe("matchersUtil", function() {
it("builds an English sentence for an arbitrary array of expected arguments", function() {
var actual = "foo",
name = "toBar",
message = jasmineUnderTest.matchersUtil.buildFailureMessage(name, false, actual, "quux", "corge");
pp = jasmineUnderTest.makePrettyPrinter(),
matchersUtil = new jasmineUnderTest.MatchersUtil({pp: pp}),
message = matchersUtil.buildFailureMessage(name, false, actual, "quux", "corge");
expect(message).toEqual("Expected 'foo' to bar 'quux', 'corge'.");
});
it("uses the injected pretty-printer to format the expecteds and actual", function() {
var actual = "foo",
expected1 = "qux",
expected2 = "grault",
name = "toBar",
isNot = false,
pp = function(value) { return '<' + value + '>'; },
matchersUtil = new jasmineUnderTest.MatchersUtil({pp: pp}),
message = message = matchersUtil.buildFailureMessage(name, isNot, actual, expected1, expected2);
expect(message).toEqual("Expected <foo> to bar <qux>, <grault>.");
});
});
});