Module:File/sandbox/testcases

Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

✓[OK] All tests passed.

Name Expected Actual
✓[OK] test_exists
✓[OK] test_extension
✓[OK] test_mime
✓[OK] test_sizes

Code

local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

local function testFunc(func, file)
	return string.format('{{#invoke:File/sandbox|%s|file=File:%s}}',func, file)
end

function suite:test_extension()
	local fname1 = 'Test.C.JpeG';
	self:assertResultEquals('jpeg', testFunc('extension',fname1))
	self:assertResultEquals('JpeG', testFunc('csExtension',fname1))
	self:assertResultEquals('JPEG', testFunc('extensionUpper',fname1))
	self:assertResultEquals('File:Test.C', testFunc('woExtension',fname1))
	self:assertResultEquals('File:Indoor Climbing Kid', testFunc('woExtension','Indoor Climbing Kid.jpg'))
	self:assertResultEquals('', testFunc('extension','random string without extension'))
	local fname2 = 'Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V).JPG'
	self:assertResultEquals('jpg', testFunc('extension',fname2))
end

function suite:test_mime()
	-- deprecated (but works even if the file does not exist and has no metadata)
	self:assertResultEquals('image/png', testFunc('mime','DOESNOTEXIST-0a8de8c83.Png'))
	self:assertResultEquals('image/jpeg', testFunc('mime','Indoor Climbing Kid.jpg'))
	-- preferred (but will not work if the file does not exist to parse its metadata)
	self:assertResultEquals('', testFunc('mimeType','DOESNOTEXIST-0a8de8c83.Png'))
	self:assertResultEquals('image/jpeg', testFunc('mimeType','Indoor Climbing Kid.jpg'))
end

function suite:test_exists()
	self:assertResultEquals('true', testFunc('fileExists','Commons-logo.png'))
	self:assertResultEquals('true', '{{#ifexist:File:Commons-logo.png|true}}')
	self:assertResultEquals('', testFunc('fileExists','DOESNOTEXIST-0a8de8c83.png'))
	self:assertResultEquals('', testFunc('fileExistsRelaxed','Ba[!#llot-sprite.png'))
end

function suite:test_sizes()
	local fname1 = 'Commons-logo.png'
	local fname2 = 'DOESNOTEXIST-0a8de8c83.Png'
	local fname3 = 'Handbook of style in use at the Riverside press, Cambridge, Massachusetts (IA handbookofstylei00riverich).pdf'
	self:assertResultEquals('114', testFunc('width',fname1))
	self:assertResultEquals('153', testFunc('height',fname1))
	self:assertResultEquals('114 × 153', testFunc('dimensions',fname1))
	self:assertResultEquals('5557', testFunc('size',fname1))
	self:assertResultEquals('1', testFunc('pageCount',fname1))
	
	self:assertResultEquals('', testFunc('width',fname2))
	self:assertResultEquals('', testFunc('height',fname2))
	self:assertResultEquals('', testFunc('dimensions',fname2))
	self:assertResultEquals('', testFunc('size',fname2))
	
	self:assertResultEquals('739', testFunc('width',fname3))
	self:assertResultEquals('1237', testFunc('height',fname3))
	self:assertResultEquals('739 × 1237', testFunc('dimensions',fname3))
	self:assertResultEquals('2692203', testFunc('size',fname3))
	self:assertResultEquals('52', testFunc('pageCount',fname3))
end
return suite