Add the ability to specify the strategy to use for a spy based on which parameters are passed

[Finishes #92260826]
This commit is contained in:
Steve Gravrock
2018-01-03 08:49:34 -08:00
parent f38527ff15
commit 16e07a0e99
4 changed files with 245 additions and 27 deletions
+86 -3
View File
@@ -18,7 +18,7 @@ getJasmineRequireObj().Spy = function (j$) {
wrapper = makeFunc(numArgs, function () {
return spy.apply(this, Array.prototype.slice.call(arguments));
}),
spyStrategy = new j$.SpyStrategy({
strategyDispatcher = new SpyStrategyDispatcher({
name: name,
fn: originalFn,
getSpy: function () {
@@ -40,7 +40,7 @@ getJasmineRequireObj().Spy = function (j$) {
};
callTracker.track(callData);
var returnValue = spyStrategy.exec(this, arguments);
var returnValue = strategyDispatcher.exec(this, arguments);
callData.returnValue = returnValue;
return returnValue;
@@ -69,11 +69,94 @@ getJasmineRequireObj().Spy = function (j$) {
wrapper[prop] = originalFn[prop];
}
wrapper.and = spyStrategy;
/**
* @member {SpyStrategy} - Accesses the default strategy for the spy. This strategy will be used
* whenever the spy is called with arguments that don't match any strategy
* created with {@link Spy#withArgs}.
* @name Spy#and
* @example
* spyOn(someObj, 'func').and.returnValue(42);
*/
wrapper.and = strategyDispatcher.and;
/**
* Specifies a strategy to be used for calls to the spy that have the
* specified arguments.
* @name Spy#withArgs
* @function
* @param {...*} args - The arguments to match
* @type {SpyStrategy}
* @example
* spyOn(someObj, 'func').withArgs(1, 2, 3).and.returnValue(42);
* someObj.func(1, 2, 3); // returns 42
*/
wrapper.withArgs = function() {
return strategyDispatcher.withArgs.apply(strategyDispatcher, arguments);
};
wrapper.calls = callTracker;
return wrapper;
}
function SpyStrategyDispatcher(strategyArgs) {
var baseStrategy = new j$.SpyStrategy(strategyArgs);
var argsStrategies = new StrategyDict(function() {
return new j$.SpyStrategy(strategyArgs);
});
this.and = baseStrategy;
this.exec = function(spy, args) {
var strategy = argsStrategies.get(args);
if (!strategy) {
if (argsStrategies.any() && !baseStrategy.isConfigured()) {
throw new Error('Spy \'' + strategyArgs.name + '\' receieved a call with arguments ' + j$.pp(Array.prototype.slice.call(args)) + ' but all configured strategies specify other arguments.');
} else {
strategy = baseStrategy;
}
}
return strategy.exec(spy, args);
};
this.withArgs = function() {
return { and: argsStrategies.getOrCreate(arguments) };
};
}
function StrategyDict(strategyFactory) {
this.strategies = [];
this.strategyFactory = strategyFactory;
}
StrategyDict.prototype.any = function() {
return this.strategies.length > 0;
};
StrategyDict.prototype.getOrCreate = function(args) {
var strategy = this.get(args);
if (!strategy) {
strategy = this.strategyFactory();
this.strategies.push({
args: args,
strategy: strategy
});
}
return strategy;
};
StrategyDict.prototype.get = function(args) {
var i;
for (i = 0; i < this.strategies.length; i++) {
if (j$.matchersUtil.equals(args, this.strategies[i].args)) {
return this.strategies[i].strategy;
}
}
};
return Spy;
};