Module:Side box/testcases explained

local mSideBox = require('Module:Side box')local ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new

---------------------------------------------------------------------------------- Helper functions-------------------------------------------------------------------------------- function suite:assertArrayContainsString(expected, t) -- This only works on arrays that only contain strings. local sep = '|SEPARATOR|' local concatenated = sep .. table.concat(t, sep) .. sep self:assertStringContains(sep .. expected .. sep, concatenated, true)end

function suite:assertNotArrayContainsString(expected, t) -- This only works on arrays that only contain strings. local sep = '|SEPARATOR|' local concatenated = sep .. table.concat(t, sep) .. sep self:assertNotStringContains(sep .. expected .. sep, concatenated, true)end

---------------------------------------------------------------------------------- Test makeData--------------------------------------------------------------------------------

function suite:testDataBlank self:assertEquals('table', type(mSideBox.makeData)) self:assertEquals('table', type(mSideBox.makeData.classes))end

function suite:testDataMetadata suite:assertNotArrayContainsString('metadata', mSideBox.makeData.classes) suite:assertNotArrayContainsString('metadata', mSideBox.makeData.classes) suite:assertArrayContainsString('metadata', mSideBox.makeData.classes) suite:assertArrayContainsString('metadata', mSideBox.makeData.classes) suite:assertArrayContainsString('metadata', mSideBox.makeData.classes) suite:assertArrayContainsString('metadata', mSideBox.makeData.classes)end

function suite:testDataLeft suite:assertArrayContainsString('mbox-small', mSideBox.makeData.classes) suite:assertArrayContainsString('mbox-small', mSideBox.makeData.classes) suite:assertArrayContainsString('mbox-small', mSideBox.makeData.classes) suite:assertArrayContainsString('mbox-small-left', mSideBox.makeData.classes) suite:assertArrayContainsString('mbox-small-left', mSideBox.makeData.classes) suite:assertArrayContainsString('mbox-small-left', mSideBox.makeData.classes)end

function suite:testDataClass suite:assertArrayContainsString('some-class', mSideBox.makeData.classes)end

function suite:testDataStyle suite:assertEquals('foo:bar', mSideBox.makeData.style)end

function suite:testDataTextstyle suite:assertEquals('foo:bar', mSideBox.makeData.textstyle)end

function suite:testDataAbove suite:assertEquals('some above text', mSideBox.makeData.above)end

function suite:testDataAbovestyle suite:assertEquals('foo:bar', mSideBox.makeData.abovestyle)end

function suite:testDataText suite:assertEquals('some text', mSideBox.makeData.text)end

function suite:testDataImage suite:assertEquals('', mSideBox.makeData.image)end

function suite:testDataImageNone suite:assertEquals(nil, mSideBox.makeData.image)end

function suite:testDataImageright suite:assertEquals('', mSideBox.makeData.imageright)end

function suite:testDataBelow suite:assertEquals('some below text', mSideBox.makeData.below)end

---------------------------------------------------------------------------------- Test renderSidebox--------------------------------------------------------------------------------

function suite.cleanPattern(s) -- Cleans a pattern so that the magic characters %.[]*+-?^$ are interpreted literally. s = s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1') return send

function suite.makeHtmlPattern(tagTables) -- Makes a pattern for use with assertStringContains. -- The input is an array of tables, each of which corresponds to an opening tag, -- a piece of wikitext, or a closing tag. -- -- It is also possible to use a single table as an input, rather than an -- array of tables. -- -- Opening tags: -- -- -- -- -- Properties and values are escaped so that hyphens etc. will work in patterns. -- -- Wikitext: -- -- -- Closing tags: -- -- -- For Example, this code: -- suite.makeHtmlPattern -- -- Produces this: --

]-style="[^">]-text%-align%s*:%s*center[^">]-"[^>]->[^<]-Foo[^<]-

]->[^<]-Bar[^<]-[^<]-

if type(tagTables) ~= 'table' then error('invalid input to makeHtmlPattern', 2) end if #tagTables

0 then -- Table may be passed as a single tag table. tagTables = end local ret = for i, t in ipairs(tagTables) do if t.tag then if t.close then ret[#ret + 1] = string.format('', t.tag) elseif t.attr and t.property and t.value then ret[#ret + 1] = string.format('<%s[^>]-%s="[^">]-%s%%s*:%%s*%s[^">]-"[^>]->', t.tag, t.attr, suite.cleanPattern(t.property), suite.cleanPattern(t.value) ) elseif t.attr and t.value then ret[#ret + 1] = string.format('<%s[^>]-%s="[^">]-%s[^">]-"[^>]->', t.tag, t.attr, suite.cleanPattern(t.value) ) else ret[#ret + 1] = string.format('<%s[^>]->', t.tag) end elseif t.wikitext then ret[#ret + 1] = suite.cleanPattern(t.wikitext) end end return table.concat(ret, '[^<]-')end

function suite:testRenderDefaultStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderAboveStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderBelowStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderAboveAndBelowStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderNewlines self:assertStringContains('^

]->[^<]-
]->[^<]-]->[^<]-
]->[^<]-
$', mSideBox.renderSidebox, false )end

function suite:testRenderImagerightStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderAboveImagerightStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderBelowImagerightStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderAboveAndBelowImagerightStructure self:assertStringContains('^' .. suite.makeHtmlPattern .. '$', mSideBox.renderSidebox, false )end

function suite:testRenderOneClass local data = self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox(data), false )end

function suite:testRenderTwoClasses local data = self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox(data), false ) self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox(data), false )end

function suite:testRenderDefaultTableStyles self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false ) self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderStyle self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderAbove self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderAboveClass self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderAboveTextstyle self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderAbovestyle self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderImage self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderNoImage self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderText self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderImageright self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderBelow self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

function suite:testRenderBelowTextstyle self:assertStringContains(suite.makeHtmlPattern, mSideBox.renderSidebox, false )end

---------------------------------------------------------------------------------- Whole-module tests--------------------------------------------------------------------------------

function suite:testMain local currentFrame = mw.getCurrentFrame local parent = currentFrame:newChild local frame = parent:newChild local actual = mSideBox.main(frame) self:assertStringContains('some box text', actual, true)end

function suite:testUnderscoreMain local actual = mSideBox._main self:assertStringContains('some underscore main text', actual, true)end

return suite