improved Tester.assertEquals() to allow comparison of arrays
Showing
1 changed file
with
29 additions
and
3 deletions
... | @@ -1379,6 +1379,21 @@ | ... | @@ -1379,6 +1379,21 @@ |
1379 | var PASS = this.options.PASS || "PASS"; | 1379 | var PASS = this.options.PASS || "PASS"; |
1380 | var FAIL = this.options.FAIL || "FAIL"; | 1380 | var FAIL = this.options.FAIL || "FAIL"; |
1381 | 1381 | ||
1382 | function compareArrays(a, b) { | ||
1383 | if (a.length !== b.length) { | ||
1384 | return false; | ||
1385 | } | ||
1386 | a.forEach(function(item, i) { | ||
1387 | if (isType(item, "array") && !compareArrays(item, b[i])) { | ||
1388 | return false; | ||
1389 | } | ||
1390 | if (item !== b[i]) { | ||
1391 | return false; | ||
1392 | } | ||
1393 | }); | ||
1394 | return true; | ||
1395 | } | ||
1396 | |||
1382 | // properties | 1397 | // properties |
1383 | this.testResults = { | 1398 | this.testResults = { |
1384 | passed: 0, | 1399 | passed: 0, |
... | @@ -1410,12 +1425,23 @@ | ... | @@ -1410,12 +1425,23 @@ |
1410 | /** | 1425 | /** |
1411 | * Asserts that two values are strictly equals. | 1426 | * Asserts that two values are strictly equals. |
1412 | * | 1427 | * |
1413 | * @param Boolean testValue The value to test | 1428 | * @param Mixed testValue The value to test |
1414 | * @param Boolean expected The expected value | 1429 | * @param Mixed expected The expected value |
1415 | * @param String message Test description | 1430 | * @param String message Test description |
1416 | */ | 1431 | */ |
1417 | this.assertEquals = function(testValue, expected, message) { | 1432 | this.assertEquals = function(testValue, expected, message) { |
1418 | if (expected === testValue) { | 1433 | var result; |
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) { | ||
1419 | casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message)); | 1445 | casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message)); |
1420 | this.testResults.passed++; | 1446 | this.testResults.passed++; |
1421 | exporter.addSuccess("unknown", message); | 1447 | exporter.addSuccess("unknown", message); | ... | ... |
-
Please register or sign in to post a comment