Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added have_length matcher

  • Loading branch information...
commit 3925d7bf041bb9eb06823770614a7100ae8b3ca6 1 parent 5d64e8e
@nathansobo nathansobo authored
Showing with 31 additions and 0 deletions.
  1. +12 −0 lib/screw.matchers.js
  2. +19 −0 spec/matchers_spec.js
View
12 lib/screw.matchers.js
@@ -62,6 +62,18 @@ Screw.Matchers = (function($) {
}
},
+ have_length: {
+ match: function(expected, actual) {
+ if (actual.length == undefined) throw(actual.toString() + " does not respond to length");
+
+ return actual.length == expected;
+ },
+
+ failure_message: function(expected, actual, not) {
+ return 'expected ' + $.print(actual) + (not ? ' to not' : ' to') + ' have length ' + expected;
+ }
+ },
+
be_null: {
match: function(expected, actual) {
return actual == null;
View
19 spec/matchers_spec.js
@@ -90,6 +90,25 @@ Screw.Unit(function() {
});
});
+ describe('#have_length', function() {
+ it("matches Arrays of the expected length", function() {
+ expect([]).to(have_length, 0);
+ expect([1]).to(have_length, 1);
+ expect([1, 2, 3]).to_not(have_length, 4);
+ });
+
+ describe(".failure_message", function() {
+ it("prints 'expected [actual] to (not) have length [expected]", function() {
+ var message = null;
+ try { expect([1, 2]).to(have_length, 4) } catch(e) { message = e }
+ expect(message).to(equal, 'expected [ 1, 2 ] to have length 4');
+
+ try { expect([1]).to_not(have_length, 1) } catch(e) { message = e }
+ expect(message).to(equal, 'expected [ 1 ] to not have length 1');
+ });
+ });
+ });
+
describe('#be_null', function() {
it("matches null", function() {
expect(null).to(be_null);
Please sign in to comment.
Something went wrong with that request. Please try again.