@@ -11,12 +11,16 @@ local results = {}
1111local function doTest (description , f , ...)
1212 print (" Running test: " .. description )
1313
14- -- f(...) -- DEBUG
1514 local ok , err = pcall (f , ... )
1615 if not ok then print (" Error: " .. tostring (err )) end
1716
1817 table.insert (results , {description = description , ok = ok })
1918end
19+ local function doTestUnprotected (description , f , ...)
20+ print (" Running test: " .. description )
21+ f (... )
22+ table.insert (results , {description = description , ok = true })
23+ end
2024local function addLabel (label )
2125 table.insert (results , {label = label })
2226end
@@ -158,6 +162,9 @@ doTest("Dual code", function()
158162 !!local n, s = 5^5, "foo".."bar";
159163 ]] })
160164 assertCodeOutput (luaOut , [[ local n, s = 3125, "foobar";]] )
165+
166+ -- Invalid: Duplicate names.
167+ assert (not pp .processString { code = [[ !!x, y, x = 0 ]] })
161168end )
162169
163170doTest (" Expression or not?" , function ()
@@ -314,7 +321,7 @@ doTest("Macros", function()
314321 ]] })
315322 assertCodeOutput (luaOut , [[ v = woof_woof_woof_woof_woof_woof_woof_woof]] )
316323
317- -- Code blocks in macros.
324+ -- Metaprogram code in macros.
318325 assertCodeOutput (assert (pp .processString { code = [[ !(function ECHO(v) return v end) n = @@ECHO( !( 1 ) ) ]] }), [[ n = 1]] )
319326 assertCodeOutput (assert (pp .processString { code = [[ !(function ECHO(v) return v end) n = @@ECHO( !!("1") ) ]] }), [[ n = 1]] )
320327 assertCodeOutput (assert (pp .processString { code = [[ !(function ECHO(v) return v end) n = @@ECHO{ !( 1 ) } ]] }), [[ n = { 1 }]] )
@@ -336,6 +343,24 @@ doTest("Macros", function()
336343 assertCodeOutput (assert (pp .processString { code = [[ !(function ECHO(v) return v end) n = @@ECHO( !(do outputLua"1" end) ) ]] }), [[ n = 1]] )
337344 assertCodeOutput (assert (pp .processString { code = [[ !(function ECHO(v) return v end) n = @@ECHO{ !(do outputLua"1" end) } ]] }), [[ n = { 1 }]] )
338345
346+ local luaOut = assert (pp .processString { code = [[
347+ !function ECHO(v) return v end
348+ n = @@ECHO(
349+ !outputLua("1")
350+ )
351+ ]] })
352+ assertCodeOutput (luaOut , [[ n = 1]] )
353+
354+ -- Prefixes/suffixes.
355+ assert (pp .processString { macroPrefix = " MACRO_" , code = [[
356+ !local function MACRO_FOO() return "" end
357+ @@FOO()
358+ ]] })
359+ assert (pp .processString { macroSuffix = " _MACRO" , code = [[
360+ !local function FOO_MACRO() return "" end
361+ @@FOO()
362+ ]] })
363+
339364 -- Invalid code in arguments (which is ok).
340365 local luaOut = assert (pp .processString { code = [[
341366 !function BINOP(operator, a, b) return a..operator..b end
@@ -591,10 +616,13 @@ doTest("Resources and evaluation", function()
591616 onInsert = function (name ) return name end ,
592617 })
593618
594- assert (pp .processString {
595- code = [[ !x = 8 ; assert(evaluate("2^x") == 2^x) ]] ,
596- onInsert = function (name ) return name end ,
597- })
619+ _G . x = 8 ; assert (pp .evaluate (" 2^x" ) == 2 ^ x ) ; _G .x = nil
620+ local x = 8 ; assert (pp .evaluate (" 2^x" , {x = x }) == 2 ^ x )
621+ assert (not pp .evaluate (" 2^x" )) -- (Global) x should be nil.
622+
623+ if jit then
624+ assert (assert (pp .evaluate " 0b101" ) == 5 )
625+ end
598626end )
599627
600628doTest (" Indentation" , function ()
@@ -603,6 +631,9 @@ doTest("Indentation", function()
603631 assert (pp .getIndentation (" \t foo" ) == " \t " )
604632 assert (pp .getIndentation (" \n foo" ) == " " )
605633
634+ assertCodeOutput (assert (pp .processString { code = " \t \t indent = !(getCurrentIndentationInOutput(4))" }), [[ indent = 8]] )
635+ assertCodeOutput (assert (pp .processString { code = " \n\n\t \t indent = !(getCurrentIndentationInOutput(4))\n\n " }), [[ indent = 8]] )
636+
606637 -- Spaces.
607638 local indent , expect = pp .getIndentation (" " , 4 ), 0 ; if indent ~= expect then error (expect .. " " .. indent ) end
608639 local indent , expect = pp .getIndentation (" " , 4 ), 1 ; if indent ~= expect then error (expect .. " " .. indent ) end
626657doTest (" Misc." , function ()
627658 local pp = ppChunk ()
628659
660+ assert (pp .metaEnvironment .table == table )
661+
662+ -- Natural comparisons.
629663 assert ( (" foo9" < " foo10" ) == false )
630664 assert (pp .compareNatural (" foo9" , " foo10" ) == true )
631665
@@ -642,6 +676,11 @@ doTest("Misc.", function()
642676 assert (keys [order ] == k )
643677 end
644678 end
679+
680+ -- Current output.
681+ assertCodeOutput (assert (pp .processString { code = " x = 1 ; y = !(getOutputSoFar())" }), ' x = 1 ; y = "x = 1 ; y = "' )
682+ assertCodeOutput (assert (pp .processString { code = " x = !(getOutputSoFarOnLine ())\n\t y = !(getOutputSoFarOnLine ())" }), ' x = "x = "\n\t y = "\\ ty = "' )
683+ assertCodeOutput (assert (pp .processString { code = " x = !(getCurrentLineNumberInOutput())\n\t y = !(getCurrentLineNumberInOutput())" }), " x = 1\n\t y = 2" )
645684end )
646685
647686
0 commit comments