Permalink
Browse files

Add empty matcher

  • Loading branch information...
1 parent 3f0b33f commit 29d2081441c8089946bc117ee840de0ab0c941b2 @joshuaclayton committed Jun 6, 2010
Showing with 28 additions and 1 deletion.
  1. +1 −0 README.markdown
  2. +16 −0 specit.js
  3. +11 −1 specit.tests.js
View
1 README.markdown
@@ -62,6 +62,7 @@ I don't have the patience to write my own JS testing framework so I figured I'd
* beLessThanOrEqualTo (checks a number is less than or equal to another)
* beGreaterThan (checks a number is greater than another)
* beGreaterThanOrEqualTo (checks a number is greater than or equal to another)
+* beEmpty (checks if an array, object literal, or string is empty)
* beOnThePage (checks that an element is present on a page)
## Other supported features
View
16 specit.js
@@ -102,6 +102,7 @@
beGreaterThan: "Expected {actual} {not} to be greater than {expected}",
beGreaterThanOrEqualTo: "Expected {actual} {not} to be greater than or equal to {expected}",
beOnThePage: "Expected {actual} {not} to be on the page",
+ beEmpty: "Expected {actual} {not} to be empty",
}, message, options = arguments[3];
message = matcherMessages[matcher];
@@ -197,6 +198,21 @@
expected: {value: arguments[0], parse: true},
actual: {value: $(this).selector, parse: true}});
},
+ beEmpty: function() {
+ var empty = true;
+ if (this.constructor == Object && this.length == undefined) {
+ for(var key in this) {
+ if(!/should|shouldNot|indexOf/.test(key)) { empty = false; }
+ }
+ } else {
+ if(this.length > 0) { empty = false; }
+ }
+
+ Matcher("beEmpty", "ok",
+ {assert: empty,
+ expected: {value: arguments[0], parse: true},
+ actual: {value: this, parse: true}});
+ },
}
});
View
12 specit.tests.js
@@ -150,9 +150,19 @@ describe("SpecIt", function() {
(0.01).shouldNot(beGreaterThanOrEqualTo, 0.05);
});
+ it("should match on emptiness", function() {
+ [].should(beEmpty);
+ ({}).should(beEmpty);
+ (0).should(beEmpty);
+ (5).should(beEmpty);
+ "".should(beEmpty);
+ [1, 2].shouldNot(beEmpty);
+ ({one: 1}).shouldNot(beEmpty);
+ "one".shouldNot(beEmpty);
+ });
+
it("should match on elements on a page", function() {
$(".workspace").append("<div class='great'>");
-
$(".workspace .great").should(beOnThePage);
$(".workspace .non-existant").shouldNot(beOnThePage);
$(".workspace").empty();

0 comments on commit 29d2081

Please sign in to comment.