github.com/shawnclovie/gopher-lua@v0.0.0-20200520092726-90b44ec0e2f2/_lua5.1-tests/main.lua (about)

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