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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#!/usr/bin/lua
local marigold = 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("marigold.get_metavars correctly calls os.getenv", function()
-- mock os.getenv
local old_getenv = os.getenv
local env_tbl = {
AUTH_TYPE = 'some auth type',
CONTENT_LENGTH = 'a zillion',
GATEWAY_INTERFACE = 'idk, something',
PATH_INFO = 'youll see, its a secret',
PATH_TRANSLATED = 'i told you its a secret!',
QUERY_STRING = 'which string do you want?',
REMOTE_ADDR = 'probably somewhere in nunavut',
REMOTE_HOST = 'woah wait are we talking about parasites?',
REMOTE_IDENT = 'are you a cop?',
REMOTE_USER = 'seriously, you have to tell me if you are',
REQUEST_METHOD = 'uhh, to do what?',
SCRIPT_NAME = 'pretty sure this is hamlet',
SERVER_NAME = 'this isnt a restaurant',
SERVER_PORT = 'what, like a ship?',
SERVER_PROTOCOL = 'no really, not a restaurant',
SERVER_SOFTWARE = 'not familiar with that dish...',
}
os.getenv = function(varname) return env_tbl[varname] end
-- actually call the function
metavars = marigold.get_metavars()
assert(type(metavars) == 'table')
for k, v in pairs(env_tbl) do
assert(metavars[string.lower(k)])
assert(metavars[string.lower(k)] == env_tbl[k])
end
-- restore os.getenv
os.getenv = old_getenv
end)
test("marigold.h produces a correct basic tag", function()
local h = marigold.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("marigold.h correctly adds children", function()
local h = marigold.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("marigold.h correctly ignores missing content", function()
local h = marigold.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("marigold.h correctly sets attributes and children", function()
local h = marigold.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("marigold.html correctly renders basic html table", function()
local tbl = { tag='p', content='some paragraph', attributes={}, children={}}
local html = marigold.html(tbl)
assert(html == '<p>some paragraph</p>')
end)
test("marigold.html correctly renders tag attributes", function()
local tbl = {
tag='p', content='some paragraph',
attributes={ id='p1', class='blinking bold' },
children={}}
local html = marigold.html(tbl)
assert(html == '<p class="blinking bold" id="p1">some paragraph</p>')
end)
test("marigold.html 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 html = marigold.html(tbl)
assert(html == [[<div id="root">
<p class="bold">p1</p>
<p>p2</p>
</div>]])
end)
test("marigold.decode_percent correctly decodes percent-encoded strings", function()
local str = '%20%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D'
assert(marigold.decode_percent(str) == " !#$%&'()*+,/:;=?@[]")
assert(marigold.decode_percent('%25+%2b') == '%++')
end)
test("marigold.decode_query correctly obtains data", function()
local tbl
tbl = marigold.decode_query('')
tbl = marigold.decode_query('name=john')
assert(tbl.name == 'john')
tbl = marigold.decode_query('string=hello%2C+world%21&name=jane')
assert(tbl.string == 'hello, world!')
assert(tbl.name == 'jane')
end)
|