From 395b02ecbe63ba6775dd3a3c09d6f9484d38eba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 24 Apr 2011 01:39:05 +0200 Subject: [PATCH] added metatable support --- inspect.lua | 28 +++++++++++++++++----------- spec/inspect_spec.lua | 13 +++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) 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)