@@ -2272,11 +2272,35 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
|||||||
this.sample = 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) {
|
ObjectContaining.prototype.asymmetricMatch = function(other) {
|
||||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||||
|
|
||||||
for (var property in this.sample) {
|
for (var property in this.sample) {
|
||||||
if (!Object.prototype.hasOwnProperty.call(other, property) ||
|
if (!hasProperty(other, property) ||
|
||||||
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,4 +55,35 @@ describe("ObjectContaining", function() {
|
|||||||
|
|
||||||
expect(containing.asymmetricMatch({})).toBe(false);
|
expect(containing.asymmetricMatch({})).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("matches defined properties", function(){
|
||||||
|
// IE 8 doesn't support `definePropery` on non-DOM nodes
|
||||||
|
if (jasmine.getEnv().ieVersion < 9) { return; }
|
||||||
|
|
||||||
|
var containing = new j$.ObjectContaining({ foo: "fooVal" });
|
||||||
|
|
||||||
|
var definedPropertyObject = {};
|
||||||
|
Object.defineProperty(definedPropertyObject, "foo", {
|
||||||
|
get: function() { return "fooVal" }
|
||||||
|
});
|
||||||
|
expect(containing.asymmetricMatch(definedPropertyObject)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("matches prototype properties", function(){
|
||||||
|
var containing = new j$.ObjectContaining({ foo: "fooVal" });
|
||||||
|
|
||||||
|
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)).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,11 +4,35 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
|||||||
this.sample = 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) {
|
ObjectContaining.prototype.asymmetricMatch = function(other) {
|
||||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||||
|
|
||||||
for (var property in this.sample) {
|
for (var property in this.sample) {
|
||||||
if (!Object.prototype.hasOwnProperty.call(other, property) ||
|
if (!hasProperty(other, property) ||
|
||||||
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
!j$.matchersUtil.equals(this.sample[property], other[property])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user