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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
local template = require 'pl.template'
local subst = template.substitute
local List = require 'pl.List'
local asserteq = require 'pl.test'.asserteq
local utils = require 'pl.utils'
asserteq(subst([[
# for i = 1,2 do
<p>Hello $(tostring(i))</p>
# end
]],_G),[[
<p>Hello 1</p>
<p>Hello 2</p>
]])
asserteq(subst([[
<ul>
# for name in ls:iter() do
<li>$(name)</li>
#end
</ul>
]],{ls = List{'john','alice','jane'}}),[[
<ul>
<li>john</li>
<li>alice</li>
<li>jane</li>
</ul>
]])
-- can change the default escape from '#' so we can do C/C++ output.
-- note that the environment can have a parent field.
asserteq(subst([[
> for i,v in ipairs{'alpha','beta','gamma'} do
cout << obj.${v} << endl;
> end
]],{_parent=_G, _brackets='{}', _escape='>'}),[[
cout << obj.alpha << endl;
cout << obj.beta << endl;
cout << obj.gamma << endl;
]])
-- handle templates with a lot of substitutions
asserteq(subst(("$(x)\n"):rep(300), {x = "y"}), ("y\n"):rep(300))
--------------------------------------------------
-- Test using no leading nor trailing linebreak
local tmpl = [[<ul>
# for i,val in ipairs(T) do
<li>$(i) = $(val:upper())</li>
# end
</ul>]]
local my_env = {
ipairs = ipairs,
T = {'one','two','three'},
_debug = true,
}
local res, err = template.substitute(tmpl, my_env)
--print(res, err)
asserteq(res, [[<ul>
<li>1 = ONE</li>
<li>2 = TWO</li>
<li>3 = THREE</li>
</ul>]])
--------------------------------------------------
-- Test using both leading and trailing linebreak
local tmpl = [[
<ul>
# for i,val in ipairs(T) do
<li>$(i) = $(val:upper())</li>
# end
</ul>
]]
local my_env = {
ipairs = ipairs,
T = {'one','two','three'},
_debug = true,
}
local res, err = template.substitute(tmpl, my_env)
--print(res, err)
asserteq(res, [[
<ul>
<li>1 = ONE</li>
<li>2 = TWO</li>
<li>3 = THREE</li>
</ul>
]])
--------------------------------------------------
-- Test reusing a compiled template
local tmpl = [[
<ul>
# for i,val in ipairs(T) do
<li>$(i) = $(val:upper())</li>
# end
</ul>
]]
local my_env = {
ipairs = ipairs,
T = {'one','two','three'}
}
local t, err = template.compile(tmpl, { debug = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
asserteq(res, [[
<ul>
<li>1 = ONE</li>
<li>2 = TWO</li>
<li>3 = THREE</li>
</ul>
]])
-- now reuse with different env
local my_env = {
ipairs = ipairs,
T = {'four','five','six'}
}
local t, err = template.compile(tmpl, { debug = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
asserteq(res, [[
<ul>
<li>1 = FOUR</li>
<li>2 = FIVE</li>
<li>3 = SIX</li>
</ul>
]])
--------------------------------------------------
-- Test the newline parameter
local tmpl = [[
some list: $(T[1]:upper())
# for i = 2, #T do
,$(T[i]:upper())
# end
]]
local my_env = {
ipairs = ipairs,
T = {'one','two','three'}
}
local t, err = template.compile(tmpl, { debug = true, newline = "" })
local res, err, code = t:render(my_env)
--print(res, err, code)
asserteq(res, [[some list: ONE,TWO,THREE]])
--------------------------------------------------
-- Test template run-time error
local tmpl = [[
header: $("hello" * 10)
]]
local t, err = template.compile(tmpl, { debug = true, newline = "" })
local res, err, code = t:render()
--print(res, err, code)
assert(res == nil, "expected nil here because of the runtime error")
asserteq(type(err), "string")
asserteq(type(utils.load(code)), "function")
--------------------------------------------------
-- Test template compile-time error
local tmpl = [[
header: $(this doesn't work)
]]
local my_env = {
ipairs = ipairs,
T = {'one','two','three'}
}
local t, err, code = template.compile(tmpl, { debug = true, newline = "" })
--print(t, err, code)
assert(t==nil, "expected t to be nil here because of the syntax error")
asserteq(type(err), "string")
asserteq(type(code), "string")
--------------------------------------------------
-- Test using template being a single static string
local tmpl = [[
<ul>
<p>a paragraph</p>
<p>a paragraph</p>
</ul>
]]
local t, err = template.compile(tmpl, { debug = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
asserteq(res, [[<ul>
<p>a paragraph</p>
<p>a paragraph</p>
</ul>
]])
asserteq(code, [[return "<ul>\
<p>a paragraph</p>\
<p>a paragraph</p>\
</ul>\
"]])
print("template: success")
|