From fd70eaa92072c9412bae8dd45705dc66f4d97900 Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Wed, 4 Jan 2017 19:01:16 -0200 Subject: [PATCH 1/4] Matcher for Set type --- lib/sinon/match.js | 2 ++ test/match-test.js | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/sinon/match.js b/lib/sinon/match.js index 3fed92ea8..4bd13bcf7 100644 --- a/lib/sinon/match.js +++ b/lib/sinon/match.js @@ -284,6 +284,8 @@ match.map.contains = function mapContains(expectation) { }, "contains(Map[" + iterableToString(expectation) + "])"); }; +match.set = match.typeOf("set"); + match.bool = match.typeOf("boolean"); match.number = match.typeOf("number"); match.string = match.typeOf("string"); diff --git a/test/match-test.js b/test/match-test.js index 2d8015afd..42605e394 100644 --- a/test/match-test.js +++ b/test/match-test.js @@ -848,6 +848,15 @@ 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(".regexp", function () { it("is typeOf regexp matcher", function () { var regexp = sinonMatch.regexp; From d195d6e079364dd982aad87b19c6e30218ab053c Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Wed, 4 Jan 2017 19:04:59 -0200 Subject: [PATCH 2/4] Add deepEquals matcher for Sets --- lib/sinon/match.js | 10 ++++++++++ test/match-test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/sinon/match.js b/lib/sinon/match.js index 4bd13bcf7..40672ff04 100644 --- a/lib/sinon/match.js +++ b/lib/sinon/match.js @@ -286,6 +286,16 @@ match.map.contains = function mapContains(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.bool = match.typeOf("boolean"); match.number = match.typeOf("number"); match.string = match.typeOf("string"); diff --git a/test/match-test.js b/test/match-test.js index 42605e394..c0412f5a0 100644 --- a/test/match-test.js +++ b/test/match-test.js @@ -855,6 +855,48 @@ describe("sinonMatch", function () { 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(".regexp", function () { From 330b46aa2573dfee9770e8755a081819d8795925 Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Wed, 4 Jan 2017 19:07:21 -0200 Subject: [PATCH 3/4] Add contains matcher for Sets --- lib/sinon/match.js | 8 ++++++++ test/match-test.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/sinon/match.js b/lib/sinon/match.js index 40672ff04..06041094c 100644 --- a/lib/sinon/match.js +++ b/lib/sinon/match.js @@ -296,6 +296,14 @@ match.set.deepEquals = function setDeepEquals(expectation) { }, "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"); diff --git a/test/match-test.js b/test/match-test.js index c0412f5a0..2be5be761 100644 --- a/test/match-test.js +++ b/test/match-test.js @@ -897,6 +897,51 @@ describe("sinonMatch", function () { }); } }); + + 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 () { From 75575b3c99c32df9080e451fcf193966edfa5147 Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Wed, 4 Jan 2017 19:16:05 -0200 Subject: [PATCH 4/4] Add docs for new set matchers --- docs/release-source/release/matchers.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/release-source/release/matchers.md b/docs/release-source/release/matchers.md index 1bd090904..1efad7709 100644 --- a/docs/release-source/release/matchers.md +++ b/docs/release-source/release/matchers.md @@ -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.