Module:File link/testcases

-- Unit tests for [[Module:File link]]. Click on the talk page to run the tests.

local mFileLink = require('Module:File link/sandbox')
local main = mFileLink.main
local _main = mFileLink._main
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

function suite:assertPositionalParamExists(key)
	self:assertEquals(
		_main{file = 'Example.png', [key] = 'some value'},
		'[[File:Example.png|some value]]'
	)
end

function suite:assertNamedParamExists(key)
	self:assertEquals(
		_main{file = 'Example.png', [key] = 'some value'},
		'[[File:Example.png|' .. key .. '=some value]]'
	)
end

--------------------------------------------------------------------------------
-- Basic tests
--------------------------------------------------------------------------------

function suite:testBadInput()
	self:assertThrows(_main, "bad argument #1 to '_main' (table expected, got nil)", nil)
	self:assertThrows(_main, "bad argument #1 to '_main' (table expected, got string)", nil, 'foo')
	self:assertThrows(_main, "bad argument #1 to '_main' (table expected, got boolean)", nil, false)
	self:assertThrows(_main, "bad argument #1 to '_main' (table expected, got number)", nil, 9)
end

function suite:testBadFile()
	self:assertThrows(_main, "type error in 'file' parameter of '_main' (expected string, got nil)", nil, {})
	self:assertThrows(_main, "type error in 'file' parameter of '_main' (expected string, got boolean)", nil, {file = true})
	self:assertThrows(_main, "type error in 'file' parameter of '_main' (expected string, got number)", nil, {file = 123})
	self:assertThrows(_main, "type error in 'file' parameter of '_main' (expected string, got table)", nil, {file = {}})
end

function suite:testFile()
	self:assertEquals(_main{file = 'Example.png'}, '[[File:Example.png]]')
end

--------------------------------------------------------------------------------
-- Positional parameters
--------------------------------------------------------------------------------

function suite:testFormat()
	self:assertPositionalParamExists('format')
end

function suite:testLocation()
	self:assertPositionalParamExists('location')
end

function suite:testAlignment()
	self:assertPositionalParamExists('alignment')
end

function suite:testSize()
	self:assertPositionalParamExists('size')
end

function suite:testCaption()
	self:assertPositionalParamExists('caption')
end

--------------------------------------------------------------------------------
-- Named parameters
--------------------------------------------------------------------------------

function suite:testUpright()
	self:assertNamedParamExists('upright')
end

function suite:testLink()
	self:assertNamedParamExists('link')
end

function suite:testAlt()
	self:assertNamedParamExists('alt')
end

function suite:testPage()
	self:assertNamedParamExists('page')
end

function suite:testClass()
	self:assertNamedParamExists('class')
end

function suite:testLang()
	self:assertNamedParamExists('lang')
end

function suite:testStart()
	self:assertNamedParamExists('start')
end

function suite:testEnd()
	self:assertNamedParamExists('end')
end

function suite:testThumbtime()
	self:assertNamedParamExists('thumbtime')
end

--------------------------------------------------------------------------------
-- Special parameters
--------------------------------------------------------------------------------

function suite:testBorder()
	self:assertEquals(
		_main{file = 'Example.png', border = true},
		'[[File:Example.png|border]]'
	)
	self:assertEquals(
		_main{file = 'Example.png', border = 'yes'},
		'[[File:Example.png|border]]'
	)
	self:assertEquals(
		_main{file = 'Example.png', border = 'y'},
		'[[File:Example.png|border]]'
	)
	self:assertEquals(
		_main{file = 'Example.png', border = 'YES'},
		'[[File:Example.png|border]]'
	)
end

function suite:testFormatfileWithFormat()
	self:assertEquals(
		_main{file = 'Example.png', format = 'a format', formatfile = 'foo'},
		'[[File:Example.png|a format=foo]]'
	)
end

function suite:testFormatfileWithoutFormat()
	self:assertEquals(
		_main{file = 'Example.png', formatfile = 'foo'},
		'[[File:Example.png]]'
	)
end

--------------------------------------------------------------------------------
-- Order
--------------------------------------------------------------------------------

function suite:testOrder()
	local params = {
		'file',
		'format',
		'formatfile',
		'border',
		'location',
		'alignment',
		'size',
		'upright',
		'link',
		'alt',
		'page',
		'class',
		'lang',
		'start',
		'end',
		'thumbtime',
		'caption'
	}
	local args = {}
	for i, param in ipairs(params) do
		args[param] = 'param ' .. i
	end
	args.border = true -- border is a special case
	local result = _main(args)

	-- Ugly hack to make border work whatever position it's in.
	local borderNum
	for i, v in ipairs(params) do
		if v == 'border' then
			borderNum = i
		end
	end
	result = result:gsub('border', 'param ' .. borderNum, 1)

	local i = 0
	for s in string.gmatch(result, 'param %d+') do
		i = i + 1
		suite:assertEquals(s, 'param ' .. i)
	end
end

return suite

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.