This allows custom equality testers to affect asymmetric matches. This avoid suprises when combining addCustomEqualityTester with objectContaining or arrayContaining. Closes #1138
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
getJasmineRequireObj().ObjectContaining = function(j$) {
|
|
|
|
function ObjectContaining(sample) {
|
|
this.sample = sample;
|
|
}
|
|
|
|
function getPrototype(obj) {
|
|
if (Object.getPrototypeOf) {
|
|
return Object.getPrototypeOf(obj);
|
|
}
|
|
|
|
if (obj.constructor.prototype == obj) {
|
|
return null;
|
|
}
|
|
|
|
return obj.constructor.prototype;
|
|
}
|
|
|
|
function hasProperty(obj, property) {
|
|
if (!obj) {
|
|
return false;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(obj, property)) {
|
|
return true;
|
|
}
|
|
|
|
return hasProperty(getPrototype(obj), property);
|
|
}
|
|
|
|
ObjectContaining.prototype.asymmetricMatch = function(other, customTesters) {
|
|
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
|
|
|
for (var property in this.sample) {
|
|
if (!hasProperty(other, property) ||
|
|
!j$.matchersUtil.equals(this.sample[property], other[property], customTesters)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
ObjectContaining.prototype.jasmineToString = function() {
|
|
return '<jasmine.objectContaining(' + j$.pp(this.sample) + ')>';
|
|
};
|
|
|
|
return ObjectContaining;
|
|
};
|