Test comparison of objects from different frames
This commit is contained in:
@@ -93,6 +93,24 @@ describe("matchersUtil", function() {
|
|||||||
expect(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
|
expect(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("fails for objects with different constructors", function() {
|
||||||
|
function One() {}
|
||||||
|
function Two() {}
|
||||||
|
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(new One(), new Two())).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (typeof document === 'object') {
|
||||||
|
it("passes for equivalent objects from different frames", function() {
|
||||||
|
var iframe = document.createElement('iframe');
|
||||||
|
document.body.appendChild(iframe);
|
||||||
|
console.log(iframe);
|
||||||
|
iframe.contentWindow.eval('window.testObject = {}');
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true);
|
||||||
|
document.body.removeChild(iframe);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
it("passes for Objects that are equivalent (simple case)", function() {
|
it("passes for Objects that are equivalent (simple case)", function() {
|
||||||
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
|
expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -178,8 +178,8 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
// Objects with different constructors are not equivalent, but `Object`s
|
// Objects with different constructors are not equivalent, but `Object`s
|
||||||
// or `Array`s from different frames are.
|
// or `Array`s from different frames are.
|
||||||
var aCtor = a.constructor, bCtor = b.constructor;
|
var aCtor = a.constructor, bCtor = b.constructor;
|
||||||
if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor &&
|
if (aCtor !== bCtor && !(isObjectConstructor(aCtor) &&
|
||||||
isFunction(bCtor) && bCtor instanceof bCtor)) {
|
isObjectConstructor(bCtor))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -239,5 +239,13 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
function isFunction(obj) {
|
function isFunction(obj) {
|
||||||
return typeof obj === 'function';
|
return typeof obj === 'function';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isObjectConstructor(ctor) {
|
||||||
|
// aCtor instanceof aCtor is true for the Object and Function
|
||||||
|
// constructors (since a constructor is-a Function and a function is-a
|
||||||
|
// Object). We don't just compare ctor === Object because the constructor
|
||||||
|
// might come from a different frame with different globals.
|
||||||
|
return isFunction(ctor) && ctor instanceof ctor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user