Skip to content
Browse files

Evaluate custom equality testers before any others.

  • Loading branch information...
1 parent e9a2b0d commit 983095246141f8a611d378ff2f323184992284bd @xian xian committed
Showing with 79 additions and 6 deletions.
  1. +73 −0 spec/suites/EnvSpec.js
  2. +6 −6 src/Env.js
View
73 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();
+ });
+ });
+ });
});
View
12 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);
};

0 comments on commit 9830952

Please sign in to comment.
Something went wrong with that request. Please try again.