Skip to content

Commit

Permalink
Add match.every (sinonjs#1624)
Browse files Browse the repository at this point in the history
Added new matcher - match.every, which matches on an object or
iterable if all of the properties (or elements, respectively) match
the given predicate.
  • Loading branch information
nivsherf committed Jan 16, 2018
1 parent acebab9 commit ac41ad1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ match.hasNested = function (property, value) {
}, message);
};

match.every = function (predicate) {
if (!isMatcher(predicate)) {
throw new TypeError("Matcher expected");
}

return match(function (actual) {
if (typeOf(actual) === "object") {
return every(Object.keys(actual), function (key) {
return predicate.test(actual[key]);
});
}

return !!actual && typeOf(actual.forEach) === "function" && every(actual, function (element) {
return predicate.test(element);
});
}, "every(" + predicate.message + ")");
};

match.array = match.typeOf("array");

match.array.deepEquals = function (expectation) {
Expand Down
73 changes: 73 additions & 0 deletions test/match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,79 @@ describe("sinonMatch", function () {
});
});

describe(".every", function () {
it("throws if given argument is not a matcher", function () {
assert.exception(function () {
sinonMatch.every({});
}, "TypeError");
assert.exception(function () {
sinonMatch.every(123);
}, "TypeError");
assert.exception(function () {
sinonMatch.every("123");
}, "TypeError");
});

it("returns matcher", function () {
var every = sinonMatch.every(sinonMatch.any);

assert(sinonMatch.isMatcher(every));
});

it("wraps the given matcher message with an \"every()\"", function () {
var every = sinonMatch.every(sinonMatch.number);

assert.equals(every.toString(), "every(typeOf(\"number\"))");
});

it("fails to match anything that is not an object or an iterable", function () {
var every = sinonMatch.every(sinonMatch.any);

assert.isFalse(every.test(1));
assert.isFalse(every.test("a"));
assert.isFalse(every.test(null));
assert.isFalse(every.test(function () {}));
});

it("matches an object if the predicate is true for every property", function () {
var every = sinonMatch.every(sinonMatch.number);

assert(every.test({a: 1, b: 2}));
});

it("fails if the predicate is false for some of the object properties", function () {
var every = sinonMatch.every(sinonMatch.number);

assert.isFalse(every.test({a: 1, b: "b"}));
});

it("matches an array if the predicate is true for every element", function () {
var every = sinonMatch.every(sinonMatch.number);

assert(every.test([1, 2]));
});

it("fails if the predicate is false for some of the array elements", function () {
var every = sinonMatch.every(sinonMatch.number);

assert.isFalse(every.test([1, "b"]));
});

if (typeof Set === "function") {
it("matches an iterable if the predicate is true for every element", function () {
var every = sinonMatch.every(sinonMatch.number);

assert(every.test(new Set([1, 2])));
});

it("fails if the predicate is false for some of the iterable elements", function () {
var every = sinonMatch.every(sinonMatch.number);

assert.isFalse(every.test(new Set([1, "b"])));
});
}
});

describe(".bool", function () {
it("is typeOf boolean matcher", function () {
var bool = sinonMatch.bool;
Expand Down

0 comments on commit ac41ad1

Please sign in to comment.