Jump to content

Module:File link/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:41, 4 June 2014 (add tests for the name method). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Unit tests for [[Module:File link]]. Click on the talk page to run the tests.

local mFileLink = require('Module:File link')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------
-- Default values
--------------------------------------------------------------------------------

local d = {}
d.name = 'Example.png'

-- Default types
d.string = 'foo'
d.number = 5
d.bool = true
d.tbl = {}
d.func = function () return end

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

function suite.dummyObject(args)
	local obj = {theName = d.name}
	for k, v in pairs(args or {}) do
		obj[k] = v
	end
	return obj
end

function suite:assertError(func, ...)
	local success, result = pcall(func, ...)
	self:assertFalse(success)
end

function suite:assertNotError(func, ...)
	local success, result = pcall(func, ...)
	self:assertTrue(success)
end

--------------------------------------------------------------------------------
-- Test fileLink.new
--------------------------------------------------------------------------------

function suite:testNewInputTypes()
	self:assertNotError(mFileLink.new, nil)
	self:assertNotError(mFileLink.new, d.string)
	self:assertError(mFileLink.new, d.number)
	self:assertError(mFileLink.new, d.bool)
	self:assertError(mFileLink.new, d.tbl)
	self:assertError(mFileLink.new, d.func)
end

function suite:testNewFilename()
	self:assertStringContains(d.name, mFileLink.new(d.name):render())
end

--------------------------------------------------------------------------------
-- Test name
--------------------------------------------------------------------------------

function suite:testNameInputTypes()
	local obj = mFileLink.new()
	self:assertNotError(obj.name, obj, d.string)
	self:assertError(obj.name, obj, nil)
	self:assertError(obj.name, obj, d.number)
	self:assertError(obj.name, obj, d.bool)
	self:assertError(obj.name, obj, d.tbl)
	self:assertError(obj.name, obj, d.func)
end

function suite:testNameSelf()
	local obj = mFileLink.new()
	self:assertError(obj.name, {}, d.string)
end

function suite:testName()
	local obj = mFileLink.new()
	obj:name(d.name)
	self:assertStringContains(d.name, obj:render())
end

function suite:testNameChaining()
	local obj = mFileLink.new()
	self:assertEquals(obj, obj:name(d.name))
end

return suite