Comparison

spec/util_interpolation_spec.lua @ 11064:af1e3b7d9ea3

util.interpolation: Add '~' as the opposite of '&' (render sub-block if falsy) One more magic character consumed!
author Matthew Wild <mwild1@gmail.com>
date Wed, 09 Sep 2020 17:10:33 +0100
parent 10413:604096ce2f82
child 11065:95eb1a981ef6
comparison
equal deleted inserted replaced
11062:dd3b1b9d867d 11064:af1e3b7d9ea3
30 {foo%{idx}: {item!} 30 {foo%{idx}: {item!}
31 }]] 31 }]]
32 local expect_map = [[ 32 local expect_map = [[
33 FOO: bar 33 FOO: bar
34 ]] 34 ]]
35 35 local template_not = [[
36 {thing~Thing is nil}{thing&Thing is not nil}
37 ]]
38 local expect_not_true = [[
39 Thing is not nil
40 ]]
41 local expect_not_nil = [[
42 Thing is nil
43 ]]
36 describe("util.interpolation", function () 44 describe("util.interpolation", function ()
37 it("renders", function () 45 it("renders", function ()
38 local render = require "util.interpolation".new("%b{}", string.upper, { sort = function (t) table.sort(t) return t end }); 46 local render = require "util.interpolation".new("%b{}", string.upper, { sort = function (t) table.sort(t) return t end });
39 assert.equal(expect1, render(template, { greet = "Hello", name = "world" })); 47 assert.equal(expect1, render(template, { greet = "Hello", name = "world" }));
40 assert.equal(expect2, render(template, { greet = "Hello" })); 48 assert.equal(expect2, render(template, { greet = "Hello" }));
41 assert.equal(expect3, render(template, { name = "you" })); 49 assert.equal(expect3, render(template, { name = "you" }));
42 assert.equal(expect_array, render(template_array, { foo = { "Hello", "World" } })); 50 assert.equal(expect_array, render(template_array, { foo = { "Hello", "World" } }));
43 assert.equal(expect_func_pipe, render(template_func_pipe, { foo = { "c", "a", "d", "b", } })); 51 assert.equal(expect_func_pipe, render(template_func_pipe, { foo = { "c", "a", "d", "b", } }));
44 -- assert.equal("", render(template_func_pipe, { foo = nil })); -- FIXME 52 -- assert.equal("", render(template_func_pipe, { foo = nil })); -- FIXME
45 assert.equal(expect_map, render(template_map, { foo = { foo = "bar" } })); 53 assert.equal(expect_map, render(template_map, { foo = { foo = "bar" } }));
54 assert.equal(expect_not_true, render(template_not, { thing = true }));
55 assert.equal(expect_not_nil, render(template_not, { thing = nil }));
46 end); 56 end);
47 end); 57 end);