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
46
inspect.lua
46
inspect.lua
@ -39,21 +39,36 @@ local function isDictionaryKey(k, length)
|
|||||||
return not isArrayKey(k, length)
|
return not isArrayKey(k, length)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isDictionary(t)
|
local sortOrdersByType = {
|
||||||
local length = #t
|
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
|
||||||
for k,_ in pairs(t) do
|
['function'] = 5, ['userdata'] = 6, ['thread'] = 7
|
||||||
if isDictionaryKey(k, length) then return true end
|
}
|
||||||
end
|
|
||||||
|
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
|
return false
|
||||||
end
|
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 = {}
|
local Inspector = {}
|
||||||
|
|
||||||
function Inspector:new()
|
function Inspector:new(v)
|
||||||
return setmetatable( { buffer = {} }, {
|
local inspector = setmetatable( { buffer = {} }, {
|
||||||
__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)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:puts(...)
|
function Inspector:puts(...)
|
||||||
@ -81,15 +96,16 @@ function Inspector:addTable(t, level)
|
|||||||
self:addValue(t[i], level + 1)
|
self:addValue(t[i], level + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k,v in pairs(t) do
|
local dictKeys, k, v = getDictionaryKeys(t)
|
||||||
if isDictionaryKey(k, length) then
|
|
||||||
if needsComma then self:puts(',') end
|
for i=1, #dictKeys do
|
||||||
needsComma = true
|
if needsComma then self:puts(',') end
|
||||||
self:tabify(level+1):addKey(k):puts(' = '):addValue(v)
|
needsComma = true
|
||||||
end
|
k = dictKeys[i]
|
||||||
|
self:tabify(level+1):addKey(k, level + 1):puts(' = '):addValue(t[k], level + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
if isDictionary(t) then self:tabify(level) end
|
if #dictKeys > 0 then self:tabify(level) end
|
||||||
self:puts('}')
|
self:puts('}')
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@ -117,7 +133,7 @@ function Inspector:addKey(k, level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function inspect(t)
|
local function inspect(t)
|
||||||
return tostring(Inspector:new():addValue(t,0))
|
return tostring(Inspector:new(t))
|
||||||
end
|
end
|
||||||
|
|
||||||
return inspect
|
return inspect
|
||||||
|
@ -46,6 +46,7 @@ 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, 2, 3}" )
|
||||||
end)
|
end)
|
||||||
@ -54,10 +55,21 @@ context( 'inspect', 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'}), '{"a", "b", "c", {"d", "e"}, "f"}' )
|
||||||
end)
|
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}")
|
assert_equal(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
|
||||||
end)
|
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)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user