Comparison

spec/util_error_spec.lua @ 10101:ba7636860bbc

util.error: Add tests
author Kim Alvefur <zash@zash.se>
date Mon, 29 Jul 2019 17:26:03 +0200
child 10365:744ca71a49f7
comparison
equal deleted inserted replaced
10100:5ca9c4917885 10101:ba7636860bbc
1 local errors = require "util.error"
2
3 describe("util.error", function ()
4 describe("new()", function ()
5 it("works", function ()
6 local err = errors.new("bork", "bork bork");
7 assert.not_nil(err);
8 assert.equal("cancel", err.type);
9 assert.equal("undefined-condition", err.condition);
10 assert.same("bork bork", err.context);
11 end);
12
13 describe("templates", function ()
14 it("works", function ()
15 local templates = {
16 ["fail"] = {
17 type = "wait",
18 condition = "internal-server-error",
19 };
20 };
21 local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates);
22 assert.equal("wait", err.type);
23 assert.equal("internal-server-error", err.condition);
24 assert.same({ traceback = "in some file, somewhere" }, err.context);
25 end);
26 end);
27
28 end);
29
30 describe("is_err()", function ()
31 it("works", function ()
32 assert.truthy(errors.is_err(errors.new()));
33 assert.falsy(errors.is_err("not an error"));
34 end);
35 end);
36
37 describe("coerce", function ()
38 it("works", function ()
39 local ok, err = errors.coerce(nil, "it dun goofed");
40 assert.is_nil(ok);
41 assert.truthy(errors.is_err(err))
42 end);
43 end);
44
45 describe("from_stanza", function ()
46 it("works", function ()
47 local st = require "util.stanza";
48 local m = st.message({ type = "chat" });
49 local e = st.error_reply(m, "modify", "bad-request");
50 local err = errors.from_stanza(e);
51 assert.truthy(errors.is_err(err));
52 assert.equal("modify", err.type);
53 assert.equal("bad-request", err.condition);
54 assert.equal(e, err.context.stanza);
55 end);
56 end);
57
58 describe("__tostring", function ()
59 it("doesn't throw", function ()
60 assert.has_no.errors(function ()
61 -- See 6f317e51544d
62 tostring(errors.new());
63 end);
64 end);
65 end);
66
67 end);
68