diff --git a/spec/suites/EnvSpec.js b/spec/suites/EnvSpec.js index 7da6ee0..e907834 100644 --- a/spec/suites/EnvSpec.js +++ b/spec/suites/EnvSpec.js @@ -65,4 +65,77 @@ describe("jasmine.Env", function() { expect(fakeReporter.log).wasCalledWith("message"); }); }); + + describe("equality testing", function() { + describe("with custom equality testers", function() { + var aObj, bObj, isEqual; + + beforeEach(function() { + env.addEqualityTester(function(a, b) { + aObj = a; + bObj = b; + return isEqual; + }); + }); + + it("should call the custom equality tester with two objects for comparison", function() { + env.equals_("1", "2"); + expect(aObj).toEqual("1"); + expect(bObj).toEqual("2"); + }); + + describe("when the custom equality tester returns false", function() { + beforeEach(function() { + isEqual = false; + }); + + it("should give custom equality testers precedence", function() { + expect(env.equals_('abc', 'abc')).toBeFalsy(); + var o = new Object(); + expect(env.equals_(o, o)).toBeFalsy(); + }); + }); + + + describe("when the custom equality tester returns true", function() { + beforeEach(function() { + isEqual = true; + }); + + it("should give custom equality testers precedence", function() { + expect(env.equals_('abc', 'def')).toBeTruthy(); + expect(env.equals_(true, false)).toBeTruthy(); + }); + }); + + describe("when the custom equality tester returns undefined", function() { + beforeEach(function() { + isEqual = jasmine.undefined; + }); + + it("should use normal equality rules", function() { + expect(env.equals_('abc', 'abc')).toBeTruthy(); + expect(env.equals_('abc', 'def')).toBeFalsy(); + }); + + describe("even if there are several", function() { + beforeEach(function() { + env.addEqualityTester(function(a, b) { return jasmine.undefined; }); + env.addEqualityTester(function(a, b) { return jasmine.undefined; }); + }); + + it("should use normal equality rules", function() { + expect(env.equals_('abc', 'abc')).toBeTruthy(); + expect(env.equals_('abc', 'def')).toBeFalsy(); + }); + }); + }); + + it("should evaluate custom equality testers in the order they are declared", function() { + isEqual = false; + env.addEqualityTester(function(a, b) { return true; }); + expect(env.equals_('abc', 'abc')).toBeFalsy(); + }); + }); + }); }); \ No newline at end of file diff --git a/src/Env.js b/src/Env.js index 5d288b3..8a50035 100644 --- a/src/Env.js +++ b/src/Env.js @@ -181,6 +181,12 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { mismatchKeys = mismatchKeys || []; mismatchValues = mismatchValues || []; + for (var i = 0; i < this.equalityTesters_.length; i++) { + var equalityTester = this.equalityTesters_[i]; + var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); + if (result !== jasmine.undefined) return result; + } + if (a === b) return true; if (a === jasmine.undefined || a === null || b === jasmine.undefined || b === null) { @@ -207,12 +213,6 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { return this.compareObjects_(a, b, mismatchKeys, mismatchValues); } - for (var i = 0; i < this.equalityTesters_.length; i++) { - var equalityTester = this.equalityTesters_[i]; - var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); - if (result !== jasmine.undefined) return result; - } - //Straight check return (a === b); };