Skip to content

Commit d298799

Browse files
committed
Simplified tests by extracting mock creation
1 parent e2ac10e commit d298799

File tree

1 file changed

+5
-73
lines changed

1 file changed

+5
-73
lines changed

spec/mach_spec.lua

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
describe('The mach library', function()
22
local mach = require 'mach'
33

4+
local f = mach.mock_function('f')
5+
local f1 = mach.mock_function('f1')
6+
local f2 = mach.mock_function('f2')
7+
local f3 = mach.mock_function('f3')
8+
49
local function should_fail(test)
510
if pcall(test) then
611
error('expected failure did not occur')
@@ -19,61 +24,44 @@ describe('The mach library', function()
1924
end
2025

2126
it('should allow you to verify that a function is called', function()
22-
local f = mach.mock_function('f')
23-
2427
f:should_be_called():when(function() f() end)
2528
end)
2629

2730
it('should alert you when a function is not called', function()
28-
local f = mach.mock_function('f')
29-
3031
should_fail_with('not all calls occurred', function()
3132
f:should_be_called():when(function() end)
3233
end)
3334
end)
3435

3536
it('should alert you when the wrong function is called', function()
36-
local f1 = mach.mock_function('f1')
37-
local f2 = mach.mock_function('f2')
38-
3937
should_fail_with('unexpected function call f2()', function()
4038
f1:should_be_called():when(function() f2() end)
4139
end)
4240
end)
4341

4442
it('should alert you when a function is called unexpectedly', function()
45-
local f = mach.mock_function('f')
46-
4743
should_fail_with('unexpected function call f()', function()
4844
f()
4945
end)
5046
end)
5147

5248
it('should allow you to verify that a function has been called with the correct arguments', function()
53-
local f = mach.mock_function('f')
54-
5549
f:should_be_called_with(1, '2'):when(function() f(1, '2') end)
5650
end)
5751

5852
it('should alert you when a function has been called with incorrect arguments', function()
59-
local f = mach.mock_function('f')
60-
6153
should_fail(function()
6254
f:should_be_called_with(1, '2'):when(function() f(1, '3') end)
6355
end)
6456
end)
6557

6658
it('should allow you to specify the return value of a mocked function', function()
67-
local f = mach.mock_function('f')
68-
6959
f:should_be_called():and_will_return(4):when(function()
7060
assert.is.equal(f(), 4)
7161
end)
7262
end)
7363

7464
it('should allow you to specify multiple return values for a mocked function', function()
75-
local f = mach.mock_function('f')
76-
7765
f:should_be_called():and_will_return(1, 2):when(function()
7866
r1, r2 = f()
7967
assert.is.equal(r1, 1)
@@ -82,8 +70,6 @@ describe('The mach library', function()
8270
end)
8371

8472
it('should allow you to specify errors to be raised when a mocked function is called', function()
85-
local f = mach.mock_function('f')
86-
8773
f:should_be_called():
8874
and_will_raise_error('some error message'):
8975
when(function()
@@ -94,8 +80,6 @@ describe('The mach library', function()
9480
end)
9581

9682
it('should allow calls to be completed after a call that raises an error', function()
97-
local f = mach.mock_function('f')
98-
9983
f:should_be_called():
10084
and_will_raise_error('some error message'):
10185
and_then(f:should_be_called():and_will_return(4)):
@@ -106,8 +90,6 @@ describe('The mach library', function()
10690
end)
10791

10892
it('should allow you to check that a function has been called multiple times', function()
109-
local f = mach.mock_function('f')
110-
11193
f:should_be_called():
11294
and_also(f:should_be_called_with(1, 2, 3)):
11395
when(function()
@@ -117,9 +99,6 @@ describe('The mach library', function()
11799
end)
118100

119101
it('should allow you to check that multiple functions are called', function()
120-
local f1 = mach.mock_function('f1')
121-
local f2 = mach.mock_function('f2')
122-
123102
f1:should_be_called():
124103
and_also(f2:should_be_called_with(1, 2, 3)):
125104
when(function()
@@ -129,9 +108,6 @@ describe('The mach library', function()
129108
end)
130109

131110
it('should allow you to mix and match call types', function()
132-
local f1 = mach.mock_function('f1')
133-
local f2 = mach.mock_function('f2')
134-
135111
f1:should_be_called():
136112
and_also(f2:should_be_called_with(1, 2, 3)):
137113
and_then(f2:should_be_called_with(1):and_will_return(4)):
@@ -281,8 +257,6 @@ describe('The mach library', function()
281257
end)
282258

283259
it('should let you expect a function to be called multiple times', function()
284-
local f = mach.mock_function('f')
285-
286260
f:should_be_called_with(2):and_will_return(1):multiple_times(3):when(function()
287261
assert(f(2) == 1)
288262
assert(f(2) == 1)
@@ -292,8 +266,6 @@ describe('The mach library', function()
292266

293267
it('should fail if a function is not called enough times', function()
294268
should_fail(function()
295-
local f = mach.mock_function()
296-
297269
f:should_be_called_with(2):and_will_return(1):multiple_times(3):when(function()
298270
assert(f(2) == 1)
299271
assert(f(2) == 1)
@@ -302,15 +274,11 @@ describe('The mach library', function()
302274
end)
303275

304276
it('should allow after to be used as an alias for when', function()
305-
local f = mach.mock_function()
306-
307277
f:should_be_called():after(function() f() end)
308278
end)
309279

310280
it('should fail if a function is called too many times', function()
311281
should_fail(function()
312-
local f = mach.mock_function('f')
313-
314282
f:should_be_called_with(2):and_will_return(1):multiple_times(2):when(function()
315283
assert(f(2) == 1)
316284
assert(f(2) == 1)
@@ -328,40 +296,29 @@ describe('The mach library', function()
328296

329297
it('should fail if when is not preceeded by should_be_called or should_be_called_with', function()
330298
should_fail_with('incomplete expectation', function()
331-
local f = mach.mock_function('f')
332-
333299
f:when(function() end)
334300
end)
335301
end)
336302

337303
it('should fail if after is not preceeded by should_be_called or should_be_called_with', function()
338304
should_fail_with('incomplete expectation', function()
339-
local f = mach.mock_function('f')
340-
341305
f:after(function() end)
342306
end)
343307
end)
344308

345309
it('should fail if should_be_called is used after a call has already been specified', function()
346310
should_fail_with('call already specified', function()
347-
local f = mach.mock_function('f')
348-
349311
f:should_be_called():should_be_called()
350312
end)
351313
end)
352314

353315
it('should fail if should_be_called_with is used after a call has already been specified', function()
354316
should_fail_with('call already specified', function()
355-
local f = mach.mock_function('f')
356-
357317
f:should_be_called():should_be_called_with(4)
358318
end)
359319
end)
360320

361321
it('should allow calls to happen out of order when and_also is used', function()
362-
local f1 = mach.mock_function('f1')
363-
local f2 = mach.mock_function('f2')
364-
365322
f1:should_be_called():
366323
and_also(f2:should_be_called()):
367324
when(function()
@@ -378,9 +335,6 @@ describe('The mach library', function()
378335
end)
379336

380337
it('should not allow calls to happen out of order when and_then is used', function()
381-
local f1 = mach.mock_function('f1')
382-
local f2 = mach.mock_function('f2')
383-
384338
should_fail_with('unexpected function call f2()', function()
385339
f1:should_be_called():
386340
and_then(f2:should_be_called()):
@@ -401,10 +355,6 @@ describe('The mach library', function()
401355
end)
402356

403357
it('should catch out of order calls when mixed with unordered calls', function()
404-
local f1 = mach.mock_function('f1')
405-
local f2 = mach.mock_function('f2')
406-
local f3 = mach.mock_function('f3')
407-
408358
should_fail_with('unexpected function call f3()', function()
409359
f1:should_be_called():
410360
and_also(f2:should_be_called()):
@@ -418,8 +368,6 @@ describe('The mach library', function()
418368
end)
419369

420370
it('should allow ordered and unordered calls to be mixed', function()
421-
local f = mach.mock_function('f')
422-
423371
f:should_be_called_with(1):
424372
and_also(f:should_be_called_with(2)):
425373
and_then(f:should_be_called_with(3)):
@@ -433,51 +381,35 @@ describe('The mach library', function()
433381
end)
434382

435383
it('should allow soft expectations to be called', function()
436-
local f = mach.mock_function('f')
437-
438384
f:may_be_called():when(function() f() end)
439385
end)
440386

441387
it('should allow soft expectations to be omitted', function()
442-
local f = mach.mock_function('f')
443-
444388
f:may_be_called():when(function() end)
445389
end)
446390

447391
it('should allow soft expectations with return values', function()
448-
local f = mach.mock_function('f')
449-
450392
f:may_be_called():and_will_return(3):when(function()
451393
assert(f() == 3)
452394
end)
453395
end)
454396

455397
it('should allow soft expectations with arguments to be called', function()
456-
local f = mach.mock_function('f')
457-
458-
f:may_be_called_with(4):when(function() f(4) end)
459-
460398
f:may_be_called_with(4):when(function() f(4) end)
461399
end)
462400

463401
it('should allow soft expectations with arguments to be omitted', function()
464-
local f = mach.mock_function('f')
465-
466402
f:may_be_called_with(4):when(function() end)
467403
end)
468404

469405
it('should fail if may_be_called is used after a call has already been specified', function()
470406
should_fail_with('call already specified', function()
471-
local f = mach.mock_function('f')
472-
473407
f:should_be_called():may_be_called()
474408
end)
475409
end)
476410

477411
it('should fail if may_be_called_with is used after a call has already been specified', function()
478412
should_fail_with('call already specified', function()
479-
local f = mach.mock_function('f')
480-
481413
f:should_be_called():may_be_called_with(4)
482414
end)
483415
end)

0 commit comments

Comments
 (0)