Add sinon package for mocking

This commit is contained in:
Sam Partington 2020-06-24 14:33:55 +01:00
parent 43c1bea680
commit b0af5695e6
191 changed files with 85298 additions and 0 deletions

View file

@ -0,0 +1,17 @@
"use strict";
var isMatcher = require("./is-matcher");
/**
* Throws a TypeError when `value` is not a matcher
*
* @private
* @param {*} value The value to examine
*/
function assertMatcher(value) {
if (!isMatcher(value)) {
throw new TypeError("Matcher expected");
}
}
module.exports = assertMatcher;

View file

@ -0,0 +1,21 @@
"use strict";
/**
* Throws a TypeError when expected method doesn't exist
*
* @private
* @param {*} value A value to examine
* @param {string} method The name of the method to look for
* @param {name} name A name to use for the error message
* @param {string} methodPath The name of the method to use for error messages
* @throws {TypeError} When the method doesn't exist
*/
function assertMethodExists(value, method, name, methodPath) {
if (value[method] === null || value[method] === undefined) {
throw new TypeError(
"Expected " + name + " to have method " + methodPath
);
}
}
module.exports = assertMethodExists;

View file

@ -0,0 +1,29 @@
"use strict";
var typeOf = require("@sinonjs/commons").typeOf;
/**
* Ensures that value is of type
*
* @private
* @param {*} value A value to examine
* @param {string} type A basic JavaScript type to compare to, e.g. "object", "string"
* @param {string} name A string to use for the error message
* @throws {TypeError} If value is not of the expected type
* @returns {undefined}
*/
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
);
}
}
module.exports = assertType;

View file

@ -0,0 +1,16 @@
"use strict";
var typeOf = require("@sinonjs/commons").typeOf;
/**
* Returns `true` for iterables
*
* @private
* @param {*} value A value to examine
* @returns {boolean} Returns `true` when `value` looks like an iterable
*/
function isIterable(value) {
return Boolean(value) && typeOf(value.forEach) === "function";
}
module.exports = isIterable;

View file

@ -0,0 +1,18 @@
"use strict";
var isPrototypeOf = require("@sinonjs/commons").prototypes.object.isPrototypeOf;
var matcherPrototype = require("./matcher-prototype");
/**
* Returns `true` when `object` is a matcher
*
* @private
* @param {*} object A value to examine
* @returns {boolean} Returns `true` when `object` is a matcher
*/
function isMatcher(object) {
return isPrototypeOf(matcherPrototype, object);
}
module.exports = isMatcher;

View file

@ -0,0 +1,44 @@
"use strict";
var every = require("@sinonjs/commons").prototypes.array.every;
var typeOf = require("@sinonjs/commons").typeOf;
var deepEqualFactory = require("../deep-equal").use;
var isMatcher = require("./is-matcher");
/**
* Matches `actual` with `expectation`
*
* @private
* @param {*} actual A value to examine
* @param {object} expectation An object with properties to match on
* @param {object} matcher A matcher to use for comparison
* @returns {boolean} Returns true when `actual` matches all properties in `expectation`
*/
function matchObject(actual, expectation, matcher) {
var deepEqual = deepEqualFactory(matcher);
if (actual === null || actual === undefined) {
return false;
}
return every(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(act, exp, matcher)) {
return false;
}
} else if (!deepEqual(act, exp)) {
return false;
}
return true;
});
}
module.exports = matchObject;

View file

@ -0,0 +1,49 @@
"use strict";
var matcherPrototype = {
toString: function() {
return this.message;
}
};
matcherPrototype.or = function(valueOrMatcher) {
var createMatcher = require("../create-matcher");
var isMatcher = createMatcher.isMatcher;
if (!arguments.length) {
throw new TypeError("Matcher expected");
}
var m2 = isMatcher(valueOrMatcher)
? valueOrMatcher
: createMatcher(valueOrMatcher);
var m1 = this;
var or = Object.create(matcherPrototype);
or.test = function(actual) {
return m1.test(actual) || m2.test(actual);
};
or.message = m1.message + ".or(" + m2.message + ")";
return or;
};
matcherPrototype.and = function(valueOrMatcher) {
var createMatcher = require("../create-matcher");
var isMatcher = createMatcher.isMatcher;
if (!arguments.length) {
throw new TypeError("Matcher expected");
}
var m2 = isMatcher(valueOrMatcher)
? valueOrMatcher
: createMatcher(valueOrMatcher);
var m1 = this;
var and = Object.create(matcherPrototype);
and.test = function(actual) {
return m1.test(actual) && m2.test(actual);
};
and.message = m1.message + ".and(" + m2.message + ")";
return and;
};
module.exports = matcherPrototype;

View file

@ -0,0 +1,62 @@
"use strict";
var functionName = require("@sinonjs/commons").functionName;
var join = require("@sinonjs/commons").prototypes.array.join;
var map = require("@sinonjs/commons").prototypes.array.map;
var stringIndexOf = require("@sinonjs/commons").prototypes.string.indexOf;
var valueToString = require("@sinonjs/commons").valueToString;
var matchObject = require("./match-object");
var createTypeMap = function(match) {
return {
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(actual, expectation, match);
};
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 + '")';
}
};
};
module.exports = createTypeMap;