allow mock.func() to return multiple values

develop
myk002 2021-06-15 13:15:37 -07:00
parent a938aa14a5
commit b7a970a309
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 12 additions and 4 deletions

@ -81,9 +81,9 @@ function mock.restore(...)
return _patch_impl(patches, callback, true) return _patch_impl(patches, callback, true)
end end
function mock.func(return_value) function mock.func(...)
local f = { local f = {
return_value = return_value, return_values = {...},
call_count = 0, call_count = 0,
call_args = {}, call_args = {},
} }
@ -92,7 +92,7 @@ function mock.func(return_value)
__call = function(self, ...) __call = function(self, ...)
self.call_count = self.call_count + 1 self.call_count = self.call_count + 1
table.insert(self.call_args, {...}) table.insert(self.call_args, {...})
return self.return_value return table.unpack(self.return_values)
end, end,
}) })

@ -203,6 +203,14 @@ end
function test.func_call_return_value() function test.func_call_return_value()
local f = mock.func(7) local f = mock.func(7)
expect.eq(f(), 7) expect.eq(f(), 7)
f.return_value = 9 f.return_values = {9}
expect.eq(f(), 9) expect.eq(f(), 9)
end end
function test.func_call_return_multiple_values()
local f = mock.func(7,5,{imatable='snarfsnarf'})
local a, b, c = f()
expect.eq(7, a)
expect.eq(5, b)
expect.table_eq({imatable='snarfsnarf'}, c)
end