mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
added metatable support
This commit is contained in:
parent
4ddef3ccae
commit
395b02ecbe
28
inspect.lua
28
inspect.lua
@ -105,22 +105,28 @@ function Inspector:putTable(t)
|
|||||||
local comma = false
|
local comma = false
|
||||||
self:puts('{')
|
self:puts('{')
|
||||||
self:down()
|
self:down()
|
||||||
for i=1, length do
|
for i=1, length do
|
||||||
comma = self:putComma(comma)
|
comma = self:putComma(comma)
|
||||||
self:puts(' '):putValue(t[i])
|
self:puts(' '):putValue(t[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
local dictKeys = getDictionaryKeys(t)
|
local dictKeys = getDictionaryKeys(t)
|
||||||
|
|
||||||
for _,k in ipairs(dictKeys) do
|
for _,k in ipairs(dictKeys) do
|
||||||
comma = self:putComma(comma)
|
comma = self:putComma(comma)
|
||||||
self:tabify():putKey(k):puts(' = '):putValue(t[k])
|
self:tabify():putKey(k):puts(' = '):putValue(t[k])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local mt = getmetatable(t)
|
||||||
|
if type(mt) == 'table' then
|
||||||
|
comma = self:putComma(comma)
|
||||||
|
self:tabify():puts('<metatable> = '):putValue(mt)
|
||||||
|
end
|
||||||
self:up()
|
self:up()
|
||||||
|
|
||||||
if #dictKeys > 0 then
|
if #dictKeys > 0 then -- dictionary table. Justify closing }
|
||||||
self:tabify()
|
self:tabify()
|
||||||
elseif length > 0 then
|
elseif length > 0 then -- array tables have one extra space before closing }
|
||||||
self:puts(' ')
|
self:puts(' ')
|
||||||
end
|
end
|
||||||
self:puts('}')
|
self:puts('}')
|
||||||
|
@ -147,6 +147,19 @@ context( 'inspect', function()
|
|||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test('Should include the metatable as an extra hash attribute', function()
|
||||||
|
local foo = { foo = 1, __tostring = function(k) return 'foo' end }
|
||||||
|
local bar = setmetatable({a = 1}, foo)
|
||||||
|
assert_equal(inspect(bar), [[{
|
||||||
|
a = 1,
|
||||||
|
<metatable> = {
|
||||||
|
__tostring = <function>
|
||||||
|
foo = 1,
|
||||||
|
}
|
||||||
|
}]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user