added metatable support

This commit is contained in:
Enrique García Cota 2011-04-24 01:39:05 +02:00
parent 4ddef3ccae
commit 395b02ecbe
2 changed files with 30 additions and 11 deletions

View File

@ -105,22 +105,28 @@ function Inspector:putTable(t)
local comma = false
self:puts('{')
self:down()
for i=1, length do
comma = self:putComma(comma)
self:puts(' '):putValue(t[i])
end
for i=1, length do
comma = self:putComma(comma)
self:puts(' '):putValue(t[i])
end
local dictKeys = getDictionaryKeys(t)
local dictKeys = getDictionaryKeys(t)
for _,k in ipairs(dictKeys) do
comma = self:putComma(comma)
self:tabify():putKey(k):puts(' = '):putValue(t[k])
end
for _,k in ipairs(dictKeys) do
comma = self:putComma(comma)
self:tabify():putKey(k):puts(' = '):putValue(t[k])
end
local mt = getmetatable(t)
if type(mt) == 'table' then
comma = self:putComma(comma)
self:tabify():puts('<metatable> = '):putValue(mt)
end
self:up()
if #dictKeys > 0 then
if #dictKeys > 0 then -- dictionary table. Justify closing }
self:tabify()
elseif length > 0 then
elseif length > 0 then -- array tables have one extra space before closing }
self:puts(' ')
end
self:puts('}')

View File

@ -147,6 +147,19 @@ context( 'inspect', function()
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)