Skip to content

Commit c3bddb8

Browse files
committed
Format values in a more intelligent way. Closes #24.
1 parent 0540203 commit c3bddb8

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

spec/mach/format_value_spec.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe('mach.format_value', function()
2+
local format_value = require 'mach.format_value'
3+
4+
it('should format basic types', function()
5+
assert.are.same('3', format_value(3))
6+
assert.are.same("'hello'", format_value('hello'))
7+
assert.are.same('true', format_value(true))
8+
end)
9+
10+
it('should format functions', function()
11+
local f = function() end
12+
assert.are.same(tostring(f), format_value(f))
13+
end)
14+
15+
it('should format tables', function()
16+
assert.are.same('{ [1] = true, [2] = false }', format_value({ true, false }))
17+
assert.are.same("{ ['a'] = 1, ['b'] = 2 }", format_value({ b = 2, a = 1 }))
18+
assert.are.same(
19+
"{ [1] = true, ['b'] = { [4] = 'lua', ['a'] = 1 } }",
20+
format_value({ b = { [4] = 'lua', a = 1 }, true })
21+
)
22+
end)
23+
24+
it('should respect __tostring when it is defined', function()
25+
local value = setmetatable({}, {
26+
__tostring = function() return 'foo' end
27+
})
28+
29+
assert.are.same('foo', format_value(value))
30+
end)
31+
end)

spec/mach_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ describe('The mach library', function()
493493
it('should handle table arguments in error messages', function()
494494
local a = {}
495495

496-
should_fail_with('Unexpected function call f(' .. tostring(a) ..')', function()
496+
should_fail_with('Unexpected function call f({})', function()
497497
mach.mock_function('f')(a)
498498
end)
499499
end)
@@ -685,10 +685,10 @@ describe('The mach library', function()
685685
local expected_failure =
686686
'Unexpected arguments (4) provided to function f\n' ..
687687
'Incomplete calls:\n' ..
688-
"\tf(<mach.match('3')>)"
688+
"\tf(<mach.match({ ['3'] = 1 })>)"
689689

690690
should_fail_with_exactly(expected_failure, function()
691-
f:should_be_called_with(mach.match('3')):when(function()
691+
f:should_be_called_with(mach.match({ ['3'] = 1 })):when(function()
692692
f(4)
693693
end)
694694
end)

src/mach/format_value.lua

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1-
return function(v)
2-
if type(v) == 'string' then
1+
local function pairs_sorted_by_key(t)
2+
return coroutine.wrap(function()
3+
local _pairs = {}
4+
for k, v in pairs(t) do
5+
table.insert(_pairs, { k, v })
6+
end
7+
table.sort(_pairs, function(x, y) return tostring(x[1]) < tostring(y[1]) end)
8+
for _, pair in ipairs(_pairs) do
9+
coroutine.yield(table.unpack(pair))
10+
end
11+
end)
12+
end
13+
14+
local function format_value(v)
15+
if getmetatable(v) and getmetatable(v).__tostring then
16+
return tostring(v)
17+
elseif type(v) == 'string' then
318
return "'" .. v .. "'"
19+
elseif type(v) == 'table' then
20+
local elements = {}
21+
for k, v in pairs_sorted_by_key(v) do
22+
table.insert(elements, '[' .. format_value(k) .. '] = ' .. format_value(v))
23+
end
24+
if #elements > 0 then
25+
return '{ ' .. table.concat(elements, ', ') .. ' }'
26+
else
27+
return '{}'
28+
end
429
else
530
return tostring(v)
631
end
732
end
33+
34+
return format_value

0 commit comments

Comments
 (0)