Commit 83ca92a6 83ca92a600c7245d4c6d068edcada00db472603d by Nicolas Perriault

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

1 parent c8af4268
......@@ -1430,18 +1430,7 @@
* @param String message Test description
*/
this.assertEquals = function(testValue, expected, message) {
var result;
if (typeof testValue !== typeof expected) {
result = false;
} else if (isType(testValue, "array")) {
result = compareArrays(testValue, expected);
} else if (isType(testValue, "object") && isType(expected, "object")) {
// comparing objects equality in Javascript is UTOPIA
throw "Tester.assertEquals() cannot compare objects, sorry";
} else {
result = expected === testValue;
}
if (result === true) {
if (this.testEquals(testValue, expected)) {
casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
this.testResults.passed++;
exporter.addSuccess("unknown", message);
......@@ -1509,6 +1498,16 @@
};
/**
* Asserts a condition resolves to false.
*
* @param Boolean condition
* @param String message Test description
*/
this.assertNot = function(condition, message) {
return this.assert(!condition, message);
};
/**
* Asserts that the provided function called with the given parameters
* will raise an exception.
*
......@@ -1586,6 +1585,34 @@
};
/**
* Tests equality between the two passed arguments.
*
* @param Mixed v1
* @param Mixed v2
* @param Boolean
*/
this.testEquals = function(v1, v2) {
if (betterTypeOf(v1) !== betterTypeOf(v2)) {
return false;
}
if (isType(v1, "function")) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object && v2 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;
};
/**
* Writes an error-style formatted message to stdout.
*
* @param String message
......
......@@ -18,6 +18,30 @@ phantom.args.forEach(function(arg) {
}
});
// phantom.Casper.Tester
casper.test.comment('Tester');
casper.test.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality');
casper.test.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
casper.test.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
casper.test.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
casper.test.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality');
casper.test.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality');
casper.test.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality');
casper.test.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality');
casper.test.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
casper.test.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
casper.test.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
casper.test.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
casper.test.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
casper.test.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality');
casper.test.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
casper.test.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
casper.test.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
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');
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');
casper.test.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
casper.test.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
// phantom.Casper.FunctionArgsInjector
casper.test.comment('FunctionArgsInjector');
function createInjector(fn, values) {
......@@ -49,7 +73,9 @@ casper.options.verbose = true;
// Casper#start()
casper.test.comment('navigating');
casper.start('tests/site/index.html', function(self) {
casper.test.comment('Casper.exists()');
self.test.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element exists');
casper.test.comment('Casper.start()');
self.test.assertTitle('CasperJS test index', 'Casper.start() casper can start itself an open an url');
self.test.assertEval(function() {
return typeof(__utils__) === "object";
......