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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
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;
]])
--------------------------------------------------------------------------------
-- Regression tests for issue #451 (can't use % for escapes)
asserteq(subst([[
% for i = 1,3 do
$(text[i])
% end
]],{_parent=_G,_escape='%',text={'foo','bar','baz'}}),[[
foo
bar
baz
]])
asserteq(subst([[
? for i = 1,3 do
%(text[i])
? end
]],{_parent=_G,_escape='?',_inline_escape='%',text={'foo','bar','baz'}}),[[
foo
bar
baz
]])
--------------------------------------------------------------------------------
-- 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 = true })
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 = true })
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 run-time, doesn't fail on table value
-- table.concat fails if we insert a non-string (table) value
local tmpl = [[
header: $(myParam)
]]
local t, err = template.compile(tmpl, { debug = true, newline = true })
local myParam = {}
local res, err, code = t:render( {myParam = myParam } ) -- insert a table
--print(res, err, code)
asserteq(res, "header: "..tostring(myParam))
asserteq(type(err), "nil")
--------------------------------------------------
-- 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 = true })
--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>\
";]])
--------------------------------------------------
-- Test template preserves line numbers, both when
-- stripping and not stripping newlines
local tmpl = [[
# local function foo(x)
a$(x)b$(x + 1)c
# return x + 2
# end
Hello
there
# foo(1)
foo$(foo)bar
# --
]]
local expected_num = select(2, string.gsub(tmpl, "\n", "\n"))
-- Trailing newline, no newline stripping
local t, err = template.compile(tmpl, { debug = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
assert(res, "rendering should not fail here")
asserteq(select(2, string.gsub(code, "\n", "\n")), expected_num)
-- Trailing newline, with newline stripping
local t, err = template.compile(tmpl, { debug = true, newline = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
assert(res, "rendering should not fail here")
asserteq(select(2, string.gsub(code, "\n", "\n")), expected_num)
tmpl = string.sub(code, 1, -2) -- Remove the trailing newline
-- num_expected remains unchanged because the template will append a trailing newline
-- No trailing newline, no newline stripping
local t, err = template.compile(tmpl, { debug = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
assert(res, "rendering should not fail here")
asserteq(select(2, string.gsub(code, "\n", "\n")), expected_num)
-- No trailing newline, with newline stripping
local t, err = template.compile(tmpl, { debug = true, newline = true })
local res, err, code = t:render(my_env)
--print(res, err, code)
assert(res, "rendering should not fail here")
asserteq(select(2, string.gsub(code, "\n", "\n")), expected_num)
print("template: success")
|