Merge pull request #1158 from benchristel/move-functions-to-higher-scope

Restructure eq code and tests
This commit is contained in:
Greg Cobb
2016-07-21 21:28:45 -07:00
committed by GitHub
2 changed files with 90 additions and 64 deletions
+32 -14
View File
@@ -93,6 +93,13 @@ 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);
});
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);
}); });
@@ -149,25 +156,34 @@ describe("matchersUtil", function() {
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
}); });
it("passes for equivalent DOM nodes", function() { describe("when running in a browser", function() {
beforeEach(function() {
if (typeof document === 'undefined') { if (typeof document === 'undefined') {
return; pending();
} }
});
it("passes for equivalent DOM nodes", function() {
var a = document.createElement("div"); var a = document.createElement("div");
a.setAttribute("test-attr", "attr-value") a.setAttribute("test-attr", "attr-value");
a.appendChild(document.createTextNode('test')); a.appendChild(document.createTextNode('test'));
var b = document.createElement("div"); var b = document.createElement("div");
b.setAttribute("test-attr", "attr-value") b.setAttribute("test-attr", "attr-value");
b.appendChild(document.createTextNode('test')); b.appendChild(document.createTextNode('test'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
}); });
it("passes for equivalent objects from different frames", function() {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.eval('window.testObject = {}');
expect(jasmineUnderTest.matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true);
document.body.removeChild(iframe);
});
it("fails for DOM nodes with different attributes or child nodes", function() { it("fails for DOM nodes with different attributes or child nodes", function() {
if (typeof document === 'undefined') {
return;
}
var a = document.createElement("div"); var a = document.createElement("div");
a.setAttribute("test-attr", "attr-value") a.setAttribute("test-attr", "attr-value")
a.appendChild(document.createTextNode('test')); a.appendChild(document.createTextNode('test'));
@@ -187,11 +203,16 @@ describe("matchersUtil", function() {
a.appendChild(document.createTextNode('2')); a.appendChild(document.createTextNode('2'));
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
}); });
});
describe("when running in Node", function() {
beforeEach(function() {
if (typeof require !== 'function') {
pending();
}
});
it("passes for equivalent objects from different vm contexts", function() { it("passes for equivalent objects from different vm contexts", function() {
if (typeof require !== 'function') {
return;
}
var vm = require('vm'); var vm = require('vm');
var sandbox = { var sandbox = {
obj: null obj: null
@@ -202,10 +223,6 @@ describe("matchersUtil", function() {
}); });
it("passes for equivalent arrays from different vm contexts", function() { it("passes for equivalent arrays from different vm contexts", function() {
if (typeof require !== 'function') {
return;
}
var vm = require('vm'); var vm = require('vm');
var sandbox = { var sandbox = {
arr: null arr: null
@@ -214,6 +231,7 @@ describe("matchersUtil", function() {
expect(jasmineUnderTest.matchersUtil.equals(sandbox.arr, [1, 2])).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(sandbox.arr, [1, 2])).toBe(true);
}); });
});
it("passes when Any is used", function() { it("passes when Any is used", function() {
var number = 3, var number = 3,
+10 -2
View File
@@ -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;
} }
} }
@@ -231,6 +231,7 @@ getJasmineRequireObj().matchersUtil = function(j$) {
return extraKeys; return extraKeys;
} }
}
function has(obj, key) { function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key); return Object.prototype.hasOwnProperty.call(obj, key);
@@ -239,5 +240,12 @@ 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;
} }
}; };