diff options
Diffstat (limited to 'testes/main.lua')
-rw-r--r-- | testes/main.lua | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/testes/main.lua b/testes/main.lua new file mode 100644 index 00000000..582b39c0 --- /dev/null +++ b/testes/main.lua | |||
@@ -0,0 +1,381 @@ | |||
1 | # testing special comment on first line | ||
2 | -- $Id: main.lua,v 1.69 2018/06/19 12:23:50 roberto Exp $ | ||
3 | -- See Copyright Notice in file all.lua | ||
4 | |||
5 | -- most (all?) tests here assume a reasonable "Unix-like" shell | ||
6 | if _port then return end | ||
7 | |||
8 | -- use only "double quotes" inside shell scripts (better change to | ||
9 | -- run on Windows) | ||
10 | |||
11 | |||
12 | print ("testing stand-alone interpreter") | ||
13 | |||
14 | assert(os.execute()) -- machine has a system command | ||
15 | |||
16 | local arg = arg or ARG | ||
17 | |||
18 | local prog = os.tmpname() | ||
19 | local otherprog = os.tmpname() | ||
20 | local out = os.tmpname() | ||
21 | |||
22 | local progname | ||
23 | do | ||
24 | local i = 0 | ||
25 | while arg[i] do i=i-1 end | ||
26 | progname = arg[i+1] | ||
27 | end | ||
28 | print("progname: "..progname) | ||
29 | |||
30 | local prepfile = function (s, p) | ||
31 | p = p or prog | ||
32 | io.output(p) | ||
33 | io.write(s) | ||
34 | assert(io.close()) | ||
35 | end | ||
36 | |||
37 | local function getoutput () | ||
38 | io.input(out) | ||
39 | local t = io.read("a") | ||
40 | io.input():close() | ||
41 | assert(os.remove(out)) | ||
42 | return t | ||
43 | end | ||
44 | |||
45 | local function checkprogout (s) | ||
46 | local t = getoutput() | ||
47 | for line in string.gmatch(s, ".-\n") do | ||
48 | assert(string.find(t, line, 1, true)) | ||
49 | end | ||
50 | end | ||
51 | |||
52 | local function checkout (s) | ||
53 | local t = getoutput() | ||
54 | if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end | ||
55 | assert(s == t) | ||
56 | return t | ||
57 | end | ||
58 | |||
59 | |||
60 | local function RUN (p, ...) | ||
61 | p = string.gsub(p, "lua", '"'..progname..'"', 1) | ||
62 | local s = string.format(p, ...) | ||
63 | assert(os.execute(s)) | ||
64 | end | ||
65 | |||
66 | local function NoRun (msg, p, ...) | ||
67 | p = string.gsub(p, "lua", '"'..progname..'"', 1) | ||
68 | local s = string.format(p, ...) | ||
69 | s = string.format("%s 2> %s", s, out) -- will send error to 'out' | ||
70 | assert(not os.execute(s)) | ||
71 | assert(string.find(getoutput(), msg, 1, true)) -- check error message | ||
72 | end | ||
73 | |||
74 | RUN('lua -v') | ||
75 | |||
76 | print(string.format("(temporary program file used in these tests: %s)", prog)) | ||
77 | |||
78 | -- running stdin as a file | ||
79 | prepfile"" | ||
80 | RUN('lua - < %s > %s', prog, out) | ||
81 | checkout("") | ||
82 | |||
83 | prepfile[[ | ||
84 | print( | ||
85 | 1, a | ||
86 | ) | ||
87 | ]] | ||
88 | RUN('lua - < %s > %s', prog, out) | ||
89 | checkout("1\tnil\n") | ||
90 | |||
91 | RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) | ||
92 | checkout("10\n2\n") | ||
93 | |||
94 | |||
95 | -- test option '-' | ||
96 | RUN('echo "print(arg[1])" | lua - -h > %s', out) | ||
97 | checkout("-h\n") | ||
98 | |||
99 | -- test environment variables used by Lua | ||
100 | |||
101 | prepfile("print(package.path)") | ||
102 | |||
103 | -- test LUA_PATH | ||
104 | RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) | ||
105 | checkout("x\n") | ||
106 | |||
107 | -- test LUA_PATH_version | ||
108 | RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out) | ||
109 | checkout("y\n") | ||
110 | |||
111 | -- test LUA_CPATH | ||
112 | prepfile("print(package.cpath)") | ||
113 | RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) | ||
114 | checkout("xuxu\n") | ||
115 | |||
116 | -- test LUA_CPATH_version | ||
117 | RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out) | ||
118 | checkout("yacc\n") | ||
119 | |||
120 | -- test LUA_INIT (and its access to 'arg' table) | ||
121 | prepfile("print(X)") | ||
122 | RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out) | ||
123 | checkout("3.2\n") | ||
124 | |||
125 | -- test LUA_INIT_version | ||
126 | prepfile("print(X)") | ||
127 | RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) | ||
128 | checkout("10\n") | ||
129 | |||
130 | -- test LUA_INIT for files | ||
131 | prepfile("x = x or 10; print(x); x = x + 1") | ||
132 | RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out) | ||
133 | checkout("10\n11\n") | ||
134 | |||
135 | -- test errors in LUA_INIT | ||
136 | NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') | ||
137 | |||
138 | -- test option '-E' | ||
139 | local defaultpath, defaultCpath | ||
140 | |||
141 | do | ||
142 | prepfile("print(package.path, package.cpath)") | ||
143 | RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', | ||
144 | prog, out) | ||
145 | local out = getoutput() | ||
146 | defaultpath = string.match(out, "^(.-)\t") | ||
147 | defaultCpath = string.match(out, "\t(.-)$") | ||
148 | end | ||
149 | |||
150 | -- paths did not changed | ||
151 | assert(not string.find(defaultpath, "xxx") and | ||
152 | string.find(defaultpath, "lua") and | ||
153 | not string.find(defaultCpath, "xxx") and | ||
154 | string.find(defaultCpath, "lua")) | ||
155 | |||
156 | |||
157 | -- test replacement of ';;' to default path | ||
158 | local function convert (p) | ||
159 | prepfile("print(package.path)") | ||
160 | RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) | ||
161 | local expected = getoutput() | ||
162 | expected = string.sub(expected, 1, -2) -- cut final end of line | ||
163 | assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) | ||
164 | end | ||
165 | |||
166 | convert(";") | ||
167 | convert(";;") | ||
168 | convert(";;;") | ||
169 | convert(";;;;") | ||
170 | convert(";;;;;") | ||
171 | convert(";;a;;;bc") | ||
172 | |||
173 | |||
174 | -- test -l over multiple libraries | ||
175 | prepfile("print(1); a=2; return {x=15}") | ||
176 | prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) | ||
177 | RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) | ||
178 | checkout("1\n2\n15\n2\n15\n") | ||
179 | |||
180 | -- test 'arg' table | ||
181 | local a = [[ | ||
182 | assert(#arg == 3 and arg[1] == 'a' and | ||
183 | arg[2] == 'b' and arg[3] == 'c') | ||
184 | assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s') | ||
185 | assert(arg[4] == undef and arg[-4] == undef) | ||
186 | local a, b, c = ... | ||
187 | assert(... == 'a' and a == 'a' and b == 'b' and c == 'c') | ||
188 | ]] | ||
189 | a = string.format(a, progname) | ||
190 | prepfile(a) | ||
191 | RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command | ||
192 | |||
193 | -- test 'arg' availability in libraries | ||
194 | prepfile"assert(arg)" | ||
195 | prepfile("assert(arg)", otherprog) | ||
196 | RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) | ||
197 | |||
198 | -- test messing up the 'arg' table | ||
199 | RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out) | ||
200 | checkout("100\n") | ||
201 | NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -') | ||
202 | |||
203 | -- test error in 'print' | ||
204 | RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out) | ||
205 | assert(string.find(getoutput(), "error calling 'print'")) | ||
206 | |||
207 | -- test 'debug.debug' | ||
208 | RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) | ||
209 | checkout("lua_debug> 1000lua_debug> ") | ||
210 | |||
211 | -- test many arguments | ||
212 | prepfile[[print(({...})[30])]] | ||
213 | RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) | ||
214 | checkout("a\n") | ||
215 | |||
216 | RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) | ||
217 | checkout("1\n3\n") | ||
218 | |||
219 | -- test iteractive mode | ||
220 | prepfile[[ | ||
221 | (6*2-6) -- === | ||
222 | a = | ||
223 | 10 | ||
224 | print(a) | ||
225 | a]] | ||
226 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) | ||
227 | checkprogout("6\n10\n10\n\n") | ||
228 | |||
229 | prepfile("a = [[b\nc\nd\ne]]\n=a") | ||
230 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) | ||
231 | checkprogout("b\nc\nd\ne\n\n") | ||
232 | |||
233 | prompt = "alo" | ||
234 | prepfile[[ -- | ||
235 | a = 2 | ||
236 | ]] | ||
237 | RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) | ||
238 | local t = getoutput() | ||
239 | assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) | ||
240 | |||
241 | -- test for error objects | ||
242 | prepfile[[ | ||
243 | debug = require "debug" | ||
244 | m = {x=0} | ||
245 | setmetatable(m, {__tostring = function(x) | ||
246 | return tostring(debug.getinfo(4).currentline + x.x) | ||
247 | end}) | ||
248 | error(m) | ||
249 | ]] | ||
250 | NoRun(progname .. ": 6\n", [[lua %s]], prog) | ||
251 | |||
252 | prepfile("error{}") | ||
253 | NoRun("error object is a table value", [[lua %s]], prog) | ||
254 | |||
255 | |||
256 | -- chunk broken in many lines | ||
257 | s = [=[ -- | ||
258 | function f ( x ) | ||
259 | local a = [[ | ||
260 | xuxu | ||
261 | ]] | ||
262 | local b = "\ | ||
263 | xuxu\n" | ||
264 | if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] | ||
265 | return x + 1 | ||
266 | --\\ | ||
267 | end | ||
268 | return( f( 100 ) ) | ||
269 | assert( a == b ) | ||
270 | do return f( 11 ) end ]=] | ||
271 | s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines | ||
272 | prepfile(s) | ||
273 | RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) | ||
274 | checkprogout("101\n13\t22\n\n") | ||
275 | |||
276 | prepfile[[#comment in 1st line without \n at the end]] | ||
277 | RUN('lua %s', prog) | ||
278 | |||
279 | prepfile[[#test line number when file starts with comment line | ||
280 | debug = require"debug" | ||
281 | print(debug.getinfo(1).currentline) | ||
282 | ]] | ||
283 | RUN('lua %s > %s', prog, out) | ||
284 | checkprogout('3') | ||
285 | |||
286 | -- close Lua with an open file | ||
287 | prepfile(string.format([[io.output(%q); io.write('alo')]], out)) | ||
288 | RUN('lua %s', prog) | ||
289 | checkout('alo') | ||
290 | |||
291 | -- bug in 5.2 beta (extra \0 after version line) | ||
292 | RUN([[lua -v -e"print'hello'" > %s]], out) | ||
293 | t = getoutput() | ||
294 | assert(string.find(t, "PUC%-Rio\nhello")) | ||
295 | |||
296 | |||
297 | -- testing os.exit | ||
298 | prepfile("os.exit(nil, true)") | ||
299 | RUN('lua %s', prog) | ||
300 | prepfile("os.exit(0, true)") | ||
301 | RUN('lua %s', prog) | ||
302 | prepfile("os.exit(true, true)") | ||
303 | RUN('lua %s', prog) | ||
304 | prepfile("os.exit(1, true)") | ||
305 | NoRun("", "lua %s", prog) -- no message | ||
306 | prepfile("os.exit(false, true)") | ||
307 | NoRun("", "lua %s", prog) -- no message | ||
308 | |||
309 | -- remove temporary files | ||
310 | assert(os.remove(prog)) | ||
311 | assert(os.remove(otherprog)) | ||
312 | assert(not os.remove(out)) | ||
313 | |||
314 | -- invalid options | ||
315 | NoRun("unrecognized option '-h'", "lua -h") | ||
316 | NoRun("unrecognized option '---'", "lua ---") | ||
317 | NoRun("unrecognized option '-Ex'", "lua -Ex") | ||
318 | NoRun("unrecognized option '-vv'", "lua -vv") | ||
319 | NoRun("unrecognized option '-iv'", "lua -iv") | ||
320 | NoRun("'-e' needs argument", "lua -e") | ||
321 | NoRun("syntax error", "lua -e a") | ||
322 | NoRun("'-l' needs argument", "lua -l") | ||
323 | |||
324 | |||
325 | if T then -- auxiliary library? | ||
326 | print("testing 'not enough memory' to create a state") | ||
327 | NoRun("not enough memory", "env MEMLIMIT=100 lua") | ||
328 | end | ||
329 | print('+') | ||
330 | |||
331 | print('testing Ctrl C') | ||
332 | do | ||
333 | -- interrupt a script | ||
334 | local function kill (pid) | ||
335 | return os.execute(string.format('kill -INT %s 2> /dev/null', pid)) | ||
336 | end | ||
337 | |||
338 | -- function to run a script in background, returning its output file | ||
339 | -- descriptor and its pid | ||
340 | local function runback (luaprg) | ||
341 | -- shell script to run 'luaprg' in background and echo its pid | ||
342 | local shellprg = string.format('%s -e "%s" & echo $!', progname, luaprg) | ||
343 | local f = io.popen(shellprg, "r") -- run shell script | ||
344 | local pid = f:read() -- get pid for Lua script | ||
345 | print("(if test fails now, it may leave a Lua script running in \z | ||
346 | background, pid " .. pid .. ")") | ||
347 | return f, pid | ||
348 | end | ||
349 | |||
350 | -- Lua script that runs protected infinite loop and then prints '42' | ||
351 | local f, pid = runback[[ | ||
352 | pcall(function () print(12); while true do end end); print(42)]] | ||
353 | -- wait until script is inside 'pcall' | ||
354 | assert(f:read() == "12") | ||
355 | kill(pid) -- send INT signal to Lua script | ||
356 | -- check that 'pcall' captured the exception and script continued running | ||
357 | assert(f:read() == "42") -- expected output | ||
358 | assert(f:close()) | ||
359 | print("done") | ||
360 | |||
361 | -- Lua script in a long unbreakable search | ||
362 | local f, pid = runback[[ | ||
363 | print(15); string.find(string.rep('a', 100000), '.*b')]] | ||
364 | -- wait (so script can reach the loop) | ||
365 | assert(f:read() == "15") | ||
366 | assert(os.execute("sleep 1")) | ||
367 | -- must send at least two INT signals to stop this Lua script | ||
368 | local n = 100 | ||
369 | for i = 0, 100 do -- keep sending signals | ||
370 | if not kill(pid) then -- until it fails | ||
371 | n = i -- number of non-failed kills | ||
372 | break | ||
373 | end | ||
374 | end | ||
375 | assert(f:close()) | ||
376 | assert(n >= 2) | ||
377 | print(string.format("done (with %d kills)", n)) | ||
378 | |||
379 | end | ||
380 | |||
381 | print("OK") | ||