Commit 83ca92a6 83ca92a600c7245d4c6d068edcada00db472603d by Nicolas Perriault

added Casper.testEquals(), better handling of equality testing, tests

1 parent c8af4268
...@@ -1430,18 +1430,7 @@ ...@@ -1430,18 +1430,7 @@
1430 * @param String message Test description 1430 * @param String message Test description
1431 */ 1431 */
1432 this.assertEquals = function(testValue, expected, message) { 1432 this.assertEquals = function(testValue, expected, message) {
1433 var result; 1433 if (this.testEquals(testValue, expected)) {
1434 if (typeof testValue !== typeof expected) {
1435 result = false;
1436 } else if (isType(testValue, "array")) {
1437 result = compareArrays(testValue, expected);
1438 } else if (isType(testValue, "object") && isType(expected, "object")) {
1439 // comparing objects equality in Javascript is UTOPIA
1440 throw "Tester.assertEquals() cannot compare objects, sorry";
1441 } else {
1442 result = expected === testValue;
1443 }
1444 if (result === true) {
1445 casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message)); 1434 casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
1446 this.testResults.passed++; 1435 this.testResults.passed++;
1447 exporter.addSuccess("unknown", message); 1436 exporter.addSuccess("unknown", message);
...@@ -1509,6 +1498,16 @@ ...@@ -1509,6 +1498,16 @@
1509 }; 1498 };
1510 1499
1511 /** 1500 /**
1501 * Asserts a condition resolves to false.
1502 *
1503 * @param Boolean condition
1504 * @param String message Test description
1505 */
1506 this.assertNot = function(condition, message) {
1507 return this.assert(!condition, message);
1508 };
1509
1510 /**
1512 * Asserts that the provided function called with the given parameters 1511 * Asserts that the provided function called with the given parameters
1513 * will raise an exception. 1512 * will raise an exception.
1514 * 1513 *
...@@ -1586,6 +1585,34 @@ ...@@ -1586,6 +1585,34 @@
1586 }; 1585 };
1587 1586
1588 /** 1587 /**
1588 * Tests equality between the two passed arguments.
1589 *
1590 * @param Mixed v1
1591 * @param Mixed v2
1592 * @param Boolean
1593 */
1594 this.testEquals = function(v1, v2) {
1595 if (betterTypeOf(v1) !== betterTypeOf(v2)) {
1596 return false;
1597 }
1598 if (isType(v1, "function")) {
1599 return v1.toString() === v2.toString();
1600 }
1601 if (v1 instanceof Object && v2 instanceof Object) {
1602 if (Object.keys(v1).length !== Object.keys(v2).length) {
1603 return false;
1604 }
1605 for (var k in v1) {
1606 if (!this.testEquals(v1[k], v2[k])) {
1607 return false;
1608 }
1609 }
1610 return true;
1611 }
1612 return v1 === v2;
1613 };
1614
1615 /**
1589 * Writes an error-style formatted message to stdout. 1616 * Writes an error-style formatted message to stdout.
1590 * 1617 *
1591 * @param String message 1618 * @param String message
......
...@@ -18,6 +18,30 @@ phantom.args.forEach(function(arg) { ...@@ -18,6 +18,30 @@ phantom.args.forEach(function(arg) {
18 } 18 }
19 }); 19 });
20 20
21 // phantom.Casper.Tester
22 casper.test.comment('Tester');
23 casper.test.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality');
24 casper.test.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
25 casper.test.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
26 casper.test.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
27 casper.test.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality');
28 casper.test.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality');
29 casper.test.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality');
30 casper.test.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality');
31 casper.test.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
32 casper.test.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
33 casper.test.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
34 casper.test.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
35 casper.test.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
36 casper.test.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality');
37 casper.test.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
38 casper.test.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
39 casper.test.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
40 casper.test.assert(casper.test.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');
41 casper.test.assertNot(casper.test.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');
42 casper.test.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
43 casper.test.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
44
21 // phantom.Casper.FunctionArgsInjector 45 // phantom.Casper.FunctionArgsInjector
22 casper.test.comment('FunctionArgsInjector'); 46 casper.test.comment('FunctionArgsInjector');
23 function createInjector(fn, values) { 47 function createInjector(fn, values) {
...@@ -49,7 +73,9 @@ casper.options.verbose = true; ...@@ -49,7 +73,9 @@ casper.options.verbose = true;
49 // Casper#start() 73 // Casper#start()
50 casper.test.comment('navigating'); 74 casper.test.comment('navigating');
51 casper.start('tests/site/index.html', function(self) { 75 casper.start('tests/site/index.html', function(self) {
76 casper.test.comment('Casper.exists()');
52 self.test.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element exists'); 77 self.test.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element exists');
78 casper.test.comment('Casper.start()');
53 self.test.assertTitle('CasperJS test index', 'Casper.start() casper can start itself an open an url'); 79 self.test.assertTitle('CasperJS test index', 'Casper.start() casper can start itself an open an url');
54 self.test.assertEval(function() { 80 self.test.assertEval(function() {
55 return typeof(__utils__) === "object"; 81 return typeof(__utils__) === "object";
......