From 42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 30 Oct 2021 11:32:16 +0800 Subject: + Penlight --- Data/Libraries/Penlight/tests/test-template.lua | 245 ++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 Data/Libraries/Penlight/tests/test-template.lua (limited to 'Data/Libraries/Penlight/tests/test-template.lua') diff --git a/Data/Libraries/Penlight/tests/test-template.lua b/Data/Libraries/Penlight/tests/test-template.lua new file mode 100644 index 0000000..fb13108 --- /dev/null +++ b/Data/Libraries/Penlight/tests/test-template.lua @@ -0,0 +1,245 @@ +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 +

Hello $(tostring(i))

+# end +]],_G),[[ +

Hello 1

+

Hello 2

+]]) + + + +asserteq(subst([[ + +]],{ls = List{'john','alice','jane'}}),[[ + +]]) + + + +-- 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 = [[]] + +local my_env = { + ipairs = ipairs, + T = {'one','two','three'}, + _debug = true, +} +local res, err = template.substitute(tmpl, my_env) + +--print(res, err) +asserteq(res, [[]]) + + + +-------------------------------------------------- +-- Test using both leading and trailing linebreak +local tmpl = [[ + +]] + +local my_env = { + ipairs = ipairs, + T = {'one','two','three'}, + _debug = true, +} +local res, err = template.substitute(tmpl, my_env) + +--print(res, err) +asserteq(res, [[ + +]]) + + + +-------------------------------------------------- +-- Test reusing a compiled template +local tmpl = [[ + +]] + +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, [[ + +]]) + + +-- 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, [[ + +]]) + + + +-------------------------------------------------- +-- 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 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 = "" }) +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 = "" }) +--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 = [[ + +]] + +local t, err = template.compile(tmpl, { debug = true }) +local res, err, code = t:render(my_env) +--print(res, err, code) + +asserteq(res, [[ +]]) +asserteq(code, [[return "\ +"]]) + + +print("template: success") -- cgit v1.1-26-g67d0