From d6556c358c63d75b85f6af1302a7e99652f40c88 Mon Sep 17 00:00:00 2001 From: Domas Date: Sun, 24 Mar 2019 23:18:00 +0200 Subject: [PATCH] allow limited multiple returns (#31) --- dist/spec/index.test.js | 28 +++++++++++++++++++--- dist/spec/index.test.js.map | 2 +- dist/src/Utilities.js | 6 ++--- dist/src/Utilities.js.map | 2 +- dist/src/states/FunctionState.js | 5 +--- dist/src/states/FunctionState.js.map | 2 +- dist/src/states/GetPropertyState.js | 1 + dist/src/states/GetPropertyState.js.map | 2 +- spec/index.test.ts | 32 ++++++++++++++++++++++--- src/Utilities.ts | 9 +++---- src/states/FunctionState.ts | 7 +----- src/states/GetPropertyState.ts | 1 + 12 files changed, 70 insertions(+), 27 deletions(-) diff --git a/dist/spec/index.test.js b/dist/spec/index.test.js index b2eba64..86f8fb3 100644 --- a/dist/spec/index.test.js +++ b/dist/spec/index.test.js @@ -253,12 +253,34 @@ ava_1.default('return with more arguments is not matched fails', function (t) { initialize(); substitute.bar(1, 2).returns(3); t.is(3, substitute.bar(1, 2)); - t.is(void 0, substitute.bar(1)); + t.is('function', typeof (substitute.bar(1))); }); -ava_1.default('return with less arguments is not matched', function (t) { +ava_1.default('return with less arguments is not matched', function (t) { initialize(); substitute.bar(1).returns(3); t.is(3, substitute.bar(1)); - t.is(void 0, substitute.bar(1, 2)); + t.is('function', typeof (substitute.bar(1, 2).toString)); +}); +ava_1.default('can stub multiple primitive return values', function (t) { + initialize(); + substitute.bar(1).returns(2); + substitute.bar(2).returns(3); + t.is(2, substitute.bar(1)); + t.is(3, substitute.bar(2)); +}); +ava_1.default('can stub multiple Arg values', function (t) { + initialize(); + substitute.bar(index_1.Arg.is(function (x) { return x % 2 === 0; })).returns(1); + substitute.bar(index_1.Arg.is(function (x) { return x % 2 !== 0; })).returns(2); + t.is(1, substitute.bar(4)); + t.is(2, substitute.bar(5)); +}); +ava_1.default.skip('can stub primitive & Arg values', function (t) { + initialize(); + substitute.bar(1).returns(2); + substitute.bar(index_1.Arg.any()).returns(3); // throws 'substitute.bar(...).returns is not a function' + t.is(5, substitute.bar(2)); + t.is(2, substitute.bar(1)); + t.is(3, substitute.bar(2)); }); //# sourceMappingURL=index.test.js.map \ No newline at end of file diff --git a/dist/spec/index.test.js.map b/dist/spec/index.test.js.map index ad55868..334191d 100644 --- a/dist/spec/index.test.js.map +++ b/dist/spec/index.test.js.map @@ -1 +1 @@ -{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../spec/index.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iBAoRE;;AApRF,2BAAuB;AAEvB,sCAA+C;AAC/C,8CAAqD;AAGrD;IAAA;IAEA,CAAC;IAAD,YAAC;AAAD,CAAC,AAFD,IAEC;AAED;IAAA;QACC,MAAC,GAAG,MAAM,CAAC;IA4BZ,CAAC;IA1BA,mBAAC,GAAD,UAAE,IAAY,EAAE,IAAY;QAC3B,OAAO,QAAQ,GAAG,IAAI,GAAG,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;IAClD,CAAC;IAED,sBAAI,sBAAC;aAAL;YACC,OAAO,IAAI,CAAC;QACb,CAAC;;;OAAA;IAED,sBAAI,sBAAC;aAAL,UAAM,CAAwB;QAC9B,CAAC;;;OAAA;IAED,0BAAQ,GAAR,UAAS,KAAoB;IAE7B,CAAC;IAED,+BAAa,GAAb;QACC,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;IACrC,CAAC;IAED,qBAAG,GAAH;QACC,OAAO,OAAO,CAAC;IACb,CAAC;IAED,qBAAG,GAAH,UAAK,CAAS,EAAE,CAAU;QACtB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;IACL,cAAC;AAAD,CAAC,AA7BD,IA6BC;AA7BY,0BAAO;AA+BpB,IAAI,QAAiB,CAAC;AACtB,IAAI,UAAgE,CAAC;AAErE,SAAS,UAAU;IAClB,QAAQ,GAAG,IA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;IACZ,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;IAC5B,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1B,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AACtC,CAAC,CAAC,CAAA","sourcesContent":["import test from 'ava';\r\n\r\nimport { Substitute, Arg } from '../src/index';\r\nimport { areArgumentsEqual } from '../src/Utilities';\r\nimport { OmitProxyMethods, ObjectSubstitute } from '../src/Transformations';\r\n\r\nclass Dummy {\r\n\r\n}\r\n\r\nexport class Example {\r\n\ta = \"1337\";\r\n\r\n\tc(arg1: string, arg2: string) {\r\n\t\treturn \"hello \" + arg1 + \" world (\" + arg2 + \")\";\r\n\t}\r\n\r\n\tget d() {\r\n\t\treturn 1337;\r\n\t}\r\n\r\n\tset v(x: string|null|undefined) {\r\n\t}\r\n\r\n\treceived(stuff: number|string) {\r\n\r\n\t}\r\n\r\n\treturnPromise() {\r\n\t\treturn Promise.resolve(new Dummy());\r\n\t}\r\n\r\n\tfoo(): string|undefined|null {\r\n\t\treturn 'stuff';\r\n }\r\n \r\n bar (a: number, b?: number): number{\r\n return a + b || 0\r\n }\r\n}\r\n\r\nlet instance: Example;\r\nlet substitute: ObjectSubstitute, Example>;\r\n\r\nfunction initialize() {\r\n\tinstance = new Example();\r\n\tsubstitute = Substitute.for();\r\n};\r\n\r\ntest('can call received twice', t => { \r\n\tinitialize();\r\n\r\n\tsubstitute.c('blah', 'fuzz');\r\n\r\n\tt.throws(() => substitute.received(1337).c('foo', 'bar'), \r\n`Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['blah', 'fuzz']`);\r\n\r\n\tt.throws(() => substitute.received(2117).c('foo', 'bar'),\r\n`Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['blah', 'fuzz']`);\r\n});\r\n\r\ntest('class string field get returns', t => {\r\n\tinitialize();\r\n\r\n\tsubstitute.a.returns(\"foo\", \"bar\");\r\n\r\n\tt.deepEqual(substitute.a, 'foo');\r\n\tt.deepEqual(substitute.a, 'bar');\r\n\tt.deepEqual(substitute.a, void 0);\r\n\tt.deepEqual(substitute.a, void 0);\r\n});\r\n\r\ntest('class with method called \"received\" can be used for call count verification when proxies are suspended', t => {\r\n\tinitialize();\r\n\t\r\n\tSubstitute.disableFor(substitute).received(2);\r\n\r\n\tt.throws(() => substitute.received(2).received(2));\r\n\tt.notThrows(() => substitute.received(1).received(2));\r\n});\r\n\r\ntest('class with method called \"received\" can be used for call count verification', t => {\r\n\tinitialize();\r\n\t\r\n\tSubstitute.disableFor(substitute).received('foo');\r\n\r\n\tt.notThrows(() => substitute.received(1).received('foo'));\r\n\tt.throws(() => substitute.received(2).received('foo'));\r\n});\r\n\r\ntest('partial mocks using function mimicks with all args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(Arg.all()).mimicks(instance.c);\r\n\r\n\tt.deepEqual(substitute.c('a', 'b'), 'hello a world (b)');\r\n});\r\n\r\ntest('class string field get received', t => {\r\n\tinitialize();\r\n\t\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\r\n\tt.throws(() => substitute.received(3).a);\r\n\tt.notThrows(() => substitute.received().a);\r\n\tt.notThrows(() => substitute.received(4).a);\r\n});\r\n\r\ntest('class string field set received', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.v = undefined;\r\n\tsubstitute.v = null;\r\n\tsubstitute.v = 'hello';\r\n\tsubstitute.v = 'hello';\r\n\tsubstitute.v = 'world';\r\n\t\r\n\tt.notThrows(() => substitute.received().v = 'hello');\r\n\tt.notThrows(() => substitute.received(5).v = Arg.any());\r\n\tt.notThrows(() => substitute.received().v = Arg.any());\r\n\tt.notThrows(() => substitute.received(2).v = 'hello');\r\n\tt.notThrows(() => substitute.received(2).v = Arg.is(x => x && x.indexOf('ll') > -1));\r\n\r\n\tt.throws(() => substitute.received(2).v = Arg.any());\r\n\tt.throws(() => substitute.received(1).v = Arg.any());\r\n\tt.throws(() => substitute.received(1).v = Arg.is(x => x && x.indexOf('ll') > -1));\r\n\tt.throws(() => substitute.received(3).v = 'hello');\r\n});\r\n\r\ntest('class method returns with placeholder args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(Arg.any(), \"there\").returns(\"blah\", \"haha\");\r\n\t\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'blah');\r\n\tt.is(substitute.c(\"his\", \"there\"), 'haha');\r\n\tt.is(substitute.c(\"his\", \"there\"), void 0);\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n});\r\n\r\ntest('partial mocks using function mimicks with specific args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c('a', 'b').mimicks(instance.c);\r\n\r\n\tt.is(substitute.c('a', 'b'), 'hello a world (b)');\r\n});\r\n\r\ntest('class method returns with specific args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(\"hi\", \"there\").returns(\"blah\", \"haha\");\r\n\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'blah');\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'haha');\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n});\r\n\r\ntest('returning other fake from promise works', async t => {\r\n\tinitialize();\r\n\t\r\n\tconst otherSubstitute = Substitute.for();\r\n\tsubstitute.returnPromise().returns(Promise.resolve(otherSubstitute));\r\n t.is(otherSubstitute, await substitute.returnPromise());\r\n});\r\n\r\ntest('returning resolved promises works', async t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.returnPromise().returns(Promise.resolve(1338));\r\n\r\n\tt.is(1338, await substitute.returnPromise());\r\n});\r\n\r\ntest('class void returns', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.foo().returns(void 0, null);\r\n\r\n\tt.is(substitute.foo(), void 0);\r\n\tt.is(substitute.foo(), null);\r\n}); \r\n\r\ntest('class method received', t => {\r\n\tinitialize();\r\n\t\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"the1re\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\r\n\tt.notThrows(() => substitute.received(4).c('hi', 'there'));\r\n\tt.notThrows(() => substitute.received(1).c('hi', 'the1re'));\r\n\tt.notThrows(() => substitute.received().c('hi', 'there'));\r\n\r\n\tt.throws(() => substitute.received(7).c('hi', 'there'), \r\n`Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'the1re']\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'there']`);\r\n});\r\n\r\ntest('received call matches after partial mocks using property instance mimicks', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.d.mimicks(() => instance.d);\r\n\tsubstitute.c('lala', 'bar');\r\n\r\n\tsubstitute.received(1).c('lala', 'bar');\r\n\tsubstitute.received(1).c('lala', 'bar');\r\n\r\n\tt.notThrows(() => substitute.received(1).c('lala', 'bar'));\r\n\tt.throws(() => substitute.received(2).c('lala', 'bar'),\r\n`Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.\r\nAll calls received to method c:\r\n-> call with arguments ['lala', 'bar']`);\r\n\t\r\n\tt.deepEqual(substitute.d, 1337);\r\n});\r\n\r\ntest('partial mocks using property instance mimicks', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.d.mimicks(() => instance.d);\r\n\r\n\tt.deepEqual(substitute.d, 1337);\r\n});\r\n\r\ntest('are arguments equal', t => {\r\n\tinitialize();\r\n\t\r\n\tt.true(areArgumentsEqual(Arg.any(), 'hi'));\r\n\tt.true(areArgumentsEqual(Arg.any('array'), ['foo', 'bar']));\r\n\r\n\tt.false(areArgumentsEqual(['foo', 'bar'], ['foo', 'bar']));\r\n\tt.false(areArgumentsEqual(Arg.any('array'), 1337));\r\n});\r\n\r\ntest('verifying with more arguments fails', t => {\r\n initialize()\r\n substitute.bar(1)\r\n substitute.received().bar(1)\r\n t.throws(() => substitute.received().bar(1, 2))\r\n})\r\n\r\ntest('verifying with less arguments fails', t => {\r\n initialize()\r\n substitute.bar(1, 2)\r\n substitute.received().bar(1, 2)\r\n t.throws(() => substitute.received().bar(1))\r\n})\r\n\r\ntest('return with more arguments is not matched fails', t => {\r\n initialize()\r\n substitute.bar(1, 2).returns(3)\r\n t.is(3, substitute.bar(1, 2))\r\n t.is(void 0, substitute.bar(1))\r\n})\r\n\r\ntest('return with less arguments is not matched', t => {\r\n initialize()\r\n substitute.bar(1).returns(3)\r\n t.is(3, substitute.bar(1))\r\n t.is(void 0, substitute.bar(1, 2))\r\n})"]} \ No newline at end of file +{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../spec/index.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iBA8SE;;AA9SF,2BAAuB;AAEvB,sCAA+C;AAC/C,8CAAqD;AAGrD;IAAA;IAEA,CAAC;IAAD,YAAC;AAAD,CAAC,AAFD,IAEC;AAED;IAAA;QACC,MAAC,GAAG,MAAM,CAAC;IA4BZ,CAAC;IA1BA,mBAAC,GAAD,UAAE,IAAY,EAAE,IAAY;QAC3B,OAAO,QAAQ,GAAG,IAAI,GAAG,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;IAClD,CAAC;IAED,sBAAI,sBAAC;aAAL;YACC,OAAO,IAAI,CAAC;QACb,CAAC;;;OAAA;I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ourcesContent":["import test from 'ava';\r\n\r\nimport { Substitute, Arg } from '../src/index';\r\nimport { areArgumentsEqual } from '../src/Utilities';\r\nimport { OmitProxyMethods, ObjectSubstitute } from '../src/Transformations';\r\n\r\nclass Dummy {\r\n\r\n}\r\n\r\nexport class Example {\r\n\ta = \"1337\";\r\n\r\n\tc(arg1: string, arg2: string) {\r\n\t\treturn \"hello \" + arg1 + \" world (\" + arg2 + \")\";\r\n\t}\r\n\r\n\tget d() {\r\n\t\treturn 1337;\r\n\t}\r\n\r\n\tset v(x: string|null|undefined) {\r\n\t}\r\n\r\n\treceived(stuff: number|string) {\r\n\r\n\t}\r\n\r\n\treturnPromise() {\r\n\t\treturn Promise.resolve(new Dummy());\r\n\t}\r\n\r\n\tfoo(): string|undefined|null {\r\n\t\treturn 'stuff';\r\n }\r\n \r\n bar (a: number, b?: number): number{\r\n return a + b || 0\r\n }\r\n}\r\n\r\nlet instance: Example;\r\nlet substitute: ObjectSubstitute, Example>;\r\n\r\nfunction initialize() {\r\n\tinstance = new Example();\r\n\tsubstitute = Substitute.for();\r\n};\r\n\r\ntest('can call received twice', t => { \r\n\tinitialize();\r\n\r\n\tsubstitute.c('blah', 'fuzz');\r\n\r\n\tt.throws(() => substitute.received(1337).c('foo', 'bar'), \r\n`Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['blah', 'fuzz']`);\r\n\r\n\tt.throws(() => substitute.received(2117).c('foo', 'bar'),\r\n`Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['blah', 'fuzz']`);\r\n});\r\n\r\ntest('class string field get returns', t => {\r\n\tinitialize();\r\n\r\n\tsubstitute.a.returns(\"foo\", \"bar\");\r\n\r\n\tt.deepEqual(substitute.a, 'foo');\r\n\tt.deepEqual(substitute.a, 'bar');\r\n\tt.deepEqual(substitute.a, void 0);\r\n\tt.deepEqual(substitute.a, void 0);\r\n});\r\n\r\ntest('class with method called \"received\" can be used for call count verification when proxies are suspended', t => {\r\n\tinitialize();\r\n\t\r\n\tSubstitute.disableFor(substitute).received(2);\r\n\r\n\tt.throws(() => substitute.received(2).received(2));\r\n\tt.notThrows(() => substitute.received(1).received(2));\r\n});\r\n\r\ntest('class with method called \"received\" can be used for call count verification', t => {\r\n\tinitialize();\r\n\t\r\n\tSubstitute.disableFor(substitute).received('foo');\r\n\r\n\tt.notThrows(() => substitute.received(1).received('foo'));\r\n\tt.throws(() => substitute.received(2).received('foo'));\r\n});\r\n\r\ntest('partial mocks using function mimicks with all args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(Arg.all()).mimicks(instance.c);\r\n\r\n\tt.deepEqual(substitute.c('a', 'b'), 'hello a world (b)');\r\n});\r\n\r\ntest('class string field get received', t => {\r\n\tinitialize();\r\n\t\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\tvoid substitute.a;\r\n\r\n\tt.throws(() => substitute.received(3).a);\r\n\tt.notThrows(() => substitute.received().a);\r\n\tt.notThrows(() => substitute.received(4).a);\r\n});\r\n\r\ntest('class string field set received', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.v = undefined;\r\n\tsubstitute.v = null;\r\n\tsubstitute.v = 'hello';\r\n\tsubstitute.v = 'hello';\r\n\tsubstitute.v = 'world';\r\n\t\r\n\tt.notThrows(() => substitute.received().v = 'hello');\r\n\tt.notThrows(() => substitute.received(5).v = Arg.any());\r\n\tt.notThrows(() => substitute.received().v = Arg.any());\r\n\tt.notThrows(() => substitute.received(2).v = 'hello');\r\n\tt.notThrows(() => substitute.received(2).v = Arg.is(x => x && x.indexOf('ll') > -1));\r\n\r\n\tt.throws(() => substitute.received(2).v = Arg.any());\r\n\tt.throws(() => substitute.received(1).v = Arg.any());\r\n\tt.throws(() => substitute.received(1).v = Arg.is(x => x && x.indexOf('ll') > -1));\r\n\tt.throws(() => substitute.received(3).v = 'hello');\r\n});\r\n\r\ntest('class method returns with placeholder args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(Arg.any(), \"there\").returns(\"blah\", \"haha\");\r\n\t\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'blah');\r\n\tt.is(substitute.c(\"his\", \"there\"), 'haha');\r\n\tt.is(substitute.c(\"his\", \"there\"), void 0);\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n});\r\n\r\ntest('partial mocks using function mimicks with specific args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c('a', 'b').mimicks(instance.c);\r\n\r\n\tt.is(substitute.c('a', 'b'), 'hello a world (b)');\r\n});\r\n\r\ntest('class method returns with specific args', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.c(\"hi\", \"there\").returns(\"blah\", \"haha\");\r\n\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'blah');\r\n\tt.is(substitute.c(\"hi\", \"there\"), 'haha');\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n\tt.is(substitute.c(\"hi\", \"there\"), void 0);\r\n});\r\n\r\ntest('returning other fake from promise works', async t => {\r\n\tinitialize();\r\n\t\r\n\tconst otherSubstitute = Substitute.for();\r\n\tsubstitute.returnPromise().returns(Promise.resolve(otherSubstitute));\r\n t.is(otherSubstitute, await substitute.returnPromise());\r\n});\r\n\r\ntest('returning resolved promises works', async t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.returnPromise().returns(Promise.resolve(1338));\r\n\r\n\tt.is(1338, await substitute.returnPromise());\r\n});\r\n\r\ntest('class void returns', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.foo().returns(void 0, null);\r\n\r\n\tt.is(substitute.foo(), void 0);\r\n\tt.is(substitute.foo(), null);\r\n}); \r\n\r\ntest('class method received', t => {\r\n\tinitialize();\r\n\t\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"the1re\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\tvoid substitute.c(\"hi\", \"there\");\r\n\r\n\tt.notThrows(() => substitute.received(4).c('hi', 'there'));\r\n\tt.notThrows(() => substitute.received(1).c('hi', 'the1re'));\r\n\tt.notThrows(() => substitute.received().c('hi', 'there'));\r\n\r\n\tt.throws(() => substitute.received(7).c('hi', 'there'), \r\n`Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.\r\nAll calls received to method c:\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'the1re']\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'there']\r\n-> call with arguments ['hi', 'there']`);\r\n});\r\n\r\ntest('received call matches after partial mocks using property instance mimicks', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.d.mimicks(() => instance.d);\r\n\tsubstitute.c('lala', 'bar');\r\n\r\n\tsubstitute.received(1).c('lala', 'bar');\r\n\tsubstitute.received(1).c('lala', 'bar');\r\n\r\n\tt.notThrows(() => substitute.received(1).c('lala', 'bar'));\r\n\tt.throws(() => substitute.received(2).c('lala', 'bar'),\r\n`Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.\r\nAll calls received to method c:\r\n-> call with arguments ['lala', 'bar']`);\r\n\t\r\n\tt.deepEqual(substitute.d, 1337);\r\n});\r\n\r\ntest('partial mocks using property instance mimicks', t => {\r\n\tinitialize();\r\n\t\r\n\tsubstitute.d.mimicks(() => instance.d);\r\n\r\n\tt.deepEqual(substitute.d, 1337);\r\n});\r\n\r\ntest('are arguments equal', t => {\r\n\tinitialize();\r\n\t\r\n\tt.true(areArgumentsEqual(Arg.any(), 'hi'));\r\n\tt.true(areArgumentsEqual(Arg.any('array'), ['foo', 'bar']));\r\n\r\n\tt.false(areArgumentsEqual(['foo', 'bar'], ['foo', 'bar']));\r\n\tt.false(areArgumentsEqual(Arg.any('array'), 1337));\r\n});\r\n\r\ntest('verifying with more arguments fails', t => {\r\n initialize()\r\n substitute.bar(1)\r\n substitute.received().bar(1)\r\n t.throws(() => substitute.received().bar(1, 2))\r\n})\r\n\r\ntest('verifying with less arguments fails', t => {\r\n initialize()\r\n substitute.bar(1, 2)\r\n substitute.received().bar(1, 2)\r\n t.throws(() => substitute.received().bar(1))\r\n})\r\n\r\ntest('return with more arguments is not matched fails', t => {\r\n initialize()\r\n substitute.bar(1, 2).returns(3)\r\n t.is(3, substitute.bar(1, 2))\r\n t.is('function', typeof(substitute.bar(1)))\r\n})\r\n\r\ntest('return with less arguments is not matched', t => {\r\n initialize()\r\n substitute.bar(1).returns(3)\r\n t.is(3, substitute.bar(1))\r\n t.is('function', typeof(substitute.bar(1, 2).toString))\r\n})\r\n\r\ntest('can stub multiple primitive return values', t => {\r\n initialize()\r\n substitute.bar(1).returns(2)\r\n substitute.bar(2).returns(3)\r\n t.is(2, substitute.bar(1))\r\n t.is(3, substitute.bar(2))\r\n})\r\n\r\ntest('can stub multiple Arg values', t => {\r\n initialize()\r\n substitute.bar(Arg.is(x => x % 2 === 0)).returns(1)\r\n substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2)\r\n t.is(1, substitute.bar(4))\r\n t.is(2, substitute.bar(5))\r\n})\r\n\r\n\r\ntest.skip('can stub primitive & Arg values', t => {\r\n initialize()\r\n substitute.bar(1).returns(2)\r\n substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function'\r\n t.is(5, substitute.bar(2))\r\n t.is(2, substitute.bar(1))\r\n t.is(3, substitute.bar(2))\r\n})"]} \ No newline at end of file diff --git a/dist/src/Utilities.js b/dist/src/Utilities.js index 61a6ebe..4e38d41 100644 --- a/dist/src/Utilities.js +++ b/dist/src/Utilities.js @@ -49,11 +49,11 @@ function stringifyCalls(calls) { exports.stringifyCalls = stringifyCalls; ; function areArgumentsEqual(a, b) { - if (a instanceof Arguments_1.AllArguments || b instanceof Arguments_1.AllArguments) - return true; if (a instanceof Arguments_1.Argument && b instanceof Arguments_1.Argument) { - throw new Error("`Argument` should only be used to set up value or verify, not in the implementation."); + return false; } + if (a instanceof Arguments_1.AllArguments || b instanceof Arguments_1.AllArguments) + return true; if (a instanceof Arguments_1.Argument) return a.matches(b); if (b instanceof Arguments_1.Argument) diff --git a/dist/src/Utilities.js.map b/dist/src/Utilities.js.map index 89aa8be..0cd1270 100644 --- a/dist/src/Utilities.js.map +++ b/dist/src/Utilities.js.map @@ -1 +1 @@ -{"version":3,"file":"Utilities.js","sourceRoot":"","sources":["../../src/Utilities.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAqD;AACrD,2BAA6B;AAI7B,SAAgB,kBAAkB,CAAC,IAAW;IAC1C,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAf,CAAe,CAAC,CAAC;IACtC,OAAO,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,cAAc,CAAC;AAC5F,CAAC;AAHD,gDAGC;AAAA,CAAC;AAEF,SAAgB,sBAAsB,CAAC,CAAQ,EAAE,CAAQ;IACrD,KAAI,IAAI,CAAC,GAAC,CAAC,EAAC,CAAC,GAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAC,CAAC,EAAE,EAAE,EAAE,8CAA8C;QAC5F,IAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YAC7B,OAAO,KAAK,CAAC;KACpB;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAPD,wDAOC;AAED,SAAgB,cAAc,CAAC,KAAa;;IAExC,IAAG,KAAK,CAAC,MAAM,KAAK,CAAC;QACjB,OAAO,aAAa,CAAC;IAEzB,IAAI,MAAM,GAAG,EAAE,CAAC;;QAChB,KAAiB,IAAA,UAAA,SAAA,KAAK,CAAA,4BAAA,+CAAE;YAAnB,IAAI,IAAI,kBAAA;YACT,MAAM,IAAI,iBAAiB,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAA;SAC5F;;;;;;;;;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAXD,wCAWC;AAAA,CAAC;AAEF,SAAgB,iBAAiB,CAAC,CAAM,EAAE,CAAM;IAC5C,IAAG,CAAC,YAAY,wBAAY,IAAI,CAAC,YAAY,wBAAY;QACrD,OAAO,IAAI,CAAC;IAEhB,IAAG,CAAC,YAAY,oBAAQ,IAAI,CAAC,YAAY,oBAAQ,EAAE;QAC/C,MAAM,IAAI,KAAK,CAAC,sFAAsF,CAAC,CAAA;KAC1G;IAED,IAAG,CAAC,YAAY,oBAAQ;QACpB,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAExB,IAAG,CAAC,YAAY,oBAAQ;QACpB,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAExB,0GAA0G;IAC1G,IAAI,CAAC,OAAO,CAAC,KAAK,WAAW,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,WAAW,IAAI,CAAC,KAAK,IAAI,CAAC;QACpF,OAAO,IAAI,CAAC;IAEhB,OAAO,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC;AAnBD,8CAmBC;AAAA,CAAC","sourcesContent":["import { Argument, AllArguments } from \"./Arguments\";\r\nimport util = require('util')\r\n\r\nexport type Call = any[] // list of args\r\n\r\nexport function stringifyArguments(args: any[]) {\r\n args = args.map(x => util.inspect(x));\r\n return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments';\r\n};\r\n\r\nexport function areArgumentArraysEqual(a: any[], b: any[]) {\r\n for(var i=0;i util.inspect(x));\r\n return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments';\r\n};\r\n\r\nexport function areArgumentArraysEqual(a: any[], b: any[]) {\r\n for(var i=0;i areArgumentArraysEqual(cargs, args) ? count + 1 : count, 0)\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n const hasExpectations = context.initialState.hasExpectations;\r\n this._lastArgs = args\r\n\r\n context.initialState.assertCallCountMatchesExpectations(\r\n this._calls,\r\n this.getCallCount(args),\r\n 'method',\r\n this.property,\r\n args);\r\n\r\n if(!hasExpectations) {\r\n this._calls.push(args)\r\n }\r\n\r\n if (!hasExpectations) {\r\n if(this.mimicks)\r\n return this.mimicks.apply(this.mimicks, args);\r\n\r\n if(!this.returns.length)\r\n return context.proxy;\r\n \r\n const returns = this.returns.find(r => areArgumentArraysEqual(r.args, args))\r\n\r\n if (returns) {\r\n const returnValues = returns.returnValues as any[]\r\n if (returnValues.length === 1) {\r\n return returnValues[0]\r\n }\r\n if (returnValues.length > returns.returnIndex) {\r\n return returnValues[returns.returnIndex++]\r\n }\r\n return void 0 // probably a test setup error, imho throwin is more helpful -- domasx2\r\n //throw Error(`${String(this._getPropertyState.property)} with ${stringifyArguments(returns.args)} called ${returns.returnIndex + 1} times, but only ${returnValues.length} return values were set up`)\r\n }\r\n }\r\n return void 0\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._calls.pop()\r\n\r\n context.state = context.initialState;\r\n }\r\n }\r\n\r\n if(property === 'returns') {\r\n if (this.returns.length) // I don't think this can happen -- domasx2\r\n throw new Error('BUT HOW?')\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 if (!this._lastArgs) {\r\n throw new Error('Eh, there\\'s a bug, no args recorded for this return :/')\r\n }\r\n this.returns.push({\r\n returnValues: returns,\r\n returnIndex: 0,\r\n args: this._lastArgs\r\n })\r\n this._calls.pop()\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,0CAAmH;AAInH,IAAM,OAAO,GAAG,MAAM,EAAE,CAAA;AAQxB;IAmBI,uBAAoB,iBAAmC;QAAnC,sBAAiB,GAAjB,iBAAiB,CAAkB;QACnD,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACrB,CAAC;IAhBD,sBAAW,gCAAK;aAAhB;YACI,OAAO,IAAI,CAAC,MAAM,CAAA;QACtB,CAAC;;;OAAA;IAED,sBAAW,oCAAS;aAApB;YACI,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAC9B,CAAC;;;OAAA;IAED,sBAAW,mCAAQ;aAAnB;YACI,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC;QAC3C,CAAC;;;OAAA;IAQO,oCAAY,GAApB,UAAqB,IAAU;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAC,KAAK,EAAE,KAAK,IAAK,OAAA,kCAAsB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,EAAvD,CAAuD,EAAE,CAA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["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { stringifyArguments, stringifyCalls, areArgumentsEqual, areArgumentArraysEqual, Call } from \"../Utilities\";\r\nimport { GetPropertyState } from \"./GetPropertyState\";\r\nimport { Argument, Arg } from \"../Arguments\";\r\n\r\nconst Nothing = Symbol()\r\n\r\ninterface ReturnMock {\r\n args: Call\r\n returnValues: any[] | Symbol // why symbol, what\r\n returnIndex: 0\r\n}\r\n\r\nexport class FunctionState implements ContextState {\r\n private returns: ReturnMock[];\r\n private mimicks: Function|null;\r\n\r\n private _calls: Call[]; // list of lists of arguments this was called with\r\n private _lastArgs?: Call // bit of a hack\r\n\r\n public get calls(): Call[] {\r\n return this._calls\r\n }\r\n\r\n public get callCount() {\r\n return this._calls.length;\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) {\r\n this.returns = [];\r\n this.mimicks = null;\r\n this._calls = [];\r\n }\r\n\r\n private getCallCount(args: Call): number {\r\n return this._calls.reduce((count, cargs) => areArgumentArraysEqual(cargs, args) ? count + 1 : count, 0)\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n const hasExpectations = context.initialState.hasExpectations;\r\n this._lastArgs = args\r\n\r\n context.initialState.assertCallCountMatchesExpectations(\r\n this._calls,\r\n this.getCallCount(args),\r\n 'method',\r\n this.property,\r\n args);\r\n\r\n if(!hasExpectations) {\r\n this._calls.push(args)\r\n }\r\n\r\n if (!hasExpectations) {\r\n if(this.mimicks)\r\n return this.mimicks.apply(this.mimicks, args);\r\n\r\n if(!this.returns.length)\r\n return context.proxy;\r\n const returns = this.returns.find(r => areArgumentArraysEqual(r.args, args))\r\n\r\n if (returns) {\r\n const returnValues = returns.returnValues as any[]\r\n if (returnValues.length === 1) {\r\n return returnValues[0]\r\n }\r\n if (returnValues.length > returns.returnIndex) {\r\n return returnValues[returns.returnIndex++]\r\n }\r\n return void 0 // probably a test setup error, imho throwin is more helpful -- domasx2\r\n //throw Error(`${String(this._getPropertyState.property)} with ${stringifyArguments(returns.args)} called ${returns.returnIndex + 1} times, but only ${returnValues.length} return values were set up`)\r\n }\r\n }\r\n return context.proxy\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._calls.pop()\r\n\r\n context.state = context.initialState;\r\n }\r\n }\r\n\r\n if(property === 'returns') {\r\n return (...returns: any[]) => {\r\n if (!this._lastArgs) {\r\n throw new Error('Eh, there\\'s a bug, no args recorded for this return :/')\r\n }\r\n this.returns.push({\r\n returnValues: returns,\r\n returnIndex: 0,\r\n args: this._lastArgs\r\n })\r\n this._calls.pop()\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/dist/src/states/GetPropertyState.js b/dist/src/states/GetPropertyState.js index e5d4969..2916879 100644 --- a/dist/src/states/GetPropertyState.js +++ b/dist/src/states/GetPropertyState.js @@ -40,6 +40,7 @@ var GetPropertyState = /** @class */ (function () { GetPropertyState.prototype.apply = function (context, args) { this._callCount = 0; if (this.functionState) { + context.state = this.functionState; return this.functionState.apply(context, args); } var functionState = new FunctionState_1.FunctionState(this); diff --git a/dist/src/states/GetPropertyState.js.map b/dist/src/states/GetPropertyState.js.map index b6c96d5..dcf7673 100644 --- a/dist/src/states/GetPropertyState.js.map +++ b/dist/src/states/GetPropertyState.js.map @@ -1 +1 @@ -{"version":3,"file":"GetPropertyState.js","sourceRoot":"","sources":["../../../src/states/GetPropertyState.ts"],"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CAAC;aAC1C;SACJ;QAED,OAAO,CAAC,YAAY,CAAC,kCAAkC,CACnD,CAAC,EAAE,CAAC,EAAG,8CAA8C;QACrD,IAAI,CAAC,SAAS,EACd,UAAU,EACV,IAAI,CAAC,QAAQ,EACb,EAAE,CAAC,CAAC;QAER,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,uBAAC;AAAD,CAAC,AApGD,IAoGC;AApGY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\nimport { Context } from \"src/Context\";\nimport { FunctionState } from \"./FunctionState\";\nimport { areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\nimport { Arg } from \"../Arguments\";\n\nconst Nothing = Symbol();\n\nexport class GetPropertyState implements ContextState {\n private returns: any[]|Symbol;\n private mimicks: Function|null;\n\n private _callCount: number;\n private _functionState?: FunctionState;\n\n private get isFunction(): boolean {\n return !!this._functionState\n }\n\n public get property() {\n return this._property;\n }\n\n public get callCount() {\n return this._callCount;\n }\n\n public get functionState(): FunctionState | undefined {\n return this._functionState\n }\n\n constructor(private _property: PropertyKey) {\n this.returns = Nothing;\n this.mimicks = null;\n this._callCount = 0;\n }\n\n apply(context: Context, args: any[]) {\n this._callCount = 0;\n\n if (this.functionState) {\n return this.functionState.apply(context, args);\n }\n\n var functionState = new FunctionState(this);\n context.state = functionState;\n this._functionState = functionState\n\n return context.apply(args);\n }\n\n set(context: Context, property: PropertyKey, value: any) {\n }\n\n get(context: Context, property: PropertyKey) {\n const hasExpectations = context.initialState.hasExpectations;\n\n if (property === 'then')\n return void 0;\n\n if(this.isFunction)\n return context.proxy;\n\n if(property === 'mimicks') {\n return (input: Function) => {\n this.mimicks = input;\n this._callCount--;\n\n context.state = context.initialState;\n }\n }\n\n if(property === 'returns') {\n if(this.returns !== Nothing)\n throw new Error('The return value for the property ' + this._property.toString() + ' has already been set to ' + this.returns);\n\n return (...returns: any[]) => {\n this.returns = returns;\n this._callCount--;\n\n context.state = context.initialState;\n };\n }\n\n if(!hasExpectations) {\n this._callCount++;\n\n if(this.mimicks)\n return this.mimicks.apply(this.mimicks);\n\n if(this.returns !== Nothing) {\n var returnsArray = this.returns as any[];\n if(returnsArray.length === 1)\n return returnsArray[0];\n \n return returnsArray[this._callCount-1];\n }\n }\n\n context.initialState.assertCallCountMatchesExpectations(\n [[]], // I'm not sure what this was supposed to mean\n this.callCount,\n 'property',\n this.property,\n []);\n\n return context.proxy;\n }\n}"]} \ No newline at end of file +{"version":3,"file":"GetPropertyState.js","sourceRoot":"","sources":["../../../src/states/GetPropertyState.ts"],"names":[],"mappings":";;AAEA,iDAAgD;AAIhD,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAuBI,0BAAoB,SAAsB;QAAtB,cAAS,GAAT,SAAS,CAAa;QACtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IACxB,CAAC;IApBD,sBAAY,wCAAU;aAAtB;YACI,OAAO,CAAC,CAAC,IAAI,CAAC,cAAc,CAAA;QAChC,CAAC;;;OAAA;IAED,sBAAW,sCAAQ;aAAnB;YACI,OAAO,IAAI,CAAC,SAAS,CAAC;QAC1B,CAAC;;;OAAA;IAED,sBAAW,uCAAS;aAApB;YACI,OAAO,IAAI,CAAC,UAAU,CAAC;QAC3B,CAAC;;;OAAA;IAED,sBAAW,2CAAa;aAAxB;YACI,OAAO,IAAI,CAAC,cAAc,CAAA;QAC9B,CAAC;;;OAAA;IAQD,gCAAK,GAAL,UAAM,OAAgB,EAAE,IAAW;QAC/B,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QAEpB,IAAI,IAAI,CAAC,aAAa,EAAE;YACpB,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,aAAa,CAAA;YAClC,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAA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AAG,CAAC,eAAe,EAAE;YACjB,IAAI,CAAC,UAAU,EAAE,CAAC;YAElB,IAAG,IAAI,CAAC,OAAO;gBACX,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAE5C,IAAG,IAAI,CAAC,OAAO,KAAK,OAAO,EAAE;gBACzB,IAAI,YAAY,GAAG,IAAI,CAAC,OAAgB,CAAC;gBACzC,IAAG,YAAY,CAAC,MAAM,KAAK,CAAC;oBACxB,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC;gBAE3B,OAAO,YAAY,CAAC,IAAI,CAAC,UAAU,GAAC,CAAC,CAAC,CAAC;aAC1C;SACJ;QAED,OAAO,CAAC,YAAY,CAAC,kCAAkC,CACnD,CAAC,EAAE,CAAC,EAAG,8CAA8C;QACrD,IAAI,CAAC,SAAS,EACd,UAAU,EACV,IAAI,CAAC,QAAQ,EACb,EAAE,CAAC,CAAC;QAER,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,uBAAC;AAAD,CAAC,AArGD,IAqGC;AArGY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\nimport { Context } from \"src/Context\";\nimport { FunctionState } from \"./FunctionState\";\nimport { areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\nimport { Arg } from \"../Arguments\";\n\nconst Nothing = Symbol();\n\nexport class GetPropertyState implements ContextState {\n private returns: any[]|Symbol;\n private mimicks: Function|null;\n\n private _callCount: number;\n private _functionState?: FunctionState;\n\n private get isFunction(): boolean {\n return !!this._functionState\n }\n\n public get property() {\n return this._property;\n }\n\n public get callCount() {\n return this._callCount;\n }\n\n public get functionState(): FunctionState | undefined {\n return this._functionState\n }\n\n constructor(private _property: PropertyKey) {\n this.returns = Nothing;\n this.mimicks = null;\n this._callCount = 0;\n }\n\n apply(context: Context, args: any[]) {\n this._callCount = 0;\n\n if (this.functionState) {\n context.state = this.functionState\n return this.functionState.apply(context, args);\n }\n\n var functionState = new FunctionState(this);\n context.state = functionState;\n this._functionState = functionState\n\n return context.apply(args);\n }\n\n set(context: Context, property: PropertyKey, value: any) {\n }\n\n get(context: Context, property: PropertyKey) {\n const hasExpectations = context.initialState.hasExpectations;\n\n if (property === 'then')\n return void 0;\n\n if(this.isFunction)\n return context.proxy;\n\n if(property === 'mimicks') {\n return (input: Function) => {\n this.mimicks = input;\n this._callCount--;\n\n context.state = context.initialState;\n }\n }\n\n if(property === 'returns') {\n if(this.returns !== Nothing)\n throw new Error('The return value for the property ' + this._property.toString() + ' has already been set to ' + this.returns);\n\n return (...returns: any[]) => {\n this.returns = returns;\n this._callCount--;\n\n context.state = context.initialState;\n };\n }\n\n if(!hasExpectations) {\n this._callCount++;\n\n if(this.mimicks)\n return this.mimicks.apply(this.mimicks);\n\n if(this.returns !== Nothing) {\n var returnsArray = this.returns as any[];\n if(returnsArray.length === 1)\n return returnsArray[0];\n \n return returnsArray[this._callCount-1];\n }\n }\n\n context.initialState.assertCallCountMatchesExpectations(\n [[]], // I'm not sure what this was supposed to mean\n this.callCount,\n 'property',\n this.property,\n []);\n\n return context.proxy;\n }\n}"]} \ No newline at end of file diff --git a/spec/index.test.ts b/spec/index.test.ts index c02bf02..edbdfd4 100644 --- a/spec/index.test.ts +++ b/spec/index.test.ts @@ -266,12 +266,38 @@ test('return with more arguments is not matched fails', t => { initialize() substitute.bar(1, 2).returns(3) t.is(3, substitute.bar(1, 2)) - t.is(void 0, substitute.bar(1)) + t.is('function', typeof(substitute.bar(1))) }) -test('return with less arguments is not matched', t => { +test('return with less arguments is not matched', t => { initialize() substitute.bar(1).returns(3) t.is(3, substitute.bar(1)) - t.is(void 0, substitute.bar(1, 2)) + t.is('function', typeof(substitute.bar(1, 2).toString)) +}) + +test('can stub multiple primitive return values', t => { + initialize() + substitute.bar(1).returns(2) + substitute.bar(2).returns(3) + t.is(2, substitute.bar(1)) + t.is(3, substitute.bar(2)) +}) + +test('can stub multiple Arg values', t => { + initialize() + substitute.bar(Arg.is(x => x % 2 === 0)).returns(1) + substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2) + t.is(1, substitute.bar(4)) + t.is(2, substitute.bar(5)) +}) + + +test.skip('can stub primitive & Arg values', t => { + initialize() + substitute.bar(1).returns(2) + substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function' + t.is(5, substitute.bar(2)) + t.is(2, substitute.bar(1)) + t.is(3, substitute.bar(2)) }) \ No newline at end of file diff --git a/src/Utilities.ts b/src/Utilities.ts index d2efad4..58896c7 100644 --- a/src/Utilities.ts +++ b/src/Utilities.ts @@ -31,13 +31,14 @@ export function stringifyCalls(calls: Call[]) { }; export function areArgumentsEqual(a: any, b: any) { - if(a instanceof AllArguments || b instanceof AllArguments) - return true; - + if(a instanceof Argument && b instanceof Argument) { - throw new Error("`Argument` should only be used to set up value or verify, not in the implementation.") + return false; } + if(a instanceof AllArguments || b instanceof AllArguments) + return true; + if(a instanceof Argument) return a.matches(b); diff --git a/src/states/FunctionState.ts b/src/states/FunctionState.ts index ab07da3..0691b9f 100644 --- a/src/states/FunctionState.ts +++ b/src/states/FunctionState.ts @@ -62,7 +62,6 @@ export class FunctionState implements ContextState { if(!this.returns.length) return context.proxy; - const returns = this.returns.find(r => areArgumentArraysEqual(r.args, args)) if (returns) { @@ -77,7 +76,7 @@ export class FunctionState implements ContextState { //throw Error(`${String(this._getPropertyState.property)} with ${stringifyArguments(returns.args)} called ${returns.returnIndex + 1} times, but only ${returnValues.length} return values were set up`) } } - return void 0 + return context.proxy } set(context: Context, property: PropertyKey, value: any) { @@ -97,10 +96,6 @@ export class FunctionState implements ContextState { } if(property === 'returns') { - if (this.returns.length) // I don't think this can happen -- domasx2 - throw new Error('BUT HOW?') - //throw new Error('The return value for the function ' + this._getPropertyState.toString() + ' with ' + stringifyArguments(this._arguments) + ' has already been set to ' + this.returns); - return (...returns: any[]) => { if (!this._lastArgs) { throw new Error('Eh, there\'s a bug, no args recorded for this return :/') diff --git a/src/states/GetPropertyState.ts b/src/states/GetPropertyState.ts index 0eeacc4..ee89cf8 100644 --- a/src/states/GetPropertyState.ts +++ b/src/states/GetPropertyState.ts @@ -39,6 +39,7 @@ export class GetPropertyState implements ContextState { this._callCount = 0; if (this.functionState) { + context.state = this.functionState return this.functionState.apply(context, args); }