db_mini.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. -- testing debug library
  2. local a = 1
  3. local function multi_assert(expected, ...)
  4. local arg = { ... }
  5. for i = 1, #arg do
  6. assert(arg[i] == expected[i])
  7. end
  8. end
  9. local function test_locals(x, ...)
  10. local b = "local b"
  11. assert(debug.getlocal(test_locals, 1) == "x")
  12. multi_assert({ "x", 1 }, debug.getlocal(1, 1))
  13. multi_assert({ "b", "local b" }, debug.getlocal(1, 2))
  14. multi_assert({ "(vararg)", 2 }, debug.getlocal(1, -1))
  15. multi_assert({ "(vararg)", 3 }, debug.getlocal(1, -2))
  16. multi_assert({ "a", 1 }, debug.getlocal(2, 1))
  17. assert(debug.setlocal(2, 1, "new a") == "a")
  18. end
  19. test_locals(1, 2, 3)
  20. assert(a == "new a")
  21. -- test file and string names truncation
  22. a = "function f () end"
  23. local function dostring (s, x)
  24. return load(s, x)()
  25. end
  26. dostring(a)
  27. assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
  28. dostring(a .. string.format("; %s\n=1", string.rep('p', 400)))
  29. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  30. dostring(a .. string.format("; %s=1", string.rep('p', 400)))
  31. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  32. dostring("\n" .. a)
  33. assert(debug.getinfo(f).short_src == '[string "..."]')
  34. dostring(a, "")
  35. assert(debug.getinfo(f).short_src == '[string ""]')
  36. dostring(a, "@xuxu")
  37. assert(debug.getinfo(f).short_src == "xuxu")
  38. dostring(a, "@" .. string.rep('p', 1000) .. 't')
  39. assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
  40. dostring(a, "=xuxu")
  41. assert(debug.getinfo(f).short_src == "xuxu")
  42. dostring(a, string.format("=%s", string.rep('x', 500)))
  43. assert(string.find(debug.getinfo(f).short_src, "^x*$"))
  44. dostring(a, "=")
  45. assert(debug.getinfo(f).short_src == "")
  46. a = nil;
  47. f = nil;
  48. repeat
  49. local g = { x = function()
  50. local a = debug.getinfo(2)
  51. assert(a.name == 'f' and a.namewhat == 'local')
  52. a = debug.getinfo(1)
  53. assert(a.name == 'x' and a.namewhat == 'field')
  54. return 'xixi'
  55. end }
  56. local f = function()
  57. return 1 + 1 and (not 1 or g.x())
  58. end
  59. assert(f() == 'xixi')
  60. g = debug.getinfo(f)
  61. assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)
  62. function f (x, name)
  63. -- local!
  64. if not name then
  65. name = 'f'
  66. end
  67. local a = debug.getinfo(1)
  68. print(a.name, a.namewhat, name)
  69. assert(a.name == name and a.namewhat == 'local')
  70. return x
  71. end
  72. -- breaks in different conditions
  73. if 3 > 4 then
  74. break
  75. end ;
  76. f()
  77. if 3 < 4 then
  78. a = 1
  79. else
  80. break
  81. end ;
  82. f()
  83. while 1 do
  84. local x = 10;
  85. break
  86. end ;
  87. f()
  88. local b = 1
  89. if 3 > 4 then
  90. return math.sin(1)
  91. end ;
  92. f()
  93. a = 3 < 4;
  94. f()
  95. a = 3 < 4 or 1;
  96. f()
  97. repeat local x = 20;
  98. if 4 > 3 then
  99. f()
  100. else
  101. break
  102. end ;
  103. f() until 1
  104. g = {}
  105. f(g).x = f(2) and f(10) + f(9)
  106. assert(g.x == f(19))
  107. function g(x)
  108. if not x then
  109. return 3
  110. end
  111. return (x('a', 'x'))
  112. end
  113. assert(g(f) == 'a')
  114. until 1
  115. local function test_upvalues()
  116. local a = 3
  117. local function f(x)
  118. local b = a + x
  119. local function g(y)
  120. local c = b + y
  121. local function h()
  122. return a + b + c
  123. end
  124. multi_assert({ "a", 3 }, debug.getupvalue(h, 1))
  125. multi_assert({ "b", 4 }, debug.getupvalue(h, 2))
  126. multi_assert({ "c", 6 }, debug.getupvalue(h, 3))
  127. multi_assert({ "b", 4 }, debug.getupvalue(g, 1))
  128. multi_assert({ "a", 3 }, debug.getupvalue(g, 2))
  129. multi_assert({ "a", 3 }, debug.getupvalue(f, 1))
  130. debug.setupvalue(h, 1, 10)
  131. debug.setupvalue(h, 2, 20)
  132. debug.setupvalue(h, 3, 30)
  133. assert(h() == 60)
  134. end
  135. g(2)
  136. end
  137. f(1)
  138. end
  139. test_upvalues()
  140. local mt = {
  141. __metatable = "my own metatable",
  142. __index = function(o, k)
  143. return o + k
  144. end
  145. }
  146. local a = 1
  147. local b = 2
  148. local function f()
  149. return a
  150. end
  151. local function g()
  152. return b
  153. end
  154. debug.upvaluejoin(f, 1, g, 1)
  155. assert(f() == 2)
  156. b = 3
  157. assert(f() == 3)
  158. debug.setmetatable(10, mt)
  159. assert(debug.getmetatable(10) == mt)
  160. a = 10
  161. assert(a[3] == 13)
  162. assert(debug.traceback(print) == print)
  163. assert(debug.traceback(print) == print)
  164. assert(type(debug.getregistry()) == "table")
  165. -- testing nparams, nups e isvararg
  166. local t = debug.getinfo(print, "u")
  167. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  168. t = debug.getinfo(function(a, b, c)
  169. end, "u")
  170. assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
  171. t = debug.getinfo(function(a, b, ...)
  172. return t[a]
  173. end, "u")
  174. assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
  175. t = debug.getinfo(1) -- main
  176. assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
  177. debug.getupvalue(t.func, 1) == "_ENV")
  178. -- testing debugging of coroutines
  179. local function checktraceback (co, p, level)
  180. local tb = debug.traceback(co, nil, level)
  181. local i = 0
  182. for l in string.gmatch(tb, "[^\n]+\n?") do
  183. assert(i == 0 or string.find(l, p[i]))
  184. i = i + 1
  185. end
  186. assert(p[i] == nil)
  187. end
  188. local function f (n)
  189. if n > 0 then
  190. f(n - 1)
  191. else
  192. coroutine.yield()
  193. end
  194. end
  195. local co = coroutine.create(f)
  196. coroutine.resume(co, 3)
  197. checktraceback(co, { "yield", "db_mini.lua", "db_mini.lua", "db_mini.lua", "db_mini.lua" })
  198. checktraceback(co, { "db_mini.lua", "db_mini.lua", "db_mini.lua", "db_mini.lua" }, 1)
  199. checktraceback(co, { "db_mini.lua", "db_mini.lua", "db_mini.lua" }, 2)
  200. checktraceback(co, { "db_mini.lua" }, 4)
  201. checktraceback(co, {}, 40)