#!/usr/bin/env lua5.1 local svg = require 'marigold' function test(description, func) io.write(description .. ': ') local result, msg = pcall(func) if result == true then print("OK") else print("FAIL") print(debug.traceback(msg)) print() end end test("svg.h produces a correct basic tag", function() local h = svg.h local h1 = h('h1', 'hello, world!') assert(type(h1) == 'table') assert(h1.tag == 'h1') assert(h1.content == 'hello, world!') -- should have no entries in the h1.attributes table for k, v in pairs(h1.attributes) do assert(false, string.format("%s, %s", tostring(k), tostring(v))) end assert(#(h1.children) == 0) end) test("svg.h correctly adds children", function() local h = svg.h local div = h('div', "some stuff", { h('p', "the first paragraph"), h('p', "the second paragraph"), }) assert(div.tag == 'div') assert(div.content == "some stuff") -- still should have nothing in the attributes table for k, v in ipairs(div.attributes) do assert(false, string.format("%s, %s", tostring(k), tostring(v))) end assert(#div.children == 2) assert(div.children[1].tag == 'p') assert(div.children[1].content == 'the first paragraph') assert(div.children[2].tag == 'p') assert(div.children[2].content == 'the second paragraph') end) test("svg.h correctly ignores missing content", function() local h = svg.h local div = h('div', { h('p', "the first paragraph"), h('p', "the second paragraph"), }) assert(div.tag == 'div') assert(div.content == "") -- still should have nothing in the attributes table for k, v in ipairs(div.attributes) do assert(false, string.format("%s, %s", tostring(k), tostring(v))) end assert(#div.children == 2) assert(div.children[1].tag == 'p') assert(div.children[1].content == 'the first paragraph') assert(div.children[2].tag == 'p') assert(div.children[2].content == 'the second paragraph') end) test("svg.h correctly sets attributes and children", function() local h = svg.h local div = h('div', { id="root", class="blinking bold", h('p', "the first paragraph"), h('p', "the second paragraph"), }) assert(div.tag == 'div') assert(div.content == "") assert(div.attributes.id == 'root') assert(div.attributes.class == 'blinking bold') assert(#div.children == 2) assert(div.children[1].tag == 'p') assert(div.children[1].content == 'the first paragraph') assert(div.children[2].tag == 'p') assert(div.children[2].content == 'the second paragraph') end) test("svg.render correctly renders basic render table", function() local tbl = { tag='p', content='some paragraph', attributes={}, children={}} local render = svg.render(tbl) assert(render == '\n

some paragraph

') end) test("svg.render correctly renders tag attributes", function() local tbl = { tag='p', content='some paragraph', attributes={ id='p1', class='blinking bold' }, children={}} local render = svg.render(tbl) assert(render == '\n

some paragraph

') end) test("svg.render correctly renders children", function() local tbl = { tag = 'div', content='', attributes = {id="root"}, children = { { tag='p', content='p1', attributes = {class="bold"}, children={} }, { tag='p', content='p2', attributes={}, children={} }, } } local render = svg.render(tbl) assert(render == [[

p1

p2

]]) end) test("svg.svg correctly has xmlns attribute set", function() local tbl = svg.svg() assert(tbl.tag == 'svg') assert(tbl.content == '') assert(tbl.attributes.xmlns == 'http://www.w3.org/2000/svg') assert(#tbl.children == 0) end) test("svg.g can set transform attribute", function() local tbl = svg.g{ transform = "rotate(45deg)", svg.g(), svg.g(), } assert(tbl.tag == 'g') assert(tbl.content == '') assert(tbl.attributes.transform == 'rotate(45deg)') assert(#tbl.children == 2) end)