Permalink
Browse files

equal Matcher now handles Objects js types.

  • Loading branch information...
1 parent ff06692 commit bb5a3730a614e6d2ea696b0052cd7bd5485658a5 @btakita btakita committed Apr 30, 2008
Showing with 101 additions and 14 deletions.
  1. +21 −6 lib/screw.builder.js
  2. +80 −8 spec/screwunit_spec.js
View
@@ -88,8 +88,9 @@ var Screw = {
return {
to: function(matcher, expected, not) {
var matched = matcher.match(expected, actual);
- if (not ? matched : !matched)
+ if (not ? matched : !matched) {
throw(matcher.failure_message(expected, actual, not));
+ }
},
to_not: function(matcher, expected) {
this.to(matcher, expected, true);
@@ -98,19 +99,33 @@ var Screw = {
},
equal: {
match: function(expected, actual) {
- if (expected instanceof Array)
+ if (expected instanceof Array) {
return Screw.Matchers.array_equal.match(actual, expected);
- else
- return actual == expected;
+ } else if(expected instanceof Object) {
+ for(var key in expected) {
+ if(expected[key] != actual[key]) {
+ return false;
+ }
+ }
+ for(var key in actual) {
+ if(actual[key] != expected[key]) {
+ return false;
+ }
+ }
+ return true;
+ } else {
+ return expected == actual;
+ }
},
failure_message: function(expected, actual, not) {
return 'expected ' + $([actual]).print() + (not ? ' to not equal ' : ' to equal ') + $([expected]).print();
}
},
array_equal: {
match: function(expected, actual) {
- for(var i = 0; i < actual.length; i++)
- if (actual[i] != expected[i]) { return false }
+ for(var i = 0; i < actual.length; i++) {
+ if(!Screw.Matchers.equal.match(expected[i], actual[i])) { return false }
+ }
return actual.length == expected.length;
}
},
View
@@ -22,7 +22,7 @@ Screw.Unit(function() {
describe('Screw.Unit', function() {
describe("Matchers", function() {
describe("#equal", function() {
- describe("when actual is not an Array", function() {
+ describe("when actual is a primitive", function() {
describe(".matches", function() {
it("matches when expected == actual", function() {
expect(true).to(equal, true);
@@ -58,10 +58,82 @@ Screw.Unit(function() {
});
describe("when actual is an Array", function() {
+ describe("and contents are primitives", function() {
+ describe(".matches", function() {
+ it("matches when Arrays the expected and actual have the same contents", function() {
+ expect([1, 2, 3]).to(equal, [1, 2, 3]);
+ expect([1, 2, 3]).to_not(equal, [3, 2, 1]);
+ });
+ });
+
+ describe(".failure_message", function() {
+ describe("on a positive failure", function() {
+ it("prints 'expected [actual] to equal [expected]", function() {
+ var message = null;
+ try {
+ expect([1, 2, 3]).to(equal, [1, 2, 4]);
+ } catch(e) {
+ message = e;
+ }
+ expect(message).to(equal, "expected [1,2,3] to equal [1,2,4]");
+ });
+ });
+
+ describe("on a negative failure", function() {
+ it("prints 'expected [actual] to not equal [expected]", function() {
+ var message = null;
+ try {
+ expect([1, 2, 3]).to_not(equal, [1, 2, 3]);
+ } catch(e) {
+ message = e;
+ }
+ expect(message).to(equal, "expected [1,2,3] to not equal [1,2,3]");
+ });
+ });
+ });
+ });
+
+ describe("and contents are Objects", function() {
+ describe(".matches", function() {
+ it("matches when all the items' expected's keys match all of actual's keys", function() {
+ expect([{foo: 1, bar: 2}, {foo: 3, bar: 4}]).to(equal, [{foo: 1, bar: 2}, {foo: 3, bar: 4}]);
+ expect([{foo: 1, bar: 2}, {foo: 3, bar: 4}]).to_not(equal, [{foo: 1, bar: 2}, {foo: 3, bar: 5}]);
+ });
+ });
+
+ describe(".failure_message", function() {
+ describe("on a positive failure", function() {
+ it("prints 'expected [actual] to equal [expected]", function() {
+ var message = null;
+ try {
+ expect([{foo: 1, bar: 2}, {foo: 3, bar: 4}]).to(equal, [{foo: 1, bar: 2}, {foo: 3, bar: 5}]);
+ } catch(e) {
+ message = e;
+ }
+ expect(message).to(equal, "expected [[object Object],[object Object]] to equal [[object Object],[object Object]]");
+ });
+ });
+
+ describe("on a negative failure", function() {
+ it("prints 'expected [actual] to not equal [expected]", function() {
+ var message = null;
+ try {
+ expect([{foo: 1, bar: 2}, {foo: 3, bar: 4}]).to_not(equal, [{foo: 1, bar: 2}, {foo: 3, bar: 4}]);
+ } catch(e) {
+ message = e;
+ }
+ expect(message).to(equal, "expected [[object Object],[object Object]] to not equal [[object Object],[object Object]]");
+ });
+ });
+ });
+ });
+ });
+
+ describe("when actual is an Object", function() {
describe(".matches", function() {
- it("matches when Arrays the expected and actual have the same contents", function() {
- expect([1, 2, 3]).to(equal, [1, 2, 3]);
- expect([1, 2, 3]).to_not(equal, [3, 2, 1]);
+ it("matches when all expected's keys match all of actual's keys", function() {
+ expect({foo: 1, bar: 2}).to(equal, {foo: 1, bar: 2});
+ expect({foo: 1, bar: 2}).to_not(equal, {foo: 1, bar: 3});
});
});
@@ -70,23 +142,23 @@ Screw.Unit(function() {
it("prints 'expected [actual] to equal [expected]", function() {
var message = null;
try {
- expect([1, 2, 3]).to(equal, [1, 2, 4]);
+ expect({foo: 1, bar: 2}).to(equal, {foo: 1, bar: 3});
} catch(e) {
message = e;
}
- expect(message).to(equal, "expected [1,2,3] to equal [1,2,4]");
+ expect(message).to(equal, "expected [[object Object]] to equal [[object Object]]");
});
});
describe("on a negative failure", function() {
it("prints 'expected [actual] to not equal [expected]", function() {
var message = null;
try {
- expect([1, 2, 3]).to_not(equal, [1, 2, 3]);
+ expect({foo: 1, bar: 2}).to_not(equal, {foo: 1, bar: 2});
} catch(e) {
message = e;
}
- expect(message).to(equal, "expected [1,2,3] to not equal [1,2,3]");
+ expect(message).to(equal, "expected [[object Object]] to not equal [[object Object]]");
});
});
});

0 comments on commit bb5a373

Please sign in to comment.