Skip to content

Commit 126c5eb

Browse files
committed
Documented error messages. Closes #11.
1 parent aa8c0f1 commit 126c5eb

File tree

6 files changed

+64
-39
lines changed

6 files changed

+64
-39
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,28 @@ something_should_happen():
189189
and_also(another_thing_should_happen()):
190190
when(the_code_under_test_runs)
191191
```
192+
193+
## Handy Error messages
194+
195+
```lua
196+
local mach = require 'mach'
197+
198+
local f1 = mach.mock_function('f1')
199+
local f2 = mach.mock_function('f2')
200+
local f2 = mach.mock_function('f3')
201+
202+
f1:should_be_called_with(1):
203+
and_also(f2:should_be_called_with(2)):
204+
when(function()
205+
f1(1)
206+
f3(3)
207+
end)
208+
```
209+
210+
```
211+
Unexpected function call f(3)
212+
Completed calls:
213+
f1(1)
214+
Incomplete calls:
215+
f2(2)
216+
```

spec/mach_spec.lua

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ describe('The mach library', function()
4545
end)
4646

4747
it('should alert you when the wrong function is called', function()
48-
should_fail_with('unexpected function call f2()', function()
48+
should_fail_with('Unexpected function call f2()', function()
4949
f1:should_be_called():when(function() f2() end)
5050
end)
5151
end)
5252

5353
it('should alert you when a function is called unexpectedly', function()
54-
should_fail_with('unexpected function call f()', function()
54+
should_fail_with('Unexpected function call f()', function()
5555
f()
5656
end)
5757
end)
@@ -174,15 +174,15 @@ describe('The mach library', function()
174174
it('should allow a mocked table to be named', function()
175175
mocked_table = mach.mock_table({ foo = function() end }, 'some_table')
176176

177-
should_fail_with('unexpected function call some_table.foo()', function()
177+
should_fail_with('Unexpected function call some_table.foo()', function()
178178
mocked_table.foo()
179179
end)
180180
end)
181181

182182
it('should give mocked tables a default name when none is provided', function()
183183
mocked_table = mach.mock_table({ foo = function() end })
184184

185-
should_fail_with('unexpected function call <anonymous>.foo()', function()
185+
should_fail_with('Unexpected function call <anonymous>.foo()', function()
186186
mocked_table.foo()
187187
end)
188188
end)
@@ -220,15 +220,15 @@ describe('The mach library', function()
220220
it('should allow a mocked object to be named', function()
221221
mocked_object = mach.mock_object({ foo = function() end }, 'some_object')
222222

223-
should_fail_with('unexpected function call some_object:foo()', function()
223+
should_fail_with('Unexpected function call some_object:foo()', function()
224224
mocked_object.foo()
225225
end)
226226
end)
227227

228228
it('should give mocked objects a default name when none is provided', function()
229229
mocked_object = mach.mock_object({ foo = function() end })
230230

231-
should_fail_with('unexpected function call <anonymous>:foo()', function()
231+
should_fail_with('Unexpected function call <anonymous>:foo()', function()
232232
mocked_object.foo()
233233
end)
234234
end)
@@ -363,7 +363,7 @@ describe('The mach library', function()
363363
end)
364364

365365
it('should not allow calls to happen out of order when and_then is used', function()
366-
should_fail_with('out of order function call f2()', function()
366+
should_fail_with('Out of order function call f2()', function()
367367
f1:should_be_called():
368368
and_then(f2:should_be_called()):
369369
when(function()
@@ -372,7 +372,7 @@ describe('The mach library', function()
372372
end)
373373
end)
374374

375-
should_fail_with('unexpected arguments (2) provided to function f1', function()
375+
should_fail_with('Unexpected arguments (2) provided to function f1', function()
376376
f1:should_be_called_with(1):
377377
and_then(f2:should_be_called(2)):
378378
when(function()
@@ -383,7 +383,7 @@ describe('The mach library', function()
383383
end)
384384

385385
it('should catch out of order calls when mixed with unordered calls', function()
386-
should_fail_with('out of order function call f3()', function()
386+
should_fail_with('Out of order function call f3()', function()
387387
f1:should_be_called():
388388
and_also(f2:should_be_called()):
389389
and_then(f3:should_be_called()):
@@ -415,7 +415,7 @@ describe('The mach library', function()
415415
end)
416416

417417
it('should not allow order to be violated for an optional call', function()
418-
should_fail_with('unexpected function call f1()', function()
418+
should_fail_with('Unexpected function call f1()', function()
419419
f1:may_be_called():and_then(f2:should_be_called()):when(function()
420420
f2()
421421
f1()
@@ -472,27 +472,27 @@ describe('The mach library', function()
472472
end)
473473

474474
it('should handle unexpected calls outside of an expectation', function()
475-
should_fail_with('unexpected function call f(1, 2, 3)', function()
475+
should_fail_with('Unexpected function call f(1, 2, 3)', function()
476476
mach.mock_function('f')(1, 2, 3)
477477
end)
478478
end)
479479

480480
it('should handle table arguments in error messages', function()
481481
local a = {}
482482

483-
should_fail_with('unexpected function call f(' .. tostring(a) ..')', function()
483+
should_fail_with('Unexpected function call f(' .. tostring(a) ..')', function()
484484
mach.mock_function('f')(a)
485485
end)
486486
end)
487487

488488
it('should give mocked functions a default name when none is provided', function()
489-
should_fail_with('unexpected function call <anonymous>(1, 2, 3)', function()
489+
should_fail_with('Unexpected function call <anonymous>(1, 2, 3)', function()
490490
mach.mock_function()(1, 2, 3)
491491
end)
492492
end)
493493

494494
it('should give mocked methods a default name when none is provided', function()
495-
should_fail_with('unexpected function call <anonymous>(2, 3)', function()
495+
should_fail_with('Unexpected function call <anonymous>(2, 3)', function()
496496
mach.mock_method()(1, 2, 3)
497497
end)
498498
end)
@@ -506,10 +506,10 @@ describe('The mach library', function()
506506

507507
it('should report completed and incomplete calls in unexpected call errors', function()
508508
local expected_failure =
509-
'unexpected function call f3()\n' ..
510-
'completed calls:\n' ..
509+
'Unexpected function call f3()\n' ..
510+
'Completed calls:\n' ..
511511
'\tf1()\n' ..
512-
'incomplete calls:\n' ..
512+
'Incomplete calls:\n' ..
513513
'\tf2()'
514514

515515
should_fail_with_exactly(expected_failure, function()
@@ -522,10 +522,10 @@ describe('The mach library', function()
522522

523523
it('should report completed and incomplete calls in unexpected argument errors', function()
524524
local expected_failure =
525-
'unexpected arguments (3) provided to function f2\n' ..
526-
'completed calls:\n' ..
525+
'Unexpected arguments (3) provided to function f2\n' ..
526+
'Completed calls:\n' ..
527527
'\tf1()\n' ..
528-
'incomplete calls:\n' ..
528+
'Incomplete calls:\n' ..
529529
'\tf2()'
530530

531531
should_fail_with_exactly(expected_failure, function()
@@ -538,10 +538,10 @@ describe('The mach library', function()
538538

539539
it('should report completed and incomplete calls in out of order call errors', function()
540540
local expected_failure =
541-
'out of order function call f3()\n' ..
542-
'completed calls:\n' ..
541+
'Out of order function call f3()\n' ..
542+
'Completed calls:\n' ..
543543
'\tf1()\n' ..
544-
'incomplete calls:\n' ..
544+
'Incomplete calls:\n' ..
545545
'\tf2()\n' ..
546546
'\tf3()'
547547

@@ -558,8 +558,8 @@ describe('The mach library', function()
558558

559559
it('should omit the completed call list in an error when no calls were completed', function()
560560
local expected_failure =
561-
'unexpected function call f3()\n' ..
562-
'incomplete calls:\n' ..
561+
'Unexpected function call f3()\n' ..
562+
'Incomplete calls:\n' ..
563563
'\tf1()'
564564

565565
should_fail_with_exactly(expected_failure, function()
@@ -571,8 +571,8 @@ describe('The mach library', function()
571571

572572
it('should omit the incomplete call list in an error when all calls were completed', function()
573573
local expected_failure =
574-
'unexpected function call f3()\n' ..
575-
'completed calls:\n' ..
574+
'Unexpected function call f3()\n' ..
575+
'Completed calls:\n' ..
576576
'\tf1()'
577577

578578
should_fail_with_exactly(expected_failure, function()
@@ -589,8 +589,8 @@ describe('The mach library', function()
589589
}
590590

591591
local expected_failure =
592-
'unexpected function call f()\n' ..
593-
'incomplete calls:\n' ..
592+
'Unexpected function call f()\n' ..
593+
'Incomplete calls:\n' ..
594594
'\tm()'
595595

596596
should_fail_with_exactly(expected_failure, function()
@@ -602,10 +602,10 @@ describe('The mach library', function()
602602

603603
it('should show optional function calls as optional in call status messages', function()
604604
local expected_failure =
605-
'unexpected function call f3()\n' ..
606-
'completed calls:\n' ..
605+
'Unexpected function call f3()\n' ..
606+
'Completed calls:\n' ..
607607
'\tf1()\n' ..
608-
'incomplete calls:\n' ..
608+
'Incomplete calls:\n' ..
609609
'\tf2() (optional)'
610610

611611
should_fail_with_exactly(expected_failure, function()
@@ -618,8 +618,8 @@ describe('The mach library', function()
618618

619619
it('should show actual arguments in call status messages', function()
620620
local expected_failure =
621-
'unexpected function call f3()\n' ..
622-
'completed calls:\n' ..
621+
'Unexpected function call f3()\n' ..
622+
'Completed calls:\n' ..
623623
'\tf1(1, 2, 3)'
624624

625625
should_fail_with_exactly(expected_failure, function()

src/mach/format_call_status.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ return function(completed_calls, incomplete_calls)
1313

1414
if #completed_calls > 0 then
1515
message = message ..
16-
'\ncompleted calls:' ..
16+
'\nCompleted calls:' ..
1717
'\n\t' .. table.concat(completed_call_strings, '\n\t')
1818
end
1919

2020
if #incomplete_calls > 0 then
2121
message = message ..
22-
'\nincomplete calls:' ..
22+
'\nIncomplete calls:' ..
2323
'\n\t' .. table.concat(incomplete_call_strings, '\n\t')
2424
end
2525

src/mach/out_of_order_call_error.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local format_arguments = require 'mach.format_arguments'
33

44
return function(name, args, completed_calls, incomplete_calls, level)
55
local error_message =
6-
'out of order function call ' .. name .. format_arguments(args) ..
6+
'Out of order function call ' .. name .. format_arguments(args) ..
77
format_call_status(completed_calls, incomplete_calls)
88

99
error(error_message, level + 1)

src/mach/unexpected_args_error.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local format_arguments = require 'mach.format_arguments'
33

44
return function(name, args, completed_calls, incomplete_calls, level)
55
local error_message =
6-
'unexpected arguments ' .. format_arguments(args) .. ' provided to function ' .. name ..
6+
'Unexpected arguments ' .. format_arguments(args) .. ' provided to function ' .. name ..
77
format_call_status(completed_calls, incomplete_calls)
88

99
error(error_message, level + 1)

src/mach/unexpected_call_error.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local format_arguments = require 'mach.format_arguments'
33

44
return function(name, args, completed_calls, incomplete_calls, level)
55
local message =
6-
'unexpected function call ' .. name .. format_arguments(args) ..
6+
'Unexpected function call ' .. name .. format_arguments(args) ..
77
format_call_status(completed_calls, incomplete_calls)
88

99
error(message, level + 1)

0 commit comments

Comments
 (0)