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

Add docs for new array matchers #1210

Merged
merged 5 commits into from
Dec 26, 2016
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
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