Jump to content
Wikipedia The Free Encyclopedia

Module:File link/testcases

From Wikipedia, the free encyclopedia
This is the test cases page for the module Module:File link. Results of the test cases.
 -- Unit tests for [[Module:File link]]. Click on the talk page to run the tests.

 localmFileLink=require('Module:File link/sandbox')
 localmain=mFileLink.main
 local_main=mFileLink._main
 localScribuntoUnit=require('Module:ScribuntoUnit')
 localsuite=ScribuntoUnit:new()

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

 functionsuite:assertError(func,...)
 localsuccess,result=pcall(func,...)
 self:assertFalse(success)
 end

 functionsuite:assertNotError(func,...)
 localsuccess,result=pcall(func,...)
 self:assertTrue(success)
 end

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

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

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

 functionsuite:testBadInput()
 self:assertError(_main)
 self:assertError(_main,'foo')
 self:assertError(_main,false)
 end

 functionsuite:testBadFile()
 self:assertError(_main,{})
 self:assertError(_main,{file=true})
 self:assertError(_main,{file=123})
 end

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

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

 functionsuite:testFormat()
 self:assertPositionalParamExists('format')
 end

 functionsuite:testLocation()
 self:assertPositionalParamExists('location')
 end

 functionsuite:testAlignment()
 self:assertPositionalParamExists('alignment')
 end

 functionsuite:testSize()
 self:assertPositionalParamExists('size')
 end

 functionsuite:testCaption()
 self:assertPositionalParamExists('caption')
 end

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

 functionsuite:testUpright()
 self:assertNamedParamExists('upright')
 end

 functionsuite:testLink()
 self:assertNamedParamExists('link')
 end

 functionsuite:testAlt()
 self:assertNamedParamExists('alt')
 end

 functionsuite:testPage()
 self:assertNamedParamExists('page')
 end

 functionsuite:testClass()
 self:assertNamedParamExists('class')
 end

 functionsuite:testLang()
 self:assertNamedParamExists('lang')
 end

 functionsuite:testStart()
 self:assertNamedParamExists('start')
 end

 functionsuite:testEnd()
 self:assertNamedParamExists('end')
 end

 functionsuite:testThumbtime()
 self:assertNamedParamExists('thumbtime')
 end

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

 functionsuite: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

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

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

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

 functionsuite:testOrder()
 localparams={
 'file',
 'format',
 'formatfile',
 'border',
 'location',
 'alignment',
 'size',
 'upright',
 'link',
 'alt',
 'page',
 'class',
 'lang',
 'start',
 'end',
 'thumbtime',
 'caption'
 }
 localargs={}
 fori,paraminipairs(params)do
 args[param]='param '..i
 end
 args.border=true-- border is a special case
 localresult=_main(args)

 -- Ugly hack to make border work whatever position it's in.
 localborderNum
 fori,vinipairs(params)do
 ifv=='border'then
 borderNum=i
 end
 end
 result=result:gsub('border','param '..borderNum,1)

 locali=0
 forsinstring.gmatch(result,'param %d+')do
 i=i+1
 suite:assertEquals(s,'param '..i)
 end
 end

 returnsuite

AltStyle によって変換されたページ (->オリジナル) /