-- Unit tests for [[Module:HtmlBuilder]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local HtmlBuilder = require('Module:HtmlBuilder')
function test(name, builder, expected)
p:equals(name, tostring(builder), expected, {nowiki = true})
end
function p:test_all()
test('Empty builder', HtmlBuilder.create(), '')
test('Plain text', HtmlBuilder.create().wikitext('foo').allDone(), 'foo')
test('Empty div', HtmlBuilder.create().tag('div').allDone(), '<div></div>')
test('Div with text', HtmlBuilder.create().tag('div').wikitext('foo').allDone(), '<div>foo</div>')
test('Text and div', HtmlBuilder.create().wikitext('foo').tag('div').wikitext('bar').allDone(), 'foo<div>bar</div>')
test('Nested div', HtmlBuilder.create().tag('div').tag('div').wikitext('foo').allDone(), '<div><div>foo</div></div>')
test('Div with attribute', HtmlBuilder.create().tag('div').attr('id', 'foo').allDone(), '<div id="foo"></div>')
test('Div with multiple attributes', HtmlBuilder.create().tag('div').attr('id', 'foo').attr('class', 'bar baz').attr('lang', 'es').allDone(), '<div id="foo" class="bar baz" lang="es"></div>')
test('Div with multiple attributes in other order', HtmlBuilder.create().tag('div').attr('class', 'bar baz').attr('id', 'foo').attr('lang', 'es').allDone(), '<div class="bar baz" id="foo" lang="es"></div>')
test('Div with overwritten attribute', HtmlBuilder.create().tag('div').attr('id', 'foo').attr('class', 'bar').attr('id', 'baz').allDone(), '<div id="baz" class="bar"></div>')
test('Div with overwritten attribute in other order', HtmlBuilder.create().tag('div').attr('class', 'bar').attr('id', 'foo').attr('id', 'baz').allDone(), '<div class="bar" id="baz"></div>')
test('Div with attributes and text', HtmlBuilder.create().tag('div').wikitext('bar').attr('id', 'foo').allDone(), '<div id="foo">bar</div>')
test('Div with style', HtmlBuilder.create().tag('div').css('background', 'red').allDone(), '<div style="background:red;"></div>')
test('Div with multiple styles', HtmlBuilder.create().tag('div').css('background', 'red').css('color', 'blue').allDone(), '<div style="background:red;color:blue;"></div>')
test('Div with multiple styles in other order', HtmlBuilder.create().tag('div').css('color', 'blue').css('background', 'red').allDone(), '<div style="color:blue;background:red;"></div>')
test('Div with overwritten style', HtmlBuilder.create().tag('div').css('background', 'red').css('color', 'blue').css('background', 'green').allDone(), '<div style="background:green;color:blue;"></div>')
test('Div with CSS text', HtmlBuilder.create().tag('div').cssText('width:55px;height:77px;').allDone(), '<div style="width:55px;height:77px;;"></div>')
test('Div with explicitly overwritten style attribute', HtmlBuilder.create().tag('div').css('background', 'red').attr('style', 'color:blue;').allDone(), '<div style="color:blue;;"></div>')
test('addClass', HtmlBuilder.create().tag('div').addClass('foo').allDone(), '<div class="foo"></div>')
test('addClass with multiple classes', HtmlBuilder.create().tag('div').addClass('foo').addClass('bar').addClass('baz').allDone(), '<div class="foo bar baz"></div>')
test('Unclosed div', HtmlBuilder.create().tag('div', {unclosed = true}).allDone(), '<div>')
test('Closing div tag', HtmlBuilder.create().tag('/div', {unclosed = true}).allDone(), '</div>')
local b = HtmlBuilder.create('b')
local i = HtmlBuilder.create('i')
test('Two nodes', HtmlBuilder.create().node(b).node(i).allDone(), '<b></b><i></i>')
local b = HtmlBuilder.create('b').addClass('foo').wikitext('bar')
local i = HtmlBuilder.create('i').attr('id', 'baz').css('color', 'yellow')
test('Two complicated nodes', HtmlBuilder.create().tag('div').node(b).wikitext('qux').node(i).allDone(), '<div><b class="foo">bar</b>qux<i id="baz" style="color:yellow;"></i></div>')
end
return p