Skip to content

Commit c558690

Browse files
committed
Updated to handle unexpected calls outside of a mach thunk and to handle printing table in args
1 parent a7fa43e commit c558690

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

spec/mach_spec.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('The mach library', function()
2-
mock = require 'mach'
2+
local mock = require 'mach'
33

44
local function shouldFail(test)
55
if pcall(test) then
@@ -456,4 +456,18 @@ describe('The mach library', function()
456456
mock(f):shouldBeCalled():mayBeCalledWith(4)
457457
end)
458458
end)
459+
460+
it('should handle unexpected alls outside of an expectation', function()
461+
shouldFailWith('unexpected function call f(1, 2, 3)', function()
462+
mock.mockFunction('f')(1, 2, 3)
463+
end)
464+
end)
465+
466+
it('should handle table arguments in error messages', function()
467+
local a = {}
468+
469+
shouldFailWith('unexpected function call f(' .. tostring(a) ..')', function()
470+
mock.mockFunction('f')(a)
471+
end)
472+
end)
459473
end)

src/mach.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
local ExpectedCall = require 'mach.expected-call'
22
local Expectation = require 'mach.expectation'
3+
local UnexpectedCallError = require 'mach.unexpected-call-error'
34

45
local Mock = {}
56

6-
local subscriber
7+
function unexpectedCall(m, name, args)
8+
UnexpectedCallError(name, args, 2)
9+
end
10+
11+
local subscriber = unexpectedCall
712

813
function handleMockCalls(callback, thunk)
914
subscriber = callback
1015
thunk()
11-
subscriber = nil
16+
subscriber = unexpectedCall
1217
end
1318

1419
function mockCalled(m, name, args)

src/mach/expectation.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
ExpectedCall = require 'mach.expected-call'
1+
local ExpectedCall = require 'mach.expected-call'
2+
local UnexpectedCallError = require 'mach.unexpected-call-error'
3+
local UnexpectedArgsError = require 'mach.unexpected-args-error'
24

35
local expectation = {}
46
expectation.__index = expectation
@@ -52,9 +54,9 @@ function expectation:when(thunk)
5254
end
5355

5456
if not validFunctionFound then
55-
error('unexpected function call ' .. name .. '(' .. table.concat(args, ', ') .. ')', 2)
57+
UnexpectedCallError(name, args, 2)
5658
else
57-
error('unexpected arguments (' .. table.concat(args, ', ') .. ') provided to function ' .. name, 2)
59+
UnexpectedArgsError(name, args, 2)
5860
end
5961
end
6062

0 commit comments

Comments
 (0)