Module:Sandbox
Apparence
Le module Sandbox est utile aux tests relatifs à l’étude du document Lua pour MédiaWiki. Par extension il peut servir à tout type de test sur les modules.
local p = {}
local function reciprocal1(value)
return 1 / value
end
function p.test1(frame)
local value = frame.args[1]
return reciprocal1(value)
end
local function reciprocal2(value)
if value == nil then
error('value must exist')
end
if tonumber(value) == nil then
error('value must be a number')
end
if tonumber(value) == 0 then
error('value must not be 0')
end
return 1 / value
end
function p.test2(frame)
local value = frame.args[1]
return reciprocal2(value)
end
local function reciprocal3(value)
assert(value, 'value must exist')
assert(tonumber(value), 'value must be a number')
assert(tonumber(value) ~= 0, 'value must not be zero')
return 1 / value
end
function p.test3(frame)
local value = frame.args[1]
return reciprocal3(value)
end
function p.test4(frame)
local value = frame.args[1]
if pcall(function () result = reciprocal3(value) end) then
return result
else
return 'Error: Value must exist, must be numeric, and not zero.'
end
end
return p