github.com/jslyzt/glua@v0.0.0-20210819023911-4030c8e0234a/_lua5.1-tests/attrib.lua (about)

     1  do --[
     2  
     3  print "testing require"
     4  
     5  assert(require"string" == string)
     6  assert(require"math" == math)
     7  assert(require"table" == table)
     8  assert(require"io" == io)
     9  assert(require"os" == os)
    10  assert(require"debug" == debug)
    11  assert(require"coroutine" == coroutine)
    12  
    13  assert(type(package.path) == "string")
    14  assert(type(package.cpath) == "string")
    15  assert(type(package.loaded) == "table")
    16  assert(type(package.preload) == "table")
    17  
    18  
    19  local DIR = "libs/"
    20  
    21  local function createfiles (files, preextras, posextras)
    22    for n,c in pairs(files) do
    23      io.output(DIR..n)
    24      io.write(string.format(preextras, n))
    25      io.write(c)
    26      io.write(string.format(posextras, n))
    27      io.close(io.output())
    28    end
    29  end
    30  
    31  function removefiles (files)
    32    for n in pairs(files) do
    33      os.remove(DIR..n)
    34    end
    35  end
    36  
    37  local files = {
    38    ["A.lua"] = "",
    39    ["B.lua"] = "assert(...=='B');require 'A'",
    40    ["A.lc"] = "",
    41    ["A"] = "",
    42    ["L"] = "",
    43    ["XXxX"] = "",
    44    ["C.lua"] = "package.loaded[...] = 25; require'C'"
    45  }
    46  
    47  AA = nil
    48  local extras = [[
    49  NAME = '%s'
    50  REQUIRED = ...
    51  return AA]]
    52  
    53  createfiles(files, "", extras)
    54  
    55  
    56  local oldpath = package.path
    57  
    58  package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)
    59  
    60  local try = function (p, n, r)
    61    NAME = nil
    62    local rr = require(p)
    63    assert(NAME == n)
    64    assert(REQUIRED == p)
    65    assert(rr == r)
    66  end
    67  
    68  assert(require"C" == 25)
    69  assert(require"C" == 25)
    70  AA = nil
    71  try('B', 'B.lua', true)
    72  assert(package.loaded.B)
    73  assert(require"B" == true)
    74  assert(package.loaded.A)
    75  package.loaded.A = nil
    76  try('B', nil, true)   -- should not reload package
    77  try('A', 'A.lua', true)
    78  package.loaded.A = nil
    79  os.remove(DIR..'A.lua')
    80  AA = {}
    81  try('A', 'A.lc', AA)  -- now must find second option
    82  assert(require("A") == AA)
    83  AA = false
    84  try('K', 'L', false)     -- default option
    85  try('K', 'L', false)     -- default option (should reload it)
    86  assert(rawget(_G, "_REQUIREDNAME") == nil)
    87  
    88  AA = "x"
    89  try("X", "XXxX", AA)
    90  
    91  
    92  removefiles(files)
    93  
    94  
    95  -- testing require of sub-packages
    96  
    97  package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR)
    98  
    99  files = {
   100    ["P1/init.lua"] = "AA = 10",
   101    ["P1/xuxu.lua"] = "AA = 20",
   102  }
   103  
   104  createfiles(files, "module(..., package.seeall)\n", "")
   105  AA = 0
   106  
   107  local m = assert(require"P1")
   108  assert(m == P1 and m._NAME == "P1" and AA == 0 and m.AA == 10)
   109  assert(require"P1" == P1 and P1 == m)
   110  assert(require"P1" == P1)
   111  assert(P1._PACKAGE == "")
   112  
   113  local m = assert(require"P1.xuxu")
   114  assert(m == P1.xuxu and m._NAME == "P1.xuxu" and AA == 0 and m.AA == 20)
   115  assert(require"P1.xuxu" == P1.xuxu and P1.xuxu == m)
   116  assert(require"P1.xuxu" == P1.xuxu)
   117  assert(require"P1" == P1)
   118  assert(P1.xuxu._PACKAGE == "P1.")
   119  assert(P1.AA == 10 and P1._PACKAGE == "")
   120  assert(P1._G == _G and P1.xuxu._G == _G)
   121  
   122  
   123  
   124  removefiles(files)
   125  
   126  
   127  package.path = ""
   128  assert(not pcall(require, "file_does_not_exist"))
   129  package.path = "??\0?"
   130  assert(not pcall(require, "file_does_not_exist1"))
   131  
   132  package.path = oldpath
   133  
   134  -- check 'require' error message
   135  -- local fname = "file_does_not_exist2"
   136  -- local m, err = pcall(require, fname)
   137  -- for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
   138  --   t = string.gsub(t, "?", fname)
   139  --   print(t, err)
   140  --   assert(string.find(err, t, 1, true))
   141  -- end
   142  
   143  
   144  local function import(...)
   145    local f = {...}
   146    return function (m)
   147      for i=1, #f do m[f[i]] = _G[f[i]] end
   148    end
   149  end
   150  
   151  local assert, module, package = assert, module, package
   152  X = nil; x = 0; assert(_G.x == 0)   -- `x' must be a global variable
   153  module"X"; x = 1; assert(_M.x == 1)
   154  module"X.a.b.c"; x = 2; assert(_M.x == 2)
   155  module("X.a.b", package.seeall); x = 3
   156  assert(X._NAME == "X" and X.a.b.c._NAME == "X.a.b.c" and X.a.b._NAME == "X.a.b")
   157  assert(X._M == X and X.a.b.c._M == X.a.b.c and X.a.b._M == X.a.b)
   158  assert(X.x == 1 and X.a.b.c.x == 2 and X.a.b.x == 3)
   159  assert(X._PACKAGE == "" and X.a.b.c._PACKAGE == "X.a.b." and
   160         X.a.b._PACKAGE == "X.a.")
   161  assert(_PACKAGE.."c" == "X.a.c")
   162  assert(X.a._NAME == nil and X.a._M == nil)
   163  module("X.a", import("X")) ; x = 4
   164  assert(X.a._NAME == "X.a" and X.a.x == 4 and X.a._M == X.a)
   165  module("X.a.b", package.seeall); assert(x == 3); x = 5
   166  assert(_NAME == "X.a.b" and X.a.b.x == 5)
   167  
   168  assert(X._G == nil and X.a._G == nil and X.a.b._G == _G and X.a.b.c._G == nil)
   169  
   170  setfenv(1, _G)
   171  assert(x == 0)
   172  
   173  assert(not pcall(module, "x"))
   174  assert(not pcall(module, "math.sin"))
   175  
   176  
   177  -- testing C libraries
   178  
   179  
   180  local p = ""   -- On Mac OS X, redefine this to "_"
   181  
   182  -- assert(loadlib == package.loadlib)   -- only for compatibility
   183  -- local f, err, when = package.loadlib("libs/lib1.so", p.."luaopen_lib1")
   184  local f = nil
   185  if not f then
   186    (Message or print)('\a\n >>> cannot load dynamic library <<<\n\a')
   187    print(err, when)
   188  else
   189    f()   -- open library
   190    assert(require("lib1") == lib1)
   191    collectgarbage()
   192    assert(lib1.id("x") == "x")
   193    f = assert(package.loadlib("libs/lib1.so", p.."anotherfunc"))
   194    assert(f(10, 20) == "1020\n")
   195    f, err, when = package.loadlib("libs/lib1.so", p.."xuxu")
   196    assert(not f and type(err) == "string" and when == "init")
   197    package.cpath = "libs/?.so"
   198    require"lib2"
   199    assert(lib2.id("x") == "x")
   200    local fs = require"lib1.sub"
   201    assert(fs == lib1.sub and next(lib1.sub) == nil)
   202    module("lib2", package.seeall)
   203    f = require"-lib2"
   204    assert(f.id("x") == "x" and _M == f and _NAME == "lib2")
   205    module("lib1.sub", package.seeall)
   206    assert(_M == fs)
   207    setfenv(1, _G)
   208   
   209  end
   210  -- f, err, when = package.loadlib("donotexist", p.."xuxu")
   211  -- assert(not f and type(err) == "string" and (when == "open" or when == "absent"))
   212  
   213  
   214  -- testing preload
   215  
   216  do
   217    local p = package
   218    package = {}
   219    p.preload.pl = function (...)
   220      module(...)
   221      function xuxu (x) return x+20 end
   222    end
   223  
   224    require"pl"
   225    assert(require"pl" == pl)
   226    assert(pl.xuxu(10) == 30)
   227  
   228    package = p
   229    assert(type(package.path) == "string")
   230  end
   231  
   232  
   233  
   234  end  --]
   235  
   236  print('+')
   237  
   238  print("testing assignments, logical operators, and constructors")
   239  
   240  local res, res2 = 27
   241  
   242  a, b = 1, 2+3
   243  assert(a==1 and b==5)
   244  a={}
   245  function f() return 10, 11, 12 end
   246  a.x, b, a[1] = 1, 2, f()
   247  assert(a.x==1 and b==2 and a[1]==10)
   248  a[f()], b, a[f()+3] = f(), a, 'x'
   249  assert(a[10] == 10 and b == a and a[13] == 'x')
   250  
   251  do
   252    local f = function (n) local x = {}; for i=1,n do x[i]=i end;
   253                           return unpack(x) end;
   254    local a,b,c
   255    a,b = 0, f(1)
   256    assert(a == 0 and b == 1)
   257    A,b = 0, f(1)
   258    assert(A == 0 and b == 1)
   259    a,b,c = 0,5,f(4)
   260    assert(a==0 and b==5 and c==1)
   261    a,b,c = 0,5,f(0)
   262    assert(a==0 and b==5 and c==nil)
   263  end
   264  
   265  
   266  a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
   267  assert(not a and b and c and d==6)
   268  
   269  d = 20
   270  a, b, c, d = f()
   271  assert(a==10 and b==11 and c==12 and d==nil)
   272  a,b = f(), 1, 2, 3, f()
   273  assert(a==10 and b==1)
   274  
   275  assert(a<b == false and a>b == true)
   276  assert((10 and 2) == 2)
   277  assert((10 or 2) == 10)
   278  assert((10 or assert(nil)) == 10)
   279  assert(not (nil and assert(nil)))
   280  assert((nil or "alo") == "alo")
   281  assert((nil and 10) == nil)
   282  assert((false and 10) == false)
   283  assert((true or 10) == true)
   284  assert((false or 10) == 10)
   285  assert(false ~= nil)
   286  assert(nil ~= false)
   287  assert(not nil == true)
   288  assert(not not nil == false)
   289  assert(not not 1 == true)
   290  assert(not not a == true)
   291  assert(not not (6 or nil) == true)
   292  assert(not not (nil and 56) == false)
   293  assert(not not (nil and true) == false)
   294  print('+')
   295  
   296  a = {}
   297  a[true] = 20
   298  a[false] = 10
   299  assert(a[1<2] == 20 and a[1>2] == 10)
   300  
   301  function f(a) return a end
   302  
   303  local a = {}
   304  for i=3000,-3000,-1 do a[i] = i; end
   305  a[10e30] = "alo"; a[true] = 10; a[false] = 20
   306  assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10)
   307  for i=3000,-3000,-1 do assert(a[i] == i); end
   308  a[print] = assert
   309  a[f] = print
   310  a[a] = a
   311  assert(a[a][a][a][a][print] == assert)
   312  a[print](a[a[f]] == a[print])
   313  a = nil
   314  
   315  a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
   316  a, a.x, a.y = a, a[-3]
   317  assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
   318  a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
   319  a[1].alo(a[2]==10 and b==10 and c==print)
   320  
   321  a[2^31] = 10; a[2^31+1] = 11; a[-2^31] = 12;
   322  a[2^32] = 13; a[-2^32] = 14; a[2^32+1] = 15; a[10^33] = 16;
   323  
   324  assert(a[2^31] == 10 and a[2^31+1] == 11 and a[-2^31] == 12 and
   325         a[2^32] == 13 and a[-2^32] == 14 and a[2^32+1] == 15 and
   326         a[10^33] == 16)
   327  
   328  a = nil
   329  
   330  
   331  -- do
   332  --   local a,i,j,b
   333  --   a = {'a', 'b'}; i=1; j=2; b=a
   334  --   i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
   335  --   assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
   336  --          b[3] == 1)
   337  -- end
   338  
   339  print('OK')
   340  
   341  return res