Software /
code /
prosody
Comparison
spec/util_interpolation_spec.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 05 Nov 2020 22:31:25 +0100 |
parent | 11065:95eb1a981ef6 |
child | 11308:5d4d90d1eabb |
comparison
equal
deleted
inserted
replaced
11199:6c7c50a4de32 | 11200:bf8f2da84007 |
---|---|
1 local template = [[ | |
2 {greet!?Hi}, {name?world}! | |
3 ]]; | |
4 local expect1 = [[ | |
5 Hello, WORLD! | |
6 ]]; | |
7 local expect2 = [[ | |
8 Hello, world! | |
9 ]]; | |
10 local expect3 = [[ | |
11 Hi, YOU! | |
12 ]]; | |
13 local template_array = [[ | |
14 {foo#{idx}. {item} | |
15 }]] | |
16 local expect_array = [[ | |
17 1. HELLO | |
18 2. WORLD | |
19 ]] | |
20 local template_func_pipe = [[ | |
21 {foo|sort#{idx}. {item} | |
22 }]] | |
23 local expect_func_pipe = [[ | |
24 1. A | |
25 2. B | |
26 3. C | |
27 4. D | |
28 ]] | |
29 local template_map = [[ | |
30 {foo%{idx}: {item!} | |
31 }]] | |
32 local expect_map = [[ | |
33 FOO: bar | |
34 ]] | |
35 local template_not = [[ | |
36 {thing~Thing is falsy}{thing&Thing is truthy} | |
37 ]] | |
38 local expect_not_true = [[ | |
39 Thing is truthy | |
40 ]] | |
41 local expect_not_nil = [[ | |
42 Thing is falsy | |
43 ]] | |
44 local expect_not_false = [[ | |
45 Thing is falsy | |
46 ]] | |
47 describe("util.interpolation", function () | |
48 it("renders", function () | |
49 local render = require "util.interpolation".new("%b{}", string.upper, { sort = function (t) table.sort(t) return t end }); | |
50 assert.equal(expect1, render(template, { greet = "Hello", name = "world" })); | |
51 assert.equal(expect2, render(template, { greet = "Hello" })); | |
52 assert.equal(expect3, render(template, { name = "you" })); | |
53 assert.equal(expect_array, render(template_array, { foo = { "Hello", "World" } })); | |
54 assert.equal(expect_func_pipe, render(template_func_pipe, { foo = { "c", "a", "d", "b", } })); | |
55 -- assert.equal("", render(template_func_pipe, { foo = nil })); -- FIXME | |
56 assert.equal(expect_map, render(template_map, { foo = { foo = "bar" } })); | |
57 assert.equal(expect_not_true, render(template_not, { thing = true })); | |
58 assert.equal(expect_not_nil, render(template_not, { thing = nil })); | |
59 assert.equal(expect_not_false, render(template_not, { thing = false })); | |
60 end); | |
61 end); |