Skip to content

Commit ee5c4fd

Browse files
committed
feat: symbols for methods
- removed Substitute.disableFor
1 parent cdd3d04 commit ee5c4fd

21 files changed

+262
-278
lines changed

spec/ClearSubstitute.spec.ts

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

33
import { Substitute, SubstituteOf, clearReceivedCalls, received } from '../src'
44
import { SubstituteNode } from '../src/SubstituteNode'
5+
import { returns } from '../src/Transformations'
56

67
interface Calculator {
78
add(a: number, b: number): number
@@ -17,7 +18,7 @@ type InstanceReturningSubstitute<T> = SubstituteOf<T> & {
1718
test('clears received calls on a substitute', t => {
1819
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
1920
calculator.add(1, 1)
20-
calculator.add(1, 1).returns(2)
21+
calculator.add(1, 1)[returns](2)
2122
calculator[clearReceivedCalls]();
2223

2324
t.is(calculator[SubstituteNode.instance].recorder.records.size, 2)

spec/Recorder.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { Recorder } from '../src/Recorder'
44
import { RecordsSet } from '../src/RecordsSet'
55
import { Substitute } from '../src/Substitute'
66
import { SubstituteNodeBase } from '../src/SubstituteNodeBase'
7+
import { returns } from '../src/Transformations'
78

89
const nodeFactory = (key: string) => {
910
const node = Substitute.for<SubstituteNodeBase>()
10-
node.key.returns(key)
11+
node.key[returns](key)
1112
return node
1213
}
1314

spec/regression/didNotReceive.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava'
22
import { Substitute, Arg, didNotReceive, received } from '../../src'
33
import { SubstituteException } from '../../src/SubstituteException'
4+
import { returns } from '../../src/Transformations'
45

56
interface Calculator {
67
add(a: number, b: number): number
@@ -35,7 +36,7 @@ test('not setting a property correctly asserts the call count', t => {
3536

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

4041
calculator[didNotReceive]().add(1, 1)
4142
t.throws(() => calculator[received](1).add(1, 1), { instanceOf: SubstituteException })
@@ -44,7 +45,7 @@ test('not calling a method with mock correctly asserts the call count', t => {
4445

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

4950
calculator[didNotReceive]().isEnabled
5051
t.throws(() => calculator[received](1).isEnabled, { instanceOf: SubstituteException })

spec/regression/index.test.ts

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

3-
import { Substitute, Arg, SubstituteOf } from '../../src'
3+
import { Substitute, Arg, SubstituteOf, received, mimicks, resolves, returns } from '../../src'
44

55
class Dummy {
66

@@ -47,22 +47,13 @@ function initialize() {
4747

4848
const textModifierRegex = /\x1b\[\d+m/g
4949

50-
test('class with method called \'received\' can be used for call count verification when proxies are suspended', t => {
50+
test('class with method called \'received\' can be used for call count verification when using symbols', t => {
5151
initialize()
5252

53-
Substitute.disableFor(substitute).received(2)
53+
substitute.received(2)
5454

55-
t.throws(() => substitute.received(2).received(2))
56-
t.notThrows(() => substitute.received(1).received(2))
57-
})
58-
59-
test('class with method called \'received\' can be used for call count verification', t => {
60-
initialize()
61-
62-
Substitute.disableFor(substitute).received('foo')
63-
64-
t.notThrows(() => substitute.received(1).received('foo'))
65-
t.throws(() => substitute.received(2).received('foo'))
55+
t.throws(() => substitute[received](2).received(2))
56+
t.notThrows(() => substitute[received](1).received(2))
6657
})
6758

6859
test('class string field set received', t => {
@@ -79,30 +70,30 @@ test('class string field set received', t => {
7970
runLogic(substitute)
8071

8172

82-
t.notThrows(() => substitute.received().v = 'hello')
83-
t.notThrows(() => substitute.received(5).v = Arg.any())
84-
t.notThrows(() => substitute.received().v = Arg.any())
85-
t.notThrows(() => substitute.received(2).v = 'hello')
86-
t.notThrows(() => substitute.received(2).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
73+
t.notThrows(() => substitute[received]().v = 'hello')
74+
t.notThrows(() => substitute[received](5).v = Arg.any())
75+
t.notThrows(() => substitute[received]().v = Arg.any())
76+
t.notThrows(() => substitute[received](2).v = 'hello')
77+
t.notThrows(() => substitute[received](2).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
8778

88-
t.throws(() => substitute.received(2).v = Arg.any())
89-
t.throws(() => substitute.received(1).v = Arg.any())
90-
t.throws(() => substitute.received(1).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
91-
t.throws(() => substitute.received(3).v = 'hello')
79+
t.throws(() => substitute[received](2).v = Arg.any())
80+
t.throws(() => substitute[received](1).v = Arg.any())
81+
t.throws(() => substitute[received](1).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
82+
t.throws(() => substitute[received](3).v = 'hello')
9283
})
9384

9485
test('resolving promises works', async t => {
9586
initialize()
9687

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

9990
t.is(1338, await substitute.returnPromise() as number)
10091
})
10192

10293
test('class void returns', t => {
10394
initialize()
10495

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

10798
t.is(substitute.foo(), void 0)
10899
t.is(substitute.foo(), null)
@@ -117,9 +108,9 @@ test('class method received', t => {
117108
void substitute.c('hi', 'there')
118109
void substitute.c('hi', 'there')
119110

120-
t.notThrows(() => substitute.received(4).c('hi', 'there'))
121-
t.notThrows(() => substitute.received(1).c('hi', 'the1re'))
122-
t.notThrows(() => substitute.received().c('hi', 'there'))
111+
t.notThrows(() => substitute[received](4).c('hi', 'there'))
112+
t.notThrows(() => substitute[received](1).c('hi', 'the1re'))
113+
t.notThrows(() => substitute[received]().c('hi', 'there'))
123114

124115
const expectedMessage = 'Expected 7 calls to the method c with arguments [\'hi\', \'there\'], but received 4 of such calls.\n' +
125116
'All calls received to method c:\n' +
@@ -128,32 +119,32 @@ test('class method received', t => {
128119
'-> call with arguments [\'hi\', \'there\']\n' +
129120
'-> call with arguments [\'hi\', \'there\']\n' +
130121
'-> call with arguments [\'hi\', \'there\']'
131-
const { message } = t.throws(() => { substitute.received(7).c('hi', 'there') })
122+
const { message } = t.throws(() => { substitute[received](7).c('hi', 'there') })
132123
t.is(message.replace(textModifierRegex, ''), expectedMessage)
133124
})
134125

135126
test('received call matches after partial mocks using property instance mimicks', t => {
136127
initialize()
137128

138-
substitute.d.mimicks(() => instance.d)
129+
substitute.d[mimicks](() => instance.d)
139130
substitute.c('lala', 'bar')
140131

141-
substitute.received(1).c('lala', 'bar')
142-
substitute.received(1).c('lala', 'bar')
132+
substitute[received](1).c('lala', 'bar')
133+
substitute[received](1).c('lala', 'bar')
143134

144-
t.notThrows(() => substitute.received(1).c('lala', 'bar'))
135+
t.notThrows(() => substitute[received](1).c('lala', 'bar'))
145136
const expectedMessage = 'Expected 2 calls to the method c with arguments [\'lala\', \'bar\'], but received 1 of such calls.\n' +
146137
'All calls received to method c:\n' +
147138
'-> call with arguments [\'lala\', \'bar\']'
148-
const { message } = t.throws(() => substitute.received(2).c('lala', 'bar'))
139+
const { message } = t.throws(() => substitute[received](2).c('lala', 'bar'))
149140
t.is(message.replace(textModifierRegex, ''), expectedMessage)
150141
t.deepEqual(substitute.d, 1337)
151142
})
152143

153144
test('partial mocks using property instance mimicks', t => {
154145
initialize()
155146

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

158149
t.deepEqual(substitute.d, 1337)
159150
})

spec/regression/issues/11.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava'
2-
import { Substitute, Arg } from '../../../src'
2+
import { Substitute, Arg, received, returns } from '../../../src'
33

44
type Addands = {
55
op1: number
@@ -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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import test, { ExecutionContext, ThrowsExpectation } from 'ava'
22
import * as fakeTimers from '@sinonjs/fake-timers'
33
import { types } from 'util'
44

5-
import { Substitute } from '../../../src'
5+
import { Substitute, didNotReceive, received, returns } from '../../../src'
66
import { SubstituteException } from '../../../src/SubstituteException'
77

88
interface Library {
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import { Substitute, Arg } from '../../../src'
3+
import { Substitute, Arg, received, mimicks } from '../../../src'
44

55
interface CalculatorInterface {
66
add(a: number, b: number): number
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import { Substitute, Arg } from '../../../src'
3+
import { Substitute, Arg, received, returns } from '../../../src'
44

55
class Key {
66
private constructor(private _value: string) { }
@@ -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

spec/regression/issues/45.test.ts

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

3-
import { Substitute, Arg } from '../../../src'
3+
import { Substitute, Arg, received } from '../../../src'
44

55
class DependencyClass {
66
public methodOne() { }
@@ -30,7 +30,7 @@ test('issue 45 Checking received calls off at times', async t => {
3030
subject.callToMethodTwo()
3131

3232
t.notThrows(() => {
33-
mock.received(1).methodOne()
34-
mock.received(1).methodTwo(Arg.is(x => x === 'string'))
33+
mock[received](1).methodOne()
34+
mock[received](1).methodTwo(Arg.is(x => x === 'string'))
3535
})
3636
})

spec/regression/issues/59.test.ts

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

3-
import { Substitute } from '../../../src'
3+
import { Substitute, received, returns } from '../../../src'
44

55
interface IEcho {
66
echo(a: string): string
@@ -9,10 +9,10 @@ interface IEcho {
99

1010
test('issue 59 - Mock function with optional parameters', (t) => {
1111
const echoer = Substitute.for<IEcho>()
12-
echoer.maybeEcho('foo').returns('bar')
13-
echoer.maybeEcho().returns('baz')
12+
echoer.maybeEcho('foo')[returns]('bar')
13+
echoer.maybeEcho()[returns]('baz')
1414

1515
t.is('bar', echoer.maybeEcho('foo'))
16-
echoer.received().maybeEcho('foo')
16+
echoer[received]().maybeEcho('foo')
1717
t.is('baz', echoer.maybeEcho())
1818
})

0 commit comments

Comments
 (0)