From 55f002d193eb92878064b68383c94293742a69ee Mon Sep 17 00:00:00 2001 From: ReFreezed Date: Sat, 19 Mar 2022 23:23:43 +0100 Subject: [PATCH] Updated testsuite and changelog. --- Changelog.txt | 6 ++++++ preprocess.lua | 8 ++++---- tests/suite.lua | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 4ea7558..c670b14 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,6 +1,12 @@ Changelog LuaPreprocess +v1.18 (2022-03-19) +Library: +- Added functions: loadResource(), evaluate(), pairsSorted(), sortNatural() and compareNatural(). +- $symbol now accepts callable tables in addition to functions. +- 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)). + v1.17 (2021-11-22) Library: - Added predefined macros @@ASSERT() and @@LOG(). diff --git a/preprocess.lua b/preprocess.lua index ee4ed1c..3f13d10 100644 --- a/preprocess.lua +++ b/preprocess.lua @@ -1,6 +1,6 @@ --[[============================================================ --= ---= LuaPreprocess v1.17-dev - preprocessing library +--= LuaPreprocess v1.18 - preprocessing library --= by Marcus 'ReFreezed' Thunström --= --= License: MIT (see the bottom of this file) @@ -130,7 +130,7 @@ -local PP_VERSION = "1.17.0-dev" +local PP_VERSION = "1.18.0" local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString(). @@ -1491,8 +1491,8 @@ metaFuncs.serialize = serialize -- evaluate() -- value = evaluate( expression ) --- Evaluate an expression. Returns nil and a message on error. --- Note that nil or false can also be returned if that's the value the expression results in! +-- Evaluate a Lua expression. Returns nil and a message on error. +-- Note that nil or false can also be returned as the first value if that's the value the expression results in! metaFuncs.evaluate = evaluate -- escapePattern() diff --git a/tests/suite.lua b/tests/suite.lua index 0c05111..f9f9102 100644 --- a/tests/suite.lua +++ b/tests/suite.lua @@ -333,6 +333,13 @@ doTest("Macros", function() assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !!("1")!!("+")!!("2") ) ]]}), [[n = 1+2]] ) assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !!("1")!!("+")!!("2") } ]]}), [[n = { 1+2 }]] ) + -- Invalid code in arguments (which is ok). + local luaOut = assert(pp.processString{ code=[[ + !function BINOP(operator, a, b) return a..operator..b end + v = @@BINOP(^, 3, 2) + ]]}) + assertCodeOutput(luaOut, [[v = 3^2]]) + -- Invalid: Ambiguous syntax. assert(not pp.processString{ code=[[ !function VOID() return "" end @@ -427,6 +434,12 @@ doTest("Preprocessor symbols", function() ]]}) assertCodeOutput(luaOut, [[x = y]]) + local luaOut = assert(pp.processString{ code=[[ + !local FOO = setmetatable({}, {__call=function() return "y" end}) + x = $FOO + ]]}) + assertCodeOutput(luaOut, [[x = y]]) + -- Invalid: Symbols must result in strings. assert(not pp.processString{ code=[[ !local BAD = 840 @@ -436,6 +449,10 @@ doTest("Preprocessor symbols", function() !local function BAD() return 840 end v = $BAD ]]}) + assert(not pp.processString{ code=[[ + !local BAD = {} + v = $BAD + ]]}) end) @@ -571,6 +588,41 @@ doTest("Output interception", function() assert(not pp.processString{ code=[[ !stopInterceptingOutput() ]]}) end) +doTest("Resources and evaluation", function() + local pp = ppChunk() + + assert(pp.processString{ + code = [[ !assert(loadResource("x=x+1") == "x=x+1") ]], + onInsert = function(name) return name end, + }) + + assert(pp.processString{ + code = [[ !x = 8 ; assert(evaluate("2^x") == 2^x) ]], + onInsert = function(name) return name end, + }) +end) + +doTest("Misc.", function() + local pp = ppChunk() + + assert( ("foo9" < "foo10") == false) + assert(pp.compareNatural("foo9", "foo10") == true ) + + do + local keys = {"a2", "b", "a10", "-"} + local map = {a2=2, b=4, a10=3, ["-"]=1} + local count = 0 + + pp.sortNatural(keys) + + for k, order in pp.pairsSorted(map) do + count = count + 1 + assert(order == count) + assert(keys[order] == k) + end + end +end) + addLabel("Command line")