Skip to content

Commit

Permalink
Coerce matcher.or/and arguments into matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-glenjamin authored and glenjamin committed Feb 23, 2014
1 parent ca32555 commit 0a40fe6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
8 changes: 6 additions & 2 deletions lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@
}

matcher.or = function (m2) {
if (!isMatcher(m2)) {
if (!arguments.length) {
throw new TypeError("Matcher expected");
} else if (!isMatcher(m2)) {
m2 = match(m2);
}
var m1 = this;
var or = sinon.create(matcher);
Expand All @@ -78,8 +80,10 @@
};

matcher.and = function (m2) {
if (!isMatcher(m2)) {
if (!arguments.length) {
throw new TypeError("Matcher expected");
} else if (!isMatcher(m2)) {
m2 = match(m2);
}
var m1 = this;
var and = sinon.create(matcher);
Expand Down
46 changes: 35 additions & 11 deletions test/sinon/match_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,14 @@ buster.testCase("sinon.match", {
assert.exception(function () {
sinon.match.instanceOf(Error).or();
}, "TypeError");
assert.exception(function () {
sinon.match.same({}).or({});
}, "TypeError");
},

"will coerce argument to matcher": function () {
var abcOrDef = sinon.match("abc").or("def");

assert(sinon.match.isMatcher(abcOrDef));
assert.equals(abcOrDef.toString(),
"match(\"abc\").or(match(\"def\"))");
},

"returns true if either matcher matches": function () {
Expand All @@ -614,11 +619,18 @@ buster.testCase("sinon.match", {
},

"returns false if neither matcher matches": function () {
var numberOrString = sinon.match.number.or(sinon.match.string);
var numberOrAbc = sinon.match.number.or("abc");

assert.isFalse(numberOrAbc.test(/.+/));
assert.isFalse(numberOrAbc.test(new Date()));
assert.isFalse(numberOrAbc.test({}));
},

"can be used with undefined": function () {
var numberOrUndef = sinon.match.number.or(undefined);

assert.isFalse(numberOrString.test(/.+/));
assert.isFalse(numberOrString.test(new Date()));
assert.isFalse(numberOrString.test({}));
assert(numberOrUndef.test(123));
assert(numberOrUndef.test(undefined));
}
},

Expand All @@ -634,13 +646,18 @@ buster.testCase("sinon.match", {
assert.exception(function () {
sinon.match.instanceOf(Error).and();
}, "TypeError");
assert.exception(function () {
sinon.match.same({}).and({});
}, "TypeError");
},

"will coerce to matcher": function () {
var abcOrObj = sinon.match("abc").or({a:1});

assert(sinon.match.isMatcher(abcOrObj));
assert.equals(abcOrObj.toString(),
"match(\"abc\").or(match(a: 1))");
},

"returns true if both matchers match": function () {
var fooAndBar = sinon.match.has("foo").and(sinon.match.has("bar"));
var fooAndBar = sinon.match.has("foo").and({ bar: "bar" });

assert(fooAndBar.test({ foo: "foo", bar: "bar" }));
},
Expand All @@ -650,6 +667,13 @@ buster.testCase("sinon.match", {

assert.isFalse(fooAndBar.test({ foo: "foo" }));
assert.isFalse(fooAndBar.test({ bar: "bar" }));
},

"can be used with undefined": function () {
var falsyAndUndefined = sinon.match.falsy.and(undefined);

assert.isFalse(falsyAndUndefined.test(false));
assert(falsyAndUndefined.test(undefined));
}
}
});

0 comments on commit 0a40fe6

Please sign in to comment.