db.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. -- debug lib tests
  2. -- debug stuff are partially implemented; hooks are not supported.
  3. local function f1()
  4. end
  5. local env = {}
  6. local mt = {}
  7. debug.setfenv(f1, env)
  8. assert(debug.getfenv(f1) == env)
  9. debug.setmetatable(f1, mt)
  10. assert(debug.getmetatable(f1) == mt)
  11. local function f2()
  12. local info = debug.getinfo(1, "Slunf")
  13. assert(info.currentline == 14)
  14. assert(info.linedefined == 13)
  15. assert(info.func == f2)
  16. assert(info.lastlinedefined == 25)
  17. assert(info.nups == 1)
  18. assert(info.name == "f2")
  19. assert(info.what == "Lua")
  20. if string.find(_VERSION, "GopherLua") then
  21. assert(info.source == "db.lua")
  22. end
  23. end
  24. f2()
  25. local function f3()
  26. end
  27. local info = debug.getinfo(f3)
  28. assert(info.currentline == -1)
  29. assert(info.linedefined == 28)
  30. assert(info.func == f3)
  31. assert(info.lastlinedefined == 29)
  32. assert(info.nups == 0)
  33. assert(info.name == nil)
  34. assert(info.what == "Lua")
  35. if string.find(_VERSION, "GopherLua") then
  36. assert(info.source == "db.lua")
  37. end
  38. local function f4()
  39. local a,b,c = 1,2,3
  40. local function f5()
  41. local name, value = debug.getlocal(2, 2)
  42. assert(debug.getlocal(2, 10) == nil)
  43. assert(name == "b")
  44. assert(value == 2)
  45. name = debug.setlocal(2, 2, 10)
  46. assert(debug.setlocal(2, 10, 10) == nil)
  47. assert(name == "b")
  48. local d = a
  49. local e = c
  50. local tb = debug.traceback("--msg--")
  51. assert(string.find(tb, "\\-\\-msg\\-\\-"))
  52. assert(string.find(tb, "in.*f5"))
  53. assert(string.find(tb, "in.*f4"))
  54. end
  55. f5()
  56. local name, value = debug.getupvalue(f5, 1)
  57. assert(debug.getupvalue(f5, 10) == nil)
  58. assert(name == "a")
  59. assert(value == 1)
  60. name = debug.setupvalue(f5, 1, 11)
  61. assert(debug.setupvalue(f5, 10, 11) == nil)
  62. assert(name == "a")
  63. assert(a == 11)
  64. assert(b == 10) -- changed by debug.setlocal in f4
  65. end
  66. f4()
  67. local ok, msg = pcall(function()
  68. debug.getlocal(10, 1)
  69. end)
  70. assert(not ok and string.find(msg, "level out of range"))
  71. local ok, msg = pcall(function()
  72. debug.setlocal(10, 1, 1)
  73. end)
  74. assert(not ok and string.find(msg, "level out of range"))
  75. assert(debug.getinfo(100) == nil)
  76. assert(debug.getinfo(1, "a") == nil)