Skip to content

Commit

Permalink
Merge pull request #1210 from lucasfcosta/document-new-array-matchers
Browse files Browse the repository at this point in the history
Add docs for new array matchers
  • Loading branch information
fatso83 authored Dec 26, 2016
2 parents 2df140f + cd1e396 commit c446844
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
20 changes: 20 additions & 0 deletions docs/release-source/release/matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ Requires the value to be a `Function`.
Requires the value to be an `Array`.


#### `sinon.match.array.deepEquals(arr)`

Requires an `Array` to be deep equal another one.


#### `sinon.match.array.startsWith(arr)`

Requires an `Array` to start with the same values as another one.


#### `sinon.match.array.endsWith(arr)`

Requires an `Array` to end with the same values as another one.


#### `sinon.match.array.contains(arr)`

Requires an `Array` to contain each one of the values the given array has.


#### `sinon.match.regexp"

Requires the value to be a regular expression.
Expand Down
8 changes: 4 additions & 4 deletions lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ match.array.deepEquals = function (expectation) {
return match(function (actual) {
// Comparing lengths is the fastest way to spot a difference before iterating through every item
var sameLength = actual.length === expectation.length;
return sameLength && every.call(actual, function (element, index) {
return typeOf(actual) === "array" && sameLength && every.call(actual, function (element, index) {
return expectation[index] === element;
});
}, "deepEquals([" + arrayToString.call(expectation) + "])");
};

match.array.startsWith = function (expectation) {
return match(function (actual) {
return every.call(expectation, function (expectedElement, index) {
return typeOf(actual) === "array" && every.call(expectation, function (expectedElement, index) {
return actual[index] === expectedElement;
});
}, "startsWith([" + arrayToString.call(expectation) + "])");
Expand All @@ -250,15 +250,15 @@ match.array.endsWith = function (expectation) {
// This indicates the index in which we should start matching
var offset = actual.length - expectation.length;

return every.call(expectation, function (expectedElement, index) {
return typeOf(actual) === "array" && every.call(expectation, function (expectedElement, index) {
return actual[offset + index] === expectedElement;
});
}, "endsWith([" + arrayToString.call(expectation) + "])");
};

match.array.contains = function (expectation) {
return match(function (actual) {
return every.call(expectation, function (expectedElement) {
return typeOf(actual) === "array" && every.call(expectation, function (expectedElement) {
return indexOf.call(actual, expectedElement) !== -1;
});
}, "contains([" + arrayToString.call(expectation) + "])");
Expand Down
22 changes: 22 additions & 0 deletions test/match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ describe("sinonMatch", function () {
assert.isFalse(deepEquals.test([1, 2]));
assert.isFalse(deepEquals.test([3]));
});

it("fails when passed a non-array object", function () {
var deepEquals = sinonMatch.array.deepEquals(["one", "two", "three"]);
assert.isFalse(deepEquals.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});

describe("array.startsWith", function () {
Expand All @@ -654,6 +659,11 @@ describe("sinonMatch", function () {
assert.isFalse(sinonMatch.array.startsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.startsWith([2]).test([1, 2]));
});

it("fails when passed a non-array object", function () {
var startsWith = sinonMatch.array.startsWith(["one", "two"]);
assert.isFalse(startsWith.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});

describe("array.endsWith", function () {
Expand All @@ -670,6 +680,12 @@ describe("sinonMatch", function () {
assert.isFalse(sinonMatch.array.endsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.endsWith([3]).test([1, 2]));
});

it("fails when passed a non-array object", function () {
var endsWith = sinonMatch.array.endsWith(["two", "three"]);

assert.isFalse(endsWith.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});

describe("array.contains", function () {
Expand All @@ -686,6 +702,12 @@ describe("sinonMatch", function () {
assert.isFalse(sinonMatch.array.contains([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.contains([3]).test([1, 2]));
});

it("fails when passed a non-array object", function () {
var contains = sinonMatch.array.contains(["one", "three"]);

assert.isFalse(contains.test({0: "one", 1: "two", 2: "three", length: 3}));
});
});
});

Expand Down

0 comments on commit c446844

Please sign in to comment.