<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pt-BR">
	<id>https://inricristopedia.org/w/index.php?action=history&amp;feed=atom&amp;title=M%C3%B3dulo%3AUnitTests</id>
	<title>Módulo:UnitTests - Histórico de revisão</title>
	<link rel="self" type="application/atom+xml" href="https://inricristopedia.org/w/index.php?action=history&amp;feed=atom&amp;title=M%C3%B3dulo%3AUnitTests"/>
	<link rel="alternate" type="text/html" href="https://inricristopedia.org/w/index.php?title=M%C3%B3dulo:UnitTests&amp;action=history"/>
	<updated>2026-09-15T06:53:14Z</updated>
	<subtitle>Histórico de revisões para esta página neste wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>https://inricristopedia.org/w/index.php?title=M%C3%B3dulo:UnitTests&amp;diff=555&amp;oldid=prev</id>
		<title>In7cr7pe7: uma edição</title>
		<link rel="alternate" type="text/html" href="https://inricristopedia.org/w/index.php?title=M%C3%B3dulo:UnitTests&amp;diff=555&amp;oldid=prev"/>
		<updated>2020-08-08T14:36:48Z</updated>

		<summary type="html">&lt;p&gt;uma edição&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;pt-BR&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Edição anterior&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Edição das 14h36min de 8 de agosto de 2020&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;pt-BR&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(Sem diferença)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>In7cr7pe7</name></author>
		
	</entry>
	<entry>
		<id>https://inricristopedia.org/w/index.php?title=M%C3%B3dulo:UnitTests&amp;diff=554&amp;oldid=prev</id>
		<title>br&gt;Ederporto: typo</title>
		<link rel="alternate" type="text/html" href="https://inricristopedia.org/w/index.php?title=M%C3%B3dulo:UnitTests&amp;diff=554&amp;oldid=prev"/>
		<updated>2018-07-24T02:35:34Z</updated>

		<summary type="html">&lt;p&gt;typo&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Página nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- UnitTester provides unit testing for other Lua scripts. For details see [[Wikipedia:Lua#Unit_testing]].&lt;br /&gt;
-- For user documentation see talk page.&lt;br /&gt;
local UnitTester = {}&lt;br /&gt;
&lt;br /&gt;
local frame, tick, cross&lt;br /&gt;
local result_table_header = &amp;quot;{|class=\&amp;quot;wikitable\&amp;quot;\n|+ %s\n! !! Texto !! Esperado !! Atual&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local result_table = { n = 0 }&lt;br /&gt;
local result_table_mt = {&lt;br /&gt;
	insert = function (self, ...)&lt;br /&gt;
		local n = self.n&lt;br /&gt;
		for i = 1, select('#', ...) do&lt;br /&gt;
			n = n + 1&lt;br /&gt;
			self[n] = select(i, ...)&lt;br /&gt;
		end&lt;br /&gt;
		self.n = n&lt;br /&gt;
	end,&lt;br /&gt;
	insert_format = function (self, ...)&lt;br /&gt;
		self:insert(string.format(...))&lt;br /&gt;
	end,&lt;br /&gt;
	concat = table.concat&lt;br /&gt;
}&lt;br /&gt;
result_table_mt.__index = result_table_mt&lt;br /&gt;
setmetatable(result_table, result_table_mt)&lt;br /&gt;
&lt;br /&gt;
local num_failures = 0&lt;br /&gt;
&lt;br /&gt;
function first_difference(s1, s2)&lt;br /&gt;
    if s1 == s2 then return '' end&lt;br /&gt;
    local max = math.min(#s1, #s2)&lt;br /&gt;
    for i = 1, max do&lt;br /&gt;
        if s1:sub(i,i) ~= s2:sub(i,i) then return i end&lt;br /&gt;
    end&lt;br /&gt;
    return max + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function return_varargs(...)&lt;br /&gt;
	return ...&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:preprocess_equals(text, expected, options)&lt;br /&gt;
    local actual = frame:preprocess(text)&lt;br /&gt;
    if actual == expected then&lt;br /&gt;
        result_table:insert('| ', tick)&lt;br /&gt;
    else&lt;br /&gt;
        result_table:insert('| ', cross)&lt;br /&gt;
        num_failures = num_failures + 1&lt;br /&gt;
    end&lt;br /&gt;
    local maybe_nowiki = (options and options.nowiki) and mw.text.nowiki or return_varargs&lt;br /&gt;
    local differs_at = self.differs_at and (' \n| ' .. first_difference(expected, actual)) or ''&lt;br /&gt;
    result_table:insert(' \n| ', mw.text.nowiki(text), ' \n| ',&lt;br /&gt;
    	maybe_nowiki(expected), ' \n| ', maybe_nowiki(actual), differs_at,&lt;br /&gt;
    	&amp;quot;\n|-\n&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:preprocess_equals_many(prefix, suffix, cases, options)&lt;br /&gt;
    for _, case in ipairs(cases) do&lt;br /&gt;
        self:preprocess_equals(prefix .. case[1] .. suffix, case[2], options)&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:preprocess_equals_preprocess(text1, text2, options)&lt;br /&gt;
    local actual = frame:preprocess(text1)&lt;br /&gt;
    local expected = frame:preprocess(text2)&lt;br /&gt;
    if actual == expected then&lt;br /&gt;
        result_table:insert('| ', tick)&lt;br /&gt;
    else&lt;br /&gt;
        result_table:insert('| ', cross)&lt;br /&gt;
        num_failures = num_failures + 1&lt;br /&gt;
    end&lt;br /&gt;
    local maybe_nowiki = (options and options.nowiki) and mw.text.nowiki or return_varargs&lt;br /&gt;
    local differs_at = self.differs_at and (' \n| ' .. first_difference(expected, actual)) or ''&lt;br /&gt;
    result_table:insert(' \n| ', mw.text.nowiki(text1), ' \n| ',&lt;br /&gt;
    	maybe_nowiki(expected), ' \n| ', maybe_nowiki(actual), differs_at,&lt;br /&gt;
    	&amp;quot;\n|-\n&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:preprocess_equals_preprocess_many(prefix1, suffix1, prefix2, suffix2, cases, options)&lt;br /&gt;
    for _, case in ipairs(cases) do&lt;br /&gt;
        self:preprocess_equals_preprocess(prefix1 .. case[1] .. suffix1, prefix2 .. (case[2] and case[2] or case[1]) .. suffix2, options)&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:equals(name, actual, expected, options)&lt;br /&gt;
    if actual == expected then&lt;br /&gt;
        result_table:insert('| ', tick)&lt;br /&gt;
    else&lt;br /&gt;
        result_table:insert('| ', cross)&lt;br /&gt;
        num_failures = num_failures + 1&lt;br /&gt;
    end&lt;br /&gt;
    local maybe_nowiki = (options and options.nowiki) and mw.text.nowiki or return_varargs&lt;br /&gt;
    local differs_at = self.differs_at and (' \n| ' .. first_difference(expected, actual)) or ''&lt;br /&gt;
    local display = options and options.display or function(x) return x end&lt;br /&gt;
    result_table:insert(' \n| ', name, ' \n| ',&lt;br /&gt;
    	maybe_nowiki(tostring(display(expected))), ' \n| ',&lt;br /&gt;
    	maybe_nowiki(tostring(display(actual))), differs_at, &amp;quot;\n|-\n&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function deep_compare(t1, t2, ignore_mt)&lt;br /&gt;
    local ty1 = type(t1)&lt;br /&gt;
    local ty2 = type(t2)&lt;br /&gt;
    if ty1 ~= ty2 then return false end&lt;br /&gt;
    if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end&lt;br /&gt;
&lt;br /&gt;
    local mt = getmetatable(t1)&lt;br /&gt;
    if not ignore_mt and mt and mt.__eq then return t1 == t2 end&lt;br /&gt;
&lt;br /&gt;
    for k1, v1 in pairs(t1) do&lt;br /&gt;
        local v2 = t2[k1]&lt;br /&gt;
        if v2 == nil or not deep_compare(v1, v2) then return false end&lt;br /&gt;
    end&lt;br /&gt;
    for k2, v2 in pairs(t2) do&lt;br /&gt;
        local v1 = t1[k2]&lt;br /&gt;
        if v1 == nil or not deep_compare(v1, v2) then return false end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function val_to_str(v)&lt;br /&gt;
    if type(v) == 'string' then&lt;br /&gt;
        v = mw.ustring.gsub(v, '\n', '\\n')&lt;br /&gt;
        if mw.ustring.match(mw.ustring.gsub(v, '[^\'&amp;quot;]', ''), '^&amp;quot;+$') then&lt;br /&gt;
            return &amp;quot;'&amp;quot; .. v .. &amp;quot;'&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
        return '&amp;quot;' .. mw.ustring.gsub(v, '&amp;quot;', '\\&amp;quot;' ) .. '&amp;quot;'&lt;br /&gt;
    else&lt;br /&gt;
        return type(v) == 'table' and table_to_str(v) or tostring(v)&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function table_key_to_str(k)&lt;br /&gt;
    if type(k) == 'string' and mw.ustring.match(k, '^[_%a][_%a%d]*$') then&lt;br /&gt;
        return k&lt;br /&gt;
    else&lt;br /&gt;
        return '[' .. val_to_str(k) .. ']'&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function table_to_str(tbl)&lt;br /&gt;
    local result, done = {}, {}&lt;br /&gt;
    for k, v in ipairs(tbl) do&lt;br /&gt;
        table.insert(result, val_to_str(v))&lt;br /&gt;
        done[k] = true&lt;br /&gt;
    end&lt;br /&gt;
    for k, v in pairs(tbl) do&lt;br /&gt;
        if not done[k] then&lt;br /&gt;
            table.insert(result, table_key_to_str(k) .. '=' .. val_to_str(v))&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return '{' .. table.concat(result, ',') .. '}'&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:equals_deep(name, actual, expected, options)&lt;br /&gt;
    if deep_compare(actual, expected) then&lt;br /&gt;
        result_table:insert('| ', tick)&lt;br /&gt;
    else&lt;br /&gt;
        result_table:insert('| ', cross)&lt;br /&gt;
        num_failures = num_failures + 1&lt;br /&gt;
    end&lt;br /&gt;
    local maybe_nowiki = (options and options.nowiki) and mw.text.nowiki or return_varargs&lt;br /&gt;
    local actual_str = val_to_str(actual)&lt;br /&gt;
    local expected_str = val_to_str(expected)&lt;br /&gt;
    local differs_at = self.differs_at and (' \n| ' .. first_difference(expected_str, actual_str)) or ''&lt;br /&gt;
    result_table:insert(' \n| ', name, ' \n| ', maybe_nowiki(expected_str),&lt;br /&gt;
    	' \n| ', maybe_nowiki(actual_str), differs_at, &amp;quot;\n|-\n&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:iterate(examples, func)&lt;br /&gt;
	require 'libraryUtil'.checkType('iterate', 1, examples, 'table')&lt;br /&gt;
	if type(func) == 'string' then&lt;br /&gt;
		func = self[func]&lt;br /&gt;
	elseif type(func) ~= 'function' then&lt;br /&gt;
		error((&amp;quot;Argumento #2 ruim para 'iterar' (função ou texto esperados, recebido %s)&amp;quot;)&lt;br /&gt;
			:format(type(func)), 2)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for i, example in ipairs(examples) do&lt;br /&gt;
		if type(example) == 'table' then&lt;br /&gt;
			func(self, unpack(example))&lt;br /&gt;
		elseif type(example) == 'string' then&lt;br /&gt;
			self:heading(example)&lt;br /&gt;
		else&lt;br /&gt;
			error(('Mal exemplo #%d (tabela esperada, recebido %s)')&lt;br /&gt;
				:format(i, type(example)), 2)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:heading(text)&lt;br /&gt;
	result_table:insert_format(' ! colspan=&amp;quot;%u&amp;quot; style=&amp;quot;text-align: left&amp;quot; | %s \n |- \n ',&lt;br /&gt;
		self.columns, text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:run(frame_arg)&lt;br /&gt;
    frame = frame_arg&lt;br /&gt;
    self.frame = frame&lt;br /&gt;
    self.differs_at = frame.args['differs_at']&lt;br /&gt;
    tick = frame:preprocess('{{Sim}}')&lt;br /&gt;
    cross = frame:preprocess('{{Não}}')&lt;br /&gt;
&lt;br /&gt;
    local table_header = result_table_header&lt;br /&gt;
	self.columns = 4&lt;br /&gt;
    if self.differs_at then&lt;br /&gt;
        table_header = table_header .. ' !! Diferem em'&lt;br /&gt;
        self.columns = self.columns + 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Sort results into alphabetical order.&lt;br /&gt;
    local self_sorted = {}&lt;br /&gt;
    for key,value in pairs(self) do&lt;br /&gt;
        if key:find('^test') then&lt;br /&gt;
            table.insert(self_sorted, key)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    table.sort(self_sorted)&lt;br /&gt;
    -- Add results to the results table.&lt;br /&gt;
    for i,value in ipairs(self_sorted) do&lt;br /&gt;
        result_table:insert_format(table_header .. &amp;quot;\n|-\n&amp;quot;, value)&lt;br /&gt;
        self[value](self)&lt;br /&gt;
        result_table:insert(&amp;quot;|}\n\n&amp;quot;)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return (num_failures == 0 and &amp;quot;&amp;lt;b style=\&amp;quot;color:#008000\&amp;quot;&amp;gt;Todos os testes passaram.&amp;lt;/b&amp;gt;&amp;quot; or &amp;quot;&amp;lt;b style=\&amp;quot;color:#800000\&amp;quot;&amp;gt;'''&amp;quot; .. num_failures .. &amp;quot; testes falharam.&amp;lt;/b&amp;gt;&amp;quot;) .. &amp;quot;\n\n&amp;quot; .. frame:preprocess(result_table:concat())&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function UnitTester:new()&lt;br /&gt;
    local o = {}&lt;br /&gt;
    setmetatable(o, self)&lt;br /&gt;
    self.__index = self&lt;br /&gt;
    return o&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local p = UnitTester:new()&lt;br /&gt;
function p.run_tests(frame) return p:run(frame) end&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>br&gt;Ederporto</name></author>
		
	</entry>
</feed>