diff --git a/inspect.lua b/inspect.lua index 9a3d700..193e791 100644 --- a/inspect.lua +++ b/inspect.lua @@ -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(' = '):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('}') diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index d9e35b1..df414c3 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -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, + = { + __tostring = + foo = 1, + } +}]]) + end) + + end)