Модуль:Test

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску

Модуль Lua для нужд тестирования.


local m = {}
m.serialise = function (tbl)
	-- Список уже выведенных таблиц, чтобы избежать бесконечной рекурсии. Таблица — индекс!:
	local shown_tables = {}
	local function serialise_helper (tbl, level)
	    local tp = type (tbl)
	    if tp == 'string' then
	    	if tbl == '' then
	    		return "'' --[=[ empty string ]=]"
	    	else
	        	return "'" .. tbl .. "'"
	        end
	    elseif tp == 'table' then
	    	if shown_tables [tbl] then
	    		-- таблица уже была показана:
	    		return '--[=[ table already shown, see above ]=]'
	    	else
	    		-- таблица показывается впервые:
	    		shown_tables [tbl] = true
	    	end
	    	local tab = '    '
	    	local ret = ' {\n'
	    	local offset = mw.ustring.rep (tab, level)
	    	for key, value in pairs (tbl) do
		        ret = ret .. offset .. tab .. mw.ustring.gsub ('[' .. serialise_helper (key, level + 1) .. '] = ', "%['(_*[a-zA-Z][a-zA-Z_0-9]+)'%] =", '%1 = ') .. serialise_helper (value, level + 1) .. ',\n'
		    end
	    	ret = mw.ustring.gsub (ret, ',$', '') .. offset .. '}'
	    	return ret
	    else
	    	if tbl ~= nil then
	    		return tostring (tbl)
	    	else
	    		return 'nil'
	    	end
	    end
	end
	return serialise_helper (tbl, 0)
end

m.match = function (frame)
    local matches = mw.ext.massprocess.match (frame.args [1], frame.args [2], frame.args [3])
    return m.serialise (matches)
end
m.replace = function (frame)
    return mw.ext.massprocess.replace (frame.args [1], frame.args [2], frame.args [3])
end
m.ask = function (frame)
	return m.serialise (mw.ext.smw.ask (frame.args))
end
 
return m