Jump to content

Module:Userbox/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 19:03, 8 January 2014 (make more accurate tests and create some pattern-generating helper functions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Unit tests for [[Module:Userbox]]. Click talk page to run tests.
local ubx = require('Module:Userbox')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

local userbox = ubx.userbox
local userbox2 = ubx['userbox-2']
local userboxr = ubx['userbox-r']
local render = ubx.render

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

local function inTag(s, tag)
	-- Makes a pattern that tests whether s is inside a given html tag.
	return '<' .. tag .. ' [^>]-' .. s
end

local function inAttr(s, attr)
	-- Makes a pattern that tests whether s is inside a given html attribute.
	return attr .. '="[^"]-' .. s
end

local function inTagAttr(s, tag, attr)
	-- Makes a pattern that tests whether s is inside a given html tag
	-- and a given html attribute.
	return inTag(inAttr(s, attr), tag)
end

--------------------------------------------------------------------------------
-- Render tests
--------------------------------------------------------------------------------

function suite:testRenderFloat()
    self:assertStringContains(inTagAttr('float:right;', 'div', 'style'), render{float="right"})
end

function suite:testRenderBorder()
    self:assertStringContains(inTagAttr('border:5px', 'div', 'style'), render{borderWidth="5px"})
    self:assertStringContains('solid red', render{borderColor="red"})
end

function suite:testRenderMargin()
    self:assertStringContains(inTagAttr('margin:1px;', 'div', 'style'), render{})
end

function suite:testRenderWidth()
    self:assertStringContains(inTagAttr('width:120px;', 'div', 'style'), render{width="120px"})
end

function suite:testRenderUserboxClass()
    self:assertStringContains(inTagAttr('wikipediauserbox', 'div', 'class'), render{})
end

function suite:testRenderBodyClass()
    self:assertStringContains(inTagAttr('foobar', 'div', 'class'), render{bodyClass="foobar"})
end

return suite