#!/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 == '

some paragraph

') 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 == '

some paragraph

') end)