Module:Side box/testcases

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

--------------------------------------------------------------------------------
-- Sandbox run
--------------------------------------------------------------------------------

function suite.runSandbox(...)
	mSideBox = require('Module:Side box/sandbox')
	return suite.run(...)
end

--------------------------------------------------------------------------------
-- 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, nil, 1)
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, nil, 1)
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{metadata = 'no'}.classes)
	suite:assertNotArrayContainsString('metadata', mSideBox._makeData{metadata = false}.classes)
	suite:assertArrayContainsString('metadata', mSideBox._makeData{}.classes)
	suite:assertArrayContainsString('metadata', mSideBox._makeData{metadata = 'yes'}.classes)
	suite:assertArrayContainsString('metadata', mSideBox._makeData{metadata = true}.classes)
	suite:assertArrayContainsString('metadata', mSideBox._makeData{metadata = 'foo'}.classes)
end

function suite:testDataLeft()
	suite:assertArrayContainsString('side-box-right', mSideBox._makeData{}.classes)
	suite:assertArrayContainsString('side-box-right', mSideBox._makeData{position = 'right'}.classes)
	suite:assertArrayContainsString('side-box-right', mSideBox._makeData{position = 'asdf'}.classes)
	suite:assertArrayContainsString('side-box-left', mSideBox._makeData{position = 'left'}.classes)
	suite:assertArrayContainsString('side-box-left', mSideBox._makeData{position = 'Left'}.classes)
	suite:assertArrayContainsString('side-box-left', mSideBox._makeData{position = 'LEFT'}.classes)
end

function suite:testDataClass()
	suite:assertArrayContainsString('some-class', mSideBox._makeData{class = 'some-class'}.classes)
end

function suite:testDataStyle()
	suite:assertEquals('foo:bar', mSideBox._makeData{style = 'foo:bar'}.style)
end

function suite:testDataTextstyle()
	suite:assertEquals('foo:bar', mSideBox._makeData{textstyle = 'foo:bar'}.textstyle)
end

function suite:testDataAbove()
	suite:assertEquals('some above text', mSideBox._makeData{above = 'some above text'}.above)
end

function suite:testDataAbovestyle()
	suite:assertEquals('foo:bar', mSideBox._makeData{abovestyle = 'foo:bar'}.abovestyle)
end

function suite:testDataText()
	suite:assertEquals('some text', mSideBox._makeData{text = 'some text'}.text)
end

function suite:testDataImage()
	suite:assertEquals('[[File:Example.png|thumb]]', mSideBox._makeData{image = '[[File:Example.png|thumb]]'}.image)
end

function suite:testDataImageNone()
	suite:assertEquals(nil, mSideBox._makeData{image = 'none'}.image)
end

function suite:testDataImageright()
	suite:assertEquals('[[File:Example.png|thumb]]', mSideBox._makeData{imageright = '[[File:Example.png|thumb]]'}.imageright)
end

function suite:testDataBelow()
	suite:assertEquals('some below text', mSideBox._makeData{below = 'some below text'}.below)
end

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

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

function suite.stripTemplateStyles(s)
	-- strips out the template style label
	s = s:gsub('(\127[^\127]*UNIQ%-%-templatestyles%-)(%x+)(%-QINU[^\127]*\127)', '')
	return s
end

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:
	-- {tag = 'tr'}
	-- {tag = 'table', attr = 'class', value = 'mbox-image'}
	-- {tag = 'table', attr = 'style', property = 'text-align', value = 'center'}
	-- {tag = 'td', attr = 'colspan', value = '3'}
	-- Properties and values are escaped so that hyphens etc. will work in patterns.
	--
	-- Wikitext:
	-- {wikitext = 'Foo'}
	--
	-- Closing tags:
	-- {tag = 'table', close = true}
	--
	-- For Example, this code:
	-- suite.makeHtmlPattern{
	--   {tag = 'div', attr = 'style', property = 'text-align', value = 'center'},
	--   {wikitext = 'Foo'},
	--   {tag = 'span'},
	--   {wikitext = 'Bar'},
	--   {tag = 'span', close = true},
	--   {tag = 'div', close = true}
	-- }
	--
	-- Produces this:
	-- <div[^>]-style="[^">]-text%-align%s*:%s*center[^">]-"[^>]->[^<]-Foo[^<]-<span[^>]->[^<]-Bar[^<]-</span>[^<]-</div>
	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 = {tagTables}
	end
	local ret = {}
	for i, t in ipairs(tagTables) do
		if t.tag then
			if t.close then
				ret[#ret + 1] = string.format('</%s>', 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{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{}),
		false, "why"
	)
end

function suite:testRenderAboveStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text'}),
		false
	)
end

function suite:testRenderBelowStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{below = 'some below text'}),
		false
	)
end

function suite:testRenderAboveAndBelowStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some above text', below = 'some below text'}),
		false
	)
end



function suite:testRenderImagerightStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-imageright'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{imageright = '[[File:Example.png|thumb]]'}),
		false
	)
end

function suite:testRenderAboveImagerightStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-imageright'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text', imageright = '[[File:Example.png|thumb]]'}),
		false
	)
end

function suite:testRenderBelowImagerightStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-imageright'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{below = 'some below text', imageright = '[[File:Example.png|thumb]]'}),
		false
	)
end

function suite:testRenderAboveAndBelowImagerightStructure()
	self:assertStringContains(
		'^' .. suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box'},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-flex'},
			{tag = 'div', attr = 'class', value = 'side-box-text'},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-imageright'},
			{tag = 'div', close = true},
			{tag = 'div', close = true},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{tag = 'div', close = true},
			{tag = 'div', close = true}
		} .. '$',
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some above text', below = 'some below text', imageright = '[[File:Example.png|thumb]]'}),
		false
	)
end

function suite:testRenderOneClass()
	local data = {classes = {'foo'}}
	self:assertStringContains(
		suite.makeHtmlPattern{tag = 'div', attr = 'class', value = 'foo'},
		suite.stripTemplateStyles(mSideBox._renderSidebox(data)),
		false
	)
end

function suite:testRenderTwoClasses()
	local data = {classes = {'foo', 'bar'}}
	self:assertStringContains(
		suite.makeHtmlPattern{tag = 'div', attr = 'class', value = 'foo'},
		suite.stripTemplateStyles(mSideBox._renderSidebox(data)),
		false
	)
	self:assertStringContains(
		suite.makeHtmlPattern{tag = 'div', attr = 'class', value = 'bar'},
		suite.stripTemplateStyles(mSideBox._renderSidebox(data)),
		false
	)
end

function suite:testRenderStyle()
	self:assertStringContains(
		'\127\'"`UNIQ%-%-templatestyles%-%x*%-QINU`"\'\127',
		mSideBox._renderSidebox{style = 'foo:bar'},
		false
	)
end

function suite:testRenderAbove()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'abovebelow'},
			{wikitext = '\nsome text'},
			{tag = 'div', close = true}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text'}),
		false
	)
end

function suite:testRenderAboveClass()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div'},
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{wikitext = 'some text'}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text'}),
		false
	)
end

function suite:testRenderAboveTextstyle()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div'},
			{tag = 'div', attr = 'style', value = 'foo:bar'},
			{wikitext = 'some text'}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text', textstyle = 'foo:bar'}),
		false
	)
end

function suite:testRenderAbovestyle()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div'},
			{tag = 'div', attr = 'style', value = 'bar:baz'},
			{wikitext = 'some text'}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{above = 'some text', abovestyle = 'bar:baz'}),
		false
	)
end

function suite:testRenderImage()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box-image'},
			{wikitext = '[[File:Example.png|thumb]]'}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{image = '[[File:Example.png|thumb]]'}),
		false
	)
end

function suite:testRenderText()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div'},
			{wikitext = 'the text body'},
			{tag = 'div', close = true}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{text = 'the text body'}),
		false
	)
end

function suite:testRenderImageright()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box-imageright'},
			{wikitext = '[[File:Example 2.png|thumb]]'}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{imageright = '[[File:Example 2.png|thumb]]'}),
		false
	)
end

function suite:testRenderBelow()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div', attr = 'class', value = 'side-box-abovebelow'},
			{wikitext = 'some below text'},
			{tag = 'div', close = true}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{below = 'some below text'}),
		false
	)
end

function suite:testRenderBelowTextstyle()
	self:assertStringContains(
		suite.makeHtmlPattern{
			{tag = 'div', attr = 'style', value = 'bar:foo'},
			{wikitext = 'some below text'},
			{tag = 'div', close = true}
		},
		suite.stripTemplateStyles(mSideBox._renderSidebox{below = 'some below text', textstyle = 'bar:foo'}),
		false
	)
end

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

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

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

return suite

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.