after_spec.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. _G.core = {}
  2. _G.vector = {metatable = {}}
  3. dofile("builtin/common/math.lua")
  4. dofile("builtin/common/vector.lua")
  5. dofile("builtin/common/misc_helpers.lua")
  6. function core.get_last_run_mod() return "*test*" end
  7. function core.set_last_run_mod() end
  8. local do_step
  9. function core.register_globalstep(func)
  10. do_step = func
  11. end
  12. dofile("builtin/common/after.lua")
  13. describe("after", function()
  14. it("executes callbacks when expected", function()
  15. local result = ""
  16. core.after(0, function()
  17. result = result .. "a"
  18. end)
  19. core.after(1, function()
  20. result = result .. "b"
  21. end)
  22. core.after(1, function()
  23. result = result .. "c"
  24. end)
  25. core.after(2, function()
  26. result = result .. "d"
  27. end)
  28. local cancel = core.after(2, function()
  29. result = result .. "e"
  30. end)
  31. do_step(0)
  32. assert.same("a", result)
  33. do_step(1)
  34. assert.same("abc", result)
  35. core.after(2, function()
  36. result = result .. "f"
  37. end)
  38. core.after(1, function()
  39. result = result .. "g"
  40. end)
  41. core.after(-1, function()
  42. result = result .. "h"
  43. end)
  44. cancel:cancel()
  45. do_step(1)
  46. assert.same("abchdg", result)
  47. do_step(1)
  48. assert.same("abchdgf", result)
  49. end)
  50. it("defers jobs with delay 0", function()
  51. local result = ""
  52. core.after(0, function()
  53. core.after(0, function()
  54. result = result .. "b"
  55. end)
  56. result = result .. "a"
  57. end)
  58. do_step(1)
  59. assert.same("a", result)
  60. do_step(1)
  61. assert.same("ab", result)
  62. end)
  63. it("passes arguments", function()
  64. core.after(0, function(...)
  65. assert.same(0, select("#", ...))
  66. end)
  67. core.after(0, function(...)
  68. assert.same(4, select("#", ...))
  69. assert.same(1, (select(1, ...)))
  70. assert.same(nil, (select(2, ...)))
  71. assert.same("a", (select(3, ...)))
  72. assert.same(nil, (select(4, ...)))
  73. end, 1, nil, "a", nil)
  74. do_step(0)
  75. end)
  76. it("rejects invalid arguments", function()
  77. assert.has.errors(function() core.after() end)
  78. assert.has.errors(function() core.after(nil, nil) end)
  79. assert.has.errors(function() core.after(0) end)
  80. assert.has.errors(function() core.after(0, nil) end)
  81. assert.has.errors(function() core.after(nil, function() end) end)
  82. assert.has.errors(function() core.after(0 / 0, function() end) end)
  83. end)
  84. -- Make sure that the underlying heap is working correctly
  85. it("can be abused as a heapsort", function()
  86. local t = {}
  87. for i = 1, 1000 do
  88. t[i] = math.random(100)
  89. end
  90. local sorted = table.copy(t)
  91. table.sort(sorted)
  92. local i = 0
  93. for _, v in ipairs(t) do
  94. core.after(v, function()
  95. i = i + 1
  96. assert.equal(v, sorted[i])
  97. end)
  98. end
  99. do_step(math.max(unpack(t)))
  100. assert.equal(#t, i)
  101. end)
  102. end)