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) { ...@@ -884,24 +884,7 @@ var Tester = function Tester(casper, options) {
884 * @param Boolean 884 * @param Boolean
885 */ 885 */
886 this.testEquals = this.testEqual = function testEquals(v1, v2) { 886 this.testEquals = this.testEqual = function testEquals(v1, v2) {
887 if (utils.betterTypeOf(v1) !== utils.betterTypeOf(v2)) { 887 return utils.equals(v1, v2);
888 return false;
889 }
890 if (utils.isFunction(v1)) {
891 return v1.toString() === v2.toString();
892 }
893 if (v1 instanceof Object) {
894 if (Object.keys(v1).length !== Object.keys(v2).length) {
895 return false;
896 }
897 for (var k in v1) {
898 if (!this.testEquals(v1[k], v2[k])) {
899 return false;
900 }
901 }
902 return true;
903 }
904 return v1 === v2;
905 }; 888 };
906 889
907 /** 890 /**
......
...@@ -62,6 +62,35 @@ ...@@ -62,6 +62,35 @@
62 exports.dump = dump; 62 exports.dump = dump;
63 63
64 /** 64 /**
65 * Tests equality between the two passed arguments.
66 *
67 * @param Mixed v1
68 * @param Mixed v2
69 * @param Boolean
70 */
71 function equals(v1, v2) {
72 if (betterTypeOf(v1) !== betterTypeOf(v2)) {
73 return false;
74 }
75 if (isFunction(v1)) {
76 return v1.toString() === v2.toString();
77 }
78 if (v1 instanceof Object) {
79 if (Object.keys(v1).length !== Object.keys(v2).length) {
80 return false;
81 }
82 for (var k in v1) {
83 if (!equals(v1[k], v2[k])) {
84 return false;
85 }
86 }
87 return true;
88 }
89 return v1 === v2;
90 }
91 exports.equals = equals;
92
93 /**
65 * Returns the file extension in lower case. 94 * Returns the file extension in lower case.
66 * 95 *
67 * @param String file File path 96 * @param String file File path
......
...@@ -4,31 +4,6 @@ var t = casper.test; ...@@ -4,31 +4,6 @@ var t = casper.test;
4 4
5 casper.start(); 5 casper.start();
6 6
7 t.comment('Tester.testEquals()');
8 t.assert(t.testEquals(null, null), 'Tester.testEquals() null equality');
9 t.assertNot(t.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
10 t.assert(t.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
11 t.assertNot(t.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
12 t.assert(t.testEquals(5, 5), 'Tester.testEquals() number equality');
13 t.assertNot(t.testEquals("5", 5), 'Tester.testEquals() number equality without implicit cast');
14 t.assert(t.testEquals(5, 5.0), 'Tester.testEquals() number equality with cast');
15 t.assertNot(t.testEquals(5, 10), 'Tester.testEquals() number inequality');
16 t.assert(t.testEquals([], []), 'Tester.testEquals() empty array equality');
17 t.assert(t.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
18 t.assert(t.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
19 t.assertNot(t.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
20 t.assertNot(t.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
21 t.assertNot(t.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
22 t.assert(t.testEquals({}, {}), 'Tester.testEquals() empty object equality');
23 t.assert(t.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
24 t.assert(t.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
25 t.assertNot(t.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
26 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');
27 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');
28 t.assert(t.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
29 t.assertNot(t.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
30 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');
31
32 t.comment('Tester.sortFiles()'); 7 t.comment('Tester.sortFiles()');
33 var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir'); 8 var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
34 var files = t.findTestFiles(testDirRoot); 9 var files = t.findTestFiles(testDirRoot);
......
...@@ -2,6 +2,34 @@ var utils = require('utils'), ...@@ -2,6 +2,34 @@ var utils = require('utils'),
2 t = casper.test, 2 t = casper.test,
3 x = require('casper').selectXPath; 3 x = require('casper').selectXPath;
4 4
5 t.comment('equals()');
6 (function() {
7 t.comment('Tester.testEquals()');
8 t.assert(utils.equals(null, null), 'Tester.testEquals() null equality');
9 t.assertNot(utils.equals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
10 t.assert(utils.equals("hi", "hi"), 'Tester.testEquals() string equality');
11 t.assertNot(utils.equals("hi", "ih"), 'Tester.testEquals() string inequality');
12 t.assert(utils.equals(5, 5), 'Tester.testEquals() number equality');
13 t.assertNot(utils.equals("5", 5), 'Tester.testEquals() number equality without implicit cast');
14 t.assert(utils.equals(5, 5.0), 'Tester.testEquals() number equality with cast');
15 t.assertNot(utils.equals(5, 10), 'Tester.testEquals() number inequality');
16 t.assert(utils.equals([], []), 'Tester.testEquals() empty array equality');
17 t.assert(utils.equals([1,2], [1,2]), 'Tester.testEquals() array equality');
18 t.assert(utils.equals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
19 t.assertNot(utils.equals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
20 t.assertNot(utils.equals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
21 t.assertNot(utils.equals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
22 t.assert(utils.equals({}, {}), 'Tester.testEquals() empty object equality');
23 t.assert(utils.equals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
24 t.assert(utils.equals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
25 t.assertNot(utils.equals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
26 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');
27 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');
28 t.assert(utils.equals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
29 t.assertNot(utils.equals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
30 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');
31 })();
32
5 t.comment('fileExt()'); 33 t.comment('fileExt()');
6 (function() { 34 (function() {
7 var testCases = { 35 var testCases = {
......