Module:T/testcases

From Commons Wiki
Jump to navigation Jump to search

Template:Testcases module


-- <nowiki>
local libraryUtil = require("libraryUtil")
local checkType = libraryUtil.checkType

--------------------------------------------------------------------------------
-- COMMON INPUTS
--------------------------------------------------------------------------------
local PARAMETERS = {
    '""',
    '\'\'',
    '',
    '"value"',
    '\'value\'',
    'description',
    'name=""',
    'name=\'\'',
    'name=',
    'name="value"',
    'name=\'value\'',
    'name=description',
    'name=description=',
    'name&#61;""',
    'name&#61;\'\'',
    'name&#61;',
    'name&#61;"value"',
    'name&#61;\'value\'',
    'name&#61;description',
    'name&#61;description&#61;'
}

local PARAMETERS2 = table.concat(PARAMETERS, '|'):gsub('=', '{{=}}')

--------------------------------------------------------------------------------
-- COMMON OUTPUTS
--------------------------------------------------------------------------------

--[[
-- @function buildParameterNode
-- @param {table} args
-- @param[opt] args.index
-- @param[opt] {boolean} args.multiline
-- @param[opt] args.name
-- @param args.value
-- @param args.description
--]]
local function buildParameterNode(args)
	checkType("buildParameterNode", 1, args, "table")

	local index = args.index
	local multiline = args.multiline
	local name = args.name
	local value = args.value
	local description = args.description

	if (
		(value == nil and description == nil)
		or (value ~= nil and description ~= nil)
	) then
		error('Must specify exactly one of "value" or "description"')
	end

	local span = mw.html.create("span")
		:attr("data-t-role", "parameter")
		:attr("data-t-index", index)
		:wikitext(mw.text.nowiki("|"))

	if multiline then
		span:css("display", "block")
	end

	if name then
		span
			:tag("span")
				:attr("data-t-role", "parameter-name")
				:css("font-weight", "bold")
				:wikitext(name)
			:done()
			:wikitext(" = ")
	end

	if value ~= nil then
		span
			:tag("span")
				:attr("data-t-role", "parameter-value")
				:wikitext(value)
			:done()
	else
		span
			:tag("span")
				:attr("data-t-role", "parameter-description")
				:css("opacity", "0.65")
				:wikitext(mw.text.nowiki("<"))
				:wikitext(description)
				:wikitext(mw.text.nowiki(">"))
			:done()
	end

	return span:done()
end

--[[
-- @function buildParameterNodes
-- @param[opt] {boolean} multiline
-- @param[opt] {boolean} noFirstPipe
--]]
local function buildParameterNodes(multiline)
	local function buildIndexedParameterDescription(index, description)
		return buildParameterNode{
			index = index,
			multiline = multiline,
			description = description,
		}
	end
	local function buildIndexedParameterValue(index, value)
		return buildParameterNode{
			index = index,
			multiline = multiline,
			value = value,
		}
	end
	local function buildNamedParameterDescription(index, name, description)
		return buildParameterNode{
			index = index,
			multiline = multiline,
			name = name,
			description = description,
		}
	end
	local function buildNamedParameterValue(index, name, value)
		return buildParameterNode{
			index = index,
			multiline = multiline,
			name = name,
			value = value,
		}
	end

	return mw.html.create()
		:node(buildIndexedParameterValue       ("1", ""))
		:node(buildIndexedParameterValue       ("2", ""))
		:node(buildIndexedParameterDescription ("3", "..."))
		:node(buildIndexedParameterValue       ("4", "value"))
		:node(buildIndexedParameterValue       ("5", "value"))
		:node(buildIndexedParameterDescription ("6", "description"))
		:node(buildNamedParameterValue         ("7", "name", ""))
		:node(buildNamedParameterValue         ("8", "name", ""))
		:node(buildNamedParameterDescription   ("9", "name", "..."))
		:node(buildNamedParameterValue         ("10", "name", "value"))
		:node(buildNamedParameterValue         ("11", "name", "value"))
		:node(buildNamedParameterDescription   ("12", "name", "description"))
		:node(buildNamedParameterDescription   ("13", "name", "description="))
		:node(buildNamedParameterValue         ("14", "name", ""))
		:node(buildNamedParameterValue         ("15", "name", ""))
		:node(buildNamedParameterDescription   ("16", "name", "..."))
		:node(buildNamedParameterValue         ("17", "name", "value"))
		:node(buildNamedParameterValue         ("18", "name", "value"))
		:node(buildNamedParameterDescription   ("19", "name", "description"))
		:node(buildNamedParameterDescription   ("20", "name", "description&#61;"))
end

-- Transclusion
local TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Template:t|t]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Template:t|t]]")
		:done()
		:node(buildParameterNodes(false))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

local MULTILINE_TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Template:t|t]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local MULTILINE_TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Template:t|t]]")
		:done()
		:node(buildParameterNodes(true))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

-- User Transclusion
local USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:User:Example/Template|User:Example/Template]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local USER_TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:User:Example/Template|User:Example/Template]]")
		:done()
		:node(buildParameterNodes(false))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

local MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:User:Example/Template|User:Example/Template]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:User:Example/Template|User:Example/Template]]")
		:done()
		:node(buildParameterNodes(true))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)


-- Main Namespace Transclusion
local MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Sandbox|:Sandbox]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local MAIN_TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Sandbox|:Sandbox]]")
		:done()
		:node(buildParameterNodes(false))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

local MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Sandbox|:Sandbox]]")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "transclusion")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("[[:Sandbox|:Sandbox]]")
		:done()
		:node(buildParameterNodes(true))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

-- Invocation
local INVOCATION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "invocation")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("#invoke:[[Module:t|t]]")
			:wikitext(mw.text.nowiki("|"))
			:wikitext("function")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local INVOCATION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "invocation")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("#invoke:[[Module:t|t]]")
			:wikitext(mw.text.nowiki("|"))
			:wikitext("function")
		:done()
		:node(buildParameterNodes(false))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

local MULTILINE_INVOCATION_MODE_OUTPUT_NOPARAMS = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "invocation")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("#invoke:[[Module:t|t]]")
			:wikitext(mw.text.nowiki("|"))
			:wikitext("function")
		:done()
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)
local MULTILINE_INVOCATION_MODE_OUTPUT = tostring(
	mw.html.create("code")
		:attr("data-t-role", "wrapper")
		:attr("data-t-mode", "invocation")
		:attr("data-t-multiline", "data-t-multiline")
		:css("all", "unset")
		:css("font-family", "monospace")
		:tag("span")
			:attr("data-t-role", "opener")
			:wikitext(mw.text.nowiki("{{"))
			:wikitext("#invoke:[[Module:t|t]]")
			:wikitext(mw.text.nowiki("|"))
			:wikitext("function")
		:done()
		:node(buildParameterNodes(true))
		:tag("span")
			:attr("data-t-role", "closer")
			:wikitext(mw.text.nowiki("}}"))
		:done()
	:done()
)

--------------------------------------------------------------------------------
-- TEST CASES
--------------------------------------------------------------------------------
return {
    transclusion = {
        options = {
            mode = 'method',
            unpk = true,
        },
        tests = {
            -- `Template:` namespace transclusion
            {
                {},
                'no title specified',
                {err = true}
            },
            {
                {'t'},
                TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'t', PARAMETERS},
                TRANSCLUSION_MODE_OUTPUT,
            },
            {
                {nil, nil, {multiline = true}},
                'no title specified',
                {err = true}
            },
            {
                {'t', nil, {multiline = true}},
                MULTILINE_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'t', PARAMETERS, {multiline = true}},
                MULTILINE_TRANSCLUSION_MODE_OUTPUT
            },
            -- `User:` namespace transclusion
            {
                {'User:Example/Template'},
                USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'User:Example/Template', PARAMETERS},
                USER_TRANSCLUSION_MODE_OUTPUT,
            },
            {
                {'User:Example/Template', nil, {multiline = true}},
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'User:Example/Template', PARAMETERS, {multiline = true}},
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT,
            },
            -- Main namespace transclusion
            {
                {':Sandbox'},
                MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {':Sandbox', PARAMETERS},
                MAIN_TRANSCLUSION_MODE_OUTPUT,
            },
            {
                {':Sandbox', nil, {multiline = true}},
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {':Sandbox', PARAMETERS, {multiline = true}},
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT,
            },
        }
    },

    invocation = {
        options = {
            mode = 'method',
            unpk = true,
        },
        tests = {
            {
                {},
                'no module specified',
                {err = true}
            },
            {
                {'t'},
                'no function specified',
                {err = true}
            },
            {
                {'t', 'function'},
                INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'t', 'function', PARAMETERS},
                INVOCATION_MODE_OUTPUT
            },
            {
                {nil, nil, nil, {multiline = true}},
                'no module specified',
                {err = true}
            },
            {
                {'t', nil, nil, {multiline = true}},
                'no function specified',
                {err = true}
            },
            {
                {'t', 'function', nil, {multiline = true}},
                MULTILINE_INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                {'t', 'function', PARAMETERS, {multiline = true}},
                MULTILINE_INVOCATION_MODE_OUTPUT
            }
        }
    },

    main = {
        options = {
            mode = 'invocation',
            preprocess = true,
        },
        tests = {
            --------------------------------------------------------------------
            -- TRANSCLUSION MODE
            --------------------------------------------------------------------
            {
                '',
                '<strong class="error">Error: no title specified.</strong>'
            },
            -- `Template:` namespace transclusion
            {
                't',
                TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'t', PARAMETERS2}, '|'),
                TRANSCLUSION_MODE_OUTPUT
            },
            -- `User:` namespace transclusion
            {
                'User:Example/Template',
                USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'User:Example/Template', PARAMETERS2}, '|'),
                USER_TRANSCLUSION_MODE_OUTPUT,
            },
            -- Main namespace transclusion
            {
                ':Sandbox',
                MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({':Sandbox', PARAMETERS2}, '|'),
                MAIN_TRANSCLUSION_MODE_OUTPUT,
            },

            --------------------------------------------------------------------
            -- MULTILINE TRANSCLUSION MODE (FULL FLAG NAMES)
            --------------------------------------------------------------------
            {
                'multiline=true',
                '<strong class="error">Error: no title specified.</strong>'
            },
            -- `Template:` namespace transclusion
            {
                'multiline=true|t',
                MULTILINE_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'multiline=true', 't', PARAMETERS2}, '|'),
                MULTILINE_TRANSCLUSION_MODE_OUTPUT
            },
            -- `User:` namespace transclusion
            {
                'multiline=true|User:Example/Template',
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'multiline=true', 'User:Example/Template', PARAMETERS2}, '|'),
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT,
            },
            -- Main namespace transclusion
            {
                'multiline=true|:Sandbox',
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'multiline=true', ':Sandbox', PARAMETERS2}, '|'),
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT,
            },

            --------------------------------------------------------------------
            -- MULTILINE TRANSCLUSION MODE (TRUNCATED FLAG NAMES)
            --------------------------------------------------------------------
            {
                'm=true',
                '<strong class="error">Error: no title specified.</strong>'
            },
            -- `Template:` namespace transclusion
            {
                'm=true|t',
                MULTILINE_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'m=true', 't', PARAMETERS2}, '|'),
                MULTILINE_TRANSCLUSION_MODE_OUTPUT
            },
            -- `User:` namespace transclusion
            {
                'm=true|User:Example/Template',
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'m=true', 'User:Example/Template', PARAMETERS2}, '|'),
                MULTILINE_USER_TRANSCLUSION_MODE_OUTPUT,
            },
            -- Main namespace transclusion
            {
                'm=true|:Sandbox',
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'m=true', ':Sandbox', PARAMETERS2}, '|'),
                MULTILINE_MAIN_TRANSCLUSION_MODE_OUTPUT,
            },

            --------------------------------------------------------------------
            -- INVOCATION MODE (FULL FLAG NAMES)
            --------------------------------------------------------------------
            {
                'invocation=true',
                '<strong class="error">Error: no module specified.</strong>'
            },
            {
                'invocation=true|t',
                '<strong class="error">Error: no function specified.</strong>'
            },
            {
                'invocation=true|t|function',
                INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'invocation=true', 't', 'function', PARAMETERS2}, '|'),
                INVOCATION_MODE_OUTPUT
            },

            --------------------------------------------------------------------
            -- INVOCATION MODE (TRUNCATED FLAG NAMES)
            --------------------------------------------------------------------
            {
                'i=true',
                '<strong class="error">Error: no module specified.</strong>'
            },
            {
                'i=true|t',
                '<strong class="error">Error: no function specified.</strong>'
            },
            {
                'i=true|t|function',
                INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'i=true', 't', 'function', PARAMETERS2}, '|'),
                INVOCATION_MODE_OUTPUT
            },

            --------------------------------------------------------------------
            -- MULTILINE INVOCATION MODE (FULL FLAG NAMES)
            --------------------------------------------------------------------
            {
                'invocation=true|multiline=true',
                '<strong class="error">Error: no module specified.</strong>'
            },
            {
                'invocation=true|multiline=true|t',
                '<strong class="error">Error: no function specified.</strong>'
            },
            {
                'invocation=true|multiline=true|t|function',
                MULTILINE_INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'invocation=true', 'multiline=true', 't', 'function', PARAMETERS2}, '|'),
                MULTILINE_INVOCATION_MODE_OUTPUT
            },

            --------------------------------------------------------------------
            -- MULTILINE INVOCATION MODE (TRUNCATED FLAG NAMES)
            --------------------------------------------------------------------
            {
                'i=true|m=true',
                '<strong class="error">Error: no module specified.</strong>'
            },
            {
                'i=true|m=true|t',
                '<strong class="error">Error: no function specified.</strong>'
            },
            {
                'i=true|m=true|t|function',
                MULTILINE_INVOCATION_MODE_OUTPUT_NOPARAMS,
            },
            {
                table.concat({'i=true', 'm=true', 't', 'function', PARAMETERS2}, '|'),
                MULTILINE_INVOCATION_MODE_OUTPUT
            }
        }
    }
}