aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2023-12-09 15:15:06 +0100
committerKim Alvefur <zash@zash.se>2023-12-09 15:15:06 +0100
commit36914c9d99fb5c50753ac626e895754923ce6325 (patch)
tree9c4da8e05929e1084b63a7aea4d2cb1d80cc56b9
parentc61c78447b855c3fef1def5ce962156d2ce2e7f3 (diff)
downloadprosody-36914c9d99fb5c50753ac626e895754923ce6325.tar.gz
prosody-36914c9d99fb5c50753ac626e895754923ce6325.zip
util.xtemplate: Add some initial tests
Strict typing does not magically make code correct
-rw-r--r--spec/util_xtemplate_spec.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/spec/util_xtemplate_spec.lua b/spec/util_xtemplate_spec.lua
new file mode 100644
index 00000000..97ab415c
--- /dev/null
+++ b/spec/util_xtemplate_spec.lua
@@ -0,0 +1,35 @@
+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)
+ end)
+end)