This makes it easier to write high quality matchers and asymmetric equality
testers, and is also a step toward supporting custom object formatters.
Previously, Jasmine passed custom object formatters as the second argument
to matcher factories and as and the second argument to asymmetric equality
testers' `asymmetricMatch` method. Matchers and asymmetric equality testers
were responsible for passing the custom object formatters to methods like
`matchersUtil#equals`:
function toEqual(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
// ...
result.pass = util.equals(actual, expected, customEqualityTesters, diffBuilder);
And:
ArrayContaining.prototype.asymmetricMatch = function(other, customTesters) {
// ...
for (var i = 0; i < this.sample.length; i++) {
var item = this.sample[i];
if (!j$.matchersUtil.contains(other, item, customTesters)) {
return false;
}
}
With this change, that is no longer necessary. Matchers and asymmetric
equality testers can ignore the existence of custom equality testers and
still fully support them:
function toEqual(util) {
return {
compare: function(actual, expected) {
// ...
result.pass = util.equals(actual, expected, diffBuilder);
And:
ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
// ...
for (var i = 0; i < this.sample.length; i++) {
var item = this.sample[i];
if (!matchersUtil.contains(other, item)) {
return false;
}
}
The old interfaces are still supported, for now, but will be deprecated
in a future commit and removed in the next major release after that.
In addition to making matchers and custom equality testers simpler,
this change sets the stage for adding support for custom object
formatters. Those will be architecturally similar to custom equality
testers, and by injecting a `MatchersUtil` instance everywhere we can
add them without requiring user code to pass them around as used to be
the case with custom object formatters.
110 lines
4.0 KiB
JavaScript
110 lines
4.0 KiB
JavaScript
describe("ObjectContaining", function() {
|
|
|
|
it("matches any actual to an empty object", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch("foo", matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("does not match an empty object actual", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining("foo");
|
|
|
|
expect(function() {
|
|
containing.asymmetricMatch({})
|
|
}).toThrowError(/not 'foo'/)
|
|
});
|
|
|
|
it("matches when the key/value pair is present in the actual", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({foo: "fooVal"});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({foo: "fooVal", bar: "barVal"}, matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("does not match when the key/value pair is not present in the actual", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({foo: "fooVal"});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({bar: "barVal", quux: "quuxVal"}, matchersUtil)).toBe(false);
|
|
});
|
|
|
|
it("does not match when the key is present but the value is different in the actual", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({foo: "other"});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({foo: "fooVal", bar: "barVal"}, matchersUtil)).toBe(false);
|
|
});
|
|
|
|
it("jasmineToString's itself", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({});
|
|
|
|
expect(containing.jasmineToString()).toMatch("<jasmine.objectContaining");
|
|
});
|
|
|
|
it("matches recursively", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({one: new jasmineUnderTest.ObjectContaining({two: {}})});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({one: {two: {}}}, matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("matches when key is present with undefined value", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({ one: undefined });
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({ one: undefined }, matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("does not match when key with undefined value is not present", function() {
|
|
var containing = new jasmineUnderTest.ObjectContaining({ one: undefined });
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch({}, matchersUtil)).toBe(false);
|
|
});
|
|
|
|
it("matches defined properties", function(){
|
|
var containing = new jasmineUnderTest.ObjectContaining({ foo: "fooVal" });
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
var definedPropertyObject = {};
|
|
Object.defineProperty(definedPropertyObject, "foo", {
|
|
get: function() { return "fooVal" }
|
|
});
|
|
expect(containing.asymmetricMatch(definedPropertyObject, matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("matches prototype properties", function(){
|
|
var containing = new jasmineUnderTest.ObjectContaining({ foo: "fooVal" });
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
var prototypeObject = {foo: "fooVal"};
|
|
var obj;
|
|
|
|
if (Object.create) {
|
|
obj = Object.create(prototypeObject);
|
|
} else {
|
|
function Foo() {}
|
|
Foo.prototype = prototypeObject;
|
|
Foo.prototype.constructor = Foo;
|
|
obj = new Foo();
|
|
}
|
|
|
|
expect(containing.asymmetricMatch(obj, matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("uses custom equality testers", function() {
|
|
var tester = function(a, b) {
|
|
// All "foo*" strings match each other.
|
|
if (typeof a == "string" && typeof b == "string" &&
|
|
a.substr(0, 3) == "foo" && b.substr(0, 3) == "foo") {
|
|
return true;
|
|
}
|
|
};
|
|
var containing = new jasmineUnderTest.ObjectContaining({foo: "fooVal"});
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester]});
|
|
|
|
expect(containing.asymmetricMatch({foo: "fooBar"}, matchersUtil)).toBe(true);
|
|
});
|
|
});
|