Permalink
Browse files

assertArrayEqual with tests

  • Loading branch information...
1 parent 740d86d commit 73eddeb033f69dca69bbfc51547b647410559dff @joelplane joelplane committed Jun 12, 2011
Showing with 62 additions and 0 deletions.
  1. +25 −0 assertions.js
  2. +37 −0 tests/noodleTestTest.js
View
@@ -13,4 +13,29 @@ module.exports = function(Assertion) {
return "Expected " + sys.inspect(expected) + ", but got " + sys.inspect(actual);
});
+ var arraysEqual = function(a, b) {
+ if (a.length != b.length) return false;
+ var i;
+ for(i=0; i<a.length; i++) {
+ var aVal = a[i];
+ var bVal = b[i];
+ if (aVal != bVal) {
+ if (aVal instanceof Array) {
+ if (!arraysEqual(aVal, bVal)) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+ }
+ return true;
+ };
+
+ Assertion.addAssertion('assertArrayEqual', function(expected, actual) {
+ return arraysEqual(expected,actual);
+ }, function(expected, actual) {
+ return "Expected " + sys.inspect(expected) + ", but got " + sys.inspect(actual);
+ });
+
};
View
@@ -120,6 +120,43 @@ test.context("NoodleTest dogfood test", function() {
});
});
+
+ this.it("assertArrayEqual will pass when given equal arrays", function(done) {
+ this.assertArrayEqual([1,'2'], [1,'2']);
+ this.assertArrayEqual([1,'2',[3,'4']], [1,'2',[3,'4']]);
+ done();
+ });
+
+ this.it("assertArrayEqual will fail when given inequal arrays", function(done) {
+ var myThis = this;
+
+ var failed = false;
+ var finish = function() {
+ clearTimeout(timer);
+ myThis.assert(failed);
+ done();
+ };
+
+ var t = require('../noodleTest')({quiet: true});
+ t.on('assertionFailed', function() {
+ failed = true;
+ finish();
+ });
+ t.on('assertionPassed', function() {
+ finish();
+ });
+ var timer = setTimeout(function(){
+ finish();
+ },200);
+ t.context('test context', function() {
+ this.it('test test', function(done2){
+ this.assertArrayEqual([1,'2',[3,'4']], [1,'2',[3,'5']]);
+ done2();
+ });
+ });
+
+ });
+
});
this.context('Stack trace', function() {

0 comments on commit 73eddeb

Please sign in to comment.