Comparison

spec/util_error_spec.lua @ 11120:b2331f3dfeea

Merge 0.11->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 30 Sep 2020 09:50:33 +0100
parent 11102:5a0ff475ecfd
child 11221:b0a563716334
comparison
equal deleted inserted replaced
11119:68df52bf08d5 11120:b2331f3dfeea
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 code = 555;
20 };
21 };
22 local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates);
23 assert.equal("wait", err.type);
24 assert.equal("internal-server-error", err.condition);
25 assert.equal(555, err.code);
26 assert.same({ traceback = "in some file, somewhere" }, err.context);
27 end);
28 end);
29
30 end);
31
32 describe("is_err()", function ()
33 it("works", function ()
34 assert.truthy(errors.is_err(errors.new()));
35 assert.falsy(errors.is_err("not an error"));
36 end);
37 end);
38
39 describe("coerce", function ()
40 it("works", function ()
41 local ok, err = errors.coerce(nil, "it dun goofed");
42 assert.is_nil(ok);
43 assert.truthy(errors.is_err(err))
44 end);
45 end);
46
47 describe("from_stanza", function ()
48 it("works", function ()
49 local st = require "util.stanza";
50 local m = st.message({ type = "chat" });
51 local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" });
52 local err = errors.from_stanza(e);
53 assert.truthy(errors.is_err(err));
54 assert.equal("modify", err.type);
55 assert.equal("bad-request", err.condition);
56 assert.equal(e, err.context.stanza);
57 assert.equal("error.example", err.context.by);
58 assert.not_nil(err.extra.tag);
59 end);
60 end);
61
62 describe("__tostring", function ()
63 it("doesn't throw", function ()
64 assert.has_no.errors(function ()
65 -- See 6f317e51544d
66 tostring(errors.new());
67 end);
68 end);
69 end);
70
71 describe("extra", function ()
72 it("keeps some extra fields", function ()
73 local err = errors.new({condition="gone",text="Sorry mate, it's all gone",extra={uri="file:///dev/null"}});
74 assert.is_table(err.extra);
75 assert.equal("file:///dev/null", err.extra.uri);
76 end);
77 end)
78
79 describe("init", function()
80 it("basics works", function()
81 local reg = errors.init("test", {
82 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
83 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
84 });
85
86 local broke = reg.new("broke");
87 assert.equal("cancel", broke.type);
88 assert.equal("internal-server-error", broke.condition);
89 assert.equal("It broke :(", broke.text);
90 assert.equal("test", broke.source);
91
92 local nope = reg.new("nope");
93 assert.equal("auth", nope.type);
94 assert.equal("not-authorized", nope.condition);
95 assert.equal("Can't let you do that Dave", nope.text);
96 end);
97
98 it("compact mode works", function()
99 local reg = errors.init("test", "spec", {
100 broke = {"cancel"; "internal-server-error"; "It broke :("};
101 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
102 });
103
104 local broke = reg.new("broke");
105 assert.equal("cancel", broke.type);
106 assert.equal("internal-server-error", broke.condition);
107 assert.equal("It broke :(", broke.text);
108 assert.is_nil(broke.extra);
109
110 local nope = reg.new("nope");
111 assert.equal("auth", nope.type);
112 assert.equal("not-authorized", nope.condition);
113 assert.equal("Can't let you do that Dave", nope.text);
114 assert.equal("spec", nope.extra.namespace);
115 assert.equal("sorry-dave", nope.extra.condition);
116 end);
117
118 it("registry looks the same regardless of syntax", function()
119 local normal = errors.init("test", {
120 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
121 nope = {
122 type = "auth";
123 condition = "not-authorized";
124 text = "Can't let you do that Dave";
125 extra = {namespace = "spec"; condition = "sorry-dave"};
126 };
127 });
128 local compact1 = errors.init("test", "spec", {
129 broke = {"cancel"; "internal-server-error"; "It broke :("};
130 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
131 });
132 local compact2 = errors.init("test", {
133 broke = {"cancel"; "internal-server-error"; "It broke :("};
134 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"};
135 });
136 assert.same(normal.registry, compact1.registry);
137
138 assert.same({
139 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
140 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
141 }, compact2.registry);
142 end);
143 end);
144
145 end);
146