Software /
code /
prosody
Comparison
spec/util_xtemplate_spec.lua @ 13395:1675d4b6363a
util.xtemplate: Add some initial tests
Strict typing does not magically make code correct
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 09 Dec 2023 15:15:06 +0100 |
child | 13404:034c7af177f0 |
comparison
equal
deleted
inserted
replaced
13394:6debd8dd12ab | 13395:1675d4b6363a |
---|---|
1 local st = require "prosody.util.stanza"; | |
2 local xtemplate = require "prosody.util.xtemplate"; | |
3 | |
4 describe("util.xtemplate", function () | |
5 describe("render()", function () | |
6 it("works", function () | |
7 assert.same("Hello", xtemplate.render("{greeting}", st.stanza("root"):text_tag("greeting", "Hello")), "regular text content") | |
8 assert.same("Hello", xtemplate.render("{#}", st.stanza("root"):text("Hello")), "top tag text content") | |
9 assert.same("Hello", xtemplate.render("{greeting/@en}", st.stanza("root"):tag("greeting", { en = "Hello" })), "attribute") | |
10 end) | |
11 it("supports conditionals", function () | |
12 local atom_tmpl = "{@pubsub:title|and{*{@pubsub:title}*\n\n}}{summary|or{{author/name|and{{author/name} posted }}{title}}}"; | |
13 local atom_data = st.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" }); | |
14 assert.same("", xtemplate.render(atom_tmpl, atom_data)); | |
15 | |
16 atom_data:text_tag("title", "an Entry") | |
17 assert.same("an Entry", xtemplate.render(atom_tmpl, atom_data)); | |
18 | |
19 atom_data:tag("author"):text_tag("name","Juliet"):up(); | |
20 assert.same("Juliet posted an Entry", xtemplate.render(atom_tmpl, atom_data)); | |
21 | |
22 atom_data:text_tag("summary", "Juliet just posted a new entry"); | |
23 assert.same("Juliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data)); | |
24 | |
25 atom_data.attr["xmlns:pubsub"] = "http://jabber.org/protocol/pubsub"; | |
26 atom_data.attr["pubsub:title"] = "Juliets musings"; | |
27 assert.same("*Juliets musings*\n\nJuliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data)); | |
28 end) | |
29 it("can strip surrounding whitespace", function () | |
30 assert.same("Hello ", xtemplate.render(" {-greeting} ", st.stanza("root"):text_tag("greeting", "Hello"))) | |
31 assert.same(" Hello", xtemplate.render(" {greeting-} ", st.stanza("root"):text_tag("greeting", "Hello"))) | |
32 assert.same("Hello", xtemplate.render(" {-greeting-} ", st.stanza("root"):text_tag("greeting", "Hello"))) | |
33 end) | |
34 end) | |
35 end) |