Improve wrapper function and parameter naming

This commit is contained in:
Elliot Nelson
2020-02-06 10:29:02 -05:00
parent 9febe3159d
commit bf4694333c
3 changed files with 36 additions and 30 deletions

View File

@@ -822,6 +822,12 @@ describe("Env integration", function() {
expect(result.foo).toEqual('hello world'); expect(result.foo).toEqual('hello world');
}).not.toThrow(); }).not.toThrow();
expect(function() {
var result = new spy('passing', 'extra', 'arguments', 'to', 'constructor');
expect(result instanceof MyClass).toBeTruthy();
expect(result.foo).toEqual('passing');
}).not.toThrow();
expect(function() { expect(function() {
spy('hello world'); spy('hello world');
}).toThrowError('You must use the new keyword.'); }).toThrowError('You must use the new keyword.');
@@ -840,8 +846,8 @@ describe("Env integration", function() {
env.allowRespy(true); env.allowRespy(true);
env.addReporter({ jasmineDone: done }); env.addReporter({ jasmineDone: done });
env.describe('test suite', function(){ env.describe('test suite', function() {
env.it('spec 0', function(){ env.it('spec 0', function() {
env.spyOn(foo,'bar'); env.spyOn(foo,'bar');
var error = null; var error = null;

View File

@@ -20,8 +20,8 @@ getJasmineRequireObj().Spy = function(j$) {
getPromise getPromise
) { ) {
var numArgs = typeof originalFn === 'function' ? originalFn.length : 0, var numArgs = typeof originalFn === 'function' ? originalFn.length : 0,
wrapper = makeFunc(numArgs, function(context, args, isConstructor) { wrapper = makeFunc(numArgs, function(context, args, invokeNew) {
return spy(context, args, isConstructor); return spy(context, args, invokeNew);
}), }),
strategyDispatcher = new SpyStrategyDispatcher({ strategyDispatcher = new SpyStrategyDispatcher({
name: name, name: name,
@@ -33,7 +33,7 @@ getJasmineRequireObj().Spy = function(j$) {
getPromise: getPromise getPromise: getPromise
}), }),
callTracker = new j$.CallTracker(), callTracker = new j$.CallTracker(),
spy = function(context, args, isConstructor) { spy = function(context, args, invokeNew) {
/** /**
* @name Spy.callData * @name Spy.callData
* @property {object} object - `this` context for the invocation. * @property {object} object - `this` context for the invocation.
@@ -47,7 +47,7 @@ getJasmineRequireObj().Spy = function(j$) {
}; };
callTracker.track(callData); callTracker.track(callData);
var returnValue = strategyDispatcher.exec(this, args, isConstructor); var returnValue = strategyDispatcher.exec(context, args, invokeNew);
callData.returnValue = returnValue; callData.returnValue = returnValue;
return returnValue; return returnValue;
@@ -56,44 +56,44 @@ getJasmineRequireObj().Spy = function(j$) {
function makeFunc(length, fn) { function makeFunc(length, fn) {
switch (length) { switch (length) {
case 1: case 1:
return function fnargs(a) { return function wrap1(a) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap1);
}; };
case 2: case 2:
return function fnargs(a, b) { return function wrap2(a, b) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap2);
}; };
case 3: case 3:
return function fnargs(a, b, c) { return function wrap3(a, b, c) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap3);
}; };
case 4: case 4:
return function fnargs(a, b, c, d) { return function wrap4(a, b, c, d) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap4);
}; };
case 5: case 5:
return function fnargs(a, b, c, d, e) { return function wrap5(a, b, c, d, e) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap5);
}; };
case 6: case 6:
return function fnargs(a, b, c, d, e, f) { return function wrap6(a, b, c, d, e, f) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap6);
}; };
case 7: case 7:
return function fnargs(a, b, c, d, e, f, g) { return function wrap7(a, b, c, d, e, f, g) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap7);
}; };
case 8: case 8:
return function fnargs(a, b, c, d, e, f, g, h) { return function wrap8(a, b, c, d, e, f, g, h) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap8);
}; };
case 9: case 9:
return function fnargs(a, b, c, d, e, f, g, h, i) { return function wrap9(a, b, c, d, e, f, g, h, i) {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap9);
}; };
default: default:
return function fnargs() { return function wrap() {
return fn(this, arguments, this instanceof fnargs); return fn(this, arguments, this instanceof wrap);
}; };
} }
} }
@@ -150,7 +150,7 @@ getJasmineRequireObj().Spy = function(j$) {
this.and = baseStrategy; this.and = baseStrategy;
this.exec = function(spy, args, isConstructor) { this.exec = function(spy, args, invokeNew) {
var strategy = argsStrategies.get(args); var strategy = argsStrategies.get(args);
if (!strategy) { if (!strategy) {
@@ -167,7 +167,7 @@ getJasmineRequireObj().Spy = function(j$) {
} }
} }
return strategy.exec(spy, args, isConstructor); return strategy.exec(spy, args, invokeNew);
}; };
this.withArgs = function() { this.withArgs = function() {

View File

@@ -96,11 +96,11 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
* @since 2.0.0 * @since 2.0.0
* @function * @function
*/ */
SpyStrategy.prototype.exec = function(context, args, isConstructor) { SpyStrategy.prototype.exec = function(context, args, invokeNew) {
var list = [context].concat(args ? Array.prototype.slice.call(args) : []); var list = [context].concat(args ? Array.prototype.slice.call(args) : []);
var target = this.plan.bind.apply(this.plan, list); var target = this.plan.bind.apply(this.plan, list);
if (isConstructor) { if (invokeNew) {
return new target(); return new target();
} else { } else {
return target(); return target();