forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
159 lines (131 loc) · 3.22 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# testing special comment on first line
print ("testing lua.c options")
assert(os.execute() ~= 0) -- machine has a system command
prog = os.tmpname()
otherprog = os.tmpname()
out = os.tmpname()
do
local i = 0
while arg[i] do i=i-1 end
progname = '"'..arg[i+1]..'"'
end
print(progname)
local prepfile = function (s, p)
p = p or prog
io.output(p)
io.write(s)
assert(io.close())
end
function checkout (s)
io.input(out)
local t = io.read("*a")
io.input():close()
assert(os.remove(out))
if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end
assert(s == t)
return t
end
function auxrun (...)
local s = string.format(...)
s = string.gsub(s, "lua", progname, 1)
return os.execute(s)
end
function RUN (...)
assert(auxrun(...) == 0)
end
function NoRun (...)
print("\n(the next error is expected by the test)")
assert(auxrun(...) ~= 0)
end
-- test 2 files
prepfile("print(1); a=2")
prepfile("print(a)", otherprog)
RUN("lua -l %s -l%s -lstring -l io %s > %s", prog, otherprog, otherprog, out)
checkout("1\n2\n2\n")
local a = [[
assert(table.getn(arg) == 3 and arg[1] == 'a' and
arg[2] == 'b' and arg[3] == 'c')
assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == %s)
assert(arg[4] == nil and arg[-4] == nil)
local a, b, c = ...
assert(... == 'a' and a == 'a' and b == 'b' and c == 'c')
]]
a = string.format(a, progname)
prepfile(a)
RUN('lua "-e " -- %s a b c', prog)
prepfile"assert(arg==nil)"
prepfile("assert(arg)", otherprog)
RUN("lua -l%s - < %s", prog, otherprog)
prepfile""
RUN("lua - < %s > %s", prog, out)
checkout("")
-- test many arguments
prepfile[[print(({...})[30])]]
RUN("lua %s %s > %s", prog, string.rep(" a", 30), out)
checkout("a\n")
RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
checkout("1\n3\n")
prepfile[[
print(
1, a
)
]]
RUN("lua - < %s > %s", prog, out)
checkout("1\tnil\n")
prepfile[[
= (6*2-6) -- ===
a
= 10
print(a)
= a]]
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
checkout("6\n10\n10\n\n")
prepfile("a = [[b\nc\nd\ne]]\n=a")
print(prog)
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
checkout("b\nc\nd\ne\n\n")
prompt = "alo"
prepfile[[ --
a = 2
]]
RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
checkout(string.rep(prompt, 3).."\n")
s = [=[ --
function f ( x )
local a = [[
xuxu
]]
local b = "\
xuxu\n"
if x == 11 then return 1 , 2 end --[[ test multiple returns ]]
return x + 1
--\\
end
=( f( 10 ) )
assert( a == b )
=f( 11 ) ]=]
s = string.gsub(s, ' ', '\n\n')
prepfile(s)
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
checkout("11\n1\t2\n\n")
prepfile[[#comment in 1st line without \n at the end]]
RUN("lua %s", prog)
prepfile("#comment with a binary file\n"..string.dump(loadstring("print(1)")))
RUN("lua %s > %s", prog, out)
checkout("1\n")
prepfile("#comment with a binary file\r\n"..string.dump(loadstring("print(1)")))
RUN("lua %s > %s", prog, out)
checkout("1\n")
-- close Lua with an open file
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
RUN("lua %s", prog)
checkout('alo')
assert(os.remove(prog))
assert(os.remove(otherprog))
assert(not os.remove(out))
RUN("lua -v")
NoRun("lua -h")
NoRun("lua -e")
NoRun("lua -e a")
NoRun("lua -f")
print("OK")