I didnt commit the right stuff on previous commit. fixing that

This commit is contained in:
Enrique García Cota 2011-04-24 00:46:43 +02:00
parent 71bf1a87a0
commit 4572d02f72
2 changed files with 42 additions and 27 deletions

View File

@ -63,12 +63,12 @@ end
local Inspector = {} local Inspector = {}
function Inspector:new(v) function Inspector:new(v, depth)
local inspector = setmetatable( { buffer = {} }, { local inspector = setmetatable( { buffer = {}, depth = depth }, {
__index = Inspector, __index = Inspector,
__tostring = function(instance) return table.concat(instance.buffer) end __tostring = function(instance) return table.concat(instance.buffer) end
} ) } )
return inspector:addValue(v, 0) return inspector:putValue(v, 0)
end end
function Inspector:puts(...) function Inspector:puts(...)
@ -84,32 +84,40 @@ function Inspector:tabify(level)
return self return self
end end
function Inspector:addTable(t, level) function Inspector:putTable(t, level)
local length, needsComma = #t, false if level >= self.depth then
self:puts('{') self:puts('{...}')
for i=1, length do else
if needsComma then self:puts(', ') end local length, needsComma = #t, false
needsComma = true self:puts('{')
self:addValue(t[i], level + 1) for i=1, length do
end if needsComma then self:puts(',') end
needsComma = true
self:puts(' '):putValue(t[i], level + 1)
end
local dictKeys = getDictionaryKeys(t) local dictKeys = getDictionaryKeys(t)
for _,k in ipairs(dictKeys) do for _,k in ipairs(dictKeys) do
if needsComma then self:puts(',') end if needsComma then self:puts(',') end
needsComma = true needsComma = true
self:tabify(level+1) self:tabify(level+1)
self:addKey(k, level + 1) self:addKey(k, level + 1)
self:puts(' = ') self:puts(' = ')
self:addValue(t[k], level + 1) self:putValue(t[k], level + 1)
end
if #dictKeys > 0 then
self:tabify(level)
elseif length > 0 then
self:puts(' ')
end
self:puts('}')
end end
if #dictKeys > 0 then self:tabify(level) end
self:puts('}')
return self return self
end end
function Inspector:addValue(v, level) function Inspector:putValue(v, level)
local tv = type(v) local tv = type(v)
if tv == 'string' then if tv == 'string' then
@ -117,7 +125,7 @@ function Inspector:addValue(v, level)
elseif tv == 'number' or tv == 'boolean' then elseif tv == 'number' or tv == 'boolean' then
self:puts(tostring(v)) self:puts(tostring(v))
elseif tv == 'table' then elseif tv == 'table' then
self:addTable(v, level) self:putTable(v, level)
else else
self:puts('<',tv,'>') self:puts('<',tv,'>')
end end
@ -128,11 +136,12 @@ function Inspector:addKey(k, level)
if type(k) == "string" and isIdentifier(k) then if type(k) == "string" and isIdentifier(k) then
return self:puts(k) return self:puts(k)
end end
return self:puts( "[" ):addValue(k, level):puts("]") return self:puts( "[" ):putValue(k, level):puts("]")
end end
local function inspect(t) local function inspect(t, depth)
return tostring(Inspector:new(t)) depth = depth or 4
return tostring(Inspector:new(t, depth))
end end
return inspect return inspect

View File

@ -100,6 +100,12 @@ context( 'inspect', function()
b = {...} b = {...}
} }
}]]) }]])
assert_equal(inspect(level4, 1), [[{ 1, 2, 3,
a = {...}
}]])
end) end)
end) end)