22-- = Tests for LuaPreprocess.
33-- ==============================================================
44
5+ io.stdout :setvbuf (" no" )
6+ io.stderr :setvbuf (" no" )
7+
58local results = {}
69
710local function doTest (description , f , ...)
@@ -12,7 +15,6 @@ local function doTest(description, f, ...)
1215
1316 table.insert (results , {description = description , ok = ok })
1417end
15-
1618local function addLabel (label )
1719 table.insert (results , {label = label })
1820end
@@ -21,17 +23,40 @@ local function trim(s)
2123 return (s :gsub (" ^%s+" , " " ):gsub (" %s+$" , " " ))
2224end
2325
26+ local function readFile (path )
27+ local file , err = io.open (path , " rb" )
28+ if not file then return nil , err end
29+
30+ local data = file :read (" *a" )
31+ file :close ()
32+
33+ return data
34+ end
35+ local function writeFile (path , data )
36+ local file , err = io.open (path , " wb" )
37+ if not file then return false , err end
38+
39+ file :write (data )
40+ file :close ()
41+
42+ return true
43+ end
44+
2445local function assertCodeOutput (codeOut , codeExpected , message )
2546 assert (trim (codeOut ) == codeExpected , (message or " Unexpected output: " .. codeOut ))
2647end
48+ local function assertCmd (cmd )
49+ local code = os.execute (cmd )
50+ if code ~= 0 then error (" Command failed: " .. cmd , 2 ) end
51+ end
2752
2853-- ==============================================================
2954
3055
3156
32- addLabel (" Preprocessor code. " )
57+ addLabel (" Preprocessor code" )
3358
34- doTest (" Inline block with simple expression. " , function ()
59+ doTest (" Inline block with simple expression" , function ()
3560 local pp = assert (loadfile " preprocess.lua" )()
3661 local luaIn = [[
3762 local x = !(1+2*3)
@@ -41,7 +66,7 @@ doTest("Inline block with simple expression.", function()
4166 assertCodeOutput (luaOut , [[ local x = 7]] )
4267end )
4368
44- doTest (" Static branch. " , function ()
69+ doTest (" Static branch" , function ()
4570 local pp = assert (loadfile " preprocess.lua" )()
4671 local luaIn = [[
4772 !if FLAG then
@@ -60,7 +85,7 @@ doTest("Static branch.", function()
6085 assertCodeOutput (luaOut , [[ print("No")]] , " Unexpected output with FLAG=false." )
6186end )
6287
63- doTest (" Output value from metaprogram. " , function ()
88+ doTest (" Output value from metaprogram" , function ()
6489 local pp = assert (loadfile " preprocess.lua" )()
6590 local luaIn = [[
6691 !local t = {
@@ -75,7 +100,7 @@ doTest("Output value from metaprogram.", function()
75100 assertCodeOutput (luaOut , [[ local theTable = {a=2,z=99}]] )
76101end )
77102
78- doTest (" Generate code. " , function ()
103+ doTest (" Generate code" , function ()
79104 local pp = assert (loadfile " preprocess.lua" )()
80105 local luaIn = [[
81106 !(
@@ -91,7 +116,7 @@ doTest("Generate code.", function()
91116 assertCodeOutput (luaOut , [[ local s = "\n"]] ) -- Debug mode changes how newlines appear in string values.
92117end )
93118
94- doTest (" Parsing extended preprocessor line. " , function ()
119+ doTest (" Parsing extended preprocessor line" , function ()
95120 local pp = assert (loadfile " preprocess.lua" )()
96121 local luaIn = [[
97122 !local str = "foo\
@@ -111,7 +136,7 @@ doTest("Parsing extended preprocessor line.", function()
111136 assertCodeOutput (luaOut , [[ local z = 137]] )
112137end )
113138
114- doTest (" Dual code. " , function ()
139+ doTest (" Dual code" , function ()
115140 local pp = assert (loadfile " preprocess.lua" )()
116141 local luaIn = [[
117142 !local one = 1
@@ -142,7 +167,7 @@ doTest("Expression or not?", function()
142167 assertCodeOutput (luaOut , [[ ]] )
143168end )
144169
145- doTest (" Output values of different types. " , function ()
170+ doTest (" Output values of different types" , function ()
146171 local pp = assert (loadfile " preprocess.lua" )()
147172
148173 -- Valid: Numbers, strings, tables, booleans, nil.
173198
174199
175200
176- addLabel (" Library API. " )
201+ addLabel (" Library API" )
177202
178- doTest (" Get useful tokens. " , function ()
203+ doTest (" Get useful tokens" , function ()
179204 local pp = assert (loadfile " preprocess.lua" )()
180205 local tokens = pp .tokenize [[ local x = 5 -- Foo!]]
181206
@@ -192,7 +217,7 @@ doTest("Get useful tokens.", function()
192217 assert (tokens [4 ].value == 5 , " Unexpected token value 4." )
193218end )
194219
195- doTest (" Serialize. " , function ()
220+ doTest (" Serialize" , function ()
196221 local pp = assert (loadfile " preprocess.lua" )()
197222
198223 local t = {
@@ -206,6 +231,51 @@ end)
206231
207232
208233
234+ addLabel (" Command line" )
235+
236+ doTest (" Simple processing of single file" , function ()
237+ assert (writeFile (" local/generatedTest.lua2p" , [[
238+ !outputLua("math.floor(1.5)")
239+ ]] ))
240+ assertCmd ([[ lua preprocess-cl.lua local/generatedTest.lua2p]] )
241+
242+ local luaOut = assert (readFile (" local/generatedTest.lua" ))
243+ assertCodeOutput (luaOut , [[ math.floor(1.5)]] )
244+ end )
245+
246+ doTest (" Send data" , function ()
247+ assert (writeFile (" local/generatedTest.lua2p" , [[
248+ print(!(dataFromCommandLine))
249+ ]] ))
250+ assertCmd ([[ lua preprocess-cl.lua --outputpaths --data="Hello, world!" local/generatedTest.lua2p local/generatedTest.lua]] )
251+
252+ local luaOut = assert (readFile (" local/generatedTest.lua" ))
253+ assertCodeOutput (luaOut , [[ print("Hello, world!")]] )
254+ end )
255+
256+ doTest (" Handler + multiple files" , function ()
257+ assert (writeFile (" local/generatedHandler.lua" , [[
258+ _G.one = 1
259+ return {
260+ aftermeta = function(path, luaString)
261+ print(path, luaString)
262+ return 'print("foo");'..luaString -- Prepend some code.
263+ end,
264+ }
265+ ]] ))
266+ assert (writeFile (" local/generatedTest1.lua2p" , " !!local x = one+2*3\n " ))
267+ assert (writeFile (" local/generatedTest2.lua2p" , " !!local y = one+2^10\n " ))
268+
269+ assertCmd ([[ lua preprocess-cl.lua --handler=local/generatedHandler.lua local/generatedTest1.lua2p local/generatedTest2.lua2p]] )
270+
271+ local luaOut = assert (readFile (" local/generatedTest1.lua" ))
272+ assertCodeOutput (luaOut , [[ print("foo");local x = 7]] )
273+ local luaOut = assert (readFile (" local/generatedTest2.lua" ))
274+ assertCodeOutput (luaOut , [[ print("foo");local y = 1025]] )
275+ end )
276+
277+
278+
209279-- ==============================================================
210280
211281local countResults = 0
@@ -237,3 +307,5 @@ if countFails == 0 then
237307else
238308 print (countFails .. " /" .. countResults .. " tests FAILED!!!" )
239309end
310+
311+ os.exit (countFails == 0 and 0 or 1 )
0 commit comments