Skip to content

Commit 1839c6d

Browse files
committed
Lua 5.1 compatibility; fixed accidental globals
1 parent 732abdb commit 1839c6d

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

publish-to-lit.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local f = io.open('build/mach.lua', 'w')
55
f:write([==[
66
--[[lit-meta
77
name = 'ryanplusplus/mach'
8-
version = '1.0.4'
8+
version = '1.0.5'
99
description = 'Simple mocking framework for Lua inspired by CppUMock and designed for readability.'
1010
tags = { 'testing' }
1111
license = 'MIT'

spec/mach_spec.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,11 @@ describe('The mach library', function()
682682
'Incomplete calls:\n' ..
683683
'\tf(<mach.match(3)>)'
684684

685-
should_fail_with_exactly(expected_failure, function()
686-
f:should_be_called_with(mach.match(3)):when(function()
687-
f(4)
688-
end)
685+
should_fail_with_exactly(expected_failure, function()
686+
f:should_be_called_with(mach.match(3)):when(function()
687+
f(4)
689688
end)
689+
end)
690690
end)
691691

692692
it('should allow custom matchers to be used', function()

src/mach.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,31 @@ local mach = {}
88

99
mach.any = require 'mach.any'
1010

11-
function unexpected_call(m, name, args)
11+
table.pack = table.pack or function(...)
12+
return { n = select('#',...); ... }
13+
end
14+
15+
table.unpack = table.unpack or unpack
16+
17+
local function unexpected_call(m, name, args)
1218
unexpected_call_error(name, args, {}, {}, 2)
1319
end
1420

1521
local subscriber = unexpected_call
1622

17-
function handle_mock_calls(callback, thunk)
23+
local function handle_mock_calls(callback, thunk)
1824
subscriber = callback
1925
thunk()
2026
subscriber = unexpected_call
2127
end
2228

23-
function mock_called(m, name, args)
29+
local function mock_called(m, name, args)
2430
return subscriber(m, name, args)
2531
end
2632

27-
function create_expectation(_, method)
33+
local function create_expectation(_, method)
2834
return function(self, ...)
29-
local expectation = Expectation(self)
35+
local expectation = Expectation(handle_mock_calls, self)
3036
return expectation[method](expectation, ...)
3137
end
3238
end
@@ -62,7 +68,7 @@ function mach.mock_method(name)
6268
return m
6369
end
6470

65-
function is_callable(x)
71+
local function is_callable(x)
6672
local is_function = type(x) == 'function'
6773
local has_call_metamethod = type((debug.getmetatable(x) or {}).__call) == 'function'
6874
return is_function or has_call_metamethod
@@ -98,4 +104,4 @@ function mach.match(value, matcher)
98104
return setmetatable({ value = value, matcher = matcher or default_matcher }, mach_match)
99105
end
100106

101-
return setmetatable(mach, { __call = function(_, ...) return Expectation(...) end })
107+
return mach

src/mach/Expectation.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ local not_all_calls_occurred_error = require 'mach.not_all_calls_occurred_error'
88
local expectation = {}
99
expectation.__index = expectation
1010

11-
local function create(m)
11+
local function create(handle_mock_calls, m)
1212
local o = {
1313
_m = m,
1414
_call_specified = false,
1515
_calls = {},
16-
_completed_calls = {}
16+
_completed_calls = {},
17+
_handle_mock_calls = handle_mock_calls
1718
}
1819

1920
setmetatable(o, expectation)
@@ -93,7 +94,7 @@ function expectation:when(thunk)
9394
end
9495
end
9596

96-
handle_mock_calls(called, thunk)
97+
self._handle_mock_calls(called, thunk)
9798

9899
for _, call in pairs(self._calls) do
99100
if call:is_required() then

0 commit comments

Comments
 (0)