1
1
describe (' The mach library' , function ()
2
2
local mach = require ' mach'
3
3
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
+
4
9
local function should_fail (test )
5
10
if pcall (test ) then
6
11
error (' expected failure did not occur' )
@@ -19,61 +24,44 @@ describe('The mach library', function()
19
24
end
20
25
21
26
it (' should allow you to verify that a function is called' , function ()
22
- local f = mach .mock_function (' f' )
23
-
24
27
f :should_be_called ():when (function () f () end )
25
28
end )
26
29
27
30
it (' should alert you when a function is not called' , function ()
28
- local f = mach .mock_function (' f' )
29
-
30
31
should_fail_with (' not all calls occurred' , function ()
31
32
f :should_be_called ():when (function () end )
32
33
end )
33
34
end )
34
35
35
36
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
-
39
37
should_fail_with (' unexpected function call f2()' , function ()
40
38
f1 :should_be_called ():when (function () f2 () end )
41
39
end )
42
40
end )
43
41
44
42
it (' should alert you when a function is called unexpectedly' , function ()
45
- local f = mach .mock_function (' f' )
46
-
47
43
should_fail_with (' unexpected function call f()' , function ()
48
44
f ()
49
45
end )
50
46
end )
51
47
52
48
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
-
55
49
f :should_be_called_with (1 , ' 2' ):when (function () f (1 , ' 2' ) end )
56
50
end )
57
51
58
52
it (' should alert you when a function has been called with incorrect arguments' , function ()
59
- local f = mach .mock_function (' f' )
60
-
61
53
should_fail (function ()
62
54
f :should_be_called_with (1 , ' 2' ):when (function () f (1 , ' 3' ) end )
63
55
end )
64
56
end )
65
57
66
58
it (' should allow you to specify the return value of a mocked function' , function ()
67
- local f = mach .mock_function (' f' )
68
-
69
59
f :should_be_called ():and_will_return (4 ):when (function ()
70
60
assert .is .equal (f (), 4 )
71
61
end )
72
62
end )
73
63
74
64
it (' should allow you to specify multiple return values for a mocked function' , function ()
75
- local f = mach .mock_function (' f' )
76
-
77
65
f :should_be_called ():and_will_return (1 , 2 ):when (function ()
78
66
r1 , r2 = f ()
79
67
assert .is .equal (r1 , 1 )
@@ -82,8 +70,6 @@ describe('The mach library', function()
82
70
end )
83
71
84
72
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
-
87
73
f :should_be_called ():
88
74
and_will_raise_error (' some error message' ):
89
75
when (function ()
@@ -94,8 +80,6 @@ describe('The mach library', function()
94
80
end )
95
81
96
82
it (' should allow calls to be completed after a call that raises an error' , function ()
97
- local f = mach .mock_function (' f' )
98
-
99
83
f :should_be_called ():
100
84
and_will_raise_error (' some error message' ):
101
85
and_then (f :should_be_called ():and_will_return (4 )):
@@ -106,8 +90,6 @@ describe('The mach library', function()
106
90
end )
107
91
108
92
it (' should allow you to check that a function has been called multiple times' , function ()
109
- local f = mach .mock_function (' f' )
110
-
111
93
f :should_be_called ():
112
94
and_also (f :should_be_called_with (1 , 2 , 3 )):
113
95
when (function ()
@@ -117,9 +99,6 @@ describe('The mach library', function()
117
99
end )
118
100
119
101
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
-
123
102
f1 :should_be_called ():
124
103
and_also (f2 :should_be_called_with (1 , 2 , 3 )):
125
104
when (function ()
@@ -129,9 +108,6 @@ describe('The mach library', function()
129
108
end )
130
109
131
110
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
-
135
111
f1 :should_be_called ():
136
112
and_also (f2 :should_be_called_with (1 , 2 , 3 )):
137
113
and_then (f2 :should_be_called_with (1 ):and_will_return (4 )):
@@ -281,8 +257,6 @@ describe('The mach library', function()
281
257
end )
282
258
283
259
it (' should let you expect a function to be called multiple times' , function ()
284
- local f = mach .mock_function (' f' )
285
-
286
260
f :should_be_called_with (2 ):and_will_return (1 ):multiple_times (3 ):when (function ()
287
261
assert (f (2 ) == 1 )
288
262
assert (f (2 ) == 1 )
@@ -292,8 +266,6 @@ describe('The mach library', function()
292
266
293
267
it (' should fail if a function is not called enough times' , function ()
294
268
should_fail (function ()
295
- local f = mach .mock_function ()
296
-
297
269
f :should_be_called_with (2 ):and_will_return (1 ):multiple_times (3 ):when (function ()
298
270
assert (f (2 ) == 1 )
299
271
assert (f (2 ) == 1 )
@@ -302,15 +274,11 @@ describe('The mach library', function()
302
274
end )
303
275
304
276
it (' should allow after to be used as an alias for when' , function ()
305
- local f = mach .mock_function ()
306
-
307
277
f :should_be_called ():after (function () f () end )
308
278
end )
309
279
310
280
it (' should fail if a function is called too many times' , function ()
311
281
should_fail (function ()
312
- local f = mach .mock_function (' f' )
313
-
314
282
f :should_be_called_with (2 ):and_will_return (1 ):multiple_times (2 ):when (function ()
315
283
assert (f (2 ) == 1 )
316
284
assert (f (2 ) == 1 )
@@ -328,40 +296,29 @@ describe('The mach library', function()
328
296
329
297
it (' should fail if when is not preceeded by should_be_called or should_be_called_with' , function ()
330
298
should_fail_with (' incomplete expectation' , function ()
331
- local f = mach .mock_function (' f' )
332
-
333
299
f :when (function () end )
334
300
end )
335
301
end )
336
302
337
303
it (' should fail if after is not preceeded by should_be_called or should_be_called_with' , function ()
338
304
should_fail_with (' incomplete expectation' , function ()
339
- local f = mach .mock_function (' f' )
340
-
341
305
f :after (function () end )
342
306
end )
343
307
end )
344
308
345
309
it (' should fail if should_be_called is used after a call has already been specified' , function ()
346
310
should_fail_with (' call already specified' , function ()
347
- local f = mach .mock_function (' f' )
348
-
349
311
f :should_be_called ():should_be_called ()
350
312
end )
351
313
end )
352
314
353
315
it (' should fail if should_be_called_with is used after a call has already been specified' , function ()
354
316
should_fail_with (' call already specified' , function ()
355
- local f = mach .mock_function (' f' )
356
-
357
317
f :should_be_called ():should_be_called_with (4 )
358
318
end )
359
319
end )
360
320
361
321
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
-
365
322
f1 :should_be_called ():
366
323
and_also (f2 :should_be_called ()):
367
324
when (function ()
@@ -378,9 +335,6 @@ describe('The mach library', function()
378
335
end )
379
336
380
337
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
-
384
338
should_fail_with (' unexpected function call f2()' , function ()
385
339
f1 :should_be_called ():
386
340
and_then (f2 :should_be_called ()):
@@ -401,10 +355,6 @@ describe('The mach library', function()
401
355
end )
402
356
403
357
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
-
408
358
should_fail_with (' unexpected function call f3()' , function ()
409
359
f1 :should_be_called ():
410
360
and_also (f2 :should_be_called ()):
@@ -418,8 +368,6 @@ describe('The mach library', function()
418
368
end )
419
369
420
370
it (' should allow ordered and unordered calls to be mixed' , function ()
421
- local f = mach .mock_function (' f' )
422
-
423
371
f :should_be_called_with (1 ):
424
372
and_also (f :should_be_called_with (2 )):
425
373
and_then (f :should_be_called_with (3 )):
@@ -433,51 +381,35 @@ describe('The mach library', function()
433
381
end )
434
382
435
383
it (' should allow soft expectations to be called' , function ()
436
- local f = mach .mock_function (' f' )
437
-
438
384
f :may_be_called ():when (function () f () end )
439
385
end )
440
386
441
387
it (' should allow soft expectations to be omitted' , function ()
442
- local f = mach .mock_function (' f' )
443
-
444
388
f :may_be_called ():when (function () end )
445
389
end )
446
390
447
391
it (' should allow soft expectations with return values' , function ()
448
- local f = mach .mock_function (' f' )
449
-
450
392
f :may_be_called ():and_will_return (3 ):when (function ()
451
393
assert (f () == 3 )
452
394
end )
453
395
end )
454
396
455
397
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
-
460
398
f :may_be_called_with (4 ):when (function () f (4 ) end )
461
399
end )
462
400
463
401
it (' should allow soft expectations with arguments to be omitted' , function ()
464
- local f = mach .mock_function (' f' )
465
-
466
402
f :may_be_called_with (4 ):when (function () end )
467
403
end )
468
404
469
405
it (' should fail if may_be_called is used after a call has already been specified' , function ()
470
406
should_fail_with (' call already specified' , function ()
471
- local f = mach .mock_function (' f' )
472
-
473
407
f :should_be_called ():may_be_called ()
474
408
end )
475
409
end )
476
410
477
411
it (' should fail if may_be_called_with is used after a call has already been specified' , function ()
478
412
should_fail_with (' call already specified' , function ()
479
- local f = mach .mock_function (' f' )
480
-
481
413
f :should_be_called ():may_be_called_with (4 )
482
414
end )
483
415
end )
0 commit comments