Module:QuickTest

このモジュールについての説明文ページを Module:QuickTest/doc に作成できます
-- Tests whether a module has a test API, and if so, runs these tests

local p = {}

function p.run(titleCurrentPage)
	local title = titleCurrentPage
	local titlesplit = mw.text.split(title, '/', true)
	if titlesplit[1]:find('Module:', 1, true) ~= 1 then return '' end
	if titlesplit[#titlesplit] == 'doc' then
		table.remove(titlesplit)
	end
	title = table.concat(titlesplit, '/')
	
	-- Load the module
	local m = require(title)
	local testFunction = m['runTests']
	local testFunctionType = type(testFunction)
	if ( testFunctionType ~= 'function' and not ( testFunctionType == 'table' and getmetatable(testFunction).__call ) ) then
		return '', title
	end
	
	-- Execute the test function
	local ok, result = pcall(testFunction)
	if ok then
		return result, title
	else
		return 'error', title
	end
end

function cat(title, titleCurrentPage, cat)
	if titleCurrentPage == title then return cat end
	return ''
end

function p.testModule(frame)
	local titleCurrentPage =  ( frame.args and frame.args.title ) or ( frame and frame:preprocess('{{FULLPAGENAME}}') ) or 'Frame not defined.'
	local testResult, title = p.run(titleCurrentPage)
	if testResult == true then
		return '[[File:Accept.png|16px|alt=Ok]] Tests passed. C.f. [[COM:LUA/T#auto]]' .. cat(title, titleCurrentPage, '[[Category:Scribunto modules with tests passed]]')
	elseif testResult == false then
		return '[[File:Bug error.png|16px|alt=Bug]] Tests failed. Run <code>=p.runTests()</code> in the LUA console on [[' .. title .. ']] for more details.' .. cat(title, titleCurrentPage, '[[Category:Scribunto modules with tests failed]]')
	elseif testResult == 'error' then
		return '[[File:Error.png|16px|alt=Error]] Error executing tests.' .. cat(title, titleCurrentPage, '[[Category:Scribunto modules with errors executing tests]]')
	else
		return testResult .. cat(title, titleCurrentPage, '[[Category:Scribunto modules without test API]]')
	end
end

function p.injectResult(frame)
	result = p.testModule(frame)
	if result == '' then return '' end
	
	return ( frame.args['pattern']:gsub('%%result%%', result) )
end

return p
最終更新日時 2014年3月30日 (日) 20:19