github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/main.lua (about)

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