Skip to content

Commit

Permalink
Merge dcc0e30 into b94fca0
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Dec 7, 2018
2 parents b94fca0 + dcc0e30 commit dd2a557
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 239 deletions.
6 changes: 3 additions & 3 deletions lib/deep-equal.js
Expand Up @@ -51,11 +51,11 @@ 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.isMatcher(obj2)) {
if (match && match.isMatcher(obj2)) {
if (match.isMatcher(obj1)) {
return obj1 === obj2;
}
return obj1.test(obj2);
return obj2.test(obj1);
}

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 createMatcher = require("./matcher");

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

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

module.exports = match;
77 changes: 77 additions & 0 deletions lib/matcher-integration.test.js
@@ -0,0 +1,77 @@
"use strict";

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

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

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

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

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

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

context("returns false", function() {
it("if not matching custom matcher", function() {
var matchAlways = samsam.createMatcher(function() {
return false;
});
assert.isFalse(match(42, matchAlways));
});

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

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

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

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

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

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

context("returns false", function() {
it("when not matching nested matcher", function() {
assert.isFalse(samsam.deepEqual({ x: 1 }, { x: match.string }));
});
});
});
});
14 changes: 7 additions & 7 deletions lib/sinon-match.js → lib/matcher.js
Expand Up @@ -54,7 +54,7 @@ function isMatcher(object) {
return isPrototypeOf(matcher, object);
}

function matchObject(expectation, actual) {
function matchObject(actual, expectation) {
if (actual === null || actual === undefined) {
return false;
}
Expand All @@ -68,10 +68,10 @@ function matchObject(expectation, actual) {
return false;
}
} else if (typeOf(exp) === "object") {
if (!matchObject(exp, act)) {
if (!matchObject(act, exp)) {
return false;
}
} else if (!deepEqual(exp, act)) {
} else if (!deepEqual(act, exp)) {
return false;
}

Expand Down Expand Up @@ -106,7 +106,7 @@ var TYPE_MAP = {
});

m.test = function(actual) {
return matchObject(expectation, actual);
return matchObject(actual, expectation);
};
m.message = "match(" + join(array, ", ") + ")";

Expand Down Expand Up @@ -136,7 +136,7 @@ function match(expectation, message) {
TYPE_MAP[type](m, expectation, message);
} else {
m.test = function(actual) {
return deepEqual(expectation, actual);
return deepEqual(actual, expectation);
};
}

Expand Down Expand Up @@ -258,7 +258,7 @@ function createPropertyMatcher(propertyTest, messagePrefix) {
) {
return false;
}
return onlyProperty || deepEqual(value, actual[property]);
return onlyProperty || deepEqual(actual[property], value);
}, message);
};
}
Expand Down Expand Up @@ -290,7 +290,7 @@ match.hasNested = function(property, value) {
) {
return false;
}
return onlyProperty || deepEqual(value, get(actual, property));
return onlyProperty || deepEqual(get(actual, property), value);
}, message);
};

Expand Down

0 comments on commit dd2a557

Please sign in to comment.