Skip to content

Commit 240682c

Browse files
committed
refactor: symbols only when needed
1 parent ac3983d commit 240682c

23 files changed

+1938
-247
lines changed

package-lock.json

Lines changed: 1548 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
"@types/node": "^12.20.55",
5050
"@types/sinonjs__fake-timers": "^8.1.5",
5151
"ava": "^4.3.3",
52-
"typescript": "^4.8.4"
52+
"typescript": "^5.4.2"
5353
}
5454
}

spec/ClearSubstitute.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava'
22

33
import { Substitute, SubstituteOf, clearReceivedCalls, received, returns } from '../src'
4-
import { SubstituteNode } from '../src/SubstituteNode'
4+
import { SubstituteNode, instance } from '../src/SubstituteNode'
55

66
interface Calculator {
77
add(a: number, b: number): number
@@ -11,18 +11,18 @@ interface Calculator {
1111
}
1212

1313
type InstanceReturningSubstitute<T> = SubstituteOf<T> & {
14-
[SubstituteNode.instance]: SubstituteNode
14+
[instance]: SubstituteNode
1515
}
1616

1717
test('clears received calls on a substitute', t => {
1818
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
1919
calculator.add(1, 1)
20-
calculator.add(1, 1)[returns](2)
21-
calculator[clearReceivedCalls]();
20+
calculator.add(1, 1).returns(2)
21+
calculator.clearReceivedCalls();
2222

23-
t.is(calculator[SubstituteNode.instance].recorder.records.size, 2)
24-
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 2)
23+
t.is(calculator[instance].recorder.records.size, 2)
24+
t.is(calculator[instance].recorder.indexedRecords.size, 2)
2525

26-
t.throws(() => calculator[received]().add(1, 1))
26+
t.throws(() => calculator.received().add(1, 1))
2727
t.is(2, calculator.add(1, 1))
2828
})

spec/Recorder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { returns } from '../src'
88

99
const nodeFactory = (key: string) => {
1010
const node = Substitute.for<SubstituteNodeBase>()
11-
node.key[returns](key)
11+
node.key.returns(key)
1212
return node
1313
}
1414

spec/regression/didNotReceive.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@ interface Calculator {
1212
test('not calling a method correctly asserts the call count', t => {
1313
const calculator = Substitute.for<Calculator>()
1414

15-
calculator[didNotReceive]().add(1, 1)
16-
t.throws(() => calculator[received]().add(1, 1), { instanceOf: SubstituteException })
17-
t.throws(() => calculator[received]().add(Arg.all()), { instanceOf: SubstituteException })
15+
calculator.didNotReceive().add(1, 1)
16+
t.throws(() => calculator.received().add(1, 1), { instanceOf: SubstituteException })
17+
t.throws(() => calculator.received().add(Arg.all()), { instanceOf: SubstituteException })
1818
})
1919

2020
test('not getting a property correctly asserts the call count', t => {
2121
const calculator = Substitute.for<Calculator>()
2222

23-
calculator[didNotReceive]().isEnabled
24-
t.throws(() => calculator[received](1).isEnabled, { instanceOf: SubstituteException })
25-
t.throws(() => calculator[received]().isEnabled, { instanceOf: SubstituteException })
23+
calculator.didNotReceive().isEnabled
24+
t.throws(() => calculator.received(1).isEnabled, { instanceOf: SubstituteException })
25+
t.throws(() => calculator.received().isEnabled, { instanceOf: SubstituteException })
2626
})
2727

2828
test('not setting a property correctly asserts the call count', t => {
2929
const calculator = Substitute.for<Calculator>()
3030

31-
calculator[didNotReceive]().isEnabled = true
32-
t.throws(() => calculator[received](1).isEnabled = true, { instanceOf: SubstituteException })
33-
t.throws(() => calculator[received]().isEnabled = true, { instanceOf: SubstituteException })
31+
calculator.didNotReceive().isEnabled = true
32+
t.throws(() => calculator.received(1).isEnabled = true, { instanceOf: SubstituteException })
33+
t.throws(() => calculator.received().isEnabled = true, { instanceOf: SubstituteException })
3434
})
3535

3636
test('not calling a method with mock correctly asserts the call count', t => {
3737
const calculator = Substitute.for<Calculator>()
38-
calculator.add(1, 1)[returns](2)
38+
calculator.add(1, 1).returns(2)
3939

40-
calculator[didNotReceive]().add(1, 1)
41-
t.throws(() => calculator[received](1).add(1, 1), { instanceOf: SubstituteException })
42-
t.throws(() => calculator[received]().add(Arg.all()), { instanceOf: SubstituteException })
40+
calculator.didNotReceive().add(1, 1)
41+
t.throws(() => calculator.received(1).add(1, 1), { instanceOf: SubstituteException })
42+
t.throws(() => calculator.received().add(Arg.all()), { instanceOf: SubstituteException })
4343
})
4444

4545
test('not getting a property with mock correctly asserts the call count', t => {
4646
const calculator = Substitute.for<Calculator>()
47-
calculator.isEnabled[returns](true)
47+
calculator.isEnabled.returns(true)
4848

49-
calculator[didNotReceive]().isEnabled
50-
t.throws(() => calculator[received](1).isEnabled, { instanceOf: SubstituteException })
51-
t.throws(() => calculator[received]().isEnabled, { instanceOf: SubstituteException })
49+
calculator.didNotReceive().isEnabled
50+
t.throws(() => calculator.received(1).isEnabled, { instanceOf: SubstituteException })
51+
t.throws(() => calculator.received().isEnabled, { instanceOf: SubstituteException })
5252
})

spec/regression/index.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export class Example {
2020
set v(x: string | null | undefined) {
2121
}
2222

23-
received(stuff: number | string) {
23+
received(_stuff: string) {
2424

2525
}
2626

2727
returnPromise() {
2828
return Promise.resolve(new Dummy())
2929
}
3030

31-
foo(): string | undefined | null {
31+
foo(_arg?: string): string | undefined | null {
3232
return 'stuff'
3333
}
3434

@@ -48,12 +48,12 @@ function initialize() {
4848
const textModifierRegex = /\x1b\[\d+m/g
4949

5050
test('class with method called \'received\' can be used for call count verification when using symbols', t => {
51-
initialize()
51+
const substitute = Substitute.for<Example>()
5252

53-
substitute.received(2)
53+
substitute.received("foo")
5454

55-
t.throws(() => substitute[received](2).received(2))
56-
t.notThrows(() => substitute[received](1).received(2))
55+
t.notThrows(() => substitute[received](1).received("foo"))
56+
t.throws(() => substitute[received](2).received("foo"))
5757
})
5858

5959
test('class string field set received', t => {
@@ -85,15 +85,15 @@ test('class string field set received', t => {
8585
test('resolving promises works', async t => {
8686
initialize()
8787

88-
substitute.returnPromise()[resolves](1338)
88+
substitute.returnPromise().resolves(1338)
8989

9090
t.is(1338, await substitute.returnPromise() as number)
9191
})
9292

9393
test('class void returns', t => {
9494
initialize()
9595

96-
substitute.foo()[returns](void 0, null)
96+
substitute.foo().returns(void 0, null)
9797

9898
t.is(substitute.foo(), void 0)
9999
t.is(substitute.foo(), null)
@@ -126,7 +126,7 @@ test('class method received', t => {
126126
test('received call matches after partial mocks using property instance mimicks', t => {
127127
initialize()
128128

129-
substitute.d[mimicks](() => instance.d)
129+
substitute.d.mimicks(() => instance.d)
130130
substitute.c('lala', 'bar')
131131

132132
substitute[received](1).c('lala', 'bar')
@@ -144,7 +144,7 @@ test('received call matches after partial mocks using property instance mimicks'
144144
test('partial mocks using property instance mimicks', t => {
145145
initialize()
146146

147-
substitute.d[mimicks](() => instance.d)
147+
substitute.d.mimicks(() => instance.d)
148148

149149
t.deepEqual(substitute.d, 1337)
150150
})

spec/regression/issues/11.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class RealCalculator {
1414

1515
test('issue 11: arg.is is only called once', async t => {
1616
let mockedCalculator = Substitute.for<RealCalculator>()
17-
mockedCalculator.add(Arg.any())[returns](4)
17+
mockedCalculator.add(Arg.any()).returns(4)
1818

1919
let count = 0
2020
mockedCalculator.add({ op1: 1, op2: 2 })
2121

22-
mockedCalculator[received](1).add(Arg.is(a => {
22+
mockedCalculator.received(1).add(Arg.is(a => {
2323
count++
2424
return a.op1 === 1 && a.op2 === 2
2525
}))

spec/regression/issues/178.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ const throwsUncaughtException = (cb: () => any, t: ExecutionContext, expectation
2424

2525
test('can substitute callable interfaces', async t => {
2626
const lib = Substitute.for<Library>()
27-
lib.subSection()[returns]('subSection as method')
28-
lib.subSection[returns]({ subMethod: () => 'subSection as property' } as Subsection)
27+
lib.subSection().returns('subSection as method')
28+
lib.subSection.returns({ subMethod: () => 'subSection as property' } as Subsection)
2929

3030
t.is('subSection as method', lib.subSection())
3131
t.true(types.isProxy(lib.subSection), 'Expected proxy: given the context, it\'s not possible to determine the property type')
3232
t.is('subSection as property', lib.subSection.subMethod())
3333

34-
lib[received]().subSection()
35-
lib[received](1).subSection()
36-
lib[received](2).subSection
37-
t.throws(() => lib[didNotReceive]().subSection(), { instanceOf: SubstituteException })
38-
t.throws(() => lib[received](2).subSection(), { instanceOf: SubstituteException })
39-
throwsUncaughtException(() => lib[received](3).subSection, t, { instanceOf: SubstituteException })
34+
lib.received().subSection()
35+
lib.received(1).subSection()
36+
lib.received(2).subSection
37+
t.throws(() => lib.didNotReceive().subSection(), { instanceOf: SubstituteException })
38+
t.throws(() => lib.received(2).subSection(), { instanceOf: SubstituteException })
39+
throwsUncaughtException(() => lib.received(3).subSection, t, { instanceOf: SubstituteException })
4040
})

spec/regression/issues/23.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ test('issue 23: mimick received should not call method', t => {
1414

1515
let calls = 0
1616

17-
mockedCalculator.add(Arg.all())[mimicks]((a, b) => {
17+
mockedCalculator.add(Arg.all()).mimicks((a, b) => {
1818
t.deepEqual(++calls, 1, 'mimick called twice')
1919
return a + b
2020
})
2121

2222
mockedCalculator.add(1, 1) // ok
2323

24-
mockedCalculator[received](1).add(1, 1) // not ok, calls mimick func
24+
mockedCalculator.received(1).add(1, 1) // not ok, calls mimick func
2525
})

spec/regression/issues/36.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Service {
4949

5050
test('issue 36 - promises returning object with properties', async t => {
5151
const emptyFetch = Substitute.for<IFetch>()
52-
emptyFetch.getUpdates(Key.create())[returns](Promise.resolve<IData>(IData.create()))
52+
emptyFetch.getUpdates(Key.create()).returns(Promise.resolve<IData>(IData.create()))
5353
const result = await emptyFetch.getUpdates(Key.create())
5454
t.true(result.serverCheck instanceof Date, 'given date is instanceof Date')
5555
t.deepEqual(result.data, [1], 'arrays are deep equal')
@@ -58,12 +58,12 @@ test('issue 36 - promises returning object with properties', async t => {
5858
test('using objects or classes as arguments should be able to match mock', async t => {
5959
const db = Substitute.for<IFetch>()
6060
const data = IData.create()
61-
db.getUpdates(Key.create())[returns](Promise.resolve(data))
61+
db.getUpdates(Key.create()).returns(Promise.resolve(data))
6262
const service = new Service(db)
6363

6464
await service.handle(Key.create())
6565

66-
db[received](1).storeUpdates(Arg.is((arg: IData) =>
66+
db.received(1).storeUpdates(Arg.is((arg: IData) =>
6767
arg.serverCheck instanceof Date &&
6868
arg instanceof IData &&
6969
arg.data[0] === 100

0 commit comments

Comments
 (0)