excepttest.lua 812 B

123456789101112131415161718192021222324252627282930
  1. local socket = require("socket")
  2. local finalizer_called
  3. local func = socket.protect(function(err, ...)
  4. local try = socket.newtry(function()
  5. finalizer_called = true
  6. end)
  7. if err then
  8. return error(err, 0)
  9. else
  10. return try(...)
  11. end
  12. end)
  13. local ret1, ret2, ret3 = func(false, 1, 2, 3)
  14. assert(not finalizer_called, "unexpected finalizer call")
  15. assert(ret1 == 1 and ret2 == 2 and ret3 == 3, "incorrect return values")
  16. ret1, ret2, ret3 = func(false, false, "error message")
  17. assert(finalizer_called, "finalizer not called")
  18. assert(ret1 == nil and ret2 == "error message" and ret3 == nil, "incorrect return values")
  19. local err = {key = "value"}
  20. ret1, ret2 = pcall(func, err)
  21. assert(not ret1, "error not rethrown")
  22. assert(ret2 == err, "incorrect error rethrown")
  23. print("OK")