Content deleted Content added
m →See also: Updated one link/entry to use current page/template name. |
|||
Line 20:
== Saving fall-back stable versions ==
To avoid facing too many errors at once, the general strategy is to make "one small change" at a time, do a run show-preview, and then save that tested version after a few good changes to have a stable version to restore, in case terrible errors occur after numerous later changes. Note: the saving of each working copy can be done to a set of offline text files, perhaps each with a specific version name (and internal comments), rather than saving each version into the Wikipedia module revisions.
== How debug==
===Simple review===
To see the value of a variable in a single point of the module:
====Modifying the code====
The <code>error()</code> function (for any point in the module) can be used. So to know the a variable (named here <code>var</code>) value, only adding a line in the module with <code>error(var)</code> is required.
But in the case that variable was a table (<code>tab</code>, in the next explanation) <code>mw.dumpObject(tab)</code> will be used or, if it does not have nested tables, <code>table.concat (tab,',')</code> can also be used as parameter in <code>error()</code> function.
====Without changing the code====
To obtain variables and values returned from functions (not local in both cases) the "Debug console" can be used. The "Debug console" appears below in the module page (when it is in edit mode). Then <code>mw.log</code> and <code>mw.logObject</code> will be used. Let's see its usefulness in the next example (module that can be found [[Module:Example of lua debugging|here]]):
<syntaxhighlight lang="Lua">
local p = {}
p.Hello = 'Hello'
function p.calc(num)
return num
end
function p.sum_mult(num)
return num + num, num * num
end
function p.mtable(num)
return {num, num+1}
end
return p
</syntaxhighlight>
Requests to "Debug Console":
{| class="wikitable"
|-
! Request !! Returned value
|-
| {{color|blue|mw.log(p.Hello)}}|| "Hello"
|-
| {{color|blue|mw.log(p.calc(10/2)}} || 5
|-
| {{color|blue|mw.log(p.sum_mult(3)))}} || 3 9
|-
| {{color|blue|mw.log(p.mtable(4))}} || table
|-
| {{color|blue|mw.logObject(p.table(4))}} ||
table#1 {
:8,
:9,
}
|}
The Debug console does not store the requests, then they will have to be copied or rewritten again in each module modification.
===Review of the flow and the variable values in several points===
The functions of the [[Module:SimpleDebug]] are used for cases such as those mentioned above or for more complex cases:
* When a variable or the returned value (or values) by a function are local.
* When a variable (or several ones) take the values (to see) in different points (even in required modules).
* To see if the flow of the program goes through a point (to which you will label).
* To limit the number of values returned from a loop or set conditions to enable the registration of values.
== Debugging older modules ==
|