Skip to content

Commit

Permalink
Merge df8dbc9 into b94fca0
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Dec 6, 2018
2 parents b94fca0 + df8dbc9 commit b3cdd8f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/deep-equal.js
Expand Up @@ -51,11 +51,16 @@ function deepEqualCyclic(first, second, match) {
// If both are matchers they must be the same instance in order to be
// considered equal If we didn't do that we would end up running one
// matcher against the other
if (match && match.isMatcher(obj1)) {
if (match) {
if (match.isMatcher(obj1)) {
if (match.isMatcher(obj2)) {
return obj1 === obj2;
}
return obj1.test(obj2);
}
if (match.isMatcher(obj2)) {
return obj1 === obj2;
return obj2.test(obj1);
}
return obj1.test(obj2);
}

var type1 = typeof obj1;
Expand Down
5 changes: 5 additions & 0 deletions lib/match.js
Expand Up @@ -4,6 +4,7 @@ var getClass = require("./get-class");
var isDate = require("./is-date");
var isSet = require("./is-set");
var isSubset = require("./is-subset");
var sinonMatch = require("./sinon-match");

function arrayContains(array, subset, compare) {
if (subset.length === 0) {
Expand Down Expand Up @@ -125,4 +126,8 @@ function match(object, matcher) {
);
}

Object.keys(sinonMatch).forEach(function(key) {
match[key] = sinonMatch[key];
});

module.exports = match;
6 changes: 4 additions & 2 deletions lib/samsam.js
@@ -1,17 +1,19 @@
"use strict";

var deepEqualCyclic = require("./deep-equal");
var identical = require("./identical");
var isArguments = require("./is-arguments");
var isElement = require("./is-element");
var isNegZero = require("./is-neg-zero");
var match = require("./match");
var deepEqualCyclic = require("./deep-equal").use(match);
var createMatcher = require("./sinon-match");

module.exports = {
isArguments: isArguments,
isElement: isElement,
isNegZero: isNegZero,
identical: identical,
deepEqual: deepEqualCyclic,
match: match
match: match,
createMatcher: createMatcher
};
63 changes: 63 additions & 0 deletions lib/sinon-match-integration.test.js
@@ -0,0 +1,63 @@
"use strict";

var assert = require("@sinonjs/referee").assert;
var samsam = require("./samsam");
var match = samsam.match;

describe("sinon-match-integration", function() {
context("samsam.match", function() {
it("returns true if matching custom matcher", function() {
var matchAlways = samsam.createMatcher(function() {
return true;
});
var checkMatch = match(42, matchAlways);
assert.isTrue(checkMatch);
});

it("returns true if matching boolean", function() {
assert.isTrue(samsam.match(false, match.bool));
});

it("returns false if not matching boolean", function() {
assert.isFalse(samsam.match(0, match.bool));
});

it("returns true if matching number", function() {
assert.isTrue(samsam.match(42, match.number));
});

it("returns false if not matching number", function() {
assert.isFalse(samsam.match("42", match.number));
});

it("returns true if matching string", function() {
assert.isTrue(samsam.match("abc", match.string));
});

it("returns false if not matching string", function() {
assert.isFalse(samsam.match(123, match.string));
});

it("returns true when matching nested matcher", function() {
assert.isTrue(samsam.match({ x: 1 }, { x: match.number }));
});

it("returns false when not matching nested matcher", function() {
assert.isFalse(samsam.match({ x: 1 }, { x: match.string }));
});
});

context("samsam.deepEqual", function() {
it("returns true if matching boolean", function() {
assert.isTrue(samsam.deepEqual(false, match.bool));
});

it("returns true when matching nested matcher", function() {
assert.isTrue(samsam.deepEqual({ x: 1 }, { x: match.number }));
});

it("returns false when not matching nested matcher", function() {
assert.isFalse(samsam.deepEqual({ x: 1 }, { x: match.string }));
});
});
});

0 comments on commit b3cdd8f

Please sign in to comment.