Skip to content

Instantly share code, notes, and snippets.

@fperrad
Last active June 9, 2021 17:20
Show Gist options
  • Save fperrad/694cb6d1c851a388aebe939ff9f393bb to your computer and use it in GitHub Desktop.
Save fperrad/694cb6d1c851a388aebe939ff9f393bb to your computer and use it in GitHub Desktop.
port of h2_error_spec.lua
require 'Test.Assertion' -- see https://framagit.org/fperrad/lua-testassertion
plan(43)
if debug then
diag("with debug")
else
diag("without debug")
end
local h2_error = require "http.h2_error"
for i=0, 0xd do
-- indexed by code
equals(i, h2_error.errors[i].code, "has the registered errors")
-- and indexed by name
equals(h2_error.errors[i], h2_error.errors[h2_error.errors[i].name])
end
local e = h2_error.errors[0]:new{
message = "oops";
traceback = "some traceback";
}
equals("NO_ERROR(0x0): Graceful shutdown: oops\nsome traceback", tostring(e), "has a nice tostring")
truthy(h2_error.is(h2_error.errors[0]), "`is` function works")
falsy(h2_error.is({}))
falsy(h2_error.is("string"))
falsy(h2_error.is(1))
falsy(h2_error.is(coroutine.create(function()end)))
falsy(h2_error.is(io.stdin))
--local ok, err = pcall(function() h2_error.errors[0]("oops", false, 0) end)
local ok, err = pcall(h2_error.errors[0], "oops", false, 0)
falsy(ok, "throws errors when called")
same(err, {
name = "NO_ERROR";
code = 0;
description = "Graceful shutdown";
message = "oops";
stream_error = false;
})
ok, err = pcall(h2_error.errors[0])
falsy(ok)
if debug then
truthy(err.traceback, "adds a traceback field")
else
is_nil(err.traceback, "no debug")
end
falsy((pcall(h2_error.errors[0].assert, h2_error.errors[0], false)), ":assert works")
truthy((pcall(h2_error.errors[0].assert, h2_error.errors[0], true)))
ok, err = pcall(h2_error.errors[0].assert, h2_error.errors[0], false)
falsy(ok)
if debug then
truthy(err.traceback, ":assert adds a traceback field")
else
is_nil(err.traceback, "no debug")
end
done_testing()
--[===[
$ lua h2_error_test.lua
1..43
# with debug
...
ok 36 - throws errors when called
ok 37
ok 38
ok 39 - adds a traceback field
ok 40 - :assert works
ok 41
ok 42
ok 43 - :assert adds a traceback field
$ lua -e "require[[Test.More]]; debug=nil; package.loaded.debug=false" h2_error_test.lua
1..43
# without debug
...
ok 36 - throws errors when called
ok 37
ok 38
ok 39 - no debug
ok 40 - :assert works
ok 41
ok 42
ok 43 - no debug
]===]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment