main.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # testing special comment on first line
  2. -- most (all?) tests here assume a reasonable "Unix-like" shell
  3. if _port then return end
  4. print ("testing lua.c options")
  5. assert(os.execute()) -- machine has a system command
  6. prog = os.tmpname()
  7. otherprog = os.tmpname()
  8. out = os.tmpname()
  9. do
  10. local i = 0
  11. while arg[i] do i=i-1 end
  12. progname = arg[i+1]
  13. end
  14. print("progname: "..progname)
  15. local prepfile = function (s, p)
  16. p = p or prog
  17. io.output(p)
  18. io.write(s)
  19. assert(io.close())
  20. end
  21. function getoutput ()
  22. io.input(out)
  23. local t = io.read("*a")
  24. io.input():close()
  25. assert(os.remove(out))
  26. return t
  27. end
  28. function checkprogout (s)
  29. local t = getoutput()
  30. for line in string.gmatch(s, ".-\n") do
  31. assert(string.find(t, line, 1, true))
  32. end
  33. end
  34. function checkout (s)
  35. local t = getoutput()
  36. if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end
  37. assert(s == t)
  38. return t
  39. end
  40. function auxrun (...)
  41. local s = string.format(...)
  42. s = string.gsub(s, "lua", '"'..progname..'"', 1)
  43. return os.execute(s)
  44. end
  45. function RUN (...)
  46. assert(auxrun(...))
  47. end
  48. function NoRun (...)
  49. assert(not auxrun(...))
  50. end
  51. function NoRunMsg (...)
  52. print("\n(the next error is expected by the test)")
  53. return NoRun(...)
  54. end
  55. -- test environment variables used by Lua
  56. prepfile("print(package.path)")
  57. RUN("env LUA_INIT= LUA_PATH=x lua %s > %s", prog, out)
  58. checkout("x\n")
  59. RUN("env LUA_INIT= LUA_PATH_5_2=y LUA_PATH=x lua %s > %s", prog, out)
  60. checkout("y\n")
  61. prepfile("print(package.cpath)")
  62. RUN("env LUA_INIT= LUA_CPATH=xuxu lua %s > %s", prog, out)
  63. checkout("xuxu\n")
  64. RUN("env LUA_INIT= LUA_CPATH_5_2=yacc LUA_CPATH=x lua %s > %s", prog, out)
  65. checkout("yacc\n")
  66. prepfile("print(X)")
  67. RUN('env LUA_INIT="X=3" lua %s > %s', prog, out)
  68. checkout("3\n")
  69. prepfile("print(X)")
  70. RUN('env LUA_INIT_5_2="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
  71. checkout("10\n")
  72. -- test option '-E'
  73. prepfile("print(package.path, package.cpath)")
  74. RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s',
  75. prog, out)
  76. local defaultpath = getoutput()
  77. defaultpath = string.match(defaultpath, "^(.-)\t") -- remove tab
  78. assert(not string.find(defaultpath, "xxx") and string.find(defaultpath, "lua"))
  79. -- test replacement of ';;' to default path
  80. local function convert (p)
  81. prepfile("print(package.path)")
  82. RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out)
  83. local expected = getoutput()
  84. expected = string.sub(expected, 1, -2) -- cut final end of line
  85. assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected)
  86. end
  87. convert(";")
  88. convert(";;")
  89. convert(";;;")
  90. convert(";;;;")
  91. convert(";;;;;")
  92. convert(";;a;;;bc")
  93. -- test 2 files
  94. prepfile("print(1); a=2; return {x=15}")
  95. prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
  96. RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
  97. checkout("1\n2\n15\n2\n15\n")
  98. local a = [[
  99. assert(#arg == 3 and arg[1] == 'a' and
  100. arg[2] == 'b' and arg[3] == 'c')
  101. assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s')
  102. assert(arg[4] == nil and arg[-4] == nil)
  103. local a, b, c = ...
  104. assert(... == 'a' and a == 'a' and b == 'b' and c == 'c')
  105. ]]
  106. a = string.format(a, progname)
  107. prepfile(a)
  108. RUN('lua "-e " -- %s a b c', prog)
  109. prepfile"assert(arg==nil)"
  110. prepfile("assert(arg)", otherprog)
  111. RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
  112. prepfile""
  113. RUN("lua - < %s > %s", prog, out)
  114. checkout("")
  115. -- test many arguments
  116. prepfile[[print(({...})[30])]]
  117. RUN("lua %s %s > %s", prog, string.rep(" a", 30), out)
  118. checkout("a\n")
  119. RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
  120. checkout("1\n3\n")
  121. prepfile[[
  122. print(
  123. 1, a
  124. )
  125. ]]
  126. RUN("lua - < %s > %s", prog, out)
  127. checkout("1\tnil\n")
  128. prepfile[[
  129. = (6*2-6) -- ===
  130. a
  131. = 10
  132. print(a)
  133. = a]]
  134. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  135. checkprogout("6\n10\n10\n\n")
  136. prepfile("a = [[b\nc\nd\ne]]\n=a")
  137. print("temporary program file: "..prog)
  138. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  139. checkprogout("b\nc\nd\ne\n\n")
  140. prompt = "alo"
  141. prepfile[[ --
  142. a = 2
  143. ]]
  144. RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
  145. local t = getoutput()
  146. assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
  147. -- test for error objects
  148. prepfile[[
  149. debug = require "debug"
  150. m = {x=0}
  151. setmetatable(m, {__tostring = function(x)
  152. return debug.getinfo(4).currentline + x.x
  153. end})
  154. error(m)
  155. ]]
  156. NoRun([[lua %s 2> %s]], prog, out) -- no message
  157. checkout(progname..": 6\n")
  158. s = [=[ --
  159. function f ( x )
  160. local a = [[
  161. xuxu
  162. ]]
  163. local b = "\
  164. xuxu\n"
  165. if x == 11 then return 1 , 2 end --[[ test multiple returns ]]
  166. return x + 1
  167. --\\
  168. end
  169. =( f( 10 ) )
  170. assert( a == b )
  171. =f( 11 ) ]=]
  172. s = string.gsub(s, ' ', '\n\n')
  173. prepfile(s)
  174. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  175. checkprogout("11\n1\t2\n\n")
  176. prepfile[[#comment in 1st line without \n at the end]]
  177. RUN("lua %s", prog)
  178. prepfile[[#test line number when file starts with comment line
  179. debug = require"debug"
  180. print(debug.getinfo(1).currentline)
  181. ]]
  182. RUN("lua %s > %s", prog, out)
  183. checkprogout('3')
  184. -- close Lua with an open file
  185. prepfile(string.format([[io.output(%q); io.write('alo')]], out))
  186. RUN("lua %s", prog)
  187. checkout('alo')
  188. -- bug in 5.2 beta (extra \0 after version line)
  189. RUN([[lua -v -e'print"hello"' > %s]], out)
  190. t = getoutput()
  191. assert(string.find(t, "PUC%-Rio\nhello"))
  192. -- testing os.exit
  193. prepfile("os.exit(nil, true)")
  194. RUN("lua %s", prog)
  195. prepfile("os.exit(0, true)")
  196. RUN("lua %s", prog)
  197. prepfile("os.exit(true, true)")
  198. RUN("lua %s", prog)
  199. prepfile("os.exit(1, true)")
  200. NoRun("lua %s", prog) -- no message
  201. prepfile("os.exit(false, true)")
  202. NoRun("lua %s", prog) -- no message
  203. assert(os.remove(prog))
  204. assert(os.remove(otherprog))
  205. assert(not os.remove(out))
  206. RUN("lua -v")
  207. NoRunMsg("lua -h")
  208. NoRunMsg("lua -e")
  209. NoRunMsg("lua -e a")
  210. NoRunMsg("lua -f")
  211. print("OK")