From aa84d542ce72b79c12acca2b0af99308cfb72d96 Mon Sep 17 00:00:00 2001 From: Dummy Date: Wed, 6 Mar 2019 23:56:35 +0100 Subject: [PATCH] fix for regression. --- dist/spec/issues/23.test.js | 10 +++++----- dist/spec/issues/23.test.js.map | 2 +- dist/src/states/FunctionState.js | 14 +++++++------- dist/src/states/FunctionState.js.map | 2 +- src/states/FunctionState.ts | 14 +++++++------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/dist/spec/issues/23.test.js b/dist/spec/issues/23.test.js index 7cb930c..6ede0ef 100644 --- a/dist/spec/issues/23.test.js +++ b/dist/spec/issues/23.test.js @@ -2,13 +2,13 @@ Object.defineProperty(exports, "__esModule", { value: true }); var ava_1 = require("ava"); var Index_1 = require("../../src/Index"); -ava_1.default('issue 23: mimick received should not call method', function (t) { +ava_1.default("issue 23: mimick received should not call method", function (t) { var mockedCalculator = Index_1.Substitute.for(); - var result = 0; mockedCalculator.add(Index_1.Arg.all()).mimicks(function (a, b) { - return result = a + b; + t.deepEqual(a, 1); + return a + b; }); - t.throws(function () { return mockedCalculator.received().add(Index_1.Arg.any(), Index_1.Arg.any()); }); - t.is(result, 0); + mockedCalculator.add(1, 1); // ok + mockedCalculator.received(1).add(2, 1); // not ok, calls mimick func }); //# sourceMappingURL=23.test.js.map \ No newline at end of file diff --git a/dist/spec/issues/23.test.js.map b/dist/spec/issues/23.test.js.map index fc247d7..cc75e6b 100644 --- a/dist/spec/issues/23.test.js.map +++ b/dist/spec/issues/23.test.js.map @@ -1 +1 @@ -{"version":3,"file":"23.test.js","sourceRoot":"","sources":["../../../spec/issues/23.test.ts"],"names":[],"mappings":";;AAAA,2BAAuB;AAEvB,yCAAkD;AASlD,aAAI,CAAC,kDAAkD,EAAE,UAAA,CAAC;IACtD,IAAM,gBAAgB,GAAG,kBAAU,CAAC,GAAG,EAAuB,CAAC;IAE/D,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,gBAAgB,CAAC,GAAG,CAAC,WAAG,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,CAAC,UAAC,CAAC,EAAE,CAAC;QACzC,OAAO,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,CAAC,CAAC,MAAM,CAAC,cAAM,OAAA,gBAAgB,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,WAAG,CAAC,GAAG,EAAE,EAAE,WAAG,CAAC,GAAG,EAAE,CAAC,EAArD,CAAqD,CAAC,CAAC;IAEtE,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC","sourcesContent":["import test from 'ava';\r\n\r\nimport { Substitute, Arg } from '../../src/Index';\r\n\r\ninterface CalculatorInterface {\r\n add(a: number, b: number): number\r\n subtract(a: number, b: number): number\r\n divide(a: number, b: number): number\r\n isEnabled: boolean\r\n}\r\n\r\ntest('issue 23: mimick received should not call method', t => {\r\n const mockedCalculator = Substitute.for();\r\n\r\n let result = 0;\r\n mockedCalculator.add(Arg.all()).mimicks((a, b) => {\r\n return result = a + b;\r\n });\r\n\r\n t.throws(() => mockedCalculator.received().add(Arg.any(), Arg.any()));\r\n\r\n t.is(result, 0);\r\n});"]} \ No newline at end of file +{"version":3,"file":"23.test.js","sourceRoot":"","sources":["../../../spec/issues/23.test.ts"],"names":[],"mappings":";;AAAA,2BAAuB;AAEvB,yCAAkD;AASlD,aAAI,CAAC,kDAAkD,EAAE,UAAA,CAAC;IACxD,IAAM,gBAAgB,GAAG,kBAAU,CAAC,GAAG,EAAuB,CAAC;IAE/D,gBAAgB,CAAC,GAAG,CAAC,WAAG,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,CAAC,UAAC,CAAC,EAAE,CAAC;QAC3C,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAClB,OAAO,CAAC,GAAG,CAAC,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,gBAAgB,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK;IAEjC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,4BAA4B;AACtE,CAAC,CAAC,CAAC","sourcesContent":["import test from \"ava\";\r\n\r\nimport { Substitute, Arg } from \"../../src/Index\";\r\n\r\ninterface CalculatorInterface {\r\n add(a: number, b: number): number;\r\n subtract(a: number, b: number): number;\r\n divide(a: number, b: number): number;\r\n isEnabled: boolean;\r\n}\r\n\r\ntest(\"issue 23: mimick received should not call method\", t => {\r\n const mockedCalculator = Substitute.for();\r\n\r\n mockedCalculator.add(Arg.all()).mimicks((a, b) => {\r\n t.deepEqual(a, 1);\r\n return a + b;\r\n });\r\n\r\n mockedCalculator.add(1, 1); // ok\r\n\r\n mockedCalculator.received(1).add(2, 1); // not ok, calls mimick func\r\n});\r\n"]} \ No newline at end of file diff --git a/dist/src/states/FunctionState.js b/dist/src/states/FunctionState.js index 39be7fd..86e50b1 100644 --- a/dist/src/states/FunctionState.js +++ b/dist/src/states/FunctionState.js @@ -131,13 +131,13 @@ var FunctionState = /** @class */ (function () { _this.returns = returns; _this._callCount--; if (_this._callCount === 0) { - var indexOfSelf = _this - ._getPropertyState - .recordedFunctionStates - .indexOf(_this); - _this._getPropertyState - .recordedFunctionStates - .splice(indexOfSelf, 1); + // var indexOfSelf = this + // ._getPropertyState + // .recordedFunctionStates + // .indexOf(this); + // this._getPropertyState + // .recordedFunctionStates + // .splice(indexOfSelf, 1); } context.state = context.initialState; }; diff --git a/dist/src/states/FunctionState.js.map b/dist/src/states/FunctionState.js.map index 16826a8..743d53f 100644 --- a/dist/src/states/FunctionState.js.map +++ b/dist/src/states/FunctionState.js.map @@ -1 +1 @@ -{"version":3,"file":"FunctionState.js","sourceRoot":"","sources":["../../../src/states/FunctionState.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,0CAA6G;AAE7G,0CAA6C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AK,GAAG,OAAO,CAAC,YAAY,CAAC;YACzC,CAAC,CAAC;SACL;QAED,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,oBAAC;AAAD,CAAC,AA1HD,IA0HC;AA1HY,sCAAa","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { stringifyArguments, stringifyCalls, areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\r\nimport { GetPropertyState } from \"./GetPropertyState\";\r\nimport { Argument, Arg } from \"../Arguments\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class FunctionState implements ContextState {\r\n private returns: any[]|Symbol;\r\n private mimicks: Function|null;\r\n\r\n private _callCount: number;\r\n private _arguments: any[];\r\n\r\n public get arguments() {\r\n return this._arguments;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n public get property() {\r\n return this._getPropertyState.property;\r\n }\r\n\r\n constructor(private _getPropertyState: GetPropertyState, ...args: any[]) {\r\n this.returns = Nothing;\r\n this.mimicks = null;\r\n\r\n this._arguments = args;\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context, args: any[], matchingFunctionStates: FunctionState[]) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n if(!matchingFunctionStates) {\r\n matchingFunctionStates = this._getPropertyState\r\n .recordedFunctionStates\r\n .filter(x => areArgumentArraysEqual(x.arguments, args));\r\n }\r\n\r\n if(hasExpectations) {\r\n callCount = matchingFunctionStates\r\n .filter(x => x._arguments[0] !== Arg.all())\r\n .map(x => x.callCount)\r\n .reduce((a, b) => a + b, 0);\r\n }\r\n\r\n context.initialState.assertCallCountMatchesExpectations(\r\n this._getPropertyState.recordedFunctionStates,\r\n callCount,\r\n 'method',\r\n this.property,\r\n args);\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n\r\n for(let matchingFunctionState of matchingFunctionStates)\r\n for(let argument of matchingFunctionState.arguments) {\r\n if(!(argument instanceof Argument))\r\n continue;\r\n\r\n const indexOffset = matchingFunctionState\r\n .arguments\r\n .indexOf(argument);\r\n const myArg = args[indexOffset];\r\n if(myArg instanceof Argument)\r\n continue;\r\n\r\n argument.encounteredValues.push(myArg);\r\n }\r\n }\r\n\r\n if(this.mimicks)\r\n return this.mimicks.apply(this.mimicks, args);\r\n\r\n if(this.returns === Nothing)\r\n return context.proxy;\r\n\r\n var returnsArray = this.returns as any[];\r\n if(returnsArray.length === 1)\r\n return returnsArray[0];\r\n\r\n return returnsArray[this._callCount-1];\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n }\r\n\r\n get(context: Context, property: PropertyKey) {\r\n if (property === 'then')\r\n return void 0;\r\n\r\n if(property === 'mimicks') {\r\n return (input: Function) => {\r\n this.mimicks = input;\r\n this._callCount--;\r\n\r\n context.state = context.initialState;\r\n }\r\n }\r\n\r\n if(property === 'returns') {\r\n if(this.returns !== Nothing)\r\n throw new Error('The return value for the function ' + this._getPropertyState.toString() + ' with ' + stringifyArguments(this._arguments) + ' has already been set to ' + this.returns);\r\n\r\n return (...returns: any[]) => {\r\n this.returns = returns;\r\n this._callCount--;\r\n\r\n if(this._callCount === 0) {\r\n var indexOfSelf = this\r\n ._getPropertyState\r\n .recordedFunctionStates\r\n .indexOf(this);\r\n this._getPropertyState\r\n .recordedFunctionStates\r\n .splice(indexOfSelf, 1);\r\n }\r\n\r\n context.state = context.initialState;\r\n };\r\n }\r\n\r\n return context.proxy;\r\n }\r\n}"]} \ No newline at end of file +{"version":3,"file":"FunctionState.js","sourceRoot":"","sources":["../../../src/states/FunctionState.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,0CAA6G;AAE7G,0CAA6C;AAE7C,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAmBI,uBAAoB,iBAAmC;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QAAnD,sBAAiB,GAAj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IACvD,CAAC;IAED,2BAAG,GAAH,UAAI,OAAgB,EAAE,QAAqB;QAA3C,iBAoCC;QAnCG,IAAI,QAAQ,KAAK,MAAM;YACnB,OAAO,KAAK,CAAC,CAAC;QAElB,IAAG,QAAQ,KAAK,SAAS,EAAE;YACvB,OAAO,UAAC,KAAe;gBACnB,KAAI,CAAC,OAAO,GAAG,KAAK,CAAC;gBACrB,KAAI,CAAC,UAAU,EAAE,CAAC;gBAElB,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;YACzC,CAAC,CAAA;SACJ;QAED,IAAG,QAAQ,KAAK,SAAS,EAAE;YACvB,IAAG,IAAI,CAAC,OAAO,KAAK,OAAO;gBACvB,MAAM,IAAI,KAAK,CAAC,oCAAoC,GAAG,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,GAAG,QAAQ,GAAG,8BAAkB,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,2BAA2B,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;YAE5L,OAAO;gBAAC,iBAAiB;qBAAjB,UAAiB,EAAjB,qBAAiB,EAAjB,IAAiB;oBAAjB,4BAAiB;;gBACrB,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;gBACvB,KAAI,CAAC,UAAU,EAAE,CAAC;gBAElB,IAAG,KAAI,CAAC,UAAU,KAAK,CAAC,EAAE;oBACtB,yBAAyB;oBACzB,yBAAyB;oBACzB,8BAA8B;oBAC9B,sBAAsB;oBACtB,yBAAyB;oBACzB,8BAA8B;oBAC9B,+BAA+B;iBAClC;gBAED,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;YACzC,CAAC,CAAC;SACL;QAED,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,oBAAC;AAAD,CAAC,AA1HD,IA0HC;AA1HY,sCAAa","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { stringifyArguments, stringifyCalls, areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\r\nimport { GetPropertyState } from \"./GetPropertyState\";\r\nimport { Argument, Arg } from \"../Arguments\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class FunctionState implements ContextState {\r\n private returns: any[]|Symbol;\r\n private mimicks: Function|null;\r\n\r\n private _callCount: number;\r\n private _arguments: any[];\r\n\r\n public get arguments() {\r\n return this._arguments;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n public get property() {\r\n return this._getPropertyState.property;\r\n }\r\n\r\n constructor(private _getPropertyState: GetPropertyState, ...args: any[]) {\r\n this.returns = Nothing;\r\n this.mimicks = null;\r\n\r\n this._arguments = args;\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context, args: any[], matchingFunctionStates: FunctionState[]) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n if(!matchingFunctionStates) {\r\n matchingFunctionStates = this._getPropertyState\r\n .recordedFunctionStates\r\n .filter(x => areArgumentArraysEqual(x.arguments, args));\r\n }\r\n\r\n if(hasExpectations) {\r\n callCount = matchingFunctionStates\r\n .filter(x => x._arguments[0] !== Arg.all())\r\n .map(x => x.callCount)\r\n .reduce((a, b) => a + b, 0);\r\n }\r\n\r\n context.initialState.assertCallCountMatchesExpectations(\r\n this._getPropertyState.recordedFunctionStates,\r\n callCount,\r\n 'method',\r\n this.property,\r\n args);\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n\r\n for(let matchingFunctionState of matchingFunctionStates)\r\n for(let argument of matchingFunctionState.arguments) {\r\n if(!(argument instanceof Argument))\r\n continue;\r\n\r\n const indexOffset = matchingFunctionState\r\n .arguments\r\n .indexOf(argument);\r\n const myArg = args[indexOffset];\r\n if(myArg instanceof Argument)\r\n continue;\r\n\r\n argument.encounteredValues.push(myArg);\r\n }\r\n }\r\n\r\n if(this.mimicks)\r\n return this.mimicks.apply(this.mimicks, args);\r\n\r\n if(this.returns === Nothing)\r\n return context.proxy;\r\n\r\n var returnsArray = this.returns as any[];\r\n if(returnsArray.length === 1)\r\n return returnsArray[0];\r\n\r\n return returnsArray[this._callCount-1];\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n }\r\n\r\n get(context: Context, property: PropertyKey) {\r\n if (property === 'then')\r\n return void 0;\r\n\r\n if(property === 'mimicks') {\r\n return (input: Function) => {\r\n this.mimicks = input;\r\n this._callCount--;\r\n\r\n context.state = context.initialState;\r\n }\r\n }\r\n\r\n if(property === 'returns') {\r\n if(this.returns !== Nothing)\r\n throw new Error('The return value for the function ' + this._getPropertyState.toString() + ' with ' + stringifyArguments(this._arguments) + ' has already been set to ' + this.returns);\r\n\r\n return (...returns: any[]) => {\r\n this.returns = returns;\r\n this._callCount--;\r\n\r\n if(this._callCount === 0) {\r\n // var indexOfSelf = this\r\n // ._getPropertyState\r\n // .recordedFunctionStates\r\n // .indexOf(this);\r\n // this._getPropertyState\r\n // .recordedFunctionStates\r\n // .splice(indexOfSelf, 1);\r\n }\r\n\r\n context.state = context.initialState;\r\n };\r\n }\r\n\r\n return context.proxy;\r\n }\r\n}"]} \ No newline at end of file diff --git a/src/states/FunctionState.ts b/src/states/FunctionState.ts index ebf994e..1423e67 100644 --- a/src/states/FunctionState.ts +++ b/src/states/FunctionState.ts @@ -113,13 +113,13 @@ export class FunctionState implements ContextState { this._callCount--; if(this._callCount === 0) { - var indexOfSelf = this - ._getPropertyState - .recordedFunctionStates - .indexOf(this); - this._getPropertyState - .recordedFunctionStates - .splice(indexOfSelf, 1); + // var indexOfSelf = this + // ._getPropertyState + // .recordedFunctionStates + // .indexOf(this); + // this._getPropertyState + // .recordedFunctionStates + // .splice(indexOfSelf, 1); } context.state = context.initialState;