Skip to content

Commit 7b1f5a3

Browse files
committed
Tests for command line program. Proper byte units.
1 parent 889ae73 commit 7b1f5a3

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

misc/runTests.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@ECHO OFF
22
CD /D "%~dp0.."
33

4+
IF NOT EXIST local MD local
5+
46
lua misc/runTests.lua

misc/runTests.lua

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
--= Tests for LuaPreprocess.
33
--==============================================================
44

5+
io.stdout:setvbuf("no")
6+
io.stderr:setvbuf("no")
7+
58
local results = {}
69

710
local function doTest(description, f, ...)
@@ -12,7 +15,6 @@ local function doTest(description, f, ...)
1215

1316
table.insert(results, {description=description, ok=ok})
1417
end
15-
1618
local function addLabel(label)
1719
table.insert(results, {label=label})
1820
end
@@ -21,17 +23,40 @@ local function trim(s)
2123
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
2224
end
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+
2445
local function assertCodeOutput(codeOut, codeExpected, message)
2546
assert(trim(codeOut) == codeExpected, (message or "Unexpected output: "..codeOut))
2647
end
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]])
4267
end)
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.")
6186
end)
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}]])
76101
end)
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.
92117
end)
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]])
112137
end)
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, [[]])
143168
end)
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.
@@ -173,9 +198,9 @@ end)
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.")
193218
end)
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

211281
local countResults = 0
@@ -237,3 +307,5 @@ if countFails == 0 then
237307
else
238308
print(countFails.."/"..countResults.." tests FAILED!!!")
239309
end
310+
311+
os.exit(countFails == 0 and 0 or 1)

preprocess-cl.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ exec lua "$0" "$@"
77
--= LuaPreprocess command line program
88
--= by Marcus 'ReFreezed' Thunström
99
--=
10+
--= Requires preprocess.lua!
11+
--=
1012
--= License: MIT (see the bottom of this file)
1113
--= Website: https://github.com/ReFreezed/LuaPreprocess
1214
--=
@@ -154,11 +156,11 @@ local printf, printfNoise
154156
F = string.format
155157
function formatBytes(n)
156158
if n >= 1024*1024*1024 then
157-
return F("%.2f GB", n/(1024*1024*1024))
159+
return F("%.2f GiB", n/(1024*1024*1024))
158160
elseif n >= 1024*1024 then
159-
return F("%.2f MB", n/(1024*1024))
161+
return F("%.2f MiB", n/(1024*1024))
160162
elseif n >= 1024 then
161-
return F("%.2f kB", n/(1024))
163+
return F("%.2f KiB", n/(1024))
162164
elseif n == 1 then
163165
return F("1 byte", n)
164166
else

0 commit comments

Comments
 (0)