From cd32551bfd55818482764f212c05862163e467fc Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Fri, 7 Dec 2018 22:20:49 +0100 Subject: [PATCH 1/2] Upgrade to samsam 3 --- lib/sinon/call.js | 6 +- lib/sinon/match.js | 427 +--------------------- lib/sinon/mock-expectation.js | 6 +- lib/sinon/mock.js | 3 +- lib/sinon/spy.js | 5 +- lib/sinon/util/core/deep-equal.js | 66 ---- lib/sinon/util/core/every.js | 20 - lib/sinon/util/core/iterable-to-string.js | 38 -- lib/sinon/util/core/typeOf.js | 7 - package-lock.json | 57 +-- package.json | 10 +- test/util/core/deep-equal-test.js | 16 +- test/util/core/every-test.js | 42 --- test/util/core/iterable-to-string-test.js | 43 --- test/util/core/typeOf-test.js | 52 --- 15 files changed, 55 insertions(+), 743 deletions(-) delete mode 100644 lib/sinon/util/core/deep-equal.js delete mode 100644 lib/sinon/util/core/every.js delete mode 100644 lib/sinon/util/core/iterable-to-string.js delete mode 100644 lib/sinon/util/core/typeOf.js delete mode 100644 test/util/core/every-test.js delete mode 100644 test/util/core/iterable-to-string-test.js delete mode 100644 test/util/core/typeOf-test.js diff --git a/lib/sinon/call.js b/lib/sinon/call.js index d1088adf8..ed613baca 100644 --- a/lib/sinon/call.js +++ b/lib/sinon/call.js @@ -2,7 +2,7 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var sinonMatch = require("./match"); -var deepEqual = require("./util/core/deep-equal").use(sinonMatch); +var deepEqual = require("@sinonjs/samsam").deepEqual; var functionName = require("@sinonjs/commons").functionName; var sinonFormat = require("./util/core/format"); var valueToString = require("@sinonjs/commons").valueToString; @@ -41,7 +41,7 @@ var callProto = { return reduce( calledWithArgs, function(prev, arg, i) { - return prev && deepEqual(arg, self.args[i]); + return prev && deepEqual(self.args[i], arg); }, true ); @@ -79,7 +79,7 @@ var callProto = { }, returned: function returned(value) { - return deepEqual(value, this.returnValue); + return deepEqual(this.returnValue, value); }, threw: function threw(error) { diff --git a/lib/sinon/match.js b/lib/sinon/match.js index 43e3aa9de..2422f8b32 100644 --- a/lib/sinon/match.js +++ b/lib/sinon/match.js @@ -1,428 +1,5 @@ "use strict"; -var arrayProto = require("@sinonjs/commons").prototypes.array; -var deepEqual = require("./util/core/deep-equal").use(match); // eslint-disable-line no-use-before-define -var every = require("./util/core/every"); -var functionName = require("@sinonjs/commons").functionName; -var get = require("lodash.get"); -var iterableToString = require("./util/core/iterable-to-string"); -var objectProto = require("@sinonjs/commons").prototypes.object; -var stringProto = require("@sinonjs/commons").prototypes.string; -var typeOf = require("./util/core/typeOf"); -var valueToString = require("@sinonjs/commons").valueToString; +var samsam = require("@sinonjs/samsam"); -var arrayIndexOf = arrayProto.indexOf; -var arrayEvery = arrayProto.every; -var join = arrayProto.join; -var map = arrayProto.map; -var some = arrayProto.some; - -var hasOwnProperty = objectProto.hasOwnProperty; -var isPrototypeOf = objectProto.isPrototypeOf; - -var stringIndexOf = stringProto.indexOf; - -function assertType(value, type, name) { - var actual = typeOf(value); - if (actual !== type) { - throw new TypeError("Expected type of " + name + " to be " + type + ", but was " + actual); - } -} - -function assertMethodExists(value, method, name, methodPath) { - if (typeof value[method] === "undefined") { - throw new TypeError("Expected " + name + " to have method " + methodPath); - } -} - -var matcher = { - toString: function() { - return this.message; - } -}; - -function isMatcher(object) { - return isPrototypeOf(matcher, object); -} - -function matchObject(expectation, actual) { - if (actual === null || actual === undefined) { - return false; - } - - return arrayEvery(Object.keys(expectation), function(key) { - var exp = expectation[key]; - var act = actual[key]; - - if (isMatcher(exp)) { - if (!exp.test(act)) { - return false; - } - } else if (typeOf(exp) === "object") { - if (!matchObject(exp, act)) { - return false; - } - } else if (!deepEqual(exp, act)) { - return false; - } - - return true; - }); -} - -var TYPE_MAP = { - function: function(m, expectation, message) { - m.test = expectation; - m.message = message || "match(" + functionName(expectation) + ")"; - }, - number: function(m, expectation) { - m.test = function(actual) { - // we need type coercion here - return expectation == actual; // eslint-disable-line eqeqeq - }; - }, - object: function(m, expectation) { - var array = []; - - if (typeof expectation.test === "function") { - m.test = function(actual) { - return expectation.test(actual) === true; - }; - m.message = "match(" + functionName(expectation.test) + ")"; - return m; - } - - array = map(Object.keys(expectation), function(key) { - return key + ": " + valueToString(expectation[key]); - }); - - m.test = function(actual) { - return matchObject(expectation, actual); - }; - m.message = "match(" + join(array, ", ") + ")"; - - return m; - }, - regexp: function(m, expectation) { - m.test = function(actual) { - return typeof actual === "string" && expectation.test(actual); - }; - }, - string: function(m, expectation) { - m.test = function(actual) { - return typeof actual === "string" && stringIndexOf(actual, expectation) !== -1; - }; - m.message = 'match("' + expectation + '")'; - } -}; - -function match(expectation, message) { - var m = Object.create(matcher); - var type = typeOf(expectation); - - if (type in TYPE_MAP) { - TYPE_MAP[type](m, expectation, message); - } else { - m.test = function(actual) { - return deepEqual(expectation, actual); - }; - } - - if (!m.message) { - m.message = "match(" + valueToString(expectation) + ")"; - } - - return m; -} - -matcher.or = function(m2) { - var matcher2 = m2; - if (!arguments.length) { - throw new TypeError("Matcher expected"); - } else if (!isMatcher(m2)) { - matcher2 = match(m2); - } - var m1 = this; - var or = Object.create(matcher); - or.test = function(actual) { - return m1.test(actual) || matcher2.test(actual); - }; - or.message = m1.message + ".or(" + matcher2.message + ")"; - return or; -}; - -matcher.and = function(m2) { - var matcher2 = m2; - if (!arguments.length) { - throw new TypeError("Matcher expected"); - } else if (!isMatcher(m2)) { - matcher2 = match(m2); - } - var m1 = this; - var and = Object.create(matcher); - and.test = function(actual) { - return m1.test(actual) && matcher2.test(actual); - }; - and.message = m1.message + ".and(" + matcher2.message + ")"; - return and; -}; - -match.isMatcher = isMatcher; - -match.any = match(function() { - return true; -}, "any"); - -match.defined = match(function(actual) { - return actual !== null && actual !== undefined; -}, "defined"); - -match.truthy = match(function(actual) { - return Boolean(actual); -}, "truthy"); - -match.falsy = match(function(actual) { - return !actual; -}, "falsy"); - -match.same = function(expectation) { - return match(function(actual) { - return expectation === actual; - }, "same(" + valueToString(expectation) + ")"); -}; - -match.in = function(arrayOfExpectations) { - if (!Array.isArray(arrayOfExpectations)) { - throw new TypeError("array expected"); - } - - return match(function(actual) { - return some(arrayOfExpectations, function(expectation) { - return expectation === actual; - }); - }, "in(" + valueToString(arrayOfExpectations) + ")"); -}; - -match.typeOf = function(type) { - assertType(type, "string", "type"); - return match(function(actual) { - return typeOf(actual) === type; - }, 'typeOf("' + type + '")'); -}; - -match.instanceOf = function(type) { - if (typeof Symbol === "undefined" || typeof Symbol.hasInstance === "undefined") { - assertType(type, "function", "type"); - } else { - assertMethodExists(type, Symbol.hasInstance, "type", "[Symbol.hasInstance]"); - } - return match(function(actual) { - return actual instanceof type; - }, "instanceOf(" + (functionName(type) || Object.prototype.toString.call(type)) + ")"); -}; - -function createPropertyMatcher(propertyTest, messagePrefix) { - return function(property, value) { - assertType(property, "string", "property"); - var onlyProperty = arguments.length === 1; - var message = messagePrefix + '("' + property + '"'; - if (!onlyProperty) { - message += ", " + valueToString(value); - } - message += ")"; - return match(function(actual) { - if (actual === undefined || actual === null || !propertyTest(actual, property)) { - return false; - } - return onlyProperty || deepEqual(value, actual[property]); - }, message); - }; -} - -match.has = createPropertyMatcher(function(actual, property) { - if (typeof actual === "object") { - return property in actual; - } - return actual[property] !== undefined; -}, "has"); - -match.hasOwn = createPropertyMatcher(function(actual, property) { - return hasOwnProperty(actual, property); -}, "hasOwn"); - -match.hasNested = function(property, value) { - assertType(property, "string", "property"); - var onlyProperty = arguments.length === 1; - var message = 'hasNested("' + property + '"'; - if (!onlyProperty) { - message += ", " + valueToString(value); - } - message += ")"; - return match(function(actual) { - if (actual === undefined || actual === null || get(actual, property) === undefined) { - return false; - } - return onlyProperty || deepEqual(value, get(actual, property)); - }, message); -}; - -match.every = function(predicate) { - if (!isMatcher(predicate)) { - throw new TypeError("Matcher expected"); - } - - return match(function(actual) { - if (typeOf(actual) === "object") { - return every(Object.keys(actual), function(key) { - return predicate.test(actual[key]); - }); - } - - return ( - Boolean(actual) && - typeOf(actual.forEach) === "function" && - every(actual, function(element) { - return predicate.test(element); - }) - ); - }, "every(" + predicate.message + ")"); -}; - -match.some = function(predicate) { - if (!isMatcher(predicate)) { - throw new TypeError("Matcher expected"); - } - - return match(function(actual) { - if (typeOf(actual) === "object") { - return !every(Object.keys(actual), function(key) { - return !predicate.test(actual[key]); - }); - } - - return ( - Boolean(actual) && - typeOf(actual.forEach) === "function" && - !every(actual, function(element) { - return !predicate.test(element); - }) - ); - }, "some(" + predicate.message + ")"); -}; - -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 ( - typeOf(actual) === "array" && - sameLength && - every(actual, function(element, index) { - return expectation[index] === element; - }) - ); - }, "deepEquals([" + iterableToString(expectation) + "])"); -}; - -match.array.startsWith = function(expectation) { - return match(function(actual) { - return ( - typeOf(actual) === "array" && - every(expectation, function(expectedElement, index) { - return actual[index] === expectedElement; - }) - ); - }, "startsWith([" + iterableToString(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 ( - typeOf(actual) === "array" && - every(expectation, function(expectedElement, index) { - return actual[offset + index] === expectedElement; - }) - ); - }, "endsWith([" + iterableToString(expectation) + "])"); -}; - -match.array.contains = function(expectation) { - return match(function(actual) { - return ( - typeOf(actual) === "array" && - every(expectation, function(expectedElement) { - return arrayIndexOf(actual, expectedElement) !== -1; - }) - ); - }, "contains([" + iterableToString(expectation) + "])"); -}; - -match.map = match.typeOf("map"); - -match.map.deepEquals = function mapDeepEquals(expectation) { - return match(function(actual) { - // Comparing lengths is the fastest way to spot a difference before iterating through every item - var sameLength = actual.size === expectation.size; - return ( - typeOf(actual) === "map" && - sameLength && - every(actual, function(element, key) { - return expectation.has(key) && expectation.get(key) === element; - }) - ); - }, "deepEquals(Map[" + iterableToString(expectation) + "])"); -}; - -match.map.contains = function mapContains(expectation) { - return match(function(actual) { - return ( - typeOf(actual) === "map" && - every(expectation, function(element, key) { - return actual.has(key) && actual.get(key) === element; - }) - ); - }, "contains(Map[" + iterableToString(expectation) + "])"); -}; - -match.set = match.typeOf("set"); - -match.set.deepEquals = function setDeepEquals(expectation) { - return match(function(actual) { - // Comparing lengths is the fastest way to spot a difference before iterating through every item - var sameLength = actual.size === expectation.size; - return ( - typeOf(actual) === "set" && - sameLength && - every(actual, function(element) { - return expectation.has(element); - }) - ); - }, "deepEquals(Set[" + iterableToString(expectation) + "])"); -}; - -match.set.contains = function setContains(expectation) { - return match(function(actual) { - return ( - typeOf(actual) === "set" && - every(expectation, function(element) { - return actual.has(element); - }) - ); - }, "contains(Set[" + iterableToString(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.regexp = match.typeOf("regexp"); -match.date = match.typeOf("date"); -match.symbol = match.typeOf("symbol"); - -module.exports = match; +module.exports = samsam.createMatcher; diff --git a/lib/sinon/mock-expectation.js b/lib/sinon/mock-expectation.js index aa168b97d..b644098b3 100644 --- a/lib/sinon/mock-expectation.js +++ b/lib/sinon/mock-expectation.js @@ -8,7 +8,7 @@ var extend = require("./util/core/extend"); var match = require("./match"); var stub = require("./stub"); var assert = require("./assert"); -var deepEqual = require("./util/core/deep-equal").use(match); +var deepEqual = require("@sinonjs/samsam").deepEqual; var format = require("./util/core/format"); var valueToString = require("@sinonjs/commons").valueToString; @@ -201,7 +201,7 @@ var mockExpectation = { ); } - if (!deepEqual(expectedArgument, args[i])) { + if (!deepEqual(args[i], expectedArgument)) { mockExpectation.fail( this.method + " received wrong arguments " + @@ -246,7 +246,7 @@ var mockExpectation = { return false; } - if (!deepEqual(expectedArgument, _args[i])) { + if (!deepEqual(_args[i], expectedArgument)) { return false; } diff --git a/lib/sinon/mock.js b/lib/sinon/mock.js index 5164ec832..f4c36b80d 100644 --- a/lib/sinon/mock.js +++ b/lib/sinon/mock.js @@ -4,8 +4,7 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var mockExpectation = require("./mock-expectation"); var spyCallToString = require("./call").toString; var extend = require("./util/core/extend"); -var match = require("./match"); -var deepEqual = require("./util/core/deep-equal").use(match); +var deepEqual = require("@sinonjs/samsam").deepEqual; var wrapMethod = require("./util/core/wrap-method"); var usePromiseLibrary = require("./util/core/use-promise-library"); diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index 56372c67f..584dc8f9b 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -6,8 +6,7 @@ var extend = require("./util/core/extend"); var functionName = require("@sinonjs/commons").functionName; var functionToString = require("./util/core/function-to-string"); var getPropertyDescriptor = require("./util/core/get-property-descriptor"); -var sinonMatch = require("./match"); -var deepEqual = require("./util/core/deep-equal").use(sinonMatch); +var deepEqual = require("@sinonjs/samsam").deepEqual; var isEsModule = require("./util/core/is-es-module"); var spyCall = require("./call"); var wrapMethod = require("./util/core/wrap-method"); @@ -431,7 +430,7 @@ var spyApi = { matches: function(args, strict) { var margs = this.matchingArguments; - if (margs.length <= args.length && deepEqual(margs, slice(args, 0, margs.length))) { + if (margs.length <= args.length && deepEqual(slice(args, 0, margs.length), margs)) { return !strict || margs.length === args.length; } diff --git a/lib/sinon/util/core/deep-equal.js b/lib/sinon/util/core/deep-equal.js deleted file mode 100644 index 1922d5759..000000000 --- a/lib/sinon/util/core/deep-equal.js +++ /dev/null @@ -1,66 +0,0 @@ -"use strict"; - -var arrayProto = require("@sinonjs/commons").prototypes.array; -var toString = require("@sinonjs/commons").prototypes.object.toString; -var samsam = require("@sinonjs/samsam"); - -var every = arrayProto.every; -var join = arrayProto.join; -var sort = arrayProto.sort; - -function isReallyNaN(val) { - // eslint-disable-next-line no-self-compare - return val !== val; -} - -var deepEqual = (module.exports = function deepEqual(a, b, matcher) { - if (a === null && b === null) { - return true; - } - - if (typeof a !== "object" || typeof b !== "object") { - return (isReallyNaN(a) && isReallyNaN(b)) || a === b; - } - - if (a instanceof Error && b instanceof Error) { - return a === b; - } - - if (toString(a) !== toString(b)) { - return false; - } - - if (join(sort(Object.keys(a))) !== join(sort(Object.keys(b)))) { - return false; - } - - if (samsam.deepEqual(a, b)) { - return true; - } - - if (matcher) { - var keys = Object.keys(a); - var allKeysMatch = every(keys, function(key) { - return matcher(a[key], b[key]); - }); - - return keys.length > 0 && allKeysMatch; - } - - return false; -}); - -deepEqual.use = function(match) { - return function deepEqual$matcher(a, b) { - // 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.isMatcher(a)) { - if (match.isMatcher(b)) { - return a === b; - } - return a.test(b); - } - - return deepEqual(a, b, deepEqual$matcher); - }; -}; diff --git a/lib/sinon/util/core/every.js b/lib/sinon/util/core/every.js deleted file mode 100644 index f4614ef08..000000000 --- a/lib/sinon/util/core/every.js +++ /dev/null @@ -1,20 +0,0 @@ -"use strict"; - -// This is an `every` implementation that works for all iterables -module.exports = function every(obj, fn) { - var pass = true; - - try { - /* eslint-disable-next-line local-rules/no-prototype-methods */ - obj.forEach(function() { - if (!fn.apply(this, arguments)) { - // Throwing an error is the only way to break `forEach` - throw new Error(); - } - }); - } catch (e) { - pass = false; - } - - return pass; -}; diff --git a/lib/sinon/util/core/iterable-to-string.js b/lib/sinon/util/core/iterable-to-string.js deleted file mode 100644 index 2bf2357af..000000000 --- a/lib/sinon/util/core/iterable-to-string.js +++ /dev/null @@ -1,38 +0,0 @@ -"use strict"; - -var slice = require("@sinonjs/commons").prototypes.string.slice; -var typeOf = require("./typeOf"); - -module.exports = function iterableToString(obj) { - var representation = ""; - - function stringify(item) { - return typeof item === "string" ? "'" + item + "'" : String(item); - } - - function mapToString(map) { - /* eslint-disable-next-line local-rules/no-prototype-methods */ - map.forEach(function(value, key) { - representation += "[" + stringify(key) + "," + stringify(value) + "],"; - }); - - representation = slice(representation, 0, -1); - return representation; - } - - function genericIterableToString(iterable) { - /* eslint-disable-next-line local-rules/no-prototype-methods */ - iterable.forEach(function(value) { - representation += stringify(value) + ","; - }); - - representation = slice(representation, 0, -1); - return representation; - } - - if (typeOf(obj) === "map") { - return mapToString(obj); - } - - return genericIterableToString(obj); -}; diff --git a/lib/sinon/util/core/typeOf.js b/lib/sinon/util/core/typeOf.js deleted file mode 100644 index e8d2ff75c..000000000 --- a/lib/sinon/util/core/typeOf.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -var type = require("type-detect"); - -module.exports = function typeOf(value) { - return type(value).toLowerCase(); -}; diff --git a/package-lock.json b/package-lock.json index 1b6c013f2..8eb1d78fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -187,21 +187,11 @@ } }, "@sinonjs/formatio": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-3.0.0.tgz", - "integrity": "sha512-vdjoYLDptCgvtJs57ULshak3iJe4NW3sJ3g36xVDGff5AE8P30S6A093EIEPjdi2noGhfuNOEkbxt3J3awFW1w==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-3.1.0.tgz", + "integrity": "sha512-ZAR2bPHOl4Xg6eklUGpsdiIJ4+J1SNag1DHHrG/73Uz/nVwXqjgUtRPLoS+aVyieN9cSbc0E4LsU984tWcDyNg==", "requires": { - "@sinonjs/samsam": "2.1.0" - }, - "dependencies": { - "@sinonjs/samsam": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-2.1.0.tgz", - "integrity": "sha512-5x2kFgJYupaF1ns/RmharQ90lQkd2ELS8A9X0ymkAAdemYHGtI2KiUHG8nX2WU0T1qgnOU5YMqnBM2V7NUanNw==", - "requires": { - "array-from": "^2.1.1" - } - } + "@sinonjs/samsam": "^2 || ^3" } }, "@sinonjs/referee": { @@ -218,12 +208,35 @@ "lodash.includes": "^4.3.0", "lodash.isarguments": "^3.1.0", "object-assign": "^4.1.1" + }, + "dependencies": { + "@sinonjs/samsam": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-2.1.3.tgz", + "integrity": "sha512-8zNeBkSKhU9a5cRNbpCKau2WWPfan+Q2zDlcXvXyhn9EsMqgYs4qzo0XHNVlXC6ABQL8fT6nV+zzo5RTHJzyXw==", + "dev": true + } } }, "@sinonjs/samsam": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-2.1.2.tgz", - "integrity": "sha512-ZwTHAlC9akprWDinwEPD4kOuwaYZlyMwVJIANsKNC3QVp0AHB04m7RnB4eqeWfgmxw8MGTzS9uMaw93Z3QcZbw==" + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-3.0.1.tgz", + "integrity": "sha512-pSgyLnrBNtKHosNULXd+wEk6edqlBs2qfGHagS14t+zOhDkE5Yto5HbsZwQRmRoCBNg13osPHb/4uwvLOJBkug==", + "requires": { + "@sinonjs/commons": "1.0.2", + "array-from": "^2.1.1", + "lodash.get": "4.4.2" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.0.2.tgz", + "integrity": "sha512-WR3dlgqJP4QNrLC4iXN/5/2WaLQQ0VijOOkmflqFGVJ6wLEpbSjo7c0ZeGIdtY8Crk7xBBp87sM6+Mkerz7alw==", + "requires": { + "type-detect": "4.0.8" + } + } + } }, "@types/estree": { "version": "0.0.39", @@ -5480,11 +5493,11 @@ "dev": true }, "nise": { - "version": "1.4.6", - "resolved": "https://registry.npmjs.org/nise/-/nise-1.4.6.tgz", - "integrity": "sha512-1GedetLKzmqmgwabuMSqPsT7oumdR77SBpDfNNJhADRIeA3LN/2RVqR4fFqwvzhAqcTef6PPCzQwITE/YQ8S8A==", + "version": "1.4.7", + "resolved": "https://registry.npmjs.org/nise/-/nise-1.4.7.tgz", + "integrity": "sha512-5cxvo/pEAEHBX5s0zl+zd96BvHHuua/zttIHeQuTWSDjGrWsEHamty8xbZNfocC+fx7NMrle7XHvvxtFxobIZQ==", "requires": { - "@sinonjs/formatio": "3.0.0", + "@sinonjs/formatio": "^3.1.0", "just-extend": "^3.0.0", "lolex": "^2.3.2", "path-to-regexp": "^1.7.0", @@ -8023,7 +8036,7 @@ }, "text-encoding": { "version": "0.6.4", - "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", + "resolved": "http://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", "integrity": "sha1-45mpgiV6J22uQou5KEXLcb3CbRk=" }, "text-table": { diff --git a/package.json b/package.json index 106db8ef2..9b53e3a74 100644 --- a/package.json +++ b/package.json @@ -56,14 +56,12 @@ }, "dependencies": { "@sinonjs/commons": "^1.2.0", - "@sinonjs/formatio": "^3.0.0", - "@sinonjs/samsam": "^2.1.2", + "@sinonjs/formatio": "^3.1.0", + "@sinonjs/samsam": "^3.0.1", "diff": "^3.5.0", - "lodash.get": "^4.4.2", "lolex": "^3.0.0", - "nise": "^1.4.6", - "supports-color": "^5.5.0", - "type-detect": "^4.0.8" + "nise": "^1.4.7", + "supports-color": "^5.5.0" }, "devDependencies": { "@babel/core": "^7.1.6", diff --git a/test/util/core/deep-equal-test.js b/test/util/core/deep-equal-test.js index 34e3f8e16..03f34e299 100644 --- a/test/util/core/deep-equal-test.js +++ b/test/util/core/deep-equal-test.js @@ -1,7 +1,7 @@ "use strict"; var referee = require("@sinonjs/referee"); -var deepEqual = require("../../../lib/sinon/util/core/deep-equal"); +var deepEqual = require("@sinonjs/samsam").deepEqual; var match = require("../../../lib/sinon/match"); var createSpy = require("../../../lib/sinon/spy").create; var assert = referee.assert; @@ -330,23 +330,19 @@ describe("util/core/deepEqual", function() { }); it("does not run matchers against each other when using a matcher library", function() { - var matchDeepEqual = deepEqual.use(match); - var spyA = createSpy(); var matchA = match(spyA); var spyB = createSpy(); var matchB = match(spyB); - matchDeepEqual(matchA, matchB); + deepEqual(matchA, matchB); assert.equals(spyA.callCount, 0); assert.equals(spyB.callCount, 0); }); it("strictly compares instances when passed two matchers and using a matcher library", function() { - var matchDeepEqual = deepEqual.use(match); - var matchA = match(function a() { return "a"; }); @@ -357,8 +353,8 @@ describe("util/core/deepEqual", function() { var duplicateA = matchA; - assert(matchDeepEqual(matchA, duplicateA)); - assert.isFalse(matchDeepEqual(matchA, matchB)); + assert(deepEqual(matchA, duplicateA)); + assert.isFalse(deepEqual(matchA, matchB)); }); it("handles shallow cyclic objects", function() { @@ -392,8 +388,6 @@ describe("util/core/deepEqual", function() { }); it("handles cyclic objects when a matcher provided", function() { - var matchDeepEqual = deepEqual.use(match); - var a = { foo: "bar" }; @@ -404,6 +398,6 @@ describe("util/core/deepEqual", function() { }; b.cyclicKeyName = b; - assert(matchDeepEqual(a, b)); + assert(deepEqual(a, b)); }); }); diff --git a/test/util/core/every-test.js b/test/util/core/every-test.js deleted file mode 100644 index 70fafca83..000000000 --- a/test/util/core/every-test.js +++ /dev/null @@ -1,42 +0,0 @@ -"use strict"; - -var referee = require("@sinonjs/referee"); -var createSpy = require("../../../lib/sinon/spy"); -var every = require("../../../lib/sinon/util/core/every"); -var assert = referee.assert; - -describe("util/core/every", function() { - it("returns true when the callback function returns true for every element in an iterable", function() { - var obj = [true, true, true, true]; - var allTrue = every(obj, function(val) { - return val; - }); - - assert(allTrue); - }); - - it("returns false when the callback function returns false for any element in an iterable", function() { - var obj = [true, true, true, false]; - var result = every(obj, function(val) { - return val; - }); - - assert.isFalse(result); - }); - - it("calls the given callback once for each item in an iterable until it returns false", function() { - var iterableOne = [true, true, true, true]; - var iterableTwo = [true, true, false, true]; - var callback = createSpy(function(val) { - return val; - }); - - every(iterableOne, callback); - assert.equals(callback.callCount, 4); - - callback.resetHistory(); - - every(iterableTwo, callback); - assert.equals(callback.callCount, 3); - }); -}); diff --git a/test/util/core/iterable-to-string-test.js b/test/util/core/iterable-to-string-test.js deleted file mode 100644 index ce64737ab..000000000 --- a/test/util/core/iterable-to-string-test.js +++ /dev/null @@ -1,43 +0,0 @@ -"use strict"; - -var referee = require("@sinonjs/referee"); -var iterableToString = require("../../../lib/sinon/util/core/iterable-to-string"); -var assert = referee.assert; - -describe("util/core/iterable-to-string", function() { - it("returns an String representation of Array objects", function() { - var arr = [1, "one", true, undefined, null]; - var expected = "1,'one',true,undefined,null"; - - assert.equals(iterableToString(arr), expected); - }); - - if (typeof Map === "function") { - it("returns an String representation of Map objects", function() { - var map = new Map(); - map.set(1, 1); - map.set("one", "one"); - map.set(true, true); - map.set(undefined, undefined); - map.set(null, null); - var expected = "[1,1],['one','one'],[true,true],[undefined,undefined],[null,null]"; - - assert.equals(iterableToString(map), expected); - }); - } - - if (typeof Set === "function") { - it("returns an String representation of Set objects", function() { - var set = new Set(); - set.add(1); - set.add("one"); - set.add(true); - set.add(undefined); - set.add(null); - - var expected = "1,'one',true,undefined,null"; - - assert.equals(iterableToString(set), expected); - }); - } -}); diff --git a/test/util/core/typeOf-test.js b/test/util/core/typeOf-test.js deleted file mode 100644 index 888da7f99..000000000 --- a/test/util/core/typeOf-test.js +++ /dev/null @@ -1,52 +0,0 @@ -"use strict"; - -var referee = require("@sinonjs/referee"); -var sinonTypeOf = require("../../../lib/sinon/util/core/typeOf"); -var assert = referee.assert; - -describe("typeOf", function() { - it("returns boolean", function() { - assert.equals(sinonTypeOf(false), "boolean"); - }); - - it("returns string", function() { - assert.equals(sinonTypeOf("Sinon.JS"), "string"); - }); - - it("returns number", function() { - assert.equals(sinonTypeOf(123), "number"); - }); - - it("returns object", function() { - assert.equals(sinonTypeOf({}), "object"); - }); - - it("returns function", function() { - assert.equals( - sinonTypeOf(function() { - return; - }), - "function" - ); - }); - - it("returns undefined", function() { - assert.equals(sinonTypeOf(undefined), "undefined"); - }); - - it("returns null", function() { - assert.equals(sinonTypeOf(null), "null"); - }); - - it("returns array", function() { - assert.equals(sinonTypeOf([]), "array"); - }); - - it("returns regexp", function() { - assert.equals(sinonTypeOf(/.*/), "regexp"); - }); - - it("returns date", function() { - assert.equals(sinonTypeOf(new Date()), "date"); - }); -}); From 26706c60a90d7c4e47718be94c960abe3d9dbdea Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Mon, 10 Dec 2018 08:34:02 +0100 Subject: [PATCH 2/2] Remove obsolete tests and match.js --- lib/sinon.js | 2 +- lib/sinon/assert.js | 4 +- lib/sinon/call.js | 6 +- lib/sinon/match.js | 5 - lib/sinon/mock-expectation.js | 4 +- lib/sinon/sandbox.js | 6 +- lib/sinon/spy-formatters.js | 4 +- package-lock.json | 2 +- test/assert-test.js | 28 +- test/match-test.js | 1409 ----------------------------- test/mock-test.js | 6 +- test/sandbox-test.js | 6 +- test/spy-test.js | 16 +- test/stub-test.js | 10 +- test/util/core/deep-equal-test.js | 403 --------- 15 files changed, 47 insertions(+), 1864 deletions(-) delete mode 100644 lib/sinon/match.js delete mode 100644 test/match-test.js delete mode 100644 test/util/core/deep-equal-test.js diff --git a/lib/sinon.js b/lib/sinon.js index ca1ecf649..1a115e06d 100644 --- a/lib/sinon.js +++ b/lib/sinon.js @@ -13,7 +13,7 @@ var stub = require("./sinon/stub"); var apiMethods = { createSandbox: createSandbox, assert: require("./sinon/assert"), - match: require("./sinon/match"), + match: require("@sinonjs/samsam").createMatcher, spyCall: require("./sinon/call"), expectation: require("./sinon/mock-expectation"), diff --git a/lib/sinon/assert.js b/lib/sinon/assert.js index 396f26a4f..3286e9aa1 100644 --- a/lib/sinon/assert.js +++ b/lib/sinon/assert.js @@ -2,10 +2,10 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var calledInOrder = require("@sinonjs/commons").calledInOrder; +var createMatcher = require("@sinonjs/samsam").createMatcher; var orderByFirstCall = require("@sinonjs/commons").orderByFirstCall; var timesInWords = require("./util/core/times-in-words"); var format = require("./util/core/format"); -var sinonMatch = require("./match"); var stringSlice = require("@sinonjs/commons").prototypes.string.slice; var arraySlice = arrayProto.slice; @@ -170,7 +170,7 @@ assert = { }, match: function match(actual, expectation) { - var matcher = sinonMatch(expectation); + var matcher = createMatcher(expectation); if (matcher.test(actual)) { assert.pass("match"); } else { diff --git a/lib/sinon/call.js b/lib/sinon/call.js index ed613baca..2566ceb2d 100644 --- a/lib/sinon/call.js +++ b/lib/sinon/call.js @@ -1,7 +1,7 @@ "use strict"; var arrayProto = require("@sinonjs/commons").prototypes.array; -var sinonMatch = require("./match"); +var match = require("@sinonjs/samsam").createMatcher; var deepEqual = require("@sinonjs/samsam").deepEqual; var functionName = require("@sinonjs/commons").functionName; var sinonFormat = require("./util/core/format"); @@ -24,7 +24,7 @@ function throwYieldError(proxy, text, args) { var callProto = { calledOn: function calledOn(thisValue) { - if (sinonMatch && sinonMatch.isMatcher(thisValue)) { + if (match.isMatcher(thisValue)) { return thisValue.test(this.thisValue); } return this.thisValue === thisValue; @@ -60,7 +60,7 @@ var callProto = { function(prev, expectation, i) { var actual = self.args[i]; - return prev && (sinonMatch && sinonMatch(expectation).test(actual)); + return prev && match(expectation).test(actual); }, true ); diff --git a/lib/sinon/match.js b/lib/sinon/match.js deleted file mode 100644 index 2422f8b32..000000000 --- a/lib/sinon/match.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -var samsam = require("@sinonjs/samsam"); - -module.exports = samsam.createMatcher; diff --git a/lib/sinon/mock-expectation.js b/lib/sinon/mock-expectation.js index b644098b3..7f60ae1dd 100644 --- a/lib/sinon/mock-expectation.js +++ b/lib/sinon/mock-expectation.js @@ -5,7 +5,7 @@ var spyInvoke = require("./spy").invoke; var spyCallToString = require("./call").toString; var timesInWords = require("./util/core/times-in-words"); var extend = require("./util/core/extend"); -var match = require("./match"); +var match = require("@sinonjs/samsam").createMatcher; var stub = require("./stub"); var assert = require("./assert"); var deepEqual = require("@sinonjs/samsam").deepEqual; @@ -60,7 +60,7 @@ function receivedMaxCalls(expectation) { } function verifyMatcher(possibleMatcher, arg) { - var isMatcher = match && match.isMatcher(possibleMatcher); + var isMatcher = match.isMatcher(possibleMatcher); return (isMatcher && possibleMatcher.test(arg)) || true; } diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index 261238622..38ea72820 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -6,7 +6,7 @@ var getPropertyDescriptor = require("./util/core/get-property-descriptor"); var isEsModule = require("./util/core/is-es-module"); var isPropertyConfigurable = require("./util/core/is-property-configurable"); var isNonExistentOwnProperty = require("./util/core/is-non-existent-own-property"); -var sinonMatch = require("./match"); +var match = require("@sinonjs/samsam").createMatcher; var sinonAssert = require("./assert"); var sinonClock = require("./util/fake-timers"); var sinonMock = require("./mock"); @@ -80,7 +80,7 @@ function Sandbox() { obj.requests = sandbox.server.requests; } - obj.match = sinonMatch; + obj.match = match; return obj; }; @@ -386,6 +386,6 @@ function Sandbox() { } Sandbox.prototype.assert = sinonAssert; -Sandbox.prototype.match = sinonMatch; +Sandbox.prototype.match = match; module.exports = Sandbox; diff --git a/lib/sinon/spy-formatters.js b/lib/sinon/spy-formatters.js index d79919fe8..eb4dd55ee 100644 --- a/lib/sinon/spy-formatters.js +++ b/lib/sinon/spy-formatters.js @@ -2,9 +2,9 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var color = require("./color"); +var match = require("@sinonjs/samsam").createMatcher; var timesInWords = require("./util/core/times-in-words"); var sinonFormat = require("./util/core/format"); -var sinonMatch = require("./match"); var jsDiff = require("diff"); var join = arrayProto.join; @@ -60,7 +60,7 @@ module.exports = { for (var j = 0; j < calledArgs.length || j < args.length; ++j) { message += "\n"; var calledArgMessage = j < calledArgs.length ? sinonFormat(calledArgs[j]) : ""; - if (sinonMatch.isMatcher(args[j])) { + if (match.isMatcher(args[j])) { message += colorSinonMatchText(args[j], calledArgs[j], calledArgMessage); } else { var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : ""; diff --git a/package-lock.json b/package-lock.json index 8eb1d78fc..a7384534c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8036,7 +8036,7 @@ }, "text-encoding": { "version": "0.6.4", - "resolved": "http://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", + "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", "integrity": "sha1-45mpgiV6J22uQou5KEXLcb3CbRk=" }, "text-table": { diff --git a/test/assert-test.js b/test/assert-test.js index 4ce48f039..c73730e10 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -5,7 +5,7 @@ var referee = require("@sinonjs/referee"); var sinonStub = require("../lib/sinon/stub"); var sinonSpy = require("../lib/sinon/spy"); var sinonAssert = require("../lib/sinon/assert"); -var sinonMatch = require("../lib/sinon/match"); +var match = require("@sinonjs/samsam").createMatcher; var assert = referee.assert; var refute = referee.refute; @@ -1543,7 +1543,7 @@ describe("assert", function() { this.obj.doSomething(true, true); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch.any, false).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match.any, false).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n" + "true any\n" + color.red("true") + @@ -1557,7 +1557,7 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch.defined).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match.defined).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("defined") ); }); @@ -1566,7 +1566,7 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch.truthy).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match.truthy).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("truthy") ); }); @@ -1575,7 +1575,7 @@ describe("assert", function() { this.obj.doSomething(true); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch.falsy).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match.falsy).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n" + color.green("true") + " " + color.red("falsy") ); }); @@ -1584,14 +1584,14 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch.same(1)).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match.same(1)).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("same(1)") ); }); it("assert.calledWith match.typeOf exception message", function() { this.obj.doSomething(); - var matcher = sinonMatch.typeOf("string"); + var matcher = match.typeOf("string"); assert.equals( this.message("calledWith", this.obj.doSomething, matcher).replace(/ at.*/g, ""), @@ -1601,7 +1601,7 @@ describe("assert", function() { it("assert.calledWith match.instanceOf exception message", function() { this.obj.doSomething(); - var matcher = sinonMatch.instanceOf(function CustomType() { + var matcher = match.instanceOf(function CustomType() { return; }); @@ -1613,7 +1613,7 @@ describe("assert", function() { it("assert.calledWith match object exception message", function() { this.obj.doSomething(); - var matcher = sinonMatch({ some: "value", and: 123 }); + var matcher = match({ some: "value", and: 123 }); assert.equals( this.message("calledWith", this.obj.doSomething, matcher).replace(/ at.*/g, ""), @@ -1625,7 +1625,7 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch(true)).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match(true)).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("match(true)") ); }); @@ -1634,14 +1634,14 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch(123)).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match(123)).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("match(123)") ); }); it("assert.calledWith match string exception message", function() { this.obj.doSomething(); - var matcher = sinonMatch("Sinon"); + var matcher = match("Sinon"); assert.equals( this.message("calledWith", this.obj.doSomething, matcher).replace(/ at.*/g, ""), @@ -1653,14 +1653,14 @@ describe("assert", function() { this.obj.doSomething(); assert.equals( - this.message("calledWith", this.obj.doSomething, sinonMatch(/[a-z]+/)).replace(/ at.*/g, ""), + this.message("calledWith", this.obj.doSomething, match(/[a-z]+/)).replace(/ at.*/g, ""), "expected doSomething to be called with arguments \n " + color.red("match(/[a-z]+/)") ); }); it("assert.calledWith match test function exception message", function() { this.obj.doSomething(); - var matcher = sinonMatch({ + var matcher = match({ test: function custom() { return; } diff --git a/test/match-test.js b/test/match-test.js deleted file mode 100644 index 2158fb69f..000000000 --- a/test/match-test.js +++ /dev/null @@ -1,1409 +0,0 @@ -"use strict"; - -var referee = require("@sinonjs/referee"); -var assert = referee.assert; -var refute = referee.refute; -var sinonMatch = require("../lib/sinon/match"); - -function propertyMatcherTests(matcher, additionalTests) { - return function() { - it("returns matcher", function() { - var has = matcher("foo"); - - assert(sinonMatch.isMatcher(has)); - }); - - it("throws if first argument is not string", function() { - assert.exception( - function() { - matcher(); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - matcher(123); - }, - { name: "TypeError" } - ); - }); - - it("returns false if value is undefined or null", function() { - var has = matcher("foo"); - - assert.isFalse(has.test(undefined)); - assert.isFalse(has.test(null)); - }); - - it("returns true if object has property", function() { - var has = matcher("foo"); - - assert(has.test({ foo: null })); - }); - - it("returns false if object value is not equal to given value", function() { - var has = matcher("foo", 1); - - assert.isFalse(has.test({ foo: null })); - }); - - it("returns true if object value is equal to given value", function() { - var has = matcher("message", "sinon rocks"); - - assert(has.test({ message: "sinon rocks" })); - assert(has.test(new Error("sinon rocks"))); - }); - - it("returns true if string property matches", function() { - var has = matcher("length", 5); - - assert(has.test("sinon")); - }); - - it("allows to expect undefined", function() { - var has = matcher("foo", undefined); - - assert.isFalse(has.test({ foo: 1 })); - }); - - it("compares value deeply", function() { - var has = matcher("foo", { bar: "doo", test: 42 }); - - assert(has.test({ foo: { bar: "doo", test: 42 } })); - }); - - it("compares with matcher", function() { - var has = matcher("callback", sinonMatch.typeOf("function")); - - assert( - has.test({ - callback: function() { - return; - } - }) - ); - }); - - if (typeof additionalTests === "function") { - additionalTests(); - } - }; -} - -describe("sinonMatch", function() { - it("returns matcher", function() { - var match = sinonMatch(function() { - return; - }); - - assert(sinonMatch.isMatcher(match)); - }); - - it("exposes test function", function() { - var test = function() { - return; - }; - - var match = sinonMatch(test); - - assert.same(match.test, test); - }); - - it("returns true if properties are equal", function() { - var match = sinonMatch({ str: "sinon", nr: 1 }); - - assert(match.test({ str: "sinon", nr: 1, other: "ignored" })); - }); - - it("returns true if properties are deep equal", function() { - var match = sinonMatch({ deep: { str: "sinon" } }); - - assert(match.test({ deep: { str: "sinon", ignored: "value" } })); - }); - - it("returns false if a property is not equal", function() { - var match = sinonMatch({ str: "sinon", nr: 1 }); - - assert.isFalse(match.test({ str: "sinon", nr: 2 })); - }); - - it("returns false if a property is missing", function() { - var match = sinonMatch({ str: "sinon", nr: 1 }); - - assert.isFalse(match.test({ nr: 1 })); - }); - - it("returns true if array is equal", function() { - var match = sinonMatch({ arr: ["a", "b"] }); - - assert(match.test({ arr: ["a", "b"] })); - }); - - it("returns false if array is not equal", function() { - var match = sinonMatch({ arr: ["b", "a"] }); - - assert.isFalse(match.test({ arr: ["a", "b"] })); - }); - - it("returns false if array is not equal (even if the contents would match (deep equal))", function() { - var match = sinonMatch([{ str: "sinon" }]); - - assert.isFalse(match.test([{ str: "sinon", ignored: "value" }])); - }); - - it("returns true if number objects are equal", function() { - /*eslint-disable no-new-wrappers*/ - var match = sinonMatch({ one: new Number(1) }); - - assert(match.test({ one: new Number(1) })); - /*eslint-enable no-new-wrappers*/ - }); - - it("returns true if test matches", function() { - var match = sinonMatch({ prop: sinonMatch.typeOf("boolean") }); - - assert(match.test({ prop: true })); - }); - - it("returns false if test does not match", function() { - var match = sinonMatch({ prop: sinonMatch.typeOf("boolean") }); - - assert.isFalse(match.test({ prop: "no" })); - }); - - it("returns true if deep test matches", function() { - var match = sinonMatch({ deep: { prop: sinonMatch.typeOf("boolean") } }); - - assert(match.test({ deep: { prop: true } })); - }); - - it("returns false if deep test does not match", function() { - var match = sinonMatch({ deep: { prop: sinonMatch.typeOf("boolean") } }); - - assert.isFalse(match.test({ deep: { prop: "no" } })); - }); - - it("returns false if tested value is null or undefined", function() { - var match = sinonMatch({}); - - assert.isFalse(match.test(null)); - assert.isFalse(match.test(undefined)); - }); - - it("returns true if error message matches", function() { - var match = sinonMatch({ message: "evil error" }); - - assert(match.test(new Error("evil error"))); - }); - - it("returns true if string property matches", function() { - var match = sinonMatch({ length: 5 }); - - assert(match.test("sinon")); - }); - - it("returns true if number property matches", function() { - var match = sinonMatch({ toFixed: sinonMatch.func }); - - assert(match.test(0)); - }); - - it("returns true for string match", function() { - var match = sinonMatch("sinon"); - - assert(match.test("sinon")); - }); - - it("returns true for substring match", function() { - var match = sinonMatch("no"); - - assert(match.test("sinon")); - }); - - it("returns false for string mismatch", function() { - var match = sinonMatch("Sinon.JS"); - - assert.isFalse(match.test(null)); - assert.isFalse(match.test({})); - assert.isFalse(match.test("sinon")); - assert.isFalse(match.test("sinon.js")); - }); - - it("returns true for regexp match", function() { - var match = sinonMatch(/^[sino]+$/); - - assert(match.test("sinon")); - }); - - it("returns false for regexp string mismatch", function() { - var match = sinonMatch(/^[sin]+$/); - - assert.isFalse(match.test("sinon")); - }); - - it("returns false for regexp type mismatch", function() { - var match = sinonMatch(/.*/); - - assert.isFalse(match.test()); - assert.isFalse(match.test(null)); - assert.isFalse(match.test(123)); - assert.isFalse(match.test({})); - }); - - it("returns true for number match", function() { - var match = sinonMatch(1); - - assert(match.test(1)); - assert(match.test("1")); - assert(match.test(true)); - }); - - it("returns false for number mismatch", function() { - var match = sinonMatch(1); - - assert.isFalse(match.test()); - assert.isFalse(match.test(null)); - assert.isFalse(match.test(2)); - assert.isFalse(match.test(false)); - assert.isFalse(match.test({})); - }); - - it("returns true for Symbol match", function() { - if (typeof Symbol === "function") { - var symbol = Symbol(); - - var match = sinonMatch(symbol); - - assert(match.test(symbol)); - } - }); - - it("returns false for Symbol mismatch", function() { - if (typeof Symbol === "function") { - var match = sinonMatch(Symbol()); - - assert.isFalse(match.test()); - assert.isFalse(match.test(Symbol(null))); - assert.isFalse(match.test(Symbol())); - assert.isFalse(match.test(Symbol({}))); - } - }); - - it("returns true for Symbol inside object", function() { - if (typeof Symbol === "function") { - var symbol = Symbol(); - - var match = sinonMatch({ prop: symbol }); - - assert(match.test({ prop: symbol })); - } - }); - - it("returns true if test function in object returns true", function() { - var match = sinonMatch({ - test: function() { - return true; - } - }); - - assert(match.test()); - }); - - it("returns false if test function in object returns false", function() { - var match = sinonMatch({ - test: function() { - return false; - } - }); - - assert.isFalse(match.test()); - }); - - it("returns false if test function in object returns nothing", function() { - var match = sinonMatch({ - test: function() { - return; - } - }); - - assert.isFalse(match.test()); - }); - - it("passes actual value to test function in object", function() { - var match = sinonMatch({ - test: function(arg) { - return arg; - } - }); - - assert(match.test(true)); - }); - - it("uses matcher", function() { - var match = sinonMatch(sinonMatch("test")); - - assert(match.test("testing")); - }); - - describe(".toString", function() { - it("returns message", function() { - var message = "hello sinonMatch"; - - var match = sinonMatch(function() { - return; - }, message); - - assert.same(match.toString(), message); - }); - - it("defaults to match(functionName)", function() { - var match = sinonMatch(function custom() { - return; - }); - - assert.same(match.toString(), "match(custom)"); - }); - }); - - describe(".any", function() { - it("is matcher", function() { - assert(sinonMatch.isMatcher(sinonMatch.any)); - }); - - it("returns true when tested", function() { - assert(sinonMatch.any.test()); - }); - }); - - describe(".defined", function() { - it("is matcher", function() { - assert(sinonMatch.isMatcher(sinonMatch.defined)); - }); - - it("returns false if test is called with null", function() { - assert.isFalse(sinonMatch.defined.test(null)); - }); - - it("returns false if test is called with undefined", function() { - assert.isFalse(sinonMatch.defined.test(undefined)); - }); - - it("returns true if test is called with any value", function() { - assert(sinonMatch.defined.test(false)); - assert(sinonMatch.defined.test(true)); - assert(sinonMatch.defined.test(0)); - assert(sinonMatch.defined.test(1)); - assert(sinonMatch.defined.test("")); - }); - - it("returns true if test is called with any object", function() { - assert(sinonMatch.defined.test({})); - assert( - sinonMatch.defined.test(function() { - return; - }) - ); - }); - }); - - describe(".truthy", function() { - it("is matcher", function() { - assert(sinonMatch.isMatcher(sinonMatch.truthy)); - }); - - it("returns true if test is called with trueish value", function() { - assert(sinonMatch.truthy.test(true)); - assert(sinonMatch.truthy.test(1)); - assert(sinonMatch.truthy.test("yes")); - }); - - it("returns false if test is called falsy value", function() { - assert.isFalse(sinonMatch.truthy.test(false)); - assert.isFalse(sinonMatch.truthy.test(null)); - assert.isFalse(sinonMatch.truthy.test(undefined)); - assert.isFalse(sinonMatch.truthy.test("")); - }); - }); - - describe(".falsy", function() { - it("is matcher", function() { - assert(sinonMatch.isMatcher(sinonMatch.falsy)); - }); - - it("returns true if test is called falsy value", function() { - assert(sinonMatch.falsy.test(false)); - assert(sinonMatch.falsy.test(null)); - assert(sinonMatch.falsy.test(undefined)); - assert(sinonMatch.falsy.test("")); - }); - - it("returns false if test is called with trueish value", function() { - assert.isFalse(sinonMatch.falsy.test(true)); - assert.isFalse(sinonMatch.falsy.test(1)); - assert.isFalse(sinonMatch.falsy.test("yes")); - }); - }); - - describe(".same", function() { - it("returns matcher", function() { - var same = sinonMatch.same(); - - assert(sinonMatch.isMatcher(same)); - }); - - it("returns true if test is called with same argument", function() { - var object = {}; - var same = sinonMatch.same(object); - - assert(same.test(object)); - }); - - it("returns true if test is called with same symbol", function() { - if (typeof Symbol === "function") { - var symbol = Symbol(); - var same = sinonMatch.same(symbol); - - assert(same.test(symbol)); - } - }); - - it("returns false if test is not called with same argument", function() { - var same = sinonMatch.same({}); - - assert.isFalse(same.test({})); - }); - }); - - describe(".in", function() { - it("returns matcher", function() { - var inMatcher = sinonMatch.in([]); - assert(sinonMatch.isMatcher(inMatcher)); - }); - - it("throws if given argument is not an array", function() { - var arg = "not-array"; - - assert.exception( - function() { - sinonMatch.in(arg); - }, - { name: "TypeError", message: "array expected" } - ); - }); - - describe("when given argument is an array", function() { - var arrays = [ - [1, 2, 3], - ["a", "b", "c"], - [{ a: "a" }, { b: "b" }], - [ - function() { - return; - }, - function() { - return; - } - ], - [null, undefined] - ]; - - it("returns true if the tested value in the given array", function() { - arrays.forEach(function(array) { - var inMatcher = sinonMatch.in(array); - assert.isTrue(inMatcher.test(array[0])); - }); - }); - - it("returns false if the tested value not in the given array", function() { - arrays.forEach(function(array) { - var inMatcher = sinonMatch.in(array); - assert.isFalse(inMatcher.test("something else")); - }); - }); - }); - }); - - describe(".typeOf", function() { - it("throws if given argument is not a string", function() { - assert.exception( - function() { - sinonMatch.typeOf(); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.typeOf(123); - }, - { name: "TypeError" } - ); - }); - - it("returns matcher", function() { - var typeOf = sinonMatch.typeOf("string"); - - assert(sinonMatch.isMatcher(typeOf)); - }); - - it("returns true if test is called with string", function() { - var typeOf = sinonMatch.typeOf("string"); - - assert(typeOf.test("Sinon.JS")); - }); - - it("returns false if test is not called with string", function() { - var typeOf = sinonMatch.typeOf("string"); - - assert.isFalse(typeOf.test(123)); - }); - - it("returns true if test is called with symbol", function() { - if (typeof Symbol === "function") { - var typeOf = sinonMatch.typeOf("symbol"); - - assert(typeOf.test(Symbol())); - } - }); - - it("returns true if test is called with regexp", function() { - var typeOf = sinonMatch.typeOf("regexp"); - - assert(typeOf.test(/.+/)); - }); - - it("returns false if test is not called with regexp", function() { - var typeOf = sinonMatch.typeOf("regexp"); - - assert.isFalse(typeOf.test(true)); - }); - }); - - describe(".instanceOf", function() { - it("throws if given argument is not a function", function() { - assert.exception( - function() { - sinonMatch.instanceOf(); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.instanceOf("foo"); - }, - { name: "TypeError" } - ); - }); - - if (typeof Symbol !== "undefined" && typeof Symbol.hasInstance !== "undefined") { - it("does not throw if given argument defines Symbol.hasInstance", function() { - var objectWithCustomTypeChecks = {}; - objectWithCustomTypeChecks[Symbol.hasInstance] = function() { - return; - }; - sinonMatch.instanceOf(objectWithCustomTypeChecks); - }); - } - - it("returns matcher", function() { - var instanceOf = sinonMatch.instanceOf(function() { - return; - }); - - assert(sinonMatch.isMatcher(instanceOf)); - }); - - it("returns true if test is called with instance of argument", function() { - var instanceOf = sinonMatch.instanceOf(Array); - - assert(instanceOf.test([])); - }); - - it("returns false if test is not called with instance of argument", function() { - var instanceOf = sinonMatch.instanceOf(Array); - - assert.isFalse(instanceOf.test({})); - }); - }); - - describe(".has", propertyMatcherTests(sinonMatch.has)); - describe(".hasOwn", propertyMatcherTests(sinonMatch.hasOwn)); - - describe( - ".hasNested", - propertyMatcherTests(sinonMatch.hasNested, function() { - it("compares nested value", function() { - var hasNested = sinonMatch.hasNested("foo.bar", "doo"); - - assert(hasNested.test({ foo: { bar: "doo" } })); - }); - - it("compares nested array value", function() { - var hasNested = sinonMatch.hasNested("foo[0].bar", "doo"); - - assert(hasNested.test({ foo: [{ bar: "doo" }] })); - }); - }) - ); - - describe(".hasSpecial", function() { - it("returns true if object has inherited property", function() { - var has = sinonMatch.has("toString"); - - assert(has.test({})); - }); - - it("only includes property in message", function() { - var has = sinonMatch.has("test"); - - assert.equals(has.toString(), 'has("test")'); - }); - - it("includes property and value in message", function() { - var has = sinonMatch.has("test", undefined); - - assert.equals(has.toString(), 'has("test", undefined)'); - }); - - it("returns true if string function matches", function() { - var has = sinonMatch.has("toUpperCase", sinonMatch.func); - - assert(has.test("sinon")); - }); - - it("returns true if number function matches", function() { - var has = sinonMatch.has("toFixed", sinonMatch.func); - - assert(has.test(0)); - }); - - it("returns true if object has Symbol", function() { - if (typeof Symbol === "function") { - var symbol = Symbol(); - - var has = sinonMatch.has("prop", symbol); - - assert(has.test({ prop: symbol })); - } - }); - - it("returns true if embedded object has Symbol", function() { - if (typeof Symbol === "function") { - var symbol = Symbol(); - - var has = sinonMatch.has("prop", sinonMatch.has("embedded", symbol)); - - assert(has.test({ prop: { embedded: symbol }, ignored: 42 })); - } - }); - }); - - describe(".hasOwnSpecial", function() { - it("returns false if object has inherited property", function() { - var hasOwn = sinonMatch.hasOwn("toString"); - - assert.isFalse(hasOwn.test({})); - }); - - it("only includes property in message", function() { - var hasOwn = sinonMatch.hasOwn("test"); - - assert.equals(hasOwn.toString(), 'hasOwn("test")'); - }); - - it("includes property and value in message", function() { - var hasOwn = sinonMatch.hasOwn("test", undefined); - - assert.equals(hasOwn.toString(), 'hasOwn("test", undefined)'); - }); - }); - - describe(".every", function() { - it("throws if given argument is not a matcher", function() { - assert.exception( - function() { - sinonMatch.every({}); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.every(123); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.every("123"); - }, - { name: "TypeError" } - ); - }); - - it("returns matcher", function() { - var every = sinonMatch.every(sinonMatch.any); - - assert(sinonMatch.isMatcher(every)); - }); - - it('wraps the given matcher message with an "every()"', function() { - var every = sinonMatch.every(sinonMatch.number); - - assert.equals(every.toString(), 'every(typeOf("number"))'); - }); - - it("fails to match anything that is not an object or an iterable", function() { - var every = sinonMatch.every(sinonMatch.any); - - refute(every.test(1)); - refute(every.test("a")); - refute(every.test(null)); - refute( - every.test(function() { - return; - }) - ); - }); - - it("matches an object if the predicate is true for every property", function() { - var every = sinonMatch.every(sinonMatch.number); - - assert(every.test({ a: 1, b: 2 })); - }); - - it("fails if the predicate is false for some of the object properties", function() { - var every = sinonMatch.every(sinonMatch.number); - - refute(every.test({ a: 1, b: "b" })); - }); - - it("matches an array if the predicate is true for every element", function() { - var every = sinonMatch.every(sinonMatch.number); - - assert(every.test([1, 2])); - }); - - it("fails if the predicate is false for some of the array elements", function() { - var every = sinonMatch.every(sinonMatch.number); - - refute(every.test([1, "b"])); - }); - - if (typeof Set === "function") { - it("matches an iterable if the predicate is true for every element", function() { - var every = sinonMatch.every(sinonMatch.number); - var set = new Set(); - set.add(1); - set.add(2); - - assert(every.test(set)); - }); - - it("fails if the predicate is false for some of the iterable elements", function() { - var every = sinonMatch.every(sinonMatch.number); - var set = new Set(); - set.add(1); - set.add("b"); - - refute(every.test(set)); - }); - } - }); - - describe(".some", function() { - it("throws if given argument is not a matcher", function() { - assert.exception( - function() { - sinonMatch.some({}); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.some(123); - }, - { name: "TypeError" } - ); - assert.exception( - function() { - sinonMatch.some("123"); - }, - { name: "TypeError" } - ); - }); - - it("returns matcher", function() { - var some = sinonMatch.some(sinonMatch.any); - - assert(sinonMatch.isMatcher(some)); - }); - - it('wraps the given matcher message with an "some()"', function() { - var some = sinonMatch.some(sinonMatch.number); - - assert.equals(some.toString(), 'some(typeOf("number"))'); - }); - - it("fails to match anything that is not an object or an iterable", function() { - var some = sinonMatch.some(sinonMatch.any); - - refute(some.test(1)); - refute(some.test("a")); - refute(some.test(null)); - refute( - some.test(function() { - return; - }) - ); - }); - - it("matches an object if the predicate is true for some of the properties", function() { - var some = sinonMatch.some(sinonMatch.number); - - assert(some.test({ a: 1, b: "b" })); - }); - - it("fails if the predicate is false for all of the object properties", function() { - var some = sinonMatch.some(sinonMatch.number); - - refute(some.test({ a: "a", b: "b" })); - }); - - it("matches an array if the predicate is true for some element", function() { - var some = sinonMatch.some(sinonMatch.number); - - assert(some.test([1, "b"])); - }); - - it("fails if the predicate is false for all of the array elements", function() { - var some = sinonMatch.some(sinonMatch.number); - - refute(some.test(["a", "b"])); - }); - - if (typeof Set === "function") { - it("matches an iterable if the predicate is true for some element", function() { - var some = sinonMatch.some(sinonMatch.number); - var set = new Set(); - set.add(1); - set.add("b"); - - assert(some.test(set)); - }); - - it("fails if the predicate is false for all of the iterable elements", function() { - var some = sinonMatch.some(sinonMatch.number); - var set = new Set(); - set.add("a"); - set.add("b"); - - refute(some.test(set)); - }); - } - }); - - describe(".bool", function() { - it("is typeOf boolean matcher", function() { - var bool = sinonMatch.bool; - - assert(sinonMatch.isMatcher(bool)); - assert.equals(bool.toString(), 'typeOf("boolean")'); - }); - }); - - describe(".number", function() { - it("is typeOf number matcher", function() { - var number = sinonMatch.number; - - assert(sinonMatch.isMatcher(number)); - assert.equals(number.toString(), 'typeOf("number")'); - }); - }); - - describe(".string", function() { - it("is typeOf string matcher", function() { - var string = sinonMatch.string; - - assert(sinonMatch.isMatcher(string)); - assert.equals(string.toString(), 'typeOf("string")'); - }); - }); - - describe(".object", function() { - it("is typeOf object matcher", function() { - var object = sinonMatch.object; - - assert(sinonMatch.isMatcher(object)); - assert.equals(object.toString(), 'typeOf("object")'); - }); - }); - - describe(".func", function() { - it("is typeOf function matcher", function() { - var func = sinonMatch.func; - - assert(sinonMatch.isMatcher(func)); - assert.equals(func.toString(), 'typeOf("function")'); - }); - }); - - describe(".array", function() { - it("is typeOf array matcher", function() { - var array = sinonMatch.array; - - 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])); - }); - - 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() { - 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])); - }); - - 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() { - 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])); - }); - - 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() { - 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])); - }); - - 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 })); - }); - }); - }); - - describe(".map", function() { - it("is typeOf map matcher", function() { - var map = sinonMatch.map; - - assert(sinonMatch.isMatcher(map)); - assert.equals(map.toString(), 'typeOf("map")'); - }); - - describe("map.deepEquals", function() { - if (typeof Map === "function") { - it("has a .deepEquals matcher", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var deepEquals = sinonMatch.map.deepEquals(mapOne); - assert(sinonMatch.isMatcher(deepEquals)); - assert.equals(deepEquals.toString(), "deepEquals(Map[['one',1],['two',2],['three',3]])"); - }); - - it("matches maps with the exact same elements", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var mapTwo = new Map(); - mapTwo.set("one", 1); - mapTwo.set("two", 2); - mapTwo.set("three", 3); - - var mapThree = new Map(); - mapThree.set("one", 1); - mapThree.set("two", 2); - - var deepEquals = sinonMatch.map.deepEquals(mapOne); - assert(deepEquals.test(mapTwo)); - assert.isFalse(deepEquals.test(mapThree)); - assert.isFalse(deepEquals.test(new Map())); - }); - - it("fails when maps have the same keys but different values", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var mapTwo = new Map(); - mapTwo.set("one", 2); - mapTwo.set("two", 4); - mapTwo.set("three", 8); - - var mapThree = new Map(); - mapTwo.set("one", 1); - mapTwo.set("two", 2); - mapTwo.set("three", 4); - - var deepEquals = sinonMatch.map.deepEquals(mapOne); - assert.isFalse(deepEquals.test(mapTwo)); - assert.isFalse(deepEquals.test(mapThree)); - }); - - it("fails when passed a non-map object", function() { - var deepEquals = sinonMatch.array.deepEquals(new Map()); - assert.isFalse(deepEquals.test({})); - assert.isFalse(deepEquals.test([])); - }); - } - }); - - describe("map.contains", function() { - if (typeof Map === "function") { - it("has a .contains matcher", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var contains = sinonMatch.map.contains(mapOne); - assert(sinonMatch.isMatcher(contains)); - assert.equals(contains.toString(), "contains(Map[['one',1],['two',2],['three',3]])"); - }); - - it("matches maps containing the given elements", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var mapTwo = new Map(); - mapTwo.set("one", 1); - mapTwo.set("two", 2); - mapTwo.set("three", 3); - - var mapThree = new Map(); - mapThree.set("one", 1); - mapThree.set("two", 2); - - var mapFour = new Map(); - mapFour.set("one", 1); - mapFour.set("four", 4); - - assert(sinonMatch.map.contains(mapTwo).test(mapOne)); - assert(sinonMatch.map.contains(mapThree).test(mapOne)); - assert.isFalse(sinonMatch.map.contains(mapFour).test(mapOne)); - }); - - it("fails when maps contain the same keys but different values", function() { - var mapOne = new Map(); - mapOne.set("one", 1); - mapOne.set("two", 2); - mapOne.set("three", 3); - - var mapTwo = new Map(); - mapTwo.set("one", 2); - mapTwo.set("two", 4); - mapTwo.set("three", 8); - - var mapThree = new Map(); - mapThree.set("one", 1); - mapThree.set("two", 2); - mapThree.set("three", 4); - - assert.isFalse(sinonMatch.map.contains(mapTwo).test(mapOne)); - assert.isFalse(sinonMatch.map.contains(mapThree).test(mapOne)); - }); - - it("fails when passed a non-map object", function() { - var contains = sinonMatch.map.contains(new Map()); - assert.isFalse(contains.test({})); - assert.isFalse(contains.test([])); - }); - } - }); - }); - - describe(".set", function() { - it("is typeOf set matcher", function() { - var set = sinonMatch.set; - - assert(sinonMatch.isMatcher(set)); - assert.equals(set.toString(), 'typeOf("set")'); - }); - - describe("set.deepEquals", function() { - if (typeof Set === "function") { - it("has a .deepEquals matcher", function() { - var setOne = new Set(); - setOne.add("one"); - setOne.add("two"); - setOne.add("three"); - - var deepEquals = sinonMatch.set.deepEquals(setOne); - assert(sinonMatch.isMatcher(deepEquals)); - assert.equals(deepEquals.toString(), "deepEquals(Set['one','two','three'])"); - }); - - it("matches sets with the exact same elements", function() { - var setOne = new Set(); - setOne.add("one"); - setOne.add("two"); - setOne.add("three"); - - var setTwo = new Set(); - setTwo.add("one"); - setTwo.add("two"); - setTwo.add("three"); - - var setThree = new Set(); - setThree.add("one"); - setThree.add("two"); - - var deepEquals = sinonMatch.set.deepEquals(setOne); - assert(deepEquals.test(setTwo)); - assert.isFalse(deepEquals.test(setThree)); - assert.isFalse(deepEquals.test(new Set())); - }); - - it("fails when passed a non-set object", function() { - var deepEquals = sinonMatch.array.deepEquals(new Set()); - assert.isFalse(deepEquals.test({})); - assert.isFalse(deepEquals.test([])); - }); - } - }); - - describe("set.contains", function() { - if (typeof Set === "function") { - it("has a .contains matcher", function() { - var setOne = new Set(); - setOne.add("one"); - setOne.add("two"); - setOne.add("three"); - - var contains = sinonMatch.set.contains(setOne); - assert(sinonMatch.isMatcher(contains)); - assert.equals(contains.toString(), "contains(Set['one','two','three'])"); - }); - - it("matches sets containing the given elements", function() { - var setOne = new Set(); - setOne.add("one"); - setOne.add("two"); - setOne.add("three"); - - var setTwo = new Set(); - setTwo.add("one"); - setTwo.add("two"); - setTwo.add("three"); - - var setThree = new Set(); - setThree.add("one"); - setThree.add("two"); - - var setFour = new Set(); - setFour.add("one"); - setFour.add("four"); - - assert(sinonMatch.set.contains(setTwo).test(setOne)); - assert(sinonMatch.set.contains(setThree).test(setOne)); - assert.isFalse(sinonMatch.set.contains(setFour).test(setOne)); - }); - - it("fails when passed a non-set object", function() { - var contains = sinonMatch.set.contains(new Set()); - assert.isFalse(contains.test({})); - assert.isFalse(contains.test([])); - }); - } - }); - }); - - describe(".regexp", function() { - it("is typeOf regexp matcher", function() { - var regexp = sinonMatch.regexp; - - assert(sinonMatch.isMatcher(regexp)); - assert.equals(regexp.toString(), 'typeOf("regexp")'); - }); - }); - - describe(".date", function() { - it("is typeOf regexp matcher", function() { - var date = sinonMatch.date; - - assert(sinonMatch.isMatcher(date)); - assert.equals(date.toString(), 'typeOf("date")'); - }); - }); - - describe(".symbol", function() { - it("is typeOf symbol matcher", function() { - var symbol = sinonMatch.symbol; - - assert(sinonMatch.isMatcher(symbol)); - assert.equals(symbol.toString(), 'typeOf("symbol")'); - }); - }); - - describe(".or", function() { - it("is matcher", function() { - var numberOrString = sinonMatch.number.or(sinonMatch.string); - - assert(sinonMatch.isMatcher(numberOrString)); - assert.equals(numberOrString.toString(), 'typeOf("number").or(typeOf("string"))'); - }); - - it("requires matcher argument", function() { - assert.exception( - function() { - sinonMatch.instanceOf(Error).or(); - }, - { name: "TypeError" } - ); - }); - - it("will coerce argument to matcher", function() { - var abcOrDef = sinonMatch("abc").or("def"); - - assert(sinonMatch.isMatcher(abcOrDef)); - assert.equals(abcOrDef.toString(), 'match("abc").or(match("def"))'); - }); - - it("returns true if either matcher matches", function() { - var numberOrString = sinonMatch.number.or(sinonMatch.string); - - assert(numberOrString.test(123)); - assert(numberOrString.test("abc")); - }); - - it("returns false if neither matcher matches", function() { - var numberOrAbc = sinonMatch.number.or("abc"); - - assert.isFalse(numberOrAbc.test(/.+/)); - assert.isFalse(numberOrAbc.test(new Date())); - assert.isFalse(numberOrAbc.test({})); - }); - - it("can be used with undefined", function() { - var numberOrUndef = sinonMatch.number.or(undefined); - - assert(numberOrUndef.test(123)); - assert(numberOrUndef.test(undefined)); - }); - }); - - describe(".and", function() { - it("is matcher", function() { - var fooAndBar = sinonMatch.has("foo").and(sinonMatch.has("bar")); - - assert(sinonMatch.isMatcher(fooAndBar)); - assert.equals(fooAndBar.toString(), 'has("foo").and(has("bar"))'); - }); - - it("requires matcher argument", function() { - assert.exception( - function() { - sinonMatch.instanceOf(Error).and(); - }, - { name: "TypeError" } - ); - }); - - it("will coerce to matcher", function() { - var abcOrObj = sinonMatch("abc").or({ a: 1 }); - - assert(sinonMatch.isMatcher(abcOrObj)); - assert.equals(abcOrObj.toString(), 'match("abc").or(match(a: 1))'); - }); - - it("returns true if both matchers match", function() { - var fooAndBar = sinonMatch.has("foo").and({ bar: "bar" }); - - assert(fooAndBar.test({ foo: "foo", bar: "bar" })); - }); - - it("returns false if either matcher does not match", function() { - var fooAndBar = sinonMatch.has("foo").and(sinonMatch.has("bar")); - - assert.isFalse(fooAndBar.test({ foo: "foo" })); - assert.isFalse(fooAndBar.test({ bar: "bar" })); - }); - - it("can be used with undefined", function() { - var falsyAndUndefined = sinonMatch.falsy.and(undefined); - - assert.isFalse(falsyAndUndefined.test(false)); - assert(falsyAndUndefined.test(undefined)); - }); - }); - - describe("nested", function() { - it("returns true for an object with nested matcher", function() { - var match = sinonMatch({ outer: sinonMatch({ inner: "sinon" }) }); - - assert.isTrue(match.test({ outer: { inner: "sinon", foo: "bar" } })); - }); - - it("returns true for an array of nested matchers", function() { - var match = sinonMatch([sinonMatch({ str: "sinon" })]); - - assert.isTrue(match.test([{ str: "sinon", foo: "bar" }])); - }); - }); -}); diff --git a/test/mock-test.js b/test/mock-test.js index f8cf3b8f8..b1d2631f0 100644 --- a/test/mock-test.js +++ b/test/mock-test.js @@ -1,9 +1,9 @@ "use strict"; +var match = require("@sinonjs/samsam").createMatcher; var referee = require("@sinonjs/referee"); var sinonMock = require("../lib/sinon/mock"); var sinonExpectation = require("../lib/sinon/mock-expectation"); -var sinonMatch = require("../lib/sinon/match"); var sinonStub = require("../lib/sinon/stub"); var sinonSpy = require("../lib/sinon/spy"); var assert = referee.assert; @@ -634,7 +634,7 @@ describe("sinonMock", function() { }); it("works with sinon matchers", function() { - this.expectation.withArgs(sinonMatch.number, sinonMatch.string, sinonMatch.func); + this.expectation.withArgs(match.number, match.string, match.func); this.expectation(1, "test", function() { return; }); @@ -645,7 +645,7 @@ describe("sinonMock", function() { it("throws when sinon matchers fail", function() { var expectation = this.expectation; - this.expectation.withArgs(sinonMatch.number, sinonMatch.string, sinonMatch.func); + this.expectation.withArgs(match.number, match.string, match.func); assert.exception( function() { expectation(1, 2, 3); diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 4c84cebe2..2c9e2fc28 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -8,13 +8,13 @@ var refute = referee.refute; var fakeXhr = require("nise").fakeXhr; var fakeServerWithClock = require("nise").fakeServerWithClock; var fakeServer = require("nise").fakeServer; +var match = require("@sinonjs/samsam").createMatcher; var Sandbox = require("../lib/sinon/sandbox"); var createSandbox = require("../lib/sinon/create-sandbox"); var sinonFake = require("../lib/sinon/fake"); var sinonSpy = require("../lib/sinon/spy"); var sinonStub = require("../lib/sinon/stub"); var sinonConfig = require("../lib/sinon/util/core/get-config"); -var sinonMatch = require("../lib/sinon/match"); var sinonAssert = require("../lib/sinon/assert"); var sinonClock = require("../lib/sinon/util/fake-timers"); @@ -42,7 +42,7 @@ describe("Sandbox", function() { it("exposes match", function() { var sandbox = new Sandbox(); - assert.same(sandbox.match, sinonMatch); + assert.same(sandbox.match, match); }); it("exposes assert", function() { @@ -1883,7 +1883,7 @@ describe("Sandbox", function() { }) ); - assert.same(object.match, sinonMatch); + assert.same(object.match, match); sandbox.restore(); }); diff --git a/test/spy-test.js b/test/spy-test.js index 0c13be492..dc75661c4 100644 --- a/test/spy-test.js +++ b/test/spy-test.js @@ -2,7 +2,7 @@ var referee = require("@sinonjs/referee"); var createSpy = require("../lib/sinon/spy"); -var sinonMatch = require("../lib/sinon/match"); +var match = require("@sinonjs/samsam").createMatcher; var assert = referee.assert; var refute = referee.refute; @@ -62,13 +62,13 @@ function spyCalledTests(method) { it("uses matcher", function() { this.spy("abc"); - assert(this.spy[method](sinonMatch.typeOf("string"))); + assert(this.spy[method](match.typeOf("string"))); }); it("uses matcher in object", function() { this.spy({ some: "abc" }); - assert(this.spy[method]({ some: sinonMatch.typeOf("string") })); + assert(this.spy[method]({ some: match.typeOf("string") })); }); // https://github.com/sinonjs/sinon/issues/1245 @@ -759,7 +759,7 @@ describe("spy", function() { }); it("is true if called with matcher that returns true", function() { - var matcher = sinonMatch(function() { + var matcher = match(function() { return true; }); this.spy(); @@ -768,7 +768,7 @@ describe("spy", function() { }); it("is false if called with matcher that returns false", function() { - var matcher = sinonMatch(function() { + var matcher = match(function() { return false; }); this.spy(); @@ -782,7 +782,7 @@ describe("spy", function() { this.spy.call(expected); this.spy.calledOn( - sinonMatch(function(value) { + match(function(value) { actual = value; }) ); @@ -1632,8 +1632,8 @@ describe("spy", function() { spy(); - assert.isFalse(spy.returned(sinonMatch.same({ id: 42 }))); - assert(spy.returned(sinonMatch.same(object))); + assert.isFalse(spy.returned(match.same({ id: 42 }))); + assert(spy.returned(match.same(object))); }); }); diff --git a/test/stub-test.js b/test/stub-test.js index 6771324a8..6f3fe66bf 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -4,7 +4,7 @@ var referee = require("@sinonjs/referee"); var createStub = require("../lib/sinon/stub"); var createStubInstance = require("../lib/sinon/stub").createStubInstance; var createSpy = require("../lib/sinon/spy"); -var sinonMatch = require("../lib/sinon/match"); +var match = require("@sinonjs/samsam").createMatcher; var deprecated = require("../lib/sinon/util/core/deprecated"); var assert = referee.assert; var refute = referee.refute; @@ -2179,9 +2179,9 @@ describe("stub", function() { }); }); - it("ensure stub recognizes sinonMatch fuzzy arguments", function() { + it("ensure stub recognizes samsam match fuzzy arguments", function() { var stub = createStub().returns(23); - stub.withArgs(sinonMatch({ foo: "bar" })).returns(99); + stub.withArgs(match({ foo: "bar" })).returns(99); assert.equals(stub(), 23); assert.equals(stub({ foo: "bar", bar: "foo" }), 99); @@ -2202,7 +2202,7 @@ describe("stub", function() { assert.equals(stub(expectedArgument), secondMatchedValue); }); - it("ensure stub uses last matching sinonMatch arguments", function() { + it("ensure stub uses last matching samsam match arguments", function() { var unmatchedValue = "0aa66a7d-3c50-49ef-8365-bdcab637b2dd"; var firstMatchedValue = "1ab2c601-7602-4658-9377-3346f6814caa"; var secondMatchedValue = "e2e31518-c4c4-4012-a61f-31942f603ffa"; @@ -2210,7 +2210,7 @@ describe("stub", function() { var stub = createStub().returns(unmatchedValue); stub.withArgs(expectedArgument).returns(firstMatchedValue); - stub.withArgs(sinonMatch(expectedArgument)).returns(secondMatchedValue); + stub.withArgs(match(expectedArgument)).returns(secondMatchedValue); assert.equals(stub(), unmatchedValue); assert.equals(stub(expectedArgument), secondMatchedValue); diff --git a/test/util/core/deep-equal-test.js b/test/util/core/deep-equal-test.js deleted file mode 100644 index 03f34e299..000000000 --- a/test/util/core/deep-equal-test.js +++ /dev/null @@ -1,403 +0,0 @@ -"use strict"; - -var referee = require("@sinonjs/referee"); -var deepEqual = require("@sinonjs/samsam").deepEqual; -var match = require("../../../lib/sinon/match"); -var createSpy = require("../../../lib/sinon/spy").create; -var assert = referee.assert; - -describe("util/core/deepEqual", function() { - it("passes null", function() { - assert(deepEqual(null, null)); - }); - - it("fails null and object", function() { - assert.isFalse(deepEqual(null, {})); - }); - - it("fails object and null", function() { - assert.isFalse(deepEqual({}, null)); - }); - - it("fails error and object", function() { - assert.isFalse(deepEqual(new Error(), {})); - }); - - it("fails object and error", function() { - assert.isFalse(deepEqual({}, new Error())); - }); - - it("fails regexp and object", function() { - assert.isFalse(deepEqual(/.*/, {})); - }); - - it("fails object and regexp", function() { - assert.isFalse(deepEqual({}, /.*/)); - }); - - it("passes primitives", function() { - assert(deepEqual(1, 1)); - }); - - it("passes same object", function() { - var object = {}; - - assert(deepEqual(object, object)); - }); - - it("passes same function", function() { - var func = function() { - return; - }; - - assert(deepEqual(func, func)); - }); - - it("passes same array", function() { - var arr = []; - - assert(deepEqual(arr, arr)); - }); - - it("passes same regexp", function() { - var regexp = /foo/; - - assert(deepEqual(regexp, regexp)); - }); - - it("passes same error", function() { - var error = new Error(); - - assert(deepEqual(error, error)); - }); - - it("passes equal arrays", function() { - var arr1 = [1, 2, 3, "hey", "there"]; - var arr2 = [1, 2, 3, "hey", "there"]; - - assert(deepEqual(arr1, arr2)); - }); - - it("passes equal arrays with custom properties", function() { - var arr1 = [1, 2, 3, "hey", "there"]; - var arr2 = [1, 2, 3, "hey", "there"]; - - arr1.foo = "bar"; - arr2.foo = "bar"; - - assert(deepEqual(arr1, arr2)); - }); - - it("fails arrays with unequal custom properties", function() { - var arr1 = [1, 2, 3, "hey", "there"]; - var arr2 = [1, 2, 3, "hey", "there"]; - - arr1.foo = "bar"; - arr2.foo = "not bar"; - - assert.isFalse(deepEqual(arr1, arr2)); - }); - - it("passes equal regexps", function() { - var regexp1 = /foo/; - var regexp2 = /foo/; - - assert(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal regexps", function() { - var regexp1 = /foo/; - var regexp2 = /bar/; - - assert.isFalse(deepEqual(regexp1, regexp2)); - }); - - it("passes equal regexps with same ignoreCase flags", function() { - var regexp1 = /foo/i; - var regexp2 = /foo/i; - - assert(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal regexps with different ignoreCase flags", function() { - var regexp1 = /foo/i; - var regexp2 = /foo/; - - assert.isFalse(deepEqual(regexp1, regexp2)); - }); - - it("passes equal regexps with same multiline flags", function() { - var regexp1 = /foo/m; - var regexp2 = /foo/m; - - assert(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal regexps with different multiline flags", function() { - var regexp1 = /foo/m; - var regexp2 = /foo/; - - assert.isFalse(deepEqual(regexp1, regexp2)); - }); - - it("passes equal regexps with same global flags", function() { - var regexp1 = /foo/g; - var regexp2 = /foo/g; - - assert(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal regexps with different global flags", function() { - var regexp1 = /foo/g; - var regexp2 = /foo/; - - assert.isFalse(deepEqual(regexp1, regexp2)); - }); - - it("passes equal regexps with multiple flags", function() { - var regexp1 = /bar/im; - var regexp2 = /bar/im; - - assert(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal regexps with multiple flags", function() { - var regexp1 = /bar/im; - var regexp2 = /bar/gi; - - assert.isFalse(deepEqual(regexp1, regexp2)); - }); - - it("fails unequal errors", function() { - var error1 = new Error(); - var error2 = new Error(); - - assert.isFalse(deepEqual(error1, error2)); - }); - - it("passes NaN and NaN", function() { - assert(deepEqual(NaN, NaN)); - }); - - it("passes equal objects", function() { - var obj1 = { a: 1, b: 2, c: 3, d: "hey", e: "there" }; - var obj2 = { b: 2, c: 3, a: 1, d: "hey", e: "there" }; - - assert(deepEqual(obj1, obj2)); - }); - - it("fails unequal objects with undefined properties with different names", function() { - var obj1 = { a: 1, b: 2, c: 3 }; - var obj2 = { a: 1, b: 2, foo: undefined }; - - assert.isFalse(deepEqual(obj1, obj2)); - }); - - it("fails unequal objects with undefined properties with different names (different arg order)", function() { - var obj1 = { a: 1, b: 2, foo: undefined }; - var obj2 = { a: 1, b: 2, c: 3 }; - - assert.isFalse(deepEqual(obj1, obj2)); - }); - - it("passes equal dates", function() { - var date1 = new Date(2012, 3, 5); - var date2 = new Date(2012, 3, 5); - - assert(deepEqual(date1, date2)); - }); - - it("fails different dates", function() { - var date1 = new Date(2012, 3, 5); - var date2 = new Date(2013, 3, 5); - - assert.isFalse(deepEqual(date1, date2)); - }); - - if (typeof document !== "undefined") { - describe("in browsers", function() { - it("passes same DOM elements", function() { - var element = document.createElement("div"); - - assert(deepEqual(element, element)); - }); - - it("fails different DOM elements", function() { - var element = document.createElement("div"); - var el = document.createElement("div"); - - assert.isFalse(deepEqual(element, el)); - }); - - it("does not modify DOM elements when comparing them", function() { - var el = document.createElement("div"); - document.body.appendChild(el); - deepEqual(el, {}); - - assert.same(el.parentNode, document.body); - assert.equals(el.childNodes.length, 0); - }); - }); - } - - it("passes deep objects", function() { - var func = function() { - return; - }; - - var obj1 = { - a: 1, - b: 2, - c: 3, - d: "hey", - e: "there", - f: func, - g: { - a1: [ - 1, - 2, - "3", - { - prop: [func, "b"] - } - ] - } - }; - - var obj2 = { - a: 1, - b: 2, - c: 3, - d: "hey", - e: "there", - f: func, - g: { - a1: [ - 1, - 2, - "3", - { - prop: [func, "b"] - } - ] - } - }; - - assert(deepEqual(obj1, obj2)); - }); - - it("passes object without prototype compared to equal object with prototype", function() { - var obj1 = Object.create(null); - obj1.a = 1; - obj1.b = 2; - obj1.c = "hey"; - - var obj2 = { a: 1, b: 2, c: "hey" }; - - assert(deepEqual(obj1, obj2)); - }); - - it("passes object with prototype compared to equal object without prototype", function() { - var obj1 = { a: 1, b: 2, c: "hey" }; - - var obj2 = Object.create(null); - obj2.a = 1; - obj2.b = 2; - obj2.c = "hey"; - - assert(deepEqual(obj1, obj2)); - }); - - it("passes equal objects without prototypes", function() { - var obj1 = Object.create(null); - obj1.a = 1; - obj1.b = 2; - obj1.c = "hey"; - - var obj2 = Object.create(null); - obj2.a = 1; - obj2.b = 2; - obj2.c = "hey"; - - assert(deepEqual(obj1, obj2)); - }); - - it("passes equal objects that override hasOwnProperty", function() { - var obj1 = { a: 1, b: 2, c: "hey", hasOwnProperty: "silly" }; - var obj2 = { a: 1, b: 2, c: "hey", hasOwnProperty: "silly" }; - - assert(deepEqual(obj1, obj2)); - }); - - it("does not run matchers against each other when using a matcher library", function() { - var spyA = createSpy(); - var matchA = match(spyA); - - var spyB = createSpy(); - var matchB = match(spyB); - - deepEqual(matchA, matchB); - - assert.equals(spyA.callCount, 0); - assert.equals(spyB.callCount, 0); - }); - - it("strictly compares instances when passed two matchers and using a matcher library", function() { - var matchA = match(function a() { - return "a"; - }); - - var matchB = match(function b() { - return "b"; - }); - - var duplicateA = matchA; - - assert(deepEqual(matchA, duplicateA)); - assert.isFalse(deepEqual(matchA, matchB)); - }); - - it("handles shallow cyclic objects", function() { - var a = { - foo: "bar" - }; - a.cyclicKeyName = a; - - var b = { - foo: "bar" - }; - b.cyclicKeyName = b; - - assert(deepEqual(a, b)); - }); - - it("handles deep cyclic objects", function() { - var a = { - foo: "bar", - key: {} - }; - a.key.value = a; - - var b = { - foo: "bar", - key: {} - }; - b.key.value = b; - - assert(deepEqual(a, b)); - }); - - it("handles cyclic objects when a matcher provided", function() { - var a = { - foo: "bar" - }; - a.cyclicKeyName = a; - - var b = { - foo: "bar" - }; - b.cyclicKeyName = b; - - assert(deepEqual(a, b)); - }); -});