aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_xtemplate_spec.lua
blob: 421be43f93d24dd3e9b7eecf696f51c9136f5f96 (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
local st = require "prosody.util.stanza";
local xtemplate = require "prosody.util.xtemplate";

describe("util.xtemplate", function ()
	describe("render()", function ()
		it("works", function ()
			assert.same("Hello", xtemplate.render("{greeting}", st.stanza("root"):text_tag("greeting", "Hello")), "regular text content")
			assert.same("Hello", xtemplate.render("{#}", st.stanza("root"):text("Hello")), "top tag text content")
			assert.same("Hello", xtemplate.render("{greeting/@en}", st.stanza("root"):tag("greeting", { en = "Hello" })), "attribute")
		end)
		it("supports conditionals", function ()
			local atom_tmpl = "{@pubsub:title|and{*{@pubsub:title}*\n\n}}{summary|or{{author/name|and{{author/name} posted }}{title}}}";
			local atom_data = st.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" });
			assert.same("", xtemplate.render(atom_tmpl, atom_data));

			atom_data:text_tag("title", "an Entry")
			assert.same("an Entry", xtemplate.render(atom_tmpl, atom_data));

			atom_data:tag("author"):text_tag("name","Juliet"):up();
			assert.same("Juliet posted an Entry", xtemplate.render(atom_tmpl, atom_data));

			atom_data:text_tag("summary", "Juliet just posted a new entry");
			assert.same("Juliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data));

			atom_data.attr["xmlns:pubsub"] = "http://jabber.org/protocol/pubsub";
			atom_data.attr["pubsub:title"] = "Juliets musings";
			assert.same("*Juliets musings*\n\nJuliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data));
		end)
		it("can strip surrounding whitespace", function ()
			assert.same("Hello ", xtemplate.render(" {-greeting} ", st.stanza("root"):text_tag("greeting", "Hello")))
			assert.same(" Hello", xtemplate.render(" {greeting-} ", st.stanza("root"):text_tag("greeting", "Hello")))
			assert.same("Hello", xtemplate.render(" {-greeting-} ", st.stanza("root"):text_tag("greeting", "Hello")))
		end)
		describe("each", function ()
			it("makes sense", function ()
				local x = st.stanza("root"):tag("foo"):tag("bar")
				for i = 1, 5 do x:text_tag("i", tostring(i)); end
				x:reset();
				assert.same("12345", xtemplate.render("{foo/bar|each(i){{#}}}", x));
			end)
			it("handles missing inputs", function ()
				local x = st.stanza("root");
				assert.same("", xtemplate.render("{foo/bar|each(i){{#}}}", x));
			end)
		end)
	end)
end)