Files
jasmine/src/core/asymmetric_equality/ObjectContaining.js
2025-10-05 06:53:54 -07:00

71 lines
1.6 KiB
JavaScript

getJasmineRequireObj().ObjectContaining = function(j$) {
'use strict';
function ObjectContaining(sample) {
this.sample = sample;
}
function hasProperty(obj, property) {
if (!obj || typeof obj !== 'object') {
return false;
}
if (Object.prototype.hasOwnProperty.call(obj, property)) {
return true;
}
return hasProperty(Object.getPrototypeOf(obj), property);
}
ObjectContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
if (typeof this.sample !== 'object') {
throw new Error(
"You must provide an object to objectContaining, not '" +
this.sample +
"'."
);
}
if (typeof other !== 'object') {
return false;
}
for (const property in this.sample) {
if (
!hasProperty(other, property) ||
!matchersUtil.equals(this.sample[property], other[property])
) {
return false;
}
}
return true;
};
ObjectContaining.prototype.valuesForDiff_ = function(other, pp) {
if (!j$.private.isObject(other)) {
return {
self: this.jasmineToString(pp),
other: other
};
}
const filteredOther = {};
Object.keys(this.sample).forEach(function(k) {
// eq short-circuits comparison of objects that have different key sets,
// so include all keys even if undefined.
filteredOther[k] = other[k];
});
return {
self: this.sample,
other: filteredOther
};
};
ObjectContaining.prototype.jasmineToString = function(pp) {
return '<jasmine.objectContaining(' + pp(this.sample) + ')>';
};
return ObjectContaining;
};