var behavior = require("./sinon/behavior"); var createSandbox = require("./sinon/create-sandbox"); var deprecated = require("./sinon/util/core/deprecated"); var extend = require("./sinon/util/core/extend"); var fakeTimers = require("./sinon/util/fake-timers"); var format = require("./sinon/util/core/format"); var nise = require("nise"); var Sandbox = require("./sinon/sandbox"); var stub = require("./sinon/stub");
var legacySandboxAPI = {
sandbox: {
create: deprecated.wrap(
createSandbox, // eslint-disable-next-line max-len "`sandbox.create()` is deprecated. Use default sandbox at `sinon.sandbox` or create new sandboxes with `sinon.createSandbox()`"
)
}
};
var sandbox = new Sandbox();
var api = extend(sandbox, legacySandboxAPI, apiMethods);
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 stringSlice = require("@sinonjs/commons").prototypes.string.slice;
var arraySlice = arrayProto.slice; var concat = arrayProto.concat; var forEach = arrayProto.forEach; var join = arrayProto.join; var splice = arrayProto.splice;
varassert;
function verifyIsStub() { var args = arraySlice(arguments);
forEach(args, function(method) { if (!method) { assert.fail("fake is not a spy");
}
if (method.proxy && method.proxy.isSinonProxy) {
verifyIsStub(method.proxy);
} else { if (typeof method !== "function") { assert.fail(method + " is not a function");
}
if (typeof method.getCall !== "function") { assert.fail(method + " is not stubbed");
}
}
});
}
function verifyIsValidAssertion(assertionMethod, assertionArgs) { switch (assertionMethod) { case"notCalled": case"called": case"calledOnce": case"calledTwice": case"calledThrice": if (assertionArgs.length !== 0) { assert.fail(
assertionMethod + " takes 1 argument but was called with " +
(assertionArgs.length + 1) + " arguments"
);
} break; default: break;
}
}
function failAssertion(object, msg) { var obj = object || global; var failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}
function mirrorPropAsAssertion(name, method, message) { var msg = message; var meth = method; if (arguments.length === 2) {
msg = method;
meth = name;
}
fail: function fail(message) { var error = new Error(message);
error.name = this.failException || assert.failException;
throw error;
},
pass: function pass() { return;
},
callOrder: function assertCallOrder() {
verifyIsStub.apply(null, arguments); var expected = ""; var actual = "";
if (!calledInOrder(arguments)) { try {
expected = join(arguments, ", "); var calls = arraySlice(arguments); var i = calls.length; while (i) { if (!calls[--i].called) {
splice(calls, i, 1);
}
}
actual = join(orderByFirstCall(calls), ", ");
} catch (e) { // If this fails, we'll just fall back to the blank string
}
failAssertion(this, "expected " + expected + " to be called in order but were called as " + actual);
} else { assert.pass("callOrder");
}
},
callCount: function assertCallCount(method, count) {
verifyIsStub(method);
if (method.callCount !== count) { var msg = "expected %n to be called " + timesInWords(count) + " but was called %c%C";
failAssertion(this, method.printf(msg));
} else { assert.pass("callCount");
}
},
expose: function expose(target, options) { if (!target) { thrownew TypeError("target is null or undefined");
}
var o = options || {}; var prefix = (typeof o.prefix === "undefined" && "assert") || o.prefix; var includeFail = typeof o.includeFail === "undefined" || Boolean(o.includeFail); var instance = this;
mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
mirrorPropAsAssertion( "notCalled", function(spy) { return !spy.called;
}, "expected %n to not have been called but was called %c%C"
);
mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %D");
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %D");
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %D");
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %D");
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %D");
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %D");
mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
var arrayProto = require("@sinonjs/commons").prototypes.array; var extend = require("./util/core/extend"); var functionName = require("@sinonjs/commons").functionName; var nextTick = require("./util/core/next-tick"); var valueToString = require("@sinonjs/commons").valueToString; var exportAsyncBehaviors = require("./util/core/export-async-behaviors");
var concat = arrayProto.concat; var join = arrayProto.join; var reverse = arrayProto.reverse; var slice = arrayProto.slice;
var useLeftMostCallback = -1; var useRightMostCallback = -2;
function getCallback(behavior, args) { var callArgAt = behavior.callArgAt;
if (callArgAt >= 0) { return args[callArgAt];
}
var argumentList;
if (callArgAt === useLeftMostCallback) {
argumentList = args;
}
if (callArgAt === useRightMostCallback) {
argumentList = reverse(slice(args));
}
var callArgProp = behavior.callArgProp;
for (var i = 0, l = argumentList.length; i < l; ++i) { if (!callArgProp && typeof argumentList[i] === "function") { return argumentList[i];
}
function getCallbackError(behavior, func, args) { if (behavior.callArgAt < 0) { var msg;
if (behavior.callArgProp) {
msg =
functionName(behavior.stub) + " expected to yield to '" +
valueToString(behavior.callArgProp) + "', but no object with such a property was passed.";
} else {
msg = functionName(behavior.stub) + " expected to yield, but no callback was passed.";
}
if (args.length > 0) {
msg += " Received [" + join(args, ", ") + "]";
}
return msg;
}
return"argument at index " + behavior.callArgAt + " is not a function: " + func;
}
function ensureArgs(name, behavior, args) { // map function name to internal property // callsArg => callArgAt var property = name.replace(/sArg/, "ArgAt"); var index = behavior[property];
if (index >= args.length) { thrownew TypeError(
name + " failed: " + (index + 1) + " arguments required but only " + args.length + " present"
);
}
}
function callCallback(behavior, args) { if (typeof behavior.callArgAt === "number") {
ensureArgs("callsArg", behavior, args); var func = getCallback(behavior, args);
onCall: function onCall(index) { returnthis.stub.onCall(index);
},
onFirstCall: function onFirstCall() { returnthis.stub.onFirstCall();
},
onSecondCall: function onSecondCall() { returnthis.stub.onSecondCall();
},
onThirdCall: function onThirdCall() { returnthis.stub.onThirdCall();
},
withArgs: function withArgs(/* arguments */) { thrownew Error( 'Defining a stub by invoking "stub.onCall(...).withArgs(...)" ' + 'is not supported. Use "stub.withArgs(...).onCall(...)" ' + "to define sequential behavior for calls with certain arguments."
);
}
};
var arrayProto = require("@sinonjs/commons").prototypes.array; var match = require("@sinonjs/samsam").createMatcher; var deepEqual = require("@sinonjs/samsam").deepEqual; var functionName = require("@sinonjs/commons").functionName; var sinonFormat = require("./util/core/format"); var valueToString = require("@sinonjs/commons").valueToString;
var concat = arrayProto.concat; var filter = arrayProto.filter; var join = arrayProto.join; var map = arrayProto.map; var reduce = arrayProto.reduce; var slice = arrayProto.slice;
function throwYieldError(proxy, text, args) { var msg = functionName(proxy) + text; if (args.length) {
msg += " Received [" + join(slice(args), ", ") + "]";
} thrownew Error(msg);
}
var callProto = {
calledOn: function calledOn(thisValue) { if (match.isMatcher(thisValue)) { return thisValue.test(this.thisValue);
} returnthis.thisValue === thisValue;
},
calledWith: function calledWith() { var self = this; var calledWithArgs = slice(arguments);
if (calledWithArgs.length > self.args.length) { returnfalse;
}
if (this.exception) {
callStr += " !" + this.exception.name;
if (this.exception.message) {
callStr += "(" + this.exception.message + ")";
}
} if (this.stack) { // Omit the error message and the two top stack frames in sinon itself:
callStr += (this.stack.split("\n")[3] || "unknown").replace(/^\s*(?:at\s+|@)?/, " at ");
}
return callStr;
},
ensureArgIsAFunction: function(pos) { if (typeofthis.args[pos] !== "function") { thrownew TypeError( "Expected argument at position " + pos + " to be a Function, but was " + typeofthis.args[pos]
);
}
}
};
Object.defineProperty(callProto, "stack", {
enumerable: true,
configurable: true,
get: function() { return (this.errorWithCallStack && this.errorWithCallStack.stack) || "";
}
});
callProto.invokeCallback = callProto.yield;
function createSpyCall(spy, thisValue, args, returnValue, exception, id, errorWithCallStack) { if (typeof id !== "number") { thrownew TypeError("Call id is not a number");
}
var proxyCall = Object.create(callProto); var lastArg = (args.length > 0 && args[args.length - 1]) || undefined; var callback = lastArg && typeof lastArg === "function" ? lastArg : undefined;
var walk = require("./util/core/walk"); var getPropertyDescriptor = require("./util/core/get-property-descriptor"); var hasOwnProperty = require("@sinonjs/commons").prototypes.object.hasOwnProperty; var push = require("@sinonjs/commons").prototypes.array.push;
function collectMethod(methods, object, prop, propOwner) { if (typeof getPropertyDescriptor(propOwner, prop).value === "function" && hasOwnProperty(object, prop)) {
push(methods, object[prop]);
}
}
// This function returns an array of all the own methods on the passed object function collectOwnMethods(object) { var methods = [];
var arrayProto = require("@sinonjs/commons").prototypes.array; var isPropertyConfigurable = require("./util/core/is-property-configurable"); var exportAsyncBehaviors = require("./util/core/export-async-behaviors"); var extend = require("./util/core/extend");
var slice = arrayProto.slice;
var useLeftMostCallback = -1; var useRightMostCallback = -2;
return f && f.apply(this, arguments);
}; var p = cleanProxy(spy(fakeInstance));
p.displayName = "fake";
p.id = "fake#" + uuid++;
return p;
}
function fake(f) { if (arguments.length > 0 && typeof f !== "function") { thrownew TypeError("Expected f argument to be a Function");
}
return wrapFunc(f);
}
fake.returns = function returns(value) { function f() { return value;
}
return wrapFunc(f);
};
fake.throws = functionthrows(value) { function f() { throw getError(value);
}
return wrapFunc(f);
};
fake.resolves = function resolves(value) { function f() { return Promise.resolve(value);
}
return wrapFunc(f);
};
fake.rejects = function rejects(value) { function f() { return Promise.reject(getError(value));
}
return wrapFunc(f);
};
function yieldInternal(async, values) { function f() { var callback = arguments[arguments.length - 1]; if (typeof callback !== "function") { thrownew TypeError("Expected last argument to be a function");
} if (async) {
nextTick(function() {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}
return wrapFunc(f);
}
fake.yields = function yields() { return yieldInternal(false, slice(arguments));
};
fake.yieldsAsync = function yieldsAsync() { return yieldInternal(true, slice(arguments));
};
var arrayProto = require("@sinonjs/commons").prototypes.array; 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("@sinonjs/samsam").createMatcher; var stub = require("./stub"); varassert = require("./assert"); var deepEqual = require("@sinonjs/samsam").deepEqual; var format = require("./util/core/format"); var valueToString = require("@sinonjs/commons").valueToString;
var every = arrayProto.every; var forEach = arrayProto.forEach; var push = arrayProto.push; var slice = arrayProto.slice;
function callCountInWords(callCount) { if (callCount === 0) { return"never called";
}
return"called " + timesInWords(callCount);
}
function expectedCallCountInWords(expectation) { var min = expectation.minCalls; var max = expectation.maxCalls;
if (typeof min === "number" && typeof max === "number") { var str = timesInWords(min);
if (min !== max) {
str = "at least " + str + " and at most " + timesInWords(max);
}
return str;
}
if (typeof min === "number") { return"at least " + timesInWords(min);
}
return"at most " + timesInWords(max);
}
function receivedMinCalls(expectation) { var hasMinLimit = typeof expectation.minCalls === "number"; return !hasMinLimit || expectation.callCount >= expectation.minCalls;
}
function receivedMaxCalls(expectation) { if (typeof expectation.maxCalls !== "number") { returnfalse;
}
create: function create(methodName) { var expectation = extend.nonEnum(stub.create(), mockExpectation); delete expectation.create;
expectation.method = methodName;
return expectation;
},
invoke: function invoke(func, thisValue, args) { this.verifyCallAllowed(thisValue, args);
return spyInvoke.apply(this, arguments);
},
atLeast: function atLeast(num) { if (typeof num !== "number") { thrownew TypeError("'" + valueToString(num) + "' is not number");
}
if (!this.limitsSet) { this.maxCalls = null; this.limitsSet = true;
}
this.minCalls = num;
returnthis;
},
atMost: function atMost(num) { if (typeof num !== "number") { thrownew TypeError("'" + valueToString(num) + "' is not number");
}
if (!this.limitsSet) { this.minCalls = null; this.limitsSet = true;
}
this.maxCalls = num;
returnthis;
},
never: function never() { returnthis.exactly(0);
},
once: function once() { returnthis.exactly(1);
},
twice: function twice() { returnthis.exactly(2);
},
thrice: function thrice() { returnthis.exactly(3);
},
exactly: function exactly(num) { if (typeof num !== "number") { thrownew TypeError("'" + valueToString(num) + "' is not a number");
}
this.atLeast(num); returnthis.atMost(num);
},
met: function met() { return !this.failed && receivedMinCalls(this);
},
verifyCallAllowed: function verifyCallAllowed(thisValue, args) { var expectedArguments = this.expectedArguments;
if (receivedMaxCalls(this)) { this.failed = true;
mockExpectation.fail(this.method + " already called " + timesInWords(this.maxCalls));
}
if ("expectedThis" in this && this.expectedThis !== thisValue) {
mockExpectation.fail( this.method + " called with " +
valueToString(thisValue) + " as thisValue, expected " +
valueToString(this.expectedThis)
);
}
if (!("expectedArguments" in this)) { return;
}
if (!args) {
mockExpectation.fail(this.method + " received no arguments, expected " + format(expectedArguments));
}
if (args.length < expectedArguments.length) {
mockExpectation.fail( this.method + " received too few arguments (" +
format(args) + "), expected " +
format(expectedArguments)
);
}
if (this.expectsExactArgCount && args.length !== expectedArguments.length) {
mockExpectation.fail( this.method + " received too many arguments (" +
format(args) + "), expected " +
format(expectedArguments)
);
}
forEach(
expectedArguments, function(expectedArgument, i) { if (!verifyMatcher(expectedArgument, args[i])) {
mockExpectation.fail( this.method + " received wrong arguments " +
format(args) + ", didn't match " +
String(expectedArguments)
);
}
if (!deepEqual(args[i], expectedArgument)) {
mockExpectation.fail( this.method + " received wrong arguments " +
format(args) + ", expected " +
format(expectedArguments)
);
}
}, this
);
},
allowsCall: function allowsCall(thisValue, args) { var expectedArguments = this.expectedArguments;
if (this.met() && receivedMaxCalls(this)) { returnfalse;
}
if ("expectedThis" in this && this.expectedThis !== thisValue) { returnfalse;
}
if (!("expectedArguments" in this)) { returntrue;
}
// eslint-disable-next-line no-underscore-dangle var _args = args || [];
if (_args.length < expectedArguments.length) { returnfalse;
}
if (this.expectsExactArgCount && _args.length !== expectedArguments.length) { returnfalse;
}
var arrayProto = require("@sinonjs/commons").prototypes.array; var mockExpectation = require("./mock-expectation"); var spyCallToString = require("./call").toString; var extend = require("./util/core/extend"); var deepEqual = require("@sinonjs/samsam").deepEqual; var wrapMethod = require("./util/core/wrap-method"); var usePromiseLibrary = require("./util/core/use-promise-library");
var concat = arrayProto.concat; var filter = arrayProto.filter; var forEach = arrayProto.forEach; var every = arrayProto.every; var join = arrayProto.join; var push = arrayProto.push; var slice = arrayProto.slice; var unshift = arrayProto.unshift;
function mock(object) { if (!object || typeof object === "string") { return mockExpectation.create(object ? object : "Anonymous mock");
}
return mock.create(object);
}
function each(collection, callback) { var col = collection || [];
forEach(col, callback);
}
function arrayEquals(arr1, arr2, compareLength) { if (compareLength && arr1.length !== arr2.length) { returnfalse;
}
usingPromise: function usingPromise(promiseLibrary) { this.promiseLibrary = promiseLibrary;
returnthis;
},
invokeMethod: function invokeMethod(method, thisValue, args) { /* if we cannot find any matching files we will explicitly call mockExpection#fail with error messages */ /* eslint consistent-return: "off" */ var expectations = this.expectations && this.expectations[method] ? this.expectations[method] : []; var currentArgs = args || []; var available;
var expectationsWithMatchingArgs = filter(expectations, function(expectation) { var expectedArgs = expectation.expectedArguments || [];
var err = new Error(); if (!err.stack) { // PhantomJS does not serialize the stack trace until the error has been thrown try { throw err;
} catch (e) { /* empty */
}
}
push( this.failures, "Unexpected call: " +
spyCallToString.call({
proxy: method,
args: args,
stack: err.stack
})
);
var arrayProto = require("@sinonjs/commons").prototypes.array; var collectOwnMethods = require("./collect-own-methods"); 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 match = require("@sinonjs/samsam").createMatcher; var sinonAssert = require("./assert"); var sinonClock = require("./util/fake-timers"); var sinonMock = require("./mock"); var sinonSpy = require("./spy"); var sinonStub = require("./stub"); var sinonFake = require("./fake"); var valueToString = require("@sinonjs/commons").valueToString; var fakeServer = require("nise").fakeServer; var fakeXhr = require("nise").fakeXhr; var usePromiseLibrary = require("./util/core/use-promise-library");
var filter = arrayProto.filter; var forEach = arrayProto.filter; var push = arrayProto.push; var reverse = arrayProto.reverse;
function applyOnEach(fakes, method) { var matchingFakes = filter(fakes, function(fake) { returntypeof fake[method] === "function";
});
function Sandbox() { var sandbox = this; var collection = []; var fakeRestorers = []; var promiseLib;
sandbox.serverPrototype = fakeServer;
// this is for testing only
sandbox.getFakes = function getFakes() { return collection;
};
// this is for testing only
sandbox.getRestorers = function() { return fakeRestorers;
};
sandbox.createStubInstance = function createStubInstance(constructor) { if (typeof constructor !== "function") { thrownew TypeError("The constructor should be a function.");
} returnthis.stub(Object.create(constructor.prototype));
};
sandbox.reset = function reset() {
applyOnEach(collection, "reset");
applyOnEach(collection, "resetHistory");
};
sandbox.resetBehavior = function resetBehavior() {
applyOnEach(collection, "resetBehavior");
};
sandbox.resetHistory = function resetHistory() { function privateResetHistory(f) { var method = f.resetHistory || f.reset; if (method) {
method.call(f);
}
}
var methods = []; if (fake.get) {
push(methods, fake.get);
}
if (fake.set) {
push(methods, fake.set);
}
forEach(methods, privateResetHistory);
});
};
sandbox.restore = function restore() { if (arguments.length) { thrownew Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.