Skip to content

Commit

Permalink
Merge pull request #1208 from lucasfcosta/array-matchers
Browse files Browse the repository at this point in the history
Array matchers
  • Loading branch information
fatso83 authored Dec 20, 2016
2 parents a465244 + 363e3e4 commit 1924a89
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/sinon/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var functionName = require("./util/core/function-name");
var typeOf = require("./typeOf");
var valueToString = require("./util/core/value-to-string");

var every = Array.prototype.every;
var indexOf = Array.prototype.indexOf;
var arrayToString = Array.prototype.toString;

function assertType(value, type, name) {
var actual = typeOf(value);
if (actual !== type) {
Expand Down Expand Up @@ -221,12 +225,50 @@ match.hasOwn = createPropertyMatcher(function (actual, property) {
return actual.hasOwnProperty(property);
}, "hasOwn");

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

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 expectation[index] === element;
});
}, "deepEquals([" + arrayToString.call(expectation) + "])");
};

match.array.startsWith = function (expectation) {
return match(function (actual) {
return every.call(expectation, function (expectedElement, index) {
return actual[index] === expectedElement;
});
}, "startsWith([" + arrayToString.call(expectation) + "])");
};

match.array.endsWith = function (expectation) {
return match(function (actual) {
// This indicates the index in which we should start matching
var offset = actual.length - expectation.length;

return 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 indexOf.call(actual, expectedElement) !== -1;
});
}, "contains([" + arrayToString.call(expectation) + "])");
};

match.bool = match.typeOf("boolean");
match.number = match.typeOf("number");
match.string = match.typeOf("string");
match.object = match.typeOf("object");
match.func = match.typeOf("function");
match.array = match.typeOf("array");
match.regexp = match.typeOf("regexp");
match.date = match.typeOf("date");
match.symbol = match.typeOf("symbol");
Expand Down
64 changes: 64 additions & 0 deletions test/match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,70 @@ describe("sinonMatch", function () {
assert(sinonMatch.isMatcher(array));
assert.equals(array.toString(), "typeOf(\"array\")");
});

describe("array.deepEquals", function () {
it("has a .deepEquals matcher", function () {
var deepEquals = sinonMatch.array.deepEquals([1, 2, 3]);

assert(sinonMatch.isMatcher(deepEquals));
assert.equals(deepEquals.toString(), "deepEquals([1,2,3])");
});

it("matches arrays with the exact same elements", function () {
var deepEquals = sinonMatch.array.deepEquals([1, 2, 3]);
assert(deepEquals.test([1, 2, 3]));
assert.isFalse(deepEquals.test([1, 2]));
assert.isFalse(deepEquals.test([3]));
});
});

describe("array.startsWith", function () {
it("has a .startsWith matcher", function () {
var startsWith = sinonMatch.array.startsWith([1, 2]);

assert(sinonMatch.isMatcher(startsWith));
assert.equals(startsWith.toString(), "startsWith([1,2])");
});

it("matches arrays starting with the same elements", function () {
assert(sinonMatch.array.startsWith([1]).test([1, 2]));
assert(sinonMatch.array.startsWith([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.startsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.startsWith([2]).test([1, 2]));
});
});

describe("array.endsWith", function () {
it("has an .endsWith matcher", function () {
var endsWith = sinonMatch.array.endsWith([2, 3]);

assert(sinonMatch.isMatcher(endsWith));
assert.equals(endsWith.toString(), "endsWith([2,3])");
});

it("matches arrays ending with the same elements", function () {
assert(sinonMatch.array.endsWith([2]).test([1, 2]));
assert(sinonMatch.array.endsWith([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.endsWith([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.endsWith([3]).test([1, 2]));
});
});

describe("array.contains", function () {
it("has a .contains matcher", function () {
var contains = sinonMatch.array.contains([2, 3]);

assert(sinonMatch.isMatcher(contains));
assert.equals(contains.toString(), "contains([2,3])");
});

it("matches arrays containing all the expected elements", function () {
assert(sinonMatch.array.contains([2]).test([1, 2, 3]));
assert(sinonMatch.array.contains([1, 2]).test([1, 2]));
assert.isFalse(sinonMatch.array.contains([1, 2, 3]).test([1, 2]));
assert.isFalse(sinonMatch.array.contains([3]).test([1, 2]));
});
});
});

describe(".regexp", function () {
Expand Down

0 comments on commit 1924a89

Please sign in to comment.