main.lua 3.2 KB

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