From 1cff8458f90472aa62c176c9e52e1a1eea75ff51 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Fri, 31 Dec 2021 15:17:00 +0100 Subject: chore(tests) rename template tests to relate to template module --- tests/test-template2.lua | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-text.lua | 76 ------------------------------------------------ 2 files changed, 76 insertions(+), 76 deletions(-) create mode 100644 tests/test-template2.lua delete mode 100644 tests/test-text.lua diff --git a/tests/test-template2.lua b/tests/test-template2.lua new file mode 100644 index 0000000..14e55a6 --- /dev/null +++ b/tests/test-template2.lua @@ -0,0 +1,76 @@ +local T = require 'pl.text' +local utils = require 'pl.utils' +local Template = T.Template +local asserteq = require 'pl.test'.asserteq +local OrderedMap = require 'pl.OrderedMap' +local template = require 'pl.template' + +local t = [[ +# for i = 1,3 do + print($(i+1)) +# end +]] + +asserteq(template.substitute(t),[[ + print(2) + print(3) + print(4) +]]) + +t = [[ +> for i = 1,3 do + print(${i+1}) +> end +]] + +asserteq(template.substitute(t,{_brackets='{}',_escape='>'}),[[ + print(2) + print(3) + print(4) +]]) + +t = [[ +#@ for i = 1,3 do + print(@{i+1}) +#@ end +]] + +asserteq(template.substitute(t,{_brackets='{}',_escape='#@',_inline_escape='@'}),[[ + print(2) + print(3) + print(4) +]]) + +--- iteration using pairs is usually unordered. But using OrderedMap +--- we can get the exact original ordering. + +t = [[ +# for k,v in pairs(T) do + "$(k)", -- $(v) +# end +]] + +if utils.lua51 then + -- easy enough to define a general pairs in Lua 5.1 + local rawpairs = pairs + function pairs(t) + local mt = getmetatable(t) + local f = mt and mt.__pairs + if f then + return f(t) + else + return rawpairs(t) + end + end +end + + +local Tee = OrderedMap{{Dog = 'Bonzo'}, {Cat = 'Felix'}, {Lion = 'Leo'}} + +-- note that the template will also look up global functions using _parent +asserteq(template.substitute(t,{T=Tee,_parent=_G}),[[ + "Dog", -- Bonzo + "Cat", -- Felix + "Lion", -- Leo +]]) + diff --git a/tests/test-text.lua b/tests/test-text.lua deleted file mode 100644 index 14e55a6..0000000 --- a/tests/test-text.lua +++ /dev/null @@ -1,76 +0,0 @@ -local T = require 'pl.text' -local utils = require 'pl.utils' -local Template = T.Template -local asserteq = require 'pl.test'.asserteq -local OrderedMap = require 'pl.OrderedMap' -local template = require 'pl.template' - -local t = [[ -# for i = 1,3 do - print($(i+1)) -# end -]] - -asserteq(template.substitute(t),[[ - print(2) - print(3) - print(4) -]]) - -t = [[ -> for i = 1,3 do - print(${i+1}) -> end -]] - -asserteq(template.substitute(t,{_brackets='{}',_escape='>'}),[[ - print(2) - print(3) - print(4) -]]) - -t = [[ -#@ for i = 1,3 do - print(@{i+1}) -#@ end -]] - -asserteq(template.substitute(t,{_brackets='{}',_escape='#@',_inline_escape='@'}),[[ - print(2) - print(3) - print(4) -]]) - ---- iteration using pairs is usually unordered. But using OrderedMap ---- we can get the exact original ordering. - -t = [[ -# for k,v in pairs(T) do - "$(k)", -- $(v) -# end -]] - -if utils.lua51 then - -- easy enough to define a general pairs in Lua 5.1 - local rawpairs = pairs - function pairs(t) - local mt = getmetatable(t) - local f = mt and mt.__pairs - if f then - return f(t) - else - return rawpairs(t) - end - end -end - - -local Tee = OrderedMap{{Dog = 'Bonzo'}, {Cat = 'Felix'}, {Lion = 'Leo'}} - --- note that the template will also look up global functions using _parent -asserteq(template.substitute(t,{T=Tee,_parent=_G}),[[ - "Dog", -- Bonzo - "Cat", -- Felix - "Lion", -- Leo -]]) - -- cgit v1.2.3