Skip to content

Commit

Permalink
Show call list when all calls do not occur. Closes #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed Jul 14, 2016
1 parent ad3cf46 commit b24fc15
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
31 changes: 31 additions & 0 deletions rockspecs/mach-4.4-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package = 'mach'
version = '4.4-0'
source = {
url = 'https://github.com/ryanplusplus/mach.lua/archive/v4.4-0.tar.gz',
dir = 'mach.lua-4.4-0/src'
}
description = {
summary = 'Simple mocking framework for Lua inspired by CppUMock and designed for readability.',
homepage = 'https://github.com/ryanplusplus/mach.lua/',
license = 'MIT <http://opensource.org/licenses/MIT>'
}
dependencies = {
'lua >= 5.1'
}
build = {
type = 'builtin',
modules = {
['mach'] = 'mach.lua',
['mach.Expectation'] = 'mach/Expectation.lua',
['mach.ExpectedCall'] = 'mach/ExpectedCall.lua',
['mach.CompletedCall'] = 'mach/CompletedCall.lua',
['mach.unexpected_call_error'] = 'mach/unexpected_call_error.lua',
['mach.unexpected_args_error'] = 'mach/unexpected_args_error.lua',
['mach.out_of_order_call_error'] = 'mach/out_of_order_call_error.lua',
['mach.format_call_status'] = 'mach/format_call_status.lua',
['mach.format_arguments'] = 'mach/format_arguments.lua',
['mach.deep_compare_matcher'] = 'mach/deep_compare_matcher.lua',
['mach.match'] = 'mach/match.lua',
['mach.any'] = 'mach/any.lua',
}
}
21 changes: 20 additions & 1 deletion spec/mach_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('The mach library', function()
end)

it('should alert you when a function is not called', function()
should_fail_with('not all calls occurred', function()
should_fail_with('Not all calls occurred', function()
f:should_be_called():when(function() end)
end)
end)
Expand Down Expand Up @@ -569,6 +569,25 @@ describe('The mach library', function()
end)
end)

it('should report completed and incomplete calls in not all calls occurred errors', function()
local expected_failure =
'Not all calls occurred\n' ..
'Completed calls:\n' ..
'\tf1()\n' ..
'Incomplete calls:\n' ..
'\tf2()\n' ..
'\tf3()'

should_fail_with_exactly(expected_failure, function()
f1:should_be_called():
and_then(f2:should_be_called()):
and_then(f3:should_be_called()):
when(function()
f1()
end)
end)
end)

it('should omit the completed call list in an error when no calls were completed', function()
local expected_failure =
'Unexpected function call f3()\n' ..
Expand Down
3 changes: 2 additions & 1 deletion src/mach/Expectation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local CompletedCall = require 'mach.CompletedCall'
local unexpected_call_error = require 'mach.unexpected_call_error'
local unexpected_args_error = require 'mach.unexpected_args_error'
local out_of_order_call_error = require 'mach.out_of_order_call_error'
local not_all_calls_occurred_error = require 'mach.not_all_calls_occurred_error'

local expectation = {}
expectation.__index = expectation
Expand Down Expand Up @@ -96,7 +97,7 @@ function expectation:when(thunk)

for _, call in pairs(self._calls) do
if call:is_required() then
error('not all calls occurred', 2)
not_all_calls_occurred_error(self._completed_calls, self._calls, 2)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions src/mach/not_all_calls_occurred_error.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local format_call_status = require 'mach.format_call_status'

return function(completed_calls, incomplete_calls, level)
local message =
'Not all calls occurred' ..
format_call_status(completed_calls, incomplete_calls)

error(message, level + 1)
end

0 comments on commit b24fc15

Please sign in to comment.