summaryrefslogtreecommitdiff
path: root/test.lua
blob: 17aa46dbaf1e205967eb6ab98d1ce70ac103f88f (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
#!/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)