aboutsummaryrefslogtreecommitdiff
path: root/testes/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/main.lua')
-rw-r--r--testes/main.lua381
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
6if _port then return end
7
8-- use only "double quotes" inside shell scripts (better change to
9-- run on Windows)
10
11
12print ("testing stand-alone interpreter")
13
14assert(os.execute()) -- machine has a system command
15
16local arg = arg or ARG
17
18local prog = os.tmpname()
19local otherprog = os.tmpname()
20local out = os.tmpname()
21
22local progname
23do
24 local i = 0
25 while arg[i] do i=i-1 end
26 progname = arg[i+1]
27end
28print("progname: "..progname)
29
30local prepfile = function (s, p)
31 p = p or prog
32 io.output(p)
33 io.write(s)
34 assert(io.close())
35end
36
37local function getoutput ()
38 io.input(out)
39 local t = io.read("a")
40 io.input():close()
41 assert(os.remove(out))
42 return t
43end
44
45local 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
50end
51
52local 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
57end
58
59
60local function RUN (p, ...)
61 p = string.gsub(p, "lua", '"'..progname..'"', 1)
62 local s = string.format(p, ...)
63 assert(os.execute(s))
64end
65
66local 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
72end
73
74RUN('lua -v')
75
76print(string.format("(temporary program file used in these tests: %s)", prog))
77
78-- running stdin as a file
79prepfile""
80RUN('lua - < %s > %s', prog, out)
81checkout("")
82
83prepfile[[
84 print(
851, a
86)
87]]
88RUN('lua - < %s > %s', prog, out)
89checkout("1\tnil\n")
90
91RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
92checkout("10\n2\n")
93
94
95-- test option '-'
96RUN('echo "print(arg[1])" | lua - -h > %s', out)
97checkout("-h\n")
98
99-- test environment variables used by Lua
100
101prepfile("print(package.path)")
102
103-- test LUA_PATH
104RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out)
105checkout("x\n")
106
107-- test LUA_PATH_version
108RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out)
109checkout("y\n")
110
111-- test LUA_CPATH
112prepfile("print(package.cpath)")
113RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out)
114checkout("xuxu\n")
115
116-- test LUA_CPATH_version
117RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out)
118checkout("yacc\n")
119
120-- test LUA_INIT (and its access to 'arg' table)
121prepfile("print(X)")
122RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out)
123checkout("3.2\n")
124
125-- test LUA_INIT_version
126prepfile("print(X)")
127RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
128checkout("10\n")
129
130-- test LUA_INIT for files
131prepfile("x = x or 10; print(x); x = x + 1")
132RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out)
133checkout("10\n11\n")
134
135-- test errors in LUA_INIT
136NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua')
137
138-- test option '-E'
139local defaultpath, defaultCpath
140
141do
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(.-)$")
148end
149
150-- paths did not changed
151assert(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
158local 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)
164end
165
166convert(";")
167convert(";;")
168convert(";;;")
169convert(";;;;")
170convert(";;;;;")
171convert(";;a;;;bc")
172
173
174-- test -l over multiple libraries
175prepfile("print(1); a=2; return {x=15}")
176prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
177RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
178checkout("1\n2\n15\n2\n15\n")
179
180-- test 'arg' table
181local 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]]
189a = string.format(a, progname)
190prepfile(a)
191RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
192
193-- test 'arg' availability in libraries
194prepfile"assert(arg)"
195prepfile("assert(arg)", otherprog)
196RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
197
198-- test messing up the 'arg' table
199RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out)
200checkout("100\n")
201NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -')
202
203-- test error in 'print'
204RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out)
205assert(string.find(getoutput(), "error calling 'print'"))
206
207-- test 'debug.debug'
208RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
209checkout("lua_debug> 1000lua_debug> ")
210
211-- test many arguments
212prepfile[[print(({...})[30])]]
213RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
214checkout("a\n")
215
216RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
217checkout("1\n3\n")
218
219-- test iteractive mode
220prepfile[[
221(6*2-6) -- ===
222a =
22310
224print(a)
225a]]
226RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
227checkprogout("6\n10\n10\n\n")
228
229prepfile("a = [[b\nc\nd\ne]]\n=a")
230RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
231checkprogout("b\nc\nd\ne\n\n")
232
233prompt = "alo"
234prepfile[[ --
235a = 2
236]]
237RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
238local t = getoutput()
239assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
240
241-- test for error objects
242prepfile[[
243debug = require "debug"
244m = {x=0}
245setmetatable(m, {__tostring = function(x)
246 return tostring(debug.getinfo(4).currentline + x.x)
247end})
248error(m)
249]]
250NoRun(progname .. ": 6\n", [[lua %s]], prog)
251
252prepfile("error{}")
253NoRun("error object is a table value", [[lua %s]], prog)
254
255
256-- chunk broken in many lines
257s = [=[ --
258function f ( x )
259 local a = [[
260xuxu
261]]
262 local b = "\
263xuxu\n"
264 if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]]
265 return x + 1
266 --\\
267end
268return( f( 100 ) )
269assert( a == b )
270do return f( 11 ) end ]=]
271s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines
272prepfile(s)
273RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
274checkprogout("101\n13\t22\n\n")
275
276prepfile[[#comment in 1st line without \n at the end]]
277RUN('lua %s', prog)
278
279prepfile[[#test line number when file starts with comment line
280debug = require"debug"
281print(debug.getinfo(1).currentline)
282]]
283RUN('lua %s > %s', prog, out)
284checkprogout('3')
285
286-- close Lua with an open file
287prepfile(string.format([[io.output(%q); io.write('alo')]], out))
288RUN('lua %s', prog)
289checkout('alo')
290
291-- bug in 5.2 beta (extra \0 after version line)
292RUN([[lua -v -e"print'hello'" > %s]], out)
293t = getoutput()
294assert(string.find(t, "PUC%-Rio\nhello"))
295
296
297-- testing os.exit
298prepfile("os.exit(nil, true)")
299RUN('lua %s', prog)
300prepfile("os.exit(0, true)")
301RUN('lua %s', prog)
302prepfile("os.exit(true, true)")
303RUN('lua %s', prog)
304prepfile("os.exit(1, true)")
305NoRun("", "lua %s", prog) -- no message
306prepfile("os.exit(false, true)")
307NoRun("", "lua %s", prog) -- no message
308
309-- remove temporary files
310assert(os.remove(prog))
311assert(os.remove(otherprog))
312assert(not os.remove(out))
313
314-- invalid options
315NoRun("unrecognized option '-h'", "lua -h")
316NoRun("unrecognized option '---'", "lua ---")
317NoRun("unrecognized option '-Ex'", "lua -Ex")
318NoRun("unrecognized option '-vv'", "lua -vv")
319NoRun("unrecognized option '-iv'", "lua -iv")
320NoRun("'-e' needs argument", "lua -e")
321NoRun("syntax error", "lua -e a")
322NoRun("'-l' needs argument", "lua -l")
323
324
325if T then -- auxiliary library?
326 print("testing 'not enough memory' to create a state")
327 NoRun("not enough memory", "env MEMLIMIT=100 lua")
328end
329print('+')
330
331print('testing Ctrl C')
332do
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
379end
380
381print("OK")