Extension:Scribunto/Debug console

Usage

 
Debug console on Module:Color/sandbox

Under the module editing window there is a "Debug console" window, where you can enter any line of text for Lua to evaluate. This includes the ability to return value of a variable using =, for example =a will return value of the variable "a" if it was previously defined. Additionally, the console will also show output from print(), mw.log() and mw.logObject() functions.

To simulate calling a function inside the module, you need to enter a line in the following format: =p.function(frame)
Where "function" is the function in the Lua export table you want to call (for example "main") and "frame" is a frame object analog(?) of the call that invokes the module. (The p is to be taken literally: the export table of the module is always available as p in the debug console, regardless of how it’s called in the module code, or even whether it’s assigned to a variable there.)

Examples

Invoking module directly

Given Module:Test with the following contents:

local p = {}
function p.main(frame)
   local args = frame.args
   mw.log("log test")
   return args[1]..args["delimiter"]..args[2]
end
return p

The following invoke call: {{#invoke:Test|a|delimiter=;|b}}

Can be simulated accurately with the following line in the debug console:

=p.main(mw.getCurrentFrame():newChild{title="Module:Test",args={"a",["delimiter"]=";","b"}})

Note that in this example the element in the export table which is being evaluated is called "main".

Simplified version

If the module you are debugging does not use any frame object methods (such as frame:preprocess()), like the example above, then the frame object analog(?) does not have to be complete, and the debugging line can be simplified into the following:
p.main{args={"a",["delimiter"]=";","b"}}

Invoking through a template

Given Module:Test with the following contents:

local p = {}
function p.main(frame)
   local args = frame:getParent().args
   mw.log("log test")
   return args[1]..args["delimiter"]..args[2]
end
return p

And the following "Template:Test":
{{#invoke|Test|main}}
The following template call: {{Test|a|delimiter=;|b}}
Can be simulated accurately with the following line in the debug console:

=p.main(mw.getCurrentFrame():newChild{title="Template:Test",args={"a",["delimiter"]=";","b"}}:newChild{title="Module:Test",args={}})

Note that the module uses frame:getParent() method, which means the debug line cannot be simplified. This example does not declare any extra arguments directly in the #invoke call, if that's required then appropiate elements should be added to the empty args table near the end of the debug line.