From b24fc1548e77df669b745e3ed45b34ad0eae455f Mon Sep 17 00:00:00 2001 From: Ryan Hartlage Date: Thu, 14 Jul 2016 18:34:02 -0400 Subject: [PATCH] Show call list when all calls do not occur. Closes #20. --- rockspecs/mach-4.4-0.rockspec | 31 +++++++++++++++++++++++ spec/mach_spec.lua | 21 ++++++++++++++- src/mach/Expectation.lua | 3 ++- src/mach/not_all_calls_occurred_error.lua | 9 +++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 rockspecs/mach-4.4-0.rockspec create mode 100644 src/mach/not_all_calls_occurred_error.lua diff --git a/rockspecs/mach-4.4-0.rockspec b/rockspecs/mach-4.4-0.rockspec new file mode 100644 index 0000000..562238f --- /dev/null +++ b/rockspecs/mach-4.4-0.rockspec @@ -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 ' +} +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', + } +} diff --git a/spec/mach_spec.lua b/spec/mach_spec.lua index e907f1f..dc95c2a 100644 --- a/spec/mach_spec.lua +++ b/spec/mach_spec.lua @@ -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) @@ -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' .. diff --git a/src/mach/Expectation.lua b/src/mach/Expectation.lua index 560065c..3646d10 100644 --- a/src/mach/Expectation.lua +++ b/src/mach/Expectation.lua @@ -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 @@ -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 diff --git a/src/mach/not_all_calls_occurred_error.lua b/src/mach/not_all_calls_occurred_error.lua new file mode 100644 index 0000000..e70d21d --- /dev/null +++ b/src/mach/not_all_calls_occurred_error.lua @@ -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