Snippet for dumping tables when testing
Posted: Wed Sep 21, 2011 4:31 pm
Hi guys and girls,
I was having difficulty testing my apps without being able to inspect the contents of a table by printing it on the screen like var_dump does in PHP. I pulled my sleeves, poured a cup of coffee, and coded this little snippet.
If you have any suggestions for improvements, that would be most appreciated.
All lua types are supported except functions and userData. If someone knows how to support functions by writing them out as strings, that would be great. I'm not even sure if it's possible.
The main function is below. You can include it in your code however you like. Make it a local function if you can.
You can call the function inside love draw. Here are some pseudo code examples. There are no examples showing the modification of x, y, tab, or newLine, but I'm sure you can figure that out.
I hope that helps somebody. I'm sure finding it useful.
I was having difficulty testing my apps without being able to inspect the contents of a table by printing it on the screen like var_dump does in PHP. I pulled my sleeves, poured a cup of coffee, and coded this little snippet.
If you have any suggestions for improvements, that would be most appreciated.
All lua types are supported except functions and userData. If someone knows how to support functions by writing them out as strings, that would be great. I'm not even sure if it's possible.
The main function is below. You can include it in your code however you like. Make it a local function if you can.
Code: Select all
--[[
Dumps a table for inspection
@param table table to inspect
@param mixed table name [def: 1]
@param number x starting position [def: 10]
@param number y starting position [def: 10]
@param number tab width [def: 20]
@param number newLine height [def: 20]
@param mixed number of newLines at EOL [def: 2]
@return numbers x ending position, y ending position
--]]
function dumpTable(t, k, x, y, tab, NL, numNLatEOF)
k = k or 1
x = x or 10
y = y or 10
tab = tab or 20
NL = NL or 20
if numNLatEOF == nil then numNLatEOF = 2 end
if type(k) == "number" then k = "[" .. k .. "]" end
love.graphics.print(k .. " = {", x, y)
x = x + tab
y = y + NL
for k,v in pairs(t) do
if type(v) == "table" then
x, y = dumpTable(v, k, x, y, tab, NL, false)
else
if v == true then v = "true"
elseif v == false then v = "false"
elseif type(v) == "string" then v = '"' .. v .. '"'
elseif type(v) == "function" then v = "function"
elseif type(v) == "userdata" then v = "userdata"
end
if type(k) == "number" then k = "[" .. k .. "]" end
love.graphics.print(k .. ' = ' .. v, x, y)
end
y = y + NL
end
x = x - tab
love.graphics.print("}", x, y)
if numNLatEOF then y = y + NL * numNLatEOF end
return x, y
end
Code: Select all
-- Ex: Dumping one table
function love.draw()
dumpTable(table)
end
-- Ex: Dumping one table with a name
function love.draw()
dumpTable(table, "tableName")
end
-- Ex: Dumping many tables
function love.draw()
local x, y = dumpTable(table, "tableName")
x, y = dumpTable(table, "tableName", x, y)
x, y = dumpTable(table, "tableName", x, y)
dumpTable(table, "tableName", x, y)
end