summaryrefslogtreecommitdiff
path: root/test.lua
blob: 47c7fed9017ebf3dda682e5abfa13b8a09405755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/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 == '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<p>some paragraph</p>')
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 == '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<p class="blinking bold" id="p1">some paragraph</p>')
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 == [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div id="root">
	<p class="bold">p1</p>
	<p>p2</p>
</div>]])
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)