Snippet for dumping tables when testing

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Snippet for dumping tables when testing

Post by Robin »

A good way to approach this is to look at where the variable is used, and look upwards, and up indentation levels.

You stop when you see:
  • local var: it is a local variable
  • for var = 1, 10 do or for var in somegenerator() do: it is a local variable
  • function bluh(var, bluh): it is a local variable
  • You are at the start of the file: it is a global variable
Example:

Code: Select all

function a (shazam)
 function b()
    print(shazam) -- a local variable, to be exact the argument to a
 end
end
Although there's more to it, what I just described is the gist of it.
Help us help you: attach a .love.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Snippet for dumping tables when testing

Post by pancakepalace »

Thanks for the help. Iv'e updated the code to reflect this cleaner writing.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Snippet for dumping tables when testing

Post by miko »

pancakepalace wrote: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.
If you have access to the source files, you could try:

Code: Select all

 debug.getinfo(funcname, 'S')
and then read the relevant lines from the file.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Snippet for dumping tables when testing

Post by kikito »

I'm also going to put a link to my inspect.lua in case it serves you.

It doesn't do "function dumping", but it numbers functions, tables, etc so they can be told apart easily. It also has a good deal of tests.
When I write def I mean function.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Snippet for dumping tables when testing

Post by pancakepalace »

That's really cool! Your code looks more powerful than mine. I'm sure I'll find a use for it. Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests