Skip to content

Commit 55f002d

Browse files
committed
Updated testsuite and changelog.
1 parent 014bb45 commit 55f002d

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Changelog.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
LuaPreprocess
33

4+
v1.18 (2022-03-19)
5+
Library:
6+
- Added functions: loadResource(), evaluate(), pairsSorted(), sortNatural() and compareNatural().
7+
- $symbol now accepts callable tables in addition to functions.
8+
- Argument expressions for macros are no longer validated. This was inconsistent before as they were only validated when not containing preprocessor code, like @@foo(!!(bar)).
9+
410
v1.17 (2021-11-22)
511
Library:
612
- Added predefined macros @@ASSERT() and @@LOG().

preprocess.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[============================================================
22
--=
3-
--= LuaPreprocess v1.17-dev - preprocessing library
3+
--= LuaPreprocess v1.18 - preprocessing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
66
--= License: MIT (see the bottom of this file)
@@ -130,7 +130,7 @@
130130

131131

132132

133-
local PP_VERSION = "1.17.0-dev"
133+
local PP_VERSION = "1.18.0"
134134

135135
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
136136

@@ -1491,8 +1491,8 @@ metaFuncs.serialize = serialize
14911491

14921492
-- evaluate()
14931493
-- value = evaluate( expression )
1494-
-- Evaluate an expression. Returns nil and a message on error.
1495-
-- Note that nil or false can also be returned if that's the value the expression results in!
1494+
-- Evaluate a Lua expression. Returns nil and a message on error.
1495+
-- Note that nil or false can also be returned as the first value if that's the value the expression results in!
14961496
metaFuncs.evaluate = evaluate
14971497

14981498
-- escapePattern()

tests/suite.lua

+52
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ doTest("Macros", function()
333333
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !!("1")!!("+")!!("2") ) ]]}), [[n = 1+2]] )
334334
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !!("1")!!("+")!!("2") } ]]}), [[n = { 1+2 }]] )
335335

336+
-- Invalid code in arguments (which is ok).
337+
local luaOut = assert(pp.processString{ code=[[
338+
!function BINOP(operator, a, b) return a..operator..b end
339+
v = @@BINOP(^, 3, 2)
340+
]]})
341+
assertCodeOutput(luaOut, [[v = 3^2]])
342+
336343
-- Invalid: Ambiguous syntax.
337344
assert(not pp.processString{ code=[[
338345
!function VOID() return "" end
@@ -427,6 +434,12 @@ doTest("Preprocessor symbols", function()
427434
]]})
428435
assertCodeOutput(luaOut, [[x = y]])
429436

437+
local luaOut = assert(pp.processString{ code=[[
438+
!local FOO = setmetatable({}, {__call=function() return "y" end})
439+
x = $FOO
440+
]]})
441+
assertCodeOutput(luaOut, [[x = y]])
442+
430443
-- Invalid: Symbols must result in strings.
431444
assert(not pp.processString{ code=[[
432445
!local BAD = 840
@@ -436,6 +449,10 @@ doTest("Preprocessor symbols", function()
436449
!local function BAD() return 840 end
437450
v = $BAD
438451
]]})
452+
assert(not pp.processString{ code=[[
453+
!local BAD = {}
454+
v = $BAD
455+
]]})
439456
end)
440457

441458

@@ -571,6 +588,41 @@ doTest("Output interception", function()
571588
assert(not pp.processString{ code=[[ !stopInterceptingOutput() ]]})
572589
end)
573590

591+
doTest("Resources and evaluation", function()
592+
local pp = ppChunk()
593+
594+
assert(pp.processString{
595+
code = [[ !assert(loadResource("x=x+1") == "x=x+1") ]],
596+
onInsert = function(name) return name end,
597+
})
598+
599+
assert(pp.processString{
600+
code = [[ !x = 8 ; assert(evaluate("2^x") == 2^x) ]],
601+
onInsert = function(name) return name end,
602+
})
603+
end)
604+
605+
doTest("Misc.", function()
606+
local pp = ppChunk()
607+
608+
assert( ("foo9" < "foo10") == false)
609+
assert(pp.compareNatural("foo9", "foo10") == true )
610+
611+
do
612+
local keys = {"a2", "b", "a10", "-"}
613+
local map = {a2=2, b=4, a10=3, ["-"]=1}
614+
local count = 0
615+
616+
pp.sortNatural(keys)
617+
618+
for k, order in pp.pairsSorted(map) do
619+
count = count + 1
620+
assert(order == count)
621+
assert(keys[order] == k)
622+
end
623+
end
624+
end)
625+
574626

575627

576628
addLabel("Command line")

0 commit comments

Comments
 (0)