mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
added counters for functions, tables, userdata and threads. updated to v1.1
This commit is contained in:
parent
3f50db4dbe
commit
8804f4f1a8
48
inspect.lua
48
inspect.lua
@ -1,5 +1,5 @@
|
|||||||
-----------------------------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------------------------
|
||||||
-- inspect.lua - v1.0 (2011-04)
|
-- inspect.lua - v1.1 (2011-01)
|
||||||
-- Enrique García Cota - enrique.garcia.cota [AT] gmail [DOT] com
|
-- Enrique García Cota - enrique.garcia.cota [AT] gmail [DOT] com
|
||||||
-- human-readable representations of tables.
|
-- human-readable representations of tables.
|
||||||
-- inspired by http://lua-users.org/wiki/TableSerialization
|
-- inspired by http://lua-users.org/wiki/TableSerialization
|
||||||
@ -63,7 +63,25 @@ end
|
|||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
|
|
||||||
function Inspector:new(v, depth)
|
function Inspector:new(v, depth)
|
||||||
local inspector = setmetatable( { buffer = {}, depth = depth, level = 0 }, {
|
local inspector = {
|
||||||
|
buffer = {},
|
||||||
|
depth = depth,
|
||||||
|
level = 0,
|
||||||
|
counters = {
|
||||||
|
['function'] = 0,
|
||||||
|
['userdata'] = 0,
|
||||||
|
['thread'] = 0,
|
||||||
|
['table'] = 0
|
||||||
|
},
|
||||||
|
pools = {
|
||||||
|
['function'] = setmetatable({}, {__mode = "kv"}),
|
||||||
|
['userdata'] = setmetatable({}, {__mode = "kv"}),
|
||||||
|
['thread'] = setmetatable({}, {__mode = "kv"}),
|
||||||
|
['table'] = setmetatable({}, {__mode = "kv"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable( inspector, {
|
||||||
__index = Inspector,
|
__index = Inspector,
|
||||||
__tostring = function(instance) return table.concat(instance.buffer) end
|
__tostring = function(instance) return table.concat(instance.buffer) end
|
||||||
} )
|
} )
|
||||||
@ -97,10 +115,12 @@ function Inspector:putComma(comma)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:putTable(t)
|
function Inspector:putTable(t)
|
||||||
if self.level >= self.depth then
|
if self:alreadySeen(t) then
|
||||||
|
self:puts('<table ', self:getOrCreateCounter(t), '>')
|
||||||
|
elseif self.level >= self.depth then
|
||||||
self:puts('{...}')
|
self:puts('{...}')
|
||||||
else
|
else
|
||||||
self:puts('{')
|
self:puts('<',self:getOrCreateCounter(t),'>{')
|
||||||
self:down()
|
self:down()
|
||||||
|
|
||||||
local length = #t
|
local length = #t
|
||||||
@ -142,17 +162,33 @@ function Inspector:putTable(t)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Inspector:alreadySeen(v)
|
||||||
|
local tv = type(v)
|
||||||
|
return self.pools[tv][v] ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function Inspector:getOrCreateCounter(v)
|
||||||
|
local tv = type(v)
|
||||||
|
local current = self.pools[tv][v]
|
||||||
|
if not current then
|
||||||
|
current = self.counters[tv] + 1
|
||||||
|
self.counters[tv] = current
|
||||||
|
self.pools[tv][v] = current
|
||||||
|
end
|
||||||
|
return current
|
||||||
|
end
|
||||||
|
|
||||||
function Inspector:putValue(v)
|
function Inspector:putValue(v)
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
self:puts(smartQuote(unescape(v)))
|
self:puts(smartQuote(unescape(v)))
|
||||||
elseif tv == 'number' or tv == 'boolean' then
|
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
||||||
self:puts(tostring(v))
|
self:puts(tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' then
|
||||||
self:putTable(v)
|
self:putTable(v)
|
||||||
else
|
else
|
||||||
self:puts('<',tv,'>')
|
self:puts('<',tv,' ',self:getOrCreateCounter(v),'>')
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
@ -35,8 +35,12 @@ context( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test('Should work with nil', function()
|
||||||
|
assert_equal(inspect(nil), 'nil')
|
||||||
|
end)
|
||||||
|
|
||||||
test('Should work with functions', function()
|
test('Should work with functions', function()
|
||||||
assert_equal(inspect(print), '<function>')
|
assert_equal(inspect({ print, type, print }), '<1>{ <function 1>, <function 2>, <function 1> }')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with booleans', function()
|
test('Should work with booleans', function()
|
||||||
@ -47,15 +51,15 @@ context( 'inspect', function()
|
|||||||
context('tables', function()
|
context('tables', function()
|
||||||
|
|
||||||
test('Should work with simple array-like tables', function()
|
test('Should work with simple array-like tables', function()
|
||||||
assert_equal(inspect({1,2,3}), "{ 1, 2, 3 }" )
|
assert_equal(inspect({1,2,3}), "<1>{ 1, 2, 3 }" )
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with nested arrays', function()
|
test('Should work with nested arrays', function()
|
||||||
assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '{ "a", "b", "c", { "d", "e" }, "f" }' )
|
assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '<1>{ "a", "b", "c", <2>{ "d", "e" }, "f" }' )
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with simple dictionary tables', function()
|
test('Should work with simple dictionary tables', function()
|
||||||
assert_equal(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
|
assert_equal(inspect({a = 1, b = 2}), "<1>{\n a = 1,\n b = 2\n}")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should sort keys in dictionary tables', function()
|
test('Should sort keys in dictionary tables', function()
|
||||||
@ -63,22 +67,22 @@ 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, 2, 3,
|
assert_equal(inspect(t), [[<1>{ 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
|
[<function 1>] = 1
|
||||||
}]])
|
}]])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with nested dictionary tables', function()
|
test('Should work with nested dictionary tables', function()
|
||||||
assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[{
|
assert_equal(inspect( {d=3, b={c=2}, a=1} ), [[<1>{
|
||||||
a = 1,
|
a = 1,
|
||||||
b = {
|
b = <2>{
|
||||||
c = 2
|
c = 2
|
||||||
},
|
},
|
||||||
d = 3
|
d = 3
|
||||||
@ -86,7 +90,7 @@ context( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should work with hybrid tables', function()
|
test('Should work with hybrid tables', function()
|
||||||
assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[{ "a", {
|
assert_equal(inspect({ 'a', {b = 1}, 2, c = 3, ['ahoy you'] = 4 }), [[<1>{ "a", <2>{
|
||||||
b = 1
|
b = 1
|
||||||
}, 2,
|
}, 2,
|
||||||
["ahoy you"] = 4,
|
["ahoy you"] = 4,
|
||||||
@ -99,10 +103,10 @@ context( 'inspect', function()
|
|||||||
local keys = { [level5] = true }
|
local keys = { [level5] = true }
|
||||||
|
|
||||||
test('Should have a default depth of 4', function()
|
test('Should have a default depth of 4', function()
|
||||||
assert_equal(inspect(level5), [[{ 1, 2, 3,
|
assert_equal(inspect(level5), [[<1>{ 1, 2, 3,
|
||||||
a = {
|
a = <2>{
|
||||||
b = {
|
b = <3>{
|
||||||
c = {
|
c = <4>{
|
||||||
d = {...}
|
d = {...}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,20 +114,20 @@ context( 'inspect', function()
|
|||||||
}]])
|
}]])
|
||||||
end)
|
end)
|
||||||
test('Should be modifiable by the user', function()
|
test('Should be modifiable by the user', function()
|
||||||
assert_equal(inspect(level5, 2), [[{ 1, 2, 3,
|
assert_equal(inspect(level5, 2), [[<1>{ 1, 2, 3,
|
||||||
a = {
|
a = <2>{
|
||||||
b = {...}
|
b = {...}
|
||||||
}
|
}
|
||||||
}]])
|
}]])
|
||||||
assert_equal(inspect(level5, 1), [[{ 1, 2, 3,
|
assert_equal(inspect(level5, 1), [[<1>{ 1, 2, 3,
|
||||||
a = {...}
|
a = {...}
|
||||||
}]])
|
}]])
|
||||||
assert_equal(inspect(level5, 0), "{...}")
|
assert_equal(inspect(level5, 0), "{...}")
|
||||||
assert_equal(inspect(level5, 6), [[{ 1, 2, 3,
|
assert_equal(inspect(level5, 6), [[<1>{ 1, 2, 3,
|
||||||
a = {
|
a = <2>{
|
||||||
b = {
|
b = <3>{
|
||||||
c = {
|
c = <4>{
|
||||||
d = {
|
d = <5>{
|
||||||
e = 5
|
e = 5
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,10 +138,10 @@ context( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
test('Should respect depth on keys', function()
|
test('Should respect depth on keys', function()
|
||||||
assert_equal(inspect(keys), [[{
|
assert_equal(inspect(keys), [[<1>{
|
||||||
[{ 1, 2, 3,
|
[<2>{ 1, 2, 3,
|
||||||
a = {
|
a = <3>{
|
||||||
b = {
|
b = <4>{
|
||||||
c = {...}
|
c = {...}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,6 +149,15 @@ context( 'inspect', function()
|
|||||||
}]])
|
}]])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test('Should display <table x> instead of repeating an already existing table', function()
|
||||||
|
local a = { 1, 2, 3 }
|
||||||
|
local b = { 'a', 'b', 'c', a }
|
||||||
|
a[4] = b
|
||||||
|
a[5] = a
|
||||||
|
a[6] = b
|
||||||
|
assert_equal(inspect(a), '<1>{ 1, 2, 3, <2>{ "a", "b", "c", <table 1> }, <table 1>, <table 2> }')
|
||||||
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
context('metatables', function()
|
context('metatables', function()
|
||||||
@ -152,9 +165,9 @@ context( 'inspect', function()
|
|||||||
test('Should include the metatable as an extra hash attribute', function()
|
test('Should include 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), [[{
|
assert_equal(inspect(bar), [[<1>{
|
||||||
a = 1,
|
a = 1,
|
||||||
<metatable> = {
|
<metatable> = <2>{
|
||||||
__mode = "v",
|
__mode = "v",
|
||||||
foo = 1
|
foo = 1
|
||||||
}
|
}
|
||||||
@ -164,10 +177,10 @@ context( 'inspect', function()
|
|||||||
test('Should include the __tostring metamethod if it exists', function()
|
test('Should include 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), [[{ -- hello\nworld
|
assert_equal(inspect(bar), [[<1>{ -- hello\nworld
|
||||||
a = 1,
|
a = 1,
|
||||||
<metatable> = {
|
<metatable> = <2>{
|
||||||
__tostring = <function>,
|
__tostring = <function 1>,
|
||||||
foo = 1
|
foo = 1
|
||||||
}
|
}
|
||||||
}]])
|
}]])
|
||||||
|
Loading…
Reference in New Issue
Block a user