github.com/rhettli/gopher-lua@v0.0.0-20200830072439-712e2f816099/_glua-tests/base.lua (about)

     1  local ok, msg = pcall(function()
     2    dofile("notexist")
     3  end)
     4  assert(not ok and string.find(msg, ".*notexist.*"))
     5  
     6  local ok, msg = pcall(function()
     7    assert(getfenv(2) == _G)
     8  end)
     9  assert(ok)
    10  
    11  local i = 1
    12  local fn = assert(load(function()
    13    local tbl = {"return ", "1", "+", "1"}
    14    local v = tbl[i]
    15    i = i + 1
    16    return v
    17  end))
    18  assert(fn() == 2)
    19  
    20  local fn, msg = load(function()
    21      return {}
    22  end)
    23  assert(not fn and string.find(msg, "must return a string"))
    24  
    25  local i = 1
    26  local fn, msg = load(function()
    27    if i == 1 then
    28        i = i + 1
    29        return "returna"
    30    end
    31  end)
    32  assert(not fn and string.find(string.lower(msg), "eof"))
    33  
    34  local ok, a, b = xpcall(function()
    35       return "a", "b"
    36    end, 
    37    function(err)
    38       assert(nil)
    39    end)
    40  assert(ok and a == "a" and b == "b")
    41  
    42  local ok, a, b = xpcall(function()
    43       error("error!")
    44    end, 
    45    function(err)
    46       return err .. "!", "b"
    47    end)
    48  assert(not ok and string.find(a, "error!!") and b == nil)