removed table ids from tables that appear less than twice. Fixes #2

This commit is contained in:
kikito 2012-10-29 23:26:49 +01:00
parent d01950cee3
commit b744b7f3f8
2 changed files with 76 additions and 54 deletions

View File

@ -72,30 +72,49 @@ end
local Inspector = {} local Inspector = {}
function Inspector:new(v, depth) function Inspector:new(t, depth)
local inspector = { local inspector = {
buffer = {}, buffer = {},
depth = depth, depth = depth,
level = 0, level = 0,
counters = { maxIds = {
['function'] = 0, ['function'] = 0,
['userdata'] = 0, ['userdata'] = 0,
['thread'] = 0, ['thread'] = 0,
['table'] = 0 ['table'] = 0
}, },
pools = { ids = {
['function'] = setmetatable({}, {__mode = "kv"}), ['function'] = setmetatable({}, {__mode = "kv"}),
['userdata'] = setmetatable({}, {__mode = "kv"}), ['userdata'] = setmetatable({}, {__mode = "kv"}),
['thread'] = setmetatable({}, {__mode = "kv"}), ['thread'] = setmetatable({}, {__mode = "kv"}),
['table'] = setmetatable({}, {__mode = "kv"}) ['table'] = setmetatable({}, {__mode = "kv"})
} },
tableAppearances = setmetatable({}, {__mode = "k"})
} }
setmetatable( inspector, { setmetatable( inspector, {
__index = Inspector, __index = Inspector,
__tostring = function(instance) return table.concat(instance.buffer) end __tostring = function(instance) return table.concat(instance.buffer) end
} ) } )
return inspector:putValue(v)
inspector:countTableAppearances(t)
return inspector:putValue(t)
end
function Inspector:countTableAppearances(t)
if type(t) == 'table' then
if not self.tableAppearances[t] then
self.tableAppearances[t] = 1
for k,v in pairs(t) do
self:countTableAppearances(k)
self:countTableAppearances(v)
end
else
self.tableAppearances[t] = self.tableAppearances[t] + 1
end
self:countTableAppearances(getmetatable(t))
end
end end
function Inspector:tabify() function Inspector:tabify()
@ -127,12 +146,15 @@ function Inspector:commaControl(comma)
end end
function Inspector:putTable(t) function Inspector:putTable(t)
if self:alreadySeen(t) then if self:alreadyVisited(t) then
self:puts('<table ', self:getCounter(t), '>') self:puts('<table ', self:getId(t), '>')
elseif self.level >= self.depth then elseif self.level >= self.depth then
self:puts('{...}') self:puts('{...}')
else else
self:puts('<',self:getCounter(t),'>{') if self.tableAppearances[t] > 1 then
self:puts('<',self:getId(t),'>')
end
self:puts('{')
self:down() self:down()
local length = #t local length = #t
@ -161,6 +183,7 @@ function Inspector:putTable(t)
comma = self:commaControl(comma) comma = self:commaControl(comma)
self:tabify():puts('<metatable> = '):putValue(mt) self:tabify():puts('<metatable> = '):putValue(mt)
end end
self:up() self:up()
if #dictKeys > 0 or mt then -- dictionary table. Justify closing } if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
@ -173,19 +196,19 @@ function Inspector:putTable(t)
return self return self
end end
function Inspector:alreadySeen(v) function Inspector:alreadyVisited(v)
return self.pools[type(v)][v] ~= nil return self.ids[type(v)][v] ~= nil
end end
function Inspector:getCounter(v) function Inspector:getId(v)
local tv = type(v) local tv = type(v)
local current = self.pools[tv][v] local id = self.ids[tv][v]
if not current then if not id then
current = self.counters[tv] + 1 id = self.maxIds[tv] + 1
self.counters[tv] = current self.maxIds[tv] = id
self.pools[tv][v] = current self.ids[tv][v] = id
end end
return current return id
end end
function Inspector:putValue(v) function Inspector:putValue(v)
@ -198,12 +221,11 @@ function Inspector:putValue(v)
elseif tv == 'table' then elseif tv == 'table' then
self:putTable(v) self:putTable(v)
else else
self:puts('<',tv,' ',self:getCounter(v),'>') self:puts('<',tv,' ',self:getId(v),'>')
end end
return self return self
end end
function Inspector:putKey(k) function Inspector:putKey(k)
if isIdentifier(k) then return self:puts(k) end if isIdentifier(k) then return self:puts(k) end
return self:puts( "[" ):putValue(k):puts("]") return self:puts( "[" ):putValue(k):puts("]")

View File

@ -34,7 +34,7 @@ context( 'inspect', function()
end) end)
it('works with functions', function() it('works with functions', function()
assert_equal(inspect({ print, type, print }), '<1>{ <function 1>, <function 2>, <function 1> }') assert_equal(inspect({ print, type, print }), '{ <function 1>, <function 2>, <function 1> }')
end) end)
it('works with booleans', function() it('works with booleans', function()
@ -45,15 +45,15 @@ context( 'inspect', function()
context('tables', function() context('tables', function()
it('works with simple array-like tables', function() it('works with simple array-like tables', function()
assert_equal(inspect({1,2,3}), "<1>{ 1, 2, 3 }" ) assert_equal(inspect({1,2,3}), "{ 1, 2, 3 }" )
end) end)
it('works with nested arrays', function() it('works with nested arrays', function()
assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '<1>{ "a", "b", "c", <2>{ "d", "e" }, "f" }' ) assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '{ "a", "b", "c", { "d", "e" }, "f" }' )
end) end)
it('works with simple dictionary tables', function() it('works with simple dictionary tables', function()
assert_equal(inspect({a = 1, b = 2}), "<1>{\n a = 1,\n b = 2\n}") assert_equal(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
end) end)
it('sorts keys in dictionary tables', function() it('sorts keys in dictionary tables', function()
@ -61,12 +61,12 @@ context( 'inspect', function()
[print] = 1, ["buy more"] = 1, a = 1, [print] = 1, ["buy more"] = 1, a = 1,
[14] = 1, [{c=2}] = 1, [true]= 1 [14] = 1, [{c=2}] = 1, [true]= 1
} }
assert_equal(inspect(t), [[<1>{ 1, 2, 3, assert_equal(inspect(t), [[{ 1, 2, 3,
[14] = 1, [14] = 1,
[true] = 1, [true] = 1,
a = 1, a = 1,
["buy more"] = 1, ["buy more"] = 1,
[<2>{ [{
c = 2 c = 2
}] = 1, }] = 1,
[<function 1>] = 1 [<function 1>] = 1
@ -74,9 +74,9 @@ context( 'inspect', function()
end) end)
it('works with nested dictionary tables', function() it('works with nested dictionary tables', function()
assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[<1>{ assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[{
a = 1, a = 1,
b = <2>{ b = {
c = 2 c = 2
}, },
d = 3 d = 3
@ -84,7 +84,7 @@ context( 'inspect', function()
end) end)
it('works with hybrid tables', function() it('works with hybrid tables', function()
assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[<1>{ "a", <2>{ assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[{ "a", {
b = 1 b = 1
}, 2, }, 2,
["ahoy you"] = 4, ["ahoy you"] = 4,
@ -97,10 +97,10 @@ context( 'inspect', function()
local keys = { [level5] = true } local keys = { [level5] = true }
it('has a default depth of 4', function() it('has a default depth of 4', function()
assert_equal(inspect(level5), [[<1>{ 1, 2, 3, assert_equal(inspect(level5), [[{ 1, 2, 3,
a = <2>{ a = {
b = <3>{ b = {
c = <4>{ c = {
d = {...} d = {...}
} }
} }
@ -108,20 +108,20 @@ context( 'inspect', function()
}]]) }]])
end) end)
it('is modifiable by the user', function() it('is modifiable by the user', function()
assert_equal(inspect(level5, 2), [[<1>{ 1, 2, 3, assert_equal(inspect(level5, 2), [[{ 1, 2, 3,
a = <2>{ a = {
b = {...} b = {...}
} }
}]]) }]])
assert_equal(inspect(level5, 1), [[<1>{ 1, 2, 3, assert_equal(inspect(level5, 1), [[{ 1, 2, 3,
a = {...} a = {...}
}]]) }]])
assert_equal(inspect(level5, 0), "{...}") assert_equal(inspect(level5, 0), "{...}")
assert_equal(inspect(level5, 6), [[<1>{ 1, 2, 3, assert_equal(inspect(level5, 6), [[{ 1, 2, 3,
a = <2>{ a = {
b = <3>{ b = {
c = <4>{ c = {
d = <5>{ d = {
e = 5 e = 5
} }
} }
@ -132,10 +132,10 @@ context( 'inspect', function()
end) end)
it('respects depth on keys', function() it('respects depth on keys', function()
assert_equal(inspect(keys), [[<1>{ assert_equal(inspect(keys), [[{
[<2>{ 1, 2, 3, [{ 1, 2, 3,
a = <3>{ a = {
b = <4>{ b = {
c = {...} c = {...}
} }
} }
@ -159,9 +159,9 @@ context( 'inspect', function()
it('includes the metatable as an extra hash attribute', function() it('includes the metatable as an extra hash attribute', function()
local foo = { foo = 1, __mode = 'v' } local foo = { foo = 1, __mode = 'v' }
local bar = setmetatable({a = 1}, foo) local bar = setmetatable({a = 1}, foo)
assert_equal(inspect(bar), [[<1>{ assert_equal(inspect(bar), [[{
a = 1, a = 1,
<metatable> = <2>{ <metatable> = {
__mode = "v", __mode = "v",
foo = 1 foo = 1
} }
@ -171,9 +171,9 @@ context( 'inspect', function()
it('includes the __tostring metamethod if it exists', function() it('includes the __tostring metamethod if it exists', function()
local foo = { foo = 1, __tostring = function() return 'hello\nworld' end } local foo = { foo = 1, __tostring = function() return 'hello\nworld' end }
local bar = setmetatable({a = 1}, foo) local bar = setmetatable({a = 1}, foo)
assert_equal(inspect(bar), [[<1>{ -- hello\nworld assert_equal(inspect(bar), [[{ -- hello\nworld
a = 1, a = 1,
<metatable> = <2>{ <metatable> = {
__tostring = <function 1>, __tostring = <function 1>,
foo = 1 foo = 1
} }
@ -183,9 +183,9 @@ context( 'inspect', function()
it('includes an error string if __tostring metamethod throws an error', function() it('includes an error string if __tostring metamethod throws an error', function()
local foo = { foo = 1, __tostring = function() error('hello', 0) end } local foo = { foo = 1, __tostring = function() error('hello', 0) end }
local bar = setmetatable({a = 1}, foo) local bar = setmetatable({a = 1}, foo)
assert_equal(inspect(bar), [[<1>{ -- error: hello assert_equal(inspect(bar), [[{ -- error: hello
a = 1, a = 1,
<metatable> = <2>{ <metatable> = {
__tostring = <function 1>, __tostring = <function 1>,
foo = 1 foo = 1
} }