Skip to content

Commit

Permalink
Use a custom Jasmine equality tester instead of replacing the default…
Browse files Browse the repository at this point in the history
… toEqual.
  • Loading branch information
shunter committed Jul 25, 2012
1 parent 2abce61 commit 41c8e7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
8 changes: 7 additions & 1 deletion Specs/SpecRunner.js
Expand Up @@ -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;
Expand Down
37 changes: 5 additions & 32 deletions Specs/addDefaultMatchers.js
Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions Specs/equalsMethodEqualityTester.js
@@ -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.