diff --git a/README.md b/README.md index 1cdaeae..007a7b7 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ console.log(fakeCalculator.divide(9, 5)); //prints 1338 - Doesn't rely on object instances - you can produce a strong-typed fake from nothing, ensuring that everything is mocked. # Beware +## Names that conflict with Substitute.js Let's say we have a class with a method called `received`, `didNotReceive` or `mimick` keyword - how do we mock it? Simple! We disable the proxy methods temporarily while invoking the method by using the `disableFor` method which disables these special methods. @@ -167,4 +168,18 @@ Substitute.disableFor(fake).received(1337); //now we can assert that we received a call to the "received" method. fake.received().received(1337); +``` + +## Strict mode +If you have `strict` set to `true` in your `tsconfig.json`, you may need to toggle off strict null checks. The framework does not currently support this. + +However, it is only needed for your test projects anyway. + +```json +{ + "compilerOptions": { + "strict": true, + "strictNullChecks": false + } +} ``` \ No newline at end of file diff --git a/dist/spec/index.test.d.ts b/dist/spec/index.test.d.ts index 5df72da..edf5a6a 100644 --- a/dist/spec/index.test.d.ts +++ b/dist/spec/index.test.d.ts @@ -4,9 +4,9 @@ export declare class Example { a: string; c(arg1: string, arg2: string): string; readonly d: number; - v: string; + v: string | null | undefined; received(stuff: number | string): void; returnPromise(): Promise; - foo(): string; + foo(): string | undefined | null; } export {}; diff --git a/dist/spec/index.test.js.map b/dist/spec/index.test.js.map index 903a9f4..42180f5 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,iBAkPG;;AAlPH,2BAAuB;AAEvB,sCAA+C;AAC/C,8CAAqD;AAGrD;IAAA;IAEA,CAAC;IAAD,YAAC;AAAD,CAAC,AAFD,IAEC;AAED;IAAA;QACC,MAAC,GAAG,MAAM,CAAC;IAwBZ,CAAC;IAtBA,mBAAC,GAAD,UAAE,IAAY,EAAE,IAAY;QAC3B,OAAO,QAAQ,GAAG,IA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AAC;AACjC,CAAC,CAAC,CAAC;AAEH,aAAI,CAAC,qBAAqB,EAAE,UAAA,CAAC;IAC5B,UAAU,EAAE,CAAC;IAEb,CAAC,CAAC,IAAI,CAAC,6BAAiB,CAAC,WAAG,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;IAC3C,CAAC,CAAC,IAAI,CAAC,6BAAiB,CAAC,WAAG,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAE5D,CAAC,CAAC,KAAK,CAAC,6BAAiB,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC,KAAK,CAAC,6BAAiB,CAAC,WAAG,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AACpD,CAAC,CAAC,CAAC","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) {\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() {\r\n\t\treturn 'stuff';\r\n\t}\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-> 1 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-> 1 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\r\n\tt.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-> 4 calls with arguments ['hi', 'there']\r\n-> 1 call with arguments ['hi', 'the1re']`);\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-> 1 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});"]} \ No newline at end of file +{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../spec/index.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iBAkPG;;AAlPH,2BAAuB;AAEvB,sCAA+C;AAC/C,8CAAqD;AAGrD;IAAA;IAEA,CAAC;IAAD,YAAC;AAAD,CAAC,AAFD,IAEC;AAED;IAAA;QACC,MAAC,GAAG,MAAM,CAAC;IAwBZ,CAAC;IAtBA,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;IAChB,CAAC;IACF,cAAC;AAAD,CAAC,A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AK,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC,KAAK,CAAC,6BAAiB,CAAC,WAAG,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AACpD,CAAC,CAAC,CAAC","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\t}\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-> 1 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-> 1 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\r\n\tt.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-> 4 calls with arguments ['hi', 'there']\r\n-> 1 call with arguments ['hi', 'the1re']`);\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-> 1 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});"]} \ No newline at end of file diff --git a/dist/src/Arguments.d.ts b/dist/src/Arguments.d.ts index 9f98d5e..7833be9 100644 --- a/dist/src/Arguments.d.ts +++ b/dist/src/Arguments.d.ts @@ -12,13 +12,13 @@ export declare class AllArguments extends Argument { } export declare class Arg { static all(): AllArguments; - static any(): any; + static any(): Argument & any; static any(type: T): Argument & string; static any(type: T): Argument & number; static any(type: T): Argument & boolean; static any(type: T): Argument & any[]; static any(type: T): Argument & Function; - static any(type: T): any; - static is(predicate: (input: T) => boolean): Argument & T; + static any(type: T): Argument & any; + static is(predicate: (input: any) => boolean): Argument & T; private static toStringify; } diff --git a/dist/src/Arguments.js.map b/dist/src/Arguments.js.map index 05467f7..efc5893 100644 --- a/dist/src/Arguments.js.map +++ b/dist/src/Arguments.js.map @@ -1 +1 @@ -{"version":3,"file":"Arguments.js","sourceRoot":"","sources":["../../src/Arguments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAGI,kBACY,WAAmB,EACnB,gBAAqC;QADrC,gBAAW,GAAX,WAAW,CAAQ;QACnB,qBAAgB,GAAhB,gBAAgB,CAAqB;QAE7C,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;IAChC,CAAC;IAED,0BAAO,GAAP,UAAQ,GAAM;QACV,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;IAED,2BAAQ,GAAR;QACI,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IAED,0BAAO,GAAP;QACI,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IACL,eAAC;AAAD,CAAC,AArBD,IAqBC;AArBY,4BAAQ;AAuBrB;IAAkC,gCAAa;IAC3C;eACI,kBAAM,OAAO,EAAE,cAAM,OAAA,IAAI,EAAJ,CAAI,CAAC;IAC9B,CAAC;IACL,mBAAC;AAAD,CAAC,AAJD,CAAkC,QAAQ,GAIzC;AAJY,oCAAY;AAMzB;IAAA;IAyCA,CAAC;IAxCU,OAAG,GAAV;QACI,OAAO,IAAI,YAAY,EAAE,CAAC;IAC9B,CAAC;IASM,OAAG,GAAV,UAAW,IAAa;QACpB,IAAM,WAAW,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ,GAAG,IAAI,GAAG,GAAG,CAAC;QAChE,OAAO,IAAI,QAAQ,CAAM,WAAW,EAAE,UAAA,CAAC;YACnC,IAAG,CAAC,IAAI;gBACJ,OAAO,IAAI,CAAC;YAEhB,IAAG,OAAO,CAAC,KAAK,WAAW;gBACvB,OAAO,IAAI,CAAC;YAEhB,IAAG,IAAI,KAAK,OAAO;gBACf,OAAO,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAEjC,OAAO,OAAO,CAAC,KAAK,IAAI,CAAC;QAC7B,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,MAAE,GAAT,UAAa,SAAgC;QACzC,OAAO,IAAI,QAAQ,CAAI,aAAa,GAAG,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,GAAG,EAAE,SAAS,CAAQ,CAAC;IAChG,CAAC;IAEc,eAAW,GAA1B,UAA2B,GAAQ;QAC/B,IAAG,OAAO,GAAG,CAAC,OAAO,KAAK,UAAU;YAChC,OAAO,GAAG,CAAC,OAAO,EAAE,CAAC;QAEzB,IAAG,OAAO,GAAG,CAAC,QAAQ,KAAK,UAAU;YACjC,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;QAE1B,OAAO,GAAG,CAAC;IACf,CAAC;IACL,UAAC;AAAD,CAAC,AAzCD,IAyCC;AAzCY,kBAAG","sourcesContent":["export class Argument {\r\n encounteredValues: any[];\r\n\r\n constructor(\r\n private description: string,\r\n private matchingFunction: (arg: T) => boolean\r\n ) {\r\n this.encounteredValues = [];\r\n }\r\n\r\n matches(arg: T) {\r\n return this.matchingFunction(arg);\r\n }\r\n\r\n toString() {\r\n return this.description;\r\n }\r\n\r\n inspect() {\r\n return this.description;\r\n }\r\n}\r\n\r\nexport class AllArguments extends Argument {\r\n constructor() {\r\n super('{all}', () => true);\r\n }\r\n}\r\n\r\nexport class Arg {\r\n static all() {\r\n return new AllArguments();\r\n }\r\n\r\n static any()\r\n static any(type: T): Argument & string\r\n static any(type: T): Argument & number\r\n static any(type: T): Argument & boolean\r\n static any(type: T): Argument & any[]\r\n static any(type: T): Argument & Function\r\n static any(type: T)\r\n static any(type?: string): Argument & any {\r\n const description = !type ? '{any arg}' : '{type ' + type + '}';\r\n return new Argument(description, x => {\r\n if(!type)\r\n return true;\r\n\r\n if(typeof x === 'undefined')\r\n return true;\r\n\r\n if(type === 'array')\r\n return x && Array.isArray(x);\r\n\r\n return typeof x === type;\r\n });\r\n }\r\n\r\n static is(predicate: (input: T) => boolean): Argument & T {\r\n return new Argument('{predicate ' + this.toStringify(predicate) + '}', predicate) as any;\r\n }\r\n\r\n private static toStringify(obj: any) {\r\n if(typeof obj.inspect === 'function')\r\n return obj.inspect();\r\n\r\n if(typeof obj.toString === 'function')\r\n return obj.toString();\r\n\r\n return obj;\r\n }\r\n}"]} \ No newline at end of file +{"version":3,"file":"Arguments.js","sourceRoot":"","sources":["../../src/Arguments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAGI,kBACY,WAAmB,EACnB,gBAAqC;QADrC,gBAAW,GAAX,WAAW,CAAQ;QACnB,qBAAgB,GAAhB,gBAAgB,CAAqB;QAE7C,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;IAChC,CAAC;IAED,0BAAO,GAAP,UAAQ,GAAM;QACV,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;IAED,2BAAQ,GAAR;QACI,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IAED,0BAAO,GAAP;QACI,OAAO,IAAI,CAAC,W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;IACf,CAAC;IACL,UAAC;AAAD,CAAC,AAzCD,IAyCC;AAzCY,kBAAG","sourcesContent":["export class Argument {\r\n encounteredValues: any[];\r\n\r\n constructor(\r\n private description: string,\r\n private matchingFunction: (arg: T) => boolean\r\n ) {\r\n this.encounteredValues = [];\r\n }\r\n\r\n matches(arg: T) {\r\n return this.matchingFunction(arg);\r\n }\r\n\r\n toString() {\r\n return this.description;\r\n }\r\n\r\n inspect() {\r\n return this.description;\r\n }\r\n}\r\n\r\nexport class AllArguments extends Argument {\r\n constructor() {\r\n super('{all}', () => true);\r\n }\r\n}\r\n\r\nexport class Arg {\r\n static all() {\r\n return new AllArguments();\r\n }\r\n\r\n static any(): Argument & any\r\n static any(type: T): Argument & string\r\n static any(type: T): Argument & number\r\n static any(type: T): Argument & boolean\r\n static any(type: T): Argument & any[]\r\n static any(type: T): Argument & Function\r\n static any(type: T): Argument & any\r\n static any(type?: string): Argument & any {\r\n const description = !type ? '{any arg}' : '{type ' + type + '}';\r\n return new Argument(description, x => {\r\n if(!type)\r\n return true;\r\n\r\n if(typeof x === 'undefined')\r\n return true;\r\n\r\n if(type === 'array')\r\n return x && Array.isArray(x);\r\n\r\n return typeof x === type;\r\n });\r\n }\r\n\r\n static is(predicate: (input: any) => boolean): Argument & T {\r\n return new Argument('{predicate ' + this.toStringify(predicate) + '}', predicate) as Argument & T;\r\n }\r\n\r\n private static toStringify(obj: any) {\r\n if(typeof obj.inspect === 'function')\r\n return obj.inspect();\r\n\r\n if(typeof obj.toString === 'function')\r\n return obj.toString();\r\n\r\n return obj;\r\n }\r\n}"]} \ No newline at end of file diff --git a/dist/src/Transformations.d.ts b/dist/src/Transformations.d.ts index 3a5a3e7..c04bf9f 100644 --- a/dist/src/Transformations.d.ts +++ b/dist/src/Transformations.d.ts @@ -1,7 +1,7 @@ import { AllArguments } from "./Arguments"; export declare type NoArgumentFunctionSubstitute = (() => (TReturnType & NoArgumentMockObjectMixin)); export declare type FunctionSubstitute = ((...args: TArguments) => (TReturnType & MockObjectMixin)) & ((allArguments: AllArguments) => (TReturnType & MockObjectMixin)); -export declare type PropertySubstitute = TReturnType & Partial>; +export declare type PropertySubstitute = (TReturnType & Partial>); declare type BaseMockObjectMixin = { returns: (...args: TReturnType[]) => void; }; diff --git a/dist/src/Transformations.js.map b/dist/src/Transformations.js.map index 9939a3c..05ea9ee 100644 --- a/dist/src/Transformations.js.map +++ b/dist/src/Transformations.js.map @@ -1 +1 @@ -{"version":3,"file":"Transformations.js","sourceRoot":"","sources":["../../src/Transformations.ts"],"names":[],"mappings":"","sourcesContent":["import { AllArguments } from \"./Arguments\";\r\n\r\nexport type NoArgumentFunctionSubstitute = \r\n (() => (TReturnType & NoArgumentMockObjectMixin))\r\n\r\nexport type FunctionSubstitute = \r\n ((...args: TArguments) => (TReturnType & MockObjectMixin)) & \r\n ((allArguments: AllArguments) => (TReturnType & MockObjectMixin))\r\n\r\nexport type PropertySubstitute = TReturnType & Partial>\r\n\r\ntype BaseMockObjectMixin = {\r\n returns: (...args: TReturnType[]) => void;\r\n}\r\n\r\ntype NoArgumentMockObjectMixin = BaseMockObjectMixin & {\r\n mimicks: (func: () => TReturnType) => void;\r\n}\r\n\r\ntype MockObjectMixin = BaseMockObjectMixin & {\r\n mimicks: (func: (...args: TArguments) => TReturnType) => void;\r\n}\r\n\r\nexport type ObjectSubstitute = ObjectSubstituteTransformation & {\r\n received(amount?: number): TerminatingObject;\r\n didNotReceive(amount?: number): TerminatingObject;\r\n mimick(instance: T): void;\r\n}\r\n\r\ntype TerminatingObject = {\r\n [P in keyof T]:\r\n T[P] extends () => infer R ? () => void :\r\n T[P] extends (...args: infer F) => infer R ? (...args: F) => void : \r\n T[P];\r\n}\r\n\r\ntype ObjectSubstituteTransformation = {\r\n [P in keyof T]:\r\n T[P] extends () => infer R ? NoArgumentFunctionSubstitute :\r\n T[P] extends (...args: infer F) => infer R ? FunctionSubstitute :\r\n PropertySubstitute;\r\n}\r\n\r\ntype Omit = Pick>;\r\n\r\nexport type OmitProxyMethods = Omit;\r\n\r\nexport type DisabledSubstituteObject = T extends ObjectSubstitute, infer K> ? K : never;"]} \ No newline at end of file +{"version":3,"file":"Transformations.js","sourceRoot":"","sources":["../../src/Transformations.ts"],"names":[],"mappings":"","sourcesContent":["import { AllArguments } from \"./Arguments\";\r\n\r\nexport type NoArgumentFunctionSubstitute = \r\n (() => (TReturnType & NoArgumentMockObjectMixin))\r\n\r\nexport type FunctionSubstitute = \r\n ((...args: TArguments) => (TReturnType & MockObjectMixin)) & \r\n ((allArguments: AllArguments) => (TReturnType & MockObjectMixin))\r\n\r\nexport type PropertySubstitute = (TReturnType & Partial>);\r\n\r\ntype BaseMockObjectMixin = {\r\n returns: (...args: TReturnType[]) => void;\r\n}\r\n\r\ntype NoArgumentMockObjectMixin = BaseMockObjectMixin & {\r\n mimicks: (func: () => TReturnType) => void;\r\n}\r\n\r\ntype MockObjectMixin = BaseMockObjectMixin & {\r\n mimicks: (func: (...args: TArguments) => TReturnType) => void;\r\n}\r\n\r\nexport type ObjectSubstitute = ObjectSubstituteTransformation & {\r\n received(amount?: number): TerminatingObject;\r\n didNotReceive(amount?: number): TerminatingObject;\r\n mimick(instance: T): void;\r\n}\r\n\r\ntype TerminatingObject = {\r\n [P in keyof T]:\r\n T[P] extends () => infer R ? () => void :\r\n T[P] extends (...args: infer F) => infer R ? (...args: F) => void : \r\n T[P];\r\n}\r\n\r\ntype ObjectSubstituteTransformation = {\r\n [P in keyof T]:\r\n T[P] extends () => infer R ? NoArgumentFunctionSubstitute :\r\n T[P] extends (...args: infer F) => infer R ? FunctionSubstitute :\r\n PropertySubstitute;\r\n}\r\n\r\ntype Omit = Pick>;\r\n\r\nexport type OmitProxyMethods = Omit;\r\n\r\nexport type DisabledSubstituteObject = T extends ObjectSubstitute, infer K> ? K : never;"]} \ No newline at end of file diff --git a/dist/src/states/FunctionState.js b/dist/src/states/FunctionState.js index 0678ebe..1325401 100644 --- a/dist/src/states/FunctionState.js +++ b/dist/src/states/FunctionState.js @@ -20,8 +20,9 @@ var FunctionState = /** @class */ (function () { args[_i - 1] = arguments[_i]; } this._getPropertyState = _getPropertyState; - this._arguments = args; this.returns = Nothing; + this.mimicks = null; + this._arguments = args; this._callCount = 0; } Object.defineProperty(FunctionState.prototype, "arguments", { diff --git a/dist/src/states/FunctionState.js.map b/dist/src/states/FunctionState.js.map index 0265cd8..84a0e5d 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,0CAAwC;AAExC,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAmBI,uBAAoB,iBAAmC;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QAAnD,sBAAiB,GAAjB,iBAAiB,CAAkB;QACnD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IACxB,CAAC;IAhBD,sBAAW,oCAAS;aAApB;YACI,OAAO,IAAI,CAAC,UAAU,CAAC;QAC3B,CAAC;;;OAAA;IAED,sBAAW,oCAAS;aAApB;YACI,OAAO,IAAI,CAAC,UAAU,CAAC;QAC3B,CAAC;;;OAAA;IAED,sBAAW,mCAAQ;aAAnB;YACI,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC;QAC3C,CAAC;;;OAAA;IAQD,6BAAK,GAAL,UAAM,OAAgB,EAAE,IAAW;;QAC/B,IAAI,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC;QAChC,IAAM,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC;QAC7D,IAAM,sBAAsB,GAAG,IAAI,CAAC,iBAAiB;aAChD,sBAAsB;aACtB,MA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AC;SACL;QAED,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,oBAAC;AAAD,CAAC,AA1GD,IA0GC;AA1GY,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 } 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;\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._arguments = args;\r\n this.returns = Nothing;\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n const matchingFunctionStates = this._getPropertyState\r\n .recordedFunctionStates\r\n .filter(x => areArgumentArraysEqual(x.arguments, args));\r\n if(hasExpectations) {\r\n callCount = matchingFunctionStates\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 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,0CAAwC;AAExC,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAmBI,uBAAoB,iBAAmC;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QAAnD,sBAAiB,GAAjB,iBAAiB,CAAkB;QACnD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QAEpB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IACxB,CAAC;IAlBD,sBAAW,oCAAS;aAApB;YACI,OAAO,IAAI,CAAC,UAAU,CAAC;QAC3B,CAAC;;;OAAA;IAED,sBAAW,oCAAS;aAAp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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,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;YACzC,CAAC,CAAC;SACL;QAED,OAAO,OAAO,CAAC,KAAK,CAAC;IACzB,CAAC;IACL,oBAAC;AAAD,CAAC,AA5GD,IA4GC;AA5GY,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 } 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[]) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n const matchingFunctionStates = this._getPropertyState\r\n .recordedFunctionStates\r\n .filter(x => areArgumentArraysEqual(x.arguments, args));\r\n if(hasExpectations) {\r\n callCount = matchingFunctionStates\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 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 3470ad1..4707cd5 100644 --- a/dist/src/states/GetPropertyState.js +++ b/dist/src/states/GetPropertyState.js @@ -27,6 +27,7 @@ var GetPropertyState = /** @class */ (function () { function GetPropertyState(_property) { this._property = _property; this.returns = Nothing; + this.mimicks = null; this._recordedFunctionStates = []; this._callCount = 0; } @@ -100,9 +101,8 @@ var GetPropertyState = /** @class */ (function () { } if (!hasExpectations) { this._callCount++; - if (this.mimicks) { + if (this.mimicks) return this.mimicks.apply(this.mimicks); - } if (this.returns !== Nothing) { var returnsArray = this.returns; if (returnsArray.length === 1) diff --git a/dist/src/states/GetPropertyState.js.map b/dist/src/states/GetPropertyState.js.map index 5257491..b22a9ab 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"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,iDAAgD;AAChD,0CAAyE;AAEzE,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,uBAAuB,GAAG,EAAE,CAAC;QAClC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IACxB,CAAC;IApBD,sBAAY,wCAAU;aAAtB;YACI,OAAO,IAAI,CAAC,uBAAuB,CAAC,MAAM,GAAG,CAAC,CAAC;QACnD,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,oDAAsB;aAAjC;YACI,gBAAW,IAAI,CAAC,uBAAuB,EAAE;QAC7C,CAAC;;;OAAA;IAQD,gCAAK,GAAL,UAAM,OAAgB,EAAE,IAAW;QAC/B,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QAEpB,IAAM,qBAAqB,GAAG,IAAI,CAA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AAC;YAEnI,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,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;YACzC,CAAC,CAAC;SACL;QAED,IAAG,CAAC,eAAe,EAAE;YACjB,IAAI,CAAC,UAAU,EAAE,CAAC;YAElB,IAAG,IAAI,CAAC,OAAO,EAAE;gBACb,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC3C;YAED,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,OAAO,CAAC,YAAY,CAAC,iBAAiB,EACtC,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,AAvGD,IAuGC;AAvGY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { FunctionState } from \"./FunctionState\";\r\nimport { areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class GetPropertyState implements ContextState {\r\n private returns: any[]|Symbol;\r\n private mimicks: Function;\r\n\r\n private _callCount: number;\r\n private _recordedFunctionStates: FunctionState[];\r\n\r\n private get isFunction() {\r\n return this._recordedFunctionStates.length > 0;\r\n }\r\n\r\n public get property() {\r\n return this._property;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n public get recordedFunctionStates() {\r\n return [...this._recordedFunctionStates];\r\n }\r\n\r\n constructor(private _property: PropertyKey) {\r\n this.returns = Nothing;\r\n this._recordedFunctionStates = [];\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n this._callCount = 0;\r\n\r\n const matchingFunctionState = this._recordedFunctionStates.find(x => areArgumentArraysEqual(x.arguments, args));\r\n if(matchingFunctionState) {\r\n return matchingFunctionState.apply(context, args);\r\n }\r\n\r\n var functionState = new FunctionState(this, ...args);\r\n context.state = functionState;\r\n\r\n this._recordedFunctionStates.push(functionState);\r\n\r\n return context.apply(args);\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 const hasExpectations = context.initialState.hasExpectations;\r\n\r\n if (property === 'then')\r\n return void 0;\r\n\r\n if(this.isFunction)\r\n return context.proxy;\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 property ' + this._property.toString() + ' 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 context.state = context.initialState;\r\n };\r\n }\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n\r\n if(this.mimicks) {\r\n return this.mimicks.apply(this.mimicks);\r\n }\r\n\r\n if(this.returns !== Nothing) {\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\r\n context.initialState.assertCallCountMatchesExpectations(\r\n context.initialState.getPropertyStates,\r\n this.callCount,\r\n 'property',\r\n this.property,\r\n []);\r\n\r\n return context.proxy;\r\n }\r\n}"]} \ No newline at end of file +{"version":3,"file":"GetPropertyState.js","sourceRoot":"","sources":["../../../src/states/GetPropertyState.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,iDAAgD;AAChD,0CAAyE;AAEzE,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;QAEpB,IAAI,CAAC,uBAAuB,GAAG,EAAE,CAAC;QAClC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;IACxB,CAAC;IAtBD,sBAAY,wCAAU;aAAtB;YACI,OAAO,IAAI,CAAC,uBAAuB,CAAC,MAAM,GAAG,CAAC,CAAC;QACnD,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,oDAAsB;aAAjC;YACI,gBAAW,IAAI,CAAC,uBAAuB,EAAE;QAC7C,CAAC;;;OAAA;IAUD,gCAAK,GAAL,UAAM,OAAgB,EAAE,IAAW;QAC/B,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QAEpB,IAAM,qBAAqB,GAAG,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAA,CAAC,IAAI,OAAA,kCAAsB,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,EAAzC,CAAyC,CAAC,CAAC;QAChH,IAAG,qBAAqB,EAAE;YACtB,OAAO,qBAAqB,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,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C,YAAY,CAAC;YACzC,CAAC,CAAC;SACL;QAED,IAAG,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,OAAO,CAAC,YAAY,CAAC,iBAAiB,EACtC,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,AAxGD,IAwGC;AAxGY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { FunctionState } from \"./FunctionState\";\r\nimport { areArgumentsEqual, areArgumentArraysEqual } from \"../Utilities\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class GetPropertyState implements ContextState {\r\n private returns: any[]|Symbol;\r\n private mimicks: Function|null;\r\n\r\n private _callCount: number;\r\n private _recordedFunctionStates: FunctionState[];\r\n\r\n private get isFunction() {\r\n return this._recordedFunctionStates.length > 0;\r\n }\r\n\r\n public get property() {\r\n return this._property;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n public get recordedFunctionStates() {\r\n return [...this._recordedFunctionStates];\r\n }\r\n\r\n constructor(private _property: PropertyKey) {\r\n this.returns = Nothing;\r\n this.mimicks = null;\r\n\r\n this._recordedFunctionStates = [];\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n this._callCount = 0;\r\n\r\n const matchingFunctionState = this._recordedFunctionStates.find(x => areArgumentArraysEqual(x.arguments, args));\r\n if(matchingFunctionState) {\r\n return matchingFunctionState.apply(context, args);\r\n }\r\n\r\n var functionState = new FunctionState(this, ...args);\r\n context.state = functionState;\r\n\r\n this._recordedFunctionStates.push(functionState);\r\n\r\n return context.apply(args);\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 const hasExpectations = context.initialState.hasExpectations;\r\n\r\n if (property === 'then')\r\n return void 0;\r\n\r\n if(this.isFunction)\r\n return context.proxy;\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 property ' + this._property.toString() + ' 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 context.state = context.initialState;\r\n };\r\n }\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n\r\n if(this.mimicks)\r\n return this.mimicks.apply(this.mimicks);\r\n\r\n if(this.returns !== Nothing) {\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\r\n context.initialState.assertCallCountMatchesExpectations(\r\n context.initialState.getPropertyStates,\r\n this.callCount,\r\n 'property',\r\n this.property,\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/InitialState.js b/dist/src/states/InitialState.js index aaa517b..750214f 100644 --- a/dist/src/states/InitialState.js +++ b/dist/src/states/InitialState.js @@ -28,6 +28,7 @@ var InitialState = /** @class */ (function () { function InitialState() { this.recordedGetPropertyStates = new Map(); this.recordedSetPropertyStates = []; + this._areProxiesDisabled = false; this._expectedCount = void 0; } Object.defineProperty(InitialState.prototype, "expectedCount", { diff --git a/dist/src/states/InitialState.js.map b/dist/src/states/InitialState.js.map index a65c914..69ca766 100644 --- a/dist/src/states/InitialState.js.map +++ b/dist/src/states/InitialState.js.map @@ -1 +1 @@ -{"version":3,"file":"InitialState.js","sourceRoot":"","sources":["../../../src/states/InitialState.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,uDAAsD;AACtD,uDAAsD;AACtD,0CAAwE;AACxE,4CAAsD;AAEtD;IAuBI;QACI,IAAI,CAAC,yBAAyB,GAAG,IAAI,GAAG,EAAE,CAAC;QAC3C,IAAI,CAAC,yBAAyB,GAAG,EAAE,CAAC;QAEpC,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,CAAC;IACjC,CAAC;IArBD,sBAAW,uCAAa;aAAxB;YACI,OAAO,IAAI,CAAC,cAAc,CAAC;QAC/B,CAAC;;;OAAA;IAED,sBAAW,yCAAe;aAA1B;YACI,OAAO,IAAI,CAAC,cAAc,KAAK,KAAK,CAAC,CAAC;QAC1C,CAAC;;;OAAA;IAED,sBAAW,2CAAiB;aAA5B;YACI,gBAAW,IAAI,CAAC,yBAAyB,EAAE;QAC/C,CAAC;;;OAAA;IAED,sBAAW,2CAAiB;aAA5B;YACI,gBAAW,IAAI,CAAC,yBAAyB,CAAC,MAAM,EAAE,EAAE;QACxD,CAAC;;;OAAA;IASD,yDAAkC,GAAlC,UAAmC,KAAa,EAAE,SAAiB,EAAE,IAAY,EAAE,QAAqB,EAAE,IAAW;QACjH,IAAM,aAAa,GAAG,IAAI,CAAC,cAAc,CAAC;QAE1C,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAEzB,IAAG,IAAI,CAAC,8BAA8B,CAAC,aAAa,EAAE,SAAS,CAAC;YAC5D,OAAO;QAEX,MA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K,aAAa;YAC1B,OAAO,cAAM,OAAA,OAAO,CAAC,SAAS,EAAjB,CAAiB,CAAC;QAEnC,IAAI,QAAQ,KAAK,UAAU,EAAE;YACzB,OAAO,UAAC,KAAc;gBAClB,KAAI,CAAC,cAAc,GAAG,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;gBACtD,OAAO,OAAO,CAAC,KAAK,CAAC;YACzB,CAAC,CAAC;SACL;QAED,IAAM,gBAAgB,GAAG,IAAI,CAAC,yBAAyB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACtE,IAAI,gBAAgB,EAAE;YAClB,OAAO,CAAC,KAAK,GAAG,gBAAgB,CAAC;YACjC,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SAChC;QAED,IAAM,QAAQ,GAAG,IAAI,mCAAgB,CAAC,QAAQ,CAAC,CAAC;QAChD,OAAO,CAAC,KAAK,GAAG,QAAQ,CAAC;QAEzB,IAAI,CAAC,yBAAyB,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAEvD,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;IAEO,wCAAiB,GAAzB;QACI,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,mCAAY,GAAZ;QACI,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IACL,mBAAC;AAAD,CAAC,AAzID,IAyIC;AAzIY,oCAAY","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { GetPropertyState } from \"./GetPropertyState\";\r\nimport { SetPropertyState } from \"./SetPropertyState\";\r\nimport { stringifyArguments, stringifyCalls, Call } from \"../Utilities\";\r\nimport { AreProxiesDisabledKey } from \"../Substitute\";\r\n\r\nexport class InitialState implements ContextState {\r\n private recordedGetPropertyStates: Map;\r\n private recordedSetPropertyStates: SetPropertyState[];\r\n \r\n private _expectedCount: number;\r\n private _areProxiesDisabled: boolean;\r\n\r\n public get expectedCount() {\r\n return this._expectedCount;\r\n }\r\n\r\n public get hasExpectations() {\r\n return this._expectedCount !== void 0;\r\n }\r\n\r\n public get setPropertyStates() {\r\n return [...this.recordedSetPropertyStates];\r\n }\r\n\r\n public get getPropertyStates() {\r\n return [...this.recordedGetPropertyStates.values()];\r\n }\r\n\r\n constructor() {\r\n this.recordedGetPropertyStates = new Map();\r\n this.recordedSetPropertyStates = [];\r\n\r\n this._expectedCount = void 0;\r\n }\r\n\r\n assertCallCountMatchesExpectations(calls: Call[], callCount: number, type: string, property: PropertyKey, args: any[]) {\r\n const expectedCount = this._expectedCount;\r\n\r\n this.clearExpectations();\r\n\r\n if(this.doesCallCountMatchExpectations(expectedCount, callCount))\r\n return;\r\n\r\n throw new Error('Expected ' + expectedCount + ' call' + (expectedCount === 1 ? '' : 's') + ' to the ' + type + ' ' + property.toString() + ' with ' + stringifyArguments(args) + ', but received ' + (callCount === 0 ? 'none' : callCount) + ' of such call' + (callCount === 1 ? '' : 's') + '.\\nAll calls received to ' + type + ' ' + property.toString() + ':' + stringifyCalls(calls));\r\n }\r\n\r\n private doesCallCountMatchExpectations(expectedCount: number, actualCount: number) {\r\n if (expectedCount === void 0)\r\n return true;\r\n\r\n if (expectedCount === null && actualCount > 0)\r\n return true;\r\n\r\n return expectedCount === actualCount;\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n if(property === AreProxiesDisabledKey) {\r\n this._areProxiesDisabled = value;\r\n return;\r\n }\r\n\r\n const existingSetState = this.recordedSetPropertyStates.find(x => x.arguments[0] === value);;\r\n if (existingSetState) {\r\n return existingSetState.set(context, property, value);\r\n }\r\n\r\n const setPropertyState = new SetPropertyState(property, value);\r\n context.state = setPropertyState;\r\n\r\n this.recordedSetPropertyStates.push(setPropertyState);\r\n\r\n setPropertyState.set(context, property, value);\r\n }\r\n\r\n get(context: Context, property: PropertyKey) {\r\n if (typeof property === 'symbol') {\r\n if(property === AreProxiesDisabledKey)\r\n return this._areProxiesDisabled;\r\n\r\n if (property === Symbol.toPrimitive)\r\n return () => '{SubstituteJS fake}';\r\n\r\n if (property === Symbol.iterator)\r\n return void 0;\r\n\r\n if (property === Symbol.toStringTag)\r\n return 'Substitute';\r\n\r\n if(property.toString() === 'Symbol(util.inspect.custom)')\r\n return void 0;\r\n }\r\n\r\n if (property === 'valueOf')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === '$$typeof')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'length')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'toString')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'inspect')\r\n return () => '{SubstituteJS fake}';\r\n\r\n if (property === 'constructor')\r\n return () => context.rootProxy;\r\n\r\n if (property === 'received') {\r\n return (count?: number) => {\r\n this._expectedCount = count === void 0 ? null : count;\r\n return context.proxy;\r\n };\r\n }\r\n\r\n const existingGetState = this.recordedGetPropertyStates.get(property);\r\n if (existingGetState) {\r\n context.state = existingGetState;\r\n return context.get(property);\r\n }\r\n\r\n const getState = new GetPropertyState(property);\r\n context.state = getState;\r\n\r\n this.recordedGetPropertyStates.set(property, getState);\r\n\r\n return context.get(property);\r\n }\r\n\r\n private clearExpectations() {\r\n this._expectedCount = void 0;\r\n }\r\n\r\n onSwitchedTo() {\r\n this.clearExpectations();\r\n }\r\n}"]} \ No newline at end of file +{"version":3,"file":"InitialState.js","sourceRoot":"","sources":["../../../src/states/InitialState.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,uDAAsD;AACtD,uDAAsD;AACtD,0CAAwE;AACxE,4CAAsD;AAEtD;IAuBI;QACI,IAAI,CAAC,yBAAyB,GAAG,IAAI,GAAG,EAAE,CAAC;QAC3C,IAAI,CAAC,yBAAyB,GAAG,EAAE,CAAC;QA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AC,QAAQ,CAAC,CAAC;SAChC;QAED,IAAM,QAAQ,GAAG,IAAI,mCAAgB,CAAC,QAAQ,CAAC,CAAC;QAChD,OAAO,CAAC,KAAK,GAAG,QAAQ,CAAC;QAEzB,IAAI,CAAC,yBAAyB,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAEvD,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;IAEO,wCAAiB,GAAzB;QACI,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,mCAAY,GAAZ;QACI,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IACL,mBAAC;AAAD,CAAC,AA1ID,IA0IC;AA1IY,oCAAY","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { GetPropertyState } from \"./GetPropertyState\";\r\nimport { SetPropertyState } from \"./SetPropertyState\";\r\nimport { stringifyArguments, stringifyCalls, Call } from \"../Utilities\";\r\nimport { AreProxiesDisabledKey } from \"../Substitute\";\r\n\r\nexport class InitialState implements ContextState {\r\n private recordedGetPropertyStates: Map;\r\n private recordedSetPropertyStates: SetPropertyState[];\r\n \r\n private _expectedCount: number|undefined|null;\r\n private _areProxiesDisabled: boolean;\r\n\r\n public get expectedCount() {\r\n return this._expectedCount;\r\n }\r\n\r\n public get hasExpectations() {\r\n return this._expectedCount !== void 0;\r\n }\r\n\r\n public get setPropertyStates() {\r\n return [...this.recordedSetPropertyStates];\r\n }\r\n\r\n public get getPropertyStates() {\r\n return [...this.recordedGetPropertyStates.values()];\r\n }\r\n\r\n constructor() {\r\n this.recordedGetPropertyStates = new Map();\r\n this.recordedSetPropertyStates = [];\r\n\r\n this._areProxiesDisabled = false;\r\n this._expectedCount = void 0;\r\n }\r\n\r\n assertCallCountMatchesExpectations(calls: Call[], callCount: number, type: string, property: PropertyKey, args: any[]) {\r\n const expectedCount = this._expectedCount;\r\n\r\n this.clearExpectations();\r\n\r\n if(this.doesCallCountMatchExpectations(expectedCount, callCount))\r\n return;\r\n\r\n throw new Error('Expected ' + expectedCount + ' call' + (expectedCount === 1 ? '' : 's') + ' to the ' + type + ' ' + property.toString() + ' with ' + stringifyArguments(args) + ', but received ' + (callCount === 0 ? 'none' : callCount) + ' of such call' + (callCount === 1 ? '' : 's') + '.\\nAll calls received to ' + type + ' ' + property.toString() + ':' + stringifyCalls(calls));\r\n }\r\n\r\n private doesCallCountMatchExpectations(expectedCount: number|undefined|null, actualCount: number) {\r\n if (expectedCount === void 0)\r\n return true;\r\n\r\n if (expectedCount === null && actualCount > 0)\r\n return true;\r\n\r\n return expectedCount === actualCount;\r\n }\r\n\r\n apply(context: Context, args: any[]) {\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n if(property === AreProxiesDisabledKey) {\r\n this._areProxiesDisabled = value;\r\n return;\r\n }\r\n\r\n const existingSetState = this.recordedSetPropertyStates.find(x => x.arguments[0] === value);;\r\n if (existingSetState) {\r\n return existingSetState.set(context, property, value);\r\n }\r\n\r\n const setPropertyState = new SetPropertyState(property, value);\r\n context.state = setPropertyState;\r\n\r\n this.recordedSetPropertyStates.push(setPropertyState);\r\n\r\n setPropertyState.set(context, property, value);\r\n }\r\n\r\n get(context: Context, property: PropertyKey) {\r\n if (typeof property === 'symbol') {\r\n if(property === AreProxiesDisabledKey)\r\n return this._areProxiesDisabled;\r\n\r\n if (property === Symbol.toPrimitive)\r\n return () => '{SubstituteJS fake}';\r\n\r\n if (property === Symbol.iterator)\r\n return void 0;\r\n\r\n if (property === Symbol.toStringTag)\r\n return 'Substitute';\r\n\r\n if(property.toString() === 'Symbol(util.inspect.custom)')\r\n return void 0;\r\n }\r\n\r\n if (property === 'valueOf')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === '$$typeof')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'length')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'toString')\r\n return '{SubstituteJS fake}';\r\n\r\n if (property === 'inspect')\r\n return () => '{SubstituteJS fake}';\r\n\r\n if (property === 'constructor')\r\n return () => context.rootProxy;\r\n\r\n if (property === 'received') {\r\n return (count?: number) => {\r\n this._expectedCount = count === void 0 ? null : count;\r\n return context.proxy;\r\n };\r\n }\r\n\r\n const existingGetState = this.recordedGetPropertyStates.get(property);\r\n if (existingGetState) {\r\n context.state = existingGetState;\r\n return context.get(property);\r\n }\r\n\r\n const getState = new GetPropertyState(property);\r\n context.state = getState;\r\n\r\n this.recordedGetPropertyStates.set(property, getState);\r\n\r\n return context.get(property);\r\n }\r\n\r\n private clearExpectations() {\r\n this._expectedCount = void 0;\r\n }\r\n\r\n onSwitchedTo() {\r\n this.clearExpectations();\r\n }\r\n}"]} \ No newline at end of file diff --git a/dist/src/states/SetPropertyState.d.ts b/dist/src/states/SetPropertyState.d.ts index 3bace85..c006343 100644 --- a/dist/src/states/SetPropertyState.d.ts +++ b/dist/src/states/SetPropertyState.d.ts @@ -8,7 +8,7 @@ export declare class SetPropertyState implements ContextState { readonly property: string | number | symbol; readonly callCount: number; constructor(_property: PropertyKey, ...args: any[]); - apply(context: Context): any; + apply(context: Context): undefined; set(context: Context, property: PropertyKey, value: any): void; - get(context: Context, property: PropertyKey): any; + get(context: Context, property: PropertyKey): undefined; } diff --git a/dist/src/states/SetPropertyState.js.map b/dist/src/states/SetPropertyState.js.map index 2bd219c..697d6d9 100644 --- a/dist/src/states/SetPropertyState.js.map +++ b/dist/src/states/SetPropertyState.js.map @@ -1 +1 @@ -{"version":3,"file":"SetPropertyState.js","sourceRoot":"","sources":["../../../src/states/SetPropertyState.ts"],"names":[],"mappings":";;AAEA,0CAAqE;AAErE,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAgBI,0BAAoB,SAAsB;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QAAtC,cAAS,GAAT,SAAS,C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L,CAAC;IAED,8BAAG,GAAH,UAAI,OAAgB,EAAE,QAAqB;QACvC,OAAO,KAAK,CAAC,CAAC;IAClB,CAAC;IACL,uBAAC;AAAD,CAAC,AApDD,IAoDC;AApDY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { stringifyArguments, areArgumentsEqual } from \"../Utilities\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class SetPropertyState implements ContextState {\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 property() {\r\n return this._property;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n constructor(private _property: PropertyKey, ...args: any[]) {\r\n this._arguments = args;\r\n\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context) {\r\n return void 0;\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n if(hasExpectations) {\r\n callCount = context.initialState\r\n .setPropertyStates\r\n .filter(x => areArgumentsEqual(x.arguments[0], value))\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 context.initialState.setPropertyStates,\r\n callCount,\r\n 'property',\r\n this.property,\r\n this.arguments);\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n }\r\n }\r\n\r\n get(context: Context, property: PropertyKey) {\r\n return void 0;\r\n }\r\n}"]} \ No newline at end of file +{"version":3,"file":"SetPropertyState.js","sourceRoot":"","sources":["../../../src/states/SetPropertyState.ts"],"names":[],"mappings":";;AAEA,0CAAqE;AAErE,IAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAEzB;IAgBI,0BAAoB,SAAsB;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QAAtC,cAAS,GAA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;SACrB;IACL,CAAC;IAED,8BAAG,GAAH,UAAI,OAAgB,EAAE,QAAqB;QACvC,OAAO,KAAK,CAAC,CAAC;IAClB,CAAC;IACL,uBAAC;AAAD,CAAC,AApDD,IAoDC;AApDY,4CAAgB","sourcesContent":["import { ContextState, PropertyKey } from \"./ContextState\";\r\nimport { Context } from \"src/Context\";\r\nimport { stringifyArguments, areArgumentsEqual } from \"../Utilities\";\r\n\r\nconst Nothing = Symbol();\r\n\r\nexport class SetPropertyState implements ContextState {\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 property() {\r\n return this._property;\r\n }\r\n\r\n public get callCount() {\r\n return this._callCount;\r\n }\r\n\r\n constructor(private _property: PropertyKey, ...args: any[]) {\r\n this._arguments = args;\r\n\r\n this._callCount = 0;\r\n }\r\n\r\n apply(context: Context): undefined {\r\n return void 0;\r\n }\r\n\r\n set(context: Context, property: PropertyKey, value: any) {\r\n let callCount = this._callCount;\r\n const hasExpectations = context.initialState.hasExpectations;\r\n if(hasExpectations) {\r\n callCount = context.initialState\r\n .setPropertyStates\r\n .filter(x => areArgumentsEqual(x.arguments[0], value))\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 context.initialState.setPropertyStates,\r\n callCount,\r\n 'property',\r\n this.property,\r\n this.arguments);\r\n\r\n if(!hasExpectations) {\r\n this._callCount++;\r\n }\r\n }\r\n\r\n get(context: Context, property: PropertyKey): undefined {\r\n return void 0;\r\n }\r\n}"]} \ No newline at end of file diff --git a/spec/index.test.ts b/spec/index.test.ts index 3becbdd..63d70d1 100644 --- a/spec/index.test.ts +++ b/spec/index.test.ts @@ -19,7 +19,7 @@ export class Example { return 1337; } - set v(x: string) { + set v(x: string|null|undefined) { } received(stuff: number|string) { @@ -30,7 +30,7 @@ export class Example { return Promise.resolve(new Dummy()); } - foo() { + foo(): string|undefined|null { return 'stuff'; } } diff --git a/src/Arguments.ts b/src/Arguments.ts index faa8f5b..e7c0f93 100644 --- a/src/Arguments.ts +++ b/src/Arguments.ts @@ -32,13 +32,13 @@ export class Arg { return new AllArguments(); } - static any() + static any(): Argument & any static any(type: T): Argument & string static any(type: T): Argument & number static any(type: T): Argument & boolean static any(type: T): Argument & any[] static any(type: T): Argument & Function - static any(type: T) + static any(type: T): Argument & any static any(type?: string): Argument & any { const description = !type ? '{any arg}' : '{type ' + type + '}'; return new Argument(description, x => { @@ -55,8 +55,8 @@ export class Arg { }); } - static is(predicate: (input: T) => boolean): Argument & T { - return new Argument('{predicate ' + this.toStringify(predicate) + '}', predicate) as any; + static is(predicate: (input: any) => boolean): Argument & T { + return new Argument('{predicate ' + this.toStringify(predicate) + '}', predicate) as Argument & T; } private static toStringify(obj: any) { diff --git a/src/Transformations.ts b/src/Transformations.ts index 1479e96..6734aee 100644 --- a/src/Transformations.ts +++ b/src/Transformations.ts @@ -7,7 +7,7 @@ export type FunctionSubstitute = ((...args: TArguments) => (TReturnType & MockObjectMixin)) & ((allArguments: AllArguments) => (TReturnType & MockObjectMixin)) -export type PropertySubstitute = TReturnType & Partial> +export type PropertySubstitute = (TReturnType & Partial>); type BaseMockObjectMixin = { returns: (...args: TReturnType[]) => void; diff --git a/src/states/FunctionState.ts b/src/states/FunctionState.ts index 08c3475..474d804 100644 --- a/src/states/FunctionState.ts +++ b/src/states/FunctionState.ts @@ -8,7 +8,7 @@ const Nothing = Symbol(); export class FunctionState implements ContextState { private returns: any[]|Symbol; - private mimicks: Function; + private mimicks: Function|null; private _callCount: number; private _arguments: any[]; @@ -26,8 +26,10 @@ export class FunctionState implements ContextState { } constructor(private _getPropertyState: GetPropertyState, ...args: any[]) { - this._arguments = args; this.returns = Nothing; + this.mimicks = null; + + this._arguments = args; this._callCount = 0; } diff --git a/src/states/GetPropertyState.ts b/src/states/GetPropertyState.ts index ee4706b..1de783a 100644 --- a/src/states/GetPropertyState.ts +++ b/src/states/GetPropertyState.ts @@ -7,7 +7,7 @@ const Nothing = Symbol(); export class GetPropertyState implements ContextState { private returns: any[]|Symbol; - private mimicks: Function; + private mimicks: Function|null; private _callCount: number; private _recordedFunctionStates: FunctionState[]; @@ -30,6 +30,8 @@ export class GetPropertyState implements ContextState { constructor(private _property: PropertyKey) { this.returns = Nothing; + this.mimicks = null; + this._recordedFunctionStates = []; this._callCount = 0; } @@ -86,9 +88,8 @@ export class GetPropertyState implements ContextState { if(!hasExpectations) { this._callCount++; - if(this.mimicks) { + if(this.mimicks) return this.mimicks.apply(this.mimicks); - } if(this.returns !== Nothing) { var returnsArray = this.returns as any[]; diff --git a/src/states/InitialState.ts b/src/states/InitialState.ts index 8376357..7249a5f 100644 --- a/src/states/InitialState.ts +++ b/src/states/InitialState.ts @@ -9,7 +9,7 @@ export class InitialState implements ContextState { private recordedGetPropertyStates: Map; private recordedSetPropertyStates: SetPropertyState[]; - private _expectedCount: number; + private _expectedCount: number|undefined|null; private _areProxiesDisabled: boolean; public get expectedCount() { @@ -32,6 +32,7 @@ export class InitialState implements ContextState { this.recordedGetPropertyStates = new Map(); this.recordedSetPropertyStates = []; + this._areProxiesDisabled = false; this._expectedCount = void 0; } @@ -46,7 +47,7 @@ export class InitialState implements ContextState { throw new Error('Expected ' + expectedCount + ' call' + (expectedCount === 1 ? '' : 's') + ' to the ' + type + ' ' + property.toString() + ' with ' + stringifyArguments(args) + ', but received ' + (callCount === 0 ? 'none' : callCount) + ' of such call' + (callCount === 1 ? '' : 's') + '.\nAll calls received to ' + type + ' ' + property.toString() + ':' + stringifyCalls(calls)); } - private doesCallCountMatchExpectations(expectedCount: number, actualCount: number) { + private doesCallCountMatchExpectations(expectedCount: number|undefined|null, actualCount: number) { if (expectedCount === void 0) return true; diff --git a/src/states/SetPropertyState.ts b/src/states/SetPropertyState.ts index 7934850..7dec8b6 100644 --- a/src/states/SetPropertyState.ts +++ b/src/states/SetPropertyState.ts @@ -26,7 +26,7 @@ export class SetPropertyState implements ContextState { this._callCount = 0; } - apply(context: Context) { + apply(context: Context): undefined { return void 0; } @@ -53,7 +53,7 @@ export class SetPropertyState implements ContextState { } } - get(context: Context, property: PropertyKey) { + get(context: Context, property: PropertyKey): undefined { return void 0; } } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 8e87a91..b54319d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,8 @@ "inlineSources": true, "downlevelIteration": true, "outDir": "./dist", + "strict": true, + "strictNullChecks": false, "target": "es5", "lib": [ "es2015"