Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sets matchers #1232

Merged
merged 4 commits into from
Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/release-source/release/matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ Requires a `Map` to be deep equal another one.
Requires a `Map` to contain each one of the items the given map has.


#### `sinon.match.set"

Requires the value to be a `Set`.


#### `sinon.match.set.deepEquals(set)`

Requires a `Set` to be deep equal another one.


#### `sinon.match.set.contains(set)`

Requires a `Set` to contain each one of the items the given set has.


#### `sinon.match.regexp"

Requires the value to be a regular expression.
Expand Down
20 changes: 20 additions & 0 deletions lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ match.map.contains = function mapContains(expectation) {
}, "contains(Map[" + iterableToString(expectation) + "])");
};

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

match.set.deepEquals = function setDeepEquals(expectation) {
return match(function (actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.size === expectation.size;
return typeOf(actual) === "set" && sameLength && every(actual, function (element) {
return expectation.has(element);
});
}, "deepEquals(Set[" + iterableToString(expectation) + "])");
};

match.set.contains = function setContains(expectation) {
return match(function (actual) {
return typeOf(actual) === "set" && every(expectation, function (element) {
return actual.has(element);
});
}, "contains(Set[" + iterableToString(expectation) + "])");
};

match.bool = match.typeOf("boolean");
match.number = match.typeOf("number");
match.string = match.typeOf("string");
Expand Down
96 changes: 96 additions & 0 deletions test/match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,102 @@ describe("sinonMatch", function () {
});
});

describe(".set", function () {
it("is typeOf set matcher", function () {
var set = sinonMatch.set;

assert(sinonMatch.isMatcher(set));
assert.equals(set.toString(), "typeOf(\"set\")");
});

describe("set.deepEquals", function () {
if (typeof Set === "function") {
it("has a .deepEquals matcher", function () {
var setOne = new Set();
setOne.add("one");
setOne.add("two");
setOne.add("three");

var deepEquals = sinonMatch.set.deepEquals(setOne);
assert(sinonMatch.isMatcher(deepEquals));
assert.equals(deepEquals.toString(), "deepEquals(Set['one','two','three'])");
});

it("matches sets with the exact same elements", function () {
var setOne = new Set();
setOne.add("one");
setOne.add("two");
setOne.add("three");

var setTwo = new Set();
setTwo.add("one");
setTwo.add("two");
setTwo.add("three");

var setThree = new Set();
setThree.add("one");
setThree.add("two");

var deepEquals = sinonMatch.set.deepEquals(setOne);
assert(deepEquals.test(setTwo));
assert.isFalse(deepEquals.test(setThree));
assert.isFalse(deepEquals.test(new Set()));
});

it("fails when passed a non-set object", function () {
var deepEquals = sinonMatch.array.deepEquals(new Set());
assert.isFalse(deepEquals.test({}));
assert.isFalse(deepEquals.test([]));
});
}
});

describe("set.contains", function () {
if (typeof Set === "function") {
it("has a .contains matcher", function () {
var setOne = new Set();
setOne.add("one");
setOne.add("two");
setOne.add("three");

var contains = sinonMatch.set.contains(setOne);
assert(sinonMatch.isMatcher(contains));
assert.equals(contains.toString(), "contains(Set['one','two','three'])");
});

it("matches sets containing the given elements", function () {
var setOne = new Set();
setOne.add("one");
setOne.add("two");
setOne.add("three");

var setTwo = new Set();
setTwo.add("one");
setTwo.add("two");
setTwo.add("three");

var setThree = new Set();
setThree.add("one");
setThree.add("two");

var setFour = new Set();
setFour.add("one");
setFour.add("four");

assert(sinonMatch.set.contains(setTwo).test(setOne));
assert(sinonMatch.set.contains(setThree).test(setOne));
assert.isFalse(sinonMatch.set.contains(setFour).test(setOne));
});

it("fails when passed a non-set object", function () {
var contains = sinonMatch.set.contains(new Set());
assert.isFalse(contains.test({}));
assert.isFalse(contains.test([]));
});
}
});
});

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