Permalink
Browse files

Add respondTo matcher

  • Loading branch information...
joshuaclayton committed Jun 6, 2010
1 parent 671adea commit 3f8a672d291efb5bac56826500772a0044f31278
Showing with 37 additions and 2 deletions.
  1. +9 −2 specit.js
  2. +28 −0 specit.tests.js
View
@@ -82,7 +82,8 @@
be: "Expected {actual} {not} to be true",
beA: "Expected {actual} {not} to be a",
beAn: "Expected {actual} {not} to be an",
- match: "Expected {actual} {not} to match {expected}"
+ match: "Expected {actual} {not} to match {expected}",
+ respondTo: "Expected {expected} {not} to be a method of {actual}"
}, message, options = arguments[3];
message = matcherMessages[matcher];
@@ -141,7 +142,13 @@
{assert: arguments[0].test(this),
expected: {value: arguments[0], parse: true},
actual: {value: this, parse: true}});
- }
+ },
+ respondTo: function() {
+ Matcher("respondTo", "ok",
+ {assert: typeof this[arguments[0]] === "function",
+ expected: {value: arguments[0], parse: true},
+ actual: {value: this, parse: true}});
+ },
}
});
View
@@ -74,4 +74,32 @@ describe("SpecIt", function() {
"202-555-1212".should(match, /\d{3}.\d{3}.\d{4}/);
"string".shouldNot(match, /\w{10}/);
});
+
+ it("should match on method presence", function() {
+ var myObject = {
+ attribute1: 1,
+ booleanAttr: true,
+ methodAttr: function() {}
+ };
+
+ myObject.should(respondTo, "methodAttr");
+ myObject.shouldNot(respondTo, "attribute1");
+ myObject.shouldNot(respondTo, "booleanAttr");
+ myObject.shouldNot(respondTo, "junkMethod");
+
+ var Person = function(options) {
+ this.name = options.name || "";
+ this.age = options.age || 13;
+ this.sayHi = function() {
+ return "Hello; my name is " + this.name;
+ };
+ return this;
+ };
+
+ var john = new Person({name: "John Doe", age: 35});
+ john.should(respondTo, "sayHi");
+ john.shouldNot(respondTo, "name");
+ john.shouldNot(respondTo, "age");
+ john.shouldNot(respondTo, "sayGoodbye");
+ });
});

0 comments on commit 3f8a672

Please sign in to comment.