Module:Tests

Une page de Wikiversité, la communauté pédagogique libre.

Ce module sert d'exemple dans la leçon Initiation au Lua avec Scribunto. son utilisation est décrite en détail dans celle-ci et, par conséquent, ce module ne doit pas être modifié sans tenir compte de la leçon.


local p = {}

function f(x)    -- f est une fonction dans laquelle, nous n'avons pas utilisé return
	x = x+7   
end

function p.queretourne()
	local reponse = "La fonction f a retourné "
	local a = 3
	if f(a) == nil then
	 reponse = reponse.."nil"
	end
	return reponse
end

function p.nombres(frame)
	local nombre = false
	for index,valeur in ipairs(frame.args) do
		if tonumber(valeur) ~= nil then
			nombre = true
		end
	end
	if nombre then
		return "Vous avez entré un nombre !"
	else
		return "Vous n'avez pas entré un nombre !"
	end
end

function p.compte(frame)
	local page = "Initiation au Lua avec Scribunto/Chaînes de caractères"
	local article = frame.args[1]
	local title = mw.title.new(page)
	local texte = title.getContent(title)
	local compte,position = 0,0
	repeat
		position = string.find(texte,article,position+1,true)
		if position then
			compte = compte + 1
		end
	until position == nil
		return "Le mot "..article.." a été trouvé "..compte.." fois."
end

function p.compare(frame)
	local page = frame.args[1]
	local a = os.clock()
	local title = mw.title.new(page)
	local b = os.clock()
	local texte = title.getContent(title)
	local c = os.clock()
	local num = title.namespace
	local d = os.clock()
	a = (b-a)*1000000
	b = (c-b)*1000000
	c = (d-c)*1000000
	a = string.format('%d',a)
	b = string.format('%d',b)
	c = string.format('%d',c*10)/10
	local reponse = ""
	local reponse = reponse.."<br />L'instruction '''title = mw.title.new(page)''' s'exécute en "..a.." microsecondes."
	local reponse = reponse.."<br />L'instruction '''texte = title.getContent(title)''' s'exécute en "..b.." microsecondes."
	local reponse = reponse.."<br />L'instruction '''num = title.namespace''' s'exécute en "..c.." microsecondes."
	return reponse
end

function p.bouquine(frame)
	local title = mw.title.new(frame.args[1])
	local page = title.getContent(title)
	return page
end

return p