Module:HtmlBuilder/testcases

-- Unit tests for Module:HtmlBuilder. Click talk page to run tests. local p = require('Module:UnitTests') local HtmlBuilder = require('Module:HtmlBuilder/sandbox')

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, ' ') test('Div with text', HtmlBuilder.create.tag('div').wikitext('foo').allDone, ' foo ') test('Text and div', HtmlBuilder.create.wikitext('foo').tag('div').wikitext('bar').allDone, 'foo bar ') test('Nested div', HtmlBuilder.create.tag('div').tag('div').wikitext('foo').allDone, ' foo  ') test('Div with attribute', HtmlBuilder.create.tag('div').attr('id', 'foo').allDone, ' ') test('Div with multiple attributes', HtmlBuilder.create.tag('div').attr('id', 'foo').attr('class', 'bar baz').attr('lang', 'es').allDone, ' ') test('Div with multiple attributes in other order', HtmlBuilder.create.tag('div').attr('class', 'bar baz').attr('id', 'foo').attr('lang', 'es').allDone, ' ') test('Div with overwritten attribute', HtmlBuilder.create.tag('div').attr('id', 'foo').attr('class', 'bar').attr('id', 'baz').allDone, ' ') test('Div with overwritten attribute in other order', HtmlBuilder.create.tag('div').attr('class', 'bar').attr('id', 'foo').attr('id', 'baz').allDone, ' ') test('Div with attributes and text', HtmlBuilder.create.tag('div').wikitext('bar').attr('id', 'foo').allDone, ' bar ') test('Div with style', HtmlBuilder.create.tag('div').css('background', 'red').allDone, ' ') test('Div with multiple styles', HtmlBuilder.create.tag('div').css('background', 'red').css('color', 'blue').allDone, ' ') test('Div with multiple styles in other order', HtmlBuilder.create.tag('div').css('color', 'blue').css('background', 'red').allDone, ' ') test('Div with overwritten style', HtmlBuilder.create.tag('div').css('background', 'red').css('color', 'blue').css('background', 'green').allDone, ' ') test('Div with CSS text', HtmlBuilder.create.tag('div').cssText('width:55px;height:77px;').allDone, ' ') test('Div with explicitly overwritten style attribute', HtmlBuilder.create.tag('div').css('background', 'red').attr('style', 'color:blue;').allDone, ' ') test('addClass', HtmlBuilder.create.tag('div').addClass('foo').allDone, ' ') test('addClass with multiple classes', HtmlBuilder.create.tag('div').addClass('foo').addClass('bar').addClass('baz').allDone, ' ') test('Unclosed div', HtmlBuilder.create.tag('div', {unclosed = true}).allDone, ' ') test('Closing div tag', HtmlBuilder.create.tag('/div', {unclosed = true}).allDone, ' ') test('Self-closing br tag', HtmlBuilder.create.tag('br', {selfClosing = true}).allDone, ' ') test('Ignored CSS values', HtmlBuilder.create.tag('div').css('color', nil).css('border', false).css('height', true).allDone, ' ') test('Ignored attribute values', HtmlBuilder.create.tag('div').attr('id', nil).attr('lang', false).attr('class', true).allDone, ' ') local b = HtmlBuilder.create('b') local i = HtmlBuilder.create('i') test('Two nodes', HtmlBuilder.create.node(b).node(i).allDone, '') 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, ' barqux ') end return p