base.lua 939 B

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