mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
nested dictionary tables start working
This commit is contained in:
parent
51153a31c6
commit
b51be52bf0
42
inspect.lua
42
inspect.lua
@ -39,21 +39,36 @@ local function isDictionaryKey(k, length)
|
||||
return not isArrayKey(k, length)
|
||||
end
|
||||
|
||||
local function isDictionary(t)
|
||||
local length = #t
|
||||
for k,_ in pairs(t) do
|
||||
if isDictionaryKey(k, length) then return true end
|
||||
end
|
||||
local sortOrdersByType = {
|
||||
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
|
||||
['function'] = 5, ['userdata'] = 6, ['thread'] = 7
|
||||
}
|
||||
|
||||
function sortKeys(a,b)
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then return sortOrdersByType[ta] < sortOrdersByType[tb] end
|
||||
if ta == 'string' or ta == 'number' then return a < b end
|
||||
return false
|
||||
end
|
||||
|
||||
local function getDictionaryKeys(t)
|
||||
local length = #t
|
||||
local keys = {}
|
||||
for k,_ in pairs(t) do
|
||||
if isDictionaryKey(k, length) then table.insert(keys,k) end
|
||||
end
|
||||
table.sort(keys, sortKeys)
|
||||
return keys
|
||||
end
|
||||
|
||||
local Inspector = {}
|
||||
|
||||
function Inspector:new()
|
||||
return setmetatable( { buffer = {} }, {
|
||||
function Inspector:new(v)
|
||||
local inspector = setmetatable( { buffer = {} }, {
|
||||
__index = Inspector,
|
||||
__tostring = function(instance) return table.concat(instance.buffer) end
|
||||
} )
|
||||
return inspector:addValue(v, 0)
|
||||
end
|
||||
|
||||
function Inspector:puts(...)
|
||||
@ -81,15 +96,16 @@ function Inspector:addTable(t, level)
|
||||
self:addValue(t[i], level + 1)
|
||||
end
|
||||
|
||||
for k,v in pairs(t) do
|
||||
if isDictionaryKey(k, length) then
|
||||
local dictKeys, k, v = getDictionaryKeys(t)
|
||||
|
||||
for i=1, #dictKeys do
|
||||
if needsComma then self:puts(',') end
|
||||
needsComma = true
|
||||
self:tabify(level+1):addKey(k):puts(' = '):addValue(v)
|
||||
end
|
||||
k = dictKeys[i]
|
||||
self:tabify(level+1):addKey(k, level + 1):puts(' = '):addValue(t[k], level + 1)
|
||||
end
|
||||
|
||||
if isDictionary(t) then self:tabify(level) end
|
||||
if #dictKeys > 0 then self:tabify(level) end
|
||||
self:puts('}')
|
||||
return self
|
||||
end
|
||||
@ -117,7 +133,7 @@ function Inspector:addKey(k, level)
|
||||
end
|
||||
|
||||
local function inspect(t)
|
||||
return tostring(Inspector:new():addValue(t,0))
|
||||
return tostring(Inspector:new(t))
|
||||
end
|
||||
|
||||
return inspect
|
||||
|
@ -46,6 +46,7 @@ context( 'inspect', function()
|
||||
|
||||
context('tables', function()
|
||||
|
||||
|
||||
test('Should work with simple array-like tables', function()
|
||||
assert_equal(inspect({1,2,3}), "{1, 2, 3}" )
|
||||
end)
|
||||
@ -54,10 +55,21 @@ context( 'inspect', function()
|
||||
assert_equal(inspect({'a','b','c', {'d','e'}, 'f'}), '{"a", "b", "c", {"d", "e"}, "f"}' )
|
||||
end)
|
||||
|
||||
test('Should work with simple hash-like tables', function()
|
||||
test('Should work with simple dictionary tables', function()
|
||||
assert_equal(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
|
||||
end)
|
||||
|
||||
test('Should work with nested dictionary tables', function()
|
||||
nestedDict = [[{
|
||||
a = 1,
|
||||
b = {
|
||||
c = 2
|
||||
},
|
||||
d = 3
|
||||
}]]
|
||||
assert_equal(inspect( {a=1, b={c=2}, d=3} ), nestedDict)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user