Commit 1fded36e 1fded36e18ab6354b77fcee8f9fbe7efc742238c by Nicolas Perriault

refactor: moved Tester.testEquals() code to utils.equals()

1 parent fec3cbb5
......@@ -884,24 +884,7 @@ var Tester = function Tester(casper, options) {
* @param Boolean
*/
this.testEquals = this.testEqual = function testEquals(v1, v2) {
if (utils.betterTypeOf(v1) !== utils.betterTypeOf(v2)) {
return false;
}
if (utils.isFunction(v1)) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object) {
if (Object.keys(v1).length !== Object.keys(v2).length) {
return false;
}
for (var k in v1) {
if (!this.testEquals(v1[k], v2[k])) {
return false;
}
}
return true;
}
return v1 === v2;
return utils.equals(v1, v2);
};
/**
......
......@@ -62,6 +62,35 @@
exports.dump = dump;
/**
* Tests equality between the two passed arguments.
*
* @param Mixed v1
* @param Mixed v2
* @param Boolean
*/
function equals(v1, v2) {
if (betterTypeOf(v1) !== betterTypeOf(v2)) {
return false;
}
if (isFunction(v1)) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object) {
if (Object.keys(v1).length !== Object.keys(v2).length) {
return false;
}
for (var k in v1) {
if (!equals(v1[k], v2[k])) {
return false;
}
}
return true;
}
return v1 === v2;
}
exports.equals = equals;
/**
* Returns the file extension in lower case.
*
* @param String file File path
......
......@@ -4,31 +4,6 @@ var t = casper.test;
casper.start();
t.comment('Tester.testEquals()');
t.assert(t.testEquals(null, null), 'Tester.testEquals() null equality');
t.assertNot(t.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
t.assert(t.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
t.assertNot(t.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
t.assert(t.testEquals(5, 5), 'Tester.testEquals() number equality');
t.assertNot(t.testEquals("5", 5), 'Tester.testEquals() number equality without implicit cast');
t.assert(t.testEquals(5, 5.0), 'Tester.testEquals() number equality with cast');
t.assertNot(t.testEquals(5, 10), 'Tester.testEquals() number inequality');
t.assert(t.testEquals([], []), 'Tester.testEquals() empty array equality');
t.assert(t.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
t.assert(t.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
t.assertNot(t.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
t.assertNot(t.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
t.assertNot(t.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
t.assert(t.testEquals({}, {}), 'Tester.testEquals() empty object equality');
t.assert(t.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
t.assert(t.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
t.assertNot(t.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
t.assert(t.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality');
t.assertNot(t.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality');
t.assert(t.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
t.assertNot(t.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
t.assert(t.testEquals([{a:1, b:2}, {c:3, d:4}], [{a:1, b:2}, {c:3, d:4}]), 'Tester.testEquals() arrays of objects');
t.comment('Tester.sortFiles()');
var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
var files = t.findTestFiles(testDirRoot);
......
......@@ -2,6 +2,34 @@ var utils = require('utils'),
t = casper.test,
x = require('casper').selectXPath;
t.comment('equals()');
(function() {
t.comment('Tester.testEquals()');
t.assert(utils.equals(null, null), 'Tester.testEquals() null equality');
t.assertNot(utils.equals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
t.assert(utils.equals("hi", "hi"), 'Tester.testEquals() string equality');
t.assertNot(utils.equals("hi", "ih"), 'Tester.testEquals() string inequality');
t.assert(utils.equals(5, 5), 'Tester.testEquals() number equality');
t.assertNot(utils.equals("5", 5), 'Tester.testEquals() number equality without implicit cast');
t.assert(utils.equals(5, 5.0), 'Tester.testEquals() number equality with cast');
t.assertNot(utils.equals(5, 10), 'Tester.testEquals() number inequality');
t.assert(utils.equals([], []), 'Tester.testEquals() empty array equality');
t.assert(utils.equals([1,2], [1,2]), 'Tester.testEquals() array equality');
t.assert(utils.equals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
t.assertNot(utils.equals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
t.assertNot(utils.equals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
t.assertNot(utils.equals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
t.assert(utils.equals({}, {}), 'Tester.testEquals() empty object equality');
t.assert(utils.equals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
t.assert(utils.equals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
t.assertNot(utils.equals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
t.assert(utils.equals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality');
t.assertNot(utils.equals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality');
t.assert(utils.equals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
t.assertNot(utils.equals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
t.assert(utils.equals([{a:1, b:2}, {c:3, d:4}], [{a:1, b:2}, {c:3, d:4}]), 'Tester.testEquals() arrays of objects');
})();
t.comment('fileExt()');
(function() {
var testCases = {
......