Permalink
Browse files

Use a custom Jasmine equality tester instead of replacing the default…

… toEqual.
  • Loading branch information...
1 parent 2abce61 commit 41c8e7f36d36c5e09cf8ac919ce95af2b4255c1a @shunter shunter committed Jul 25, 2012
Showing with 30 additions and 33 deletions.
  1. +7 −1 Specs/SpecRunner.js
  2. +5 −32 Specs/addDefaultMatchers.js
  3. +18 −0 Specs/equalsMethodEqualityTester.js
View
@@ -83,10 +83,16 @@ var defineSuite;
}
//specs is an array defined by SpecList.js
- require(['Specs/addDefaultMatchers'].concat(specs), function(addDefaultMatchers) {
+ require([
+ 'Specs/addDefaultMatchers',
+ 'Specs/equalsMethodEqualityTester'
+ ].concat(specs), function(
+ addDefaultMatchers,
+ equalsMethodEqualityTester) {
var env = jasmine.getEnv();
env.beforeEach(addDefaultMatchers);
+ env.addEqualityTester(equalsMethodEqualityTester);
createTests = function() {
var isSuiteFocused = jasmine.TrivialReporter.isSuiteFocused;
@@ -2,23 +2,6 @@
define(function() {
"use strict";
- function isEqual(a, b) {
- if (a === b) {
- return true;
- }
- if (a !== null && typeof a !== 'undefined' && typeof a.equals !== 'undefined') {
- return a.equals(b);
- }
- if (b !== null && typeof b !== 'undefined' && typeof b.equals !== 'undefined') {
- return b.equals(a);
- }
-
- if (a !== null && b !== null && typeof a !== 'undefined' && typeof b !== 'undefined') {
- return a.toString() === b.toString();
- }
- return false;
- }
-
function isEqualEpsilon(a, b, epsilon) {
if (typeof a !== 'undefined' && typeof a.equalsEpsilon !== 'undefined') {
return a.equalsEpsilon(b, epsilon);
@@ -32,16 +15,8 @@ define(function() {
return false;
}
- if (!f) {
- f = isEqual;
- }
-
- var args = new Array(2).concat(Array.prototype.slice.call(arguments, 3));
-
for ( var i = 0; i < a.length; i++) {
- args[0] = a[i];
- args[1] = b[i];
- if (!f.apply(null, args)) {
+ if (!f(a[i], b[i])) {
return false;
}
}
@@ -83,20 +58,18 @@ define(function() {
return this.actual <= value;
},
- toEqual : function(expected) {
- return isEqual(this.actual, expected);
- },
-
toEqualArray : function(expected) {
- return isArrayEqual(this.actual, expected);
+ return isArrayEqual(this.actual, expected, this.env.equals_.bind(this.env));
},
toEqualEpsilon : function(expected, epsilon) {
return isEqualEpsilon(this.actual, expected, epsilon);
},
toEqualArrayEpsilon : function(expected, epsilon) {
- return isArrayEqual(this.actual, expected, isEqualEpsilon, epsilon);
+ return isArrayEqual(this.actual, expected, function(a, b) {
+ return isEqualEpsilon(a, b, epsilon);
+ });
},
toBeBetween : function(lower, upper) {
@@ -0,0 +1,18 @@
+/*global define*/
+define(function() {
+ "use strict";
+
+ return function(a, b) {
+ // if either a or b have an equals method, call it.
+ if (a !== null && typeof a !== 'undefined' && typeof a.equals === 'function') {
+ return a.equals(b);
+ }
+
+ if (b !== null && typeof b !== 'undefined' && typeof b.equals === 'function') {
+ return b.equals(a);
+ }
+
+ // fall back to default equality checks.
+ return undefined;
+ };
+});

0 comments on commit 41c8e7f

Please sign in to comment.