diff --git a/lib/sinon/match.js b/lib/sinon/match.js index 38739bf91..e78e7ed56 100644 --- a/lib/sinon/match.js +++ b/lib/sinon/match.js @@ -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); @@ -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); diff --git a/test/sinon/match_test.js b/test/sinon/match_test.js index 1f90f8031..180c2131b 100644 --- a/test/sinon/match_test.js +++ b/test/sinon/match_test.js @@ -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 () { @@ -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)); } }, @@ -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" })); }, @@ -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)); } } });