Comparison

spec/util_error_spec.lua @ 13534:d532176d4334

util.error: Use is_error() instead of is_err() everywhere Continuation of 4b39691a274e
author Kim Alvefur <zash@zash.se>
date Tue, 29 Oct 2024 14:10:02 +0100
parent 13080:031382b207ec
comparison
equal deleted inserted replaced
13533:c885594f7f9a 13534:d532176d4334
27 end); 27 end);
28 end); 28 end);
29 29
30 end); 30 end);
31 31
32 describe("is_err()", function () 32 describe("is_error()", function ()
33 it("works", function () 33 it("works", function ()
34 assert.truthy(errors.is_err(errors.new())); 34 assert.truthy(errors.is_error(errors.new()));
35 assert.falsy(errors.is_err("not an error")); 35 assert.falsy(errors.is_error("not an error"));
36 end); 36 end);
37 end); 37 end);
38 38
39 describe("coerce", function () 39 describe("coerce", function ()
40 it("works", function () 40 it("works", function ()
41 local ok, err = errors.coerce(nil, "it dun goofed"); 41 local ok, err = errors.coerce(nil, "it dun goofed");
42 assert.is_nil(ok); 42 assert.is_nil(ok);
43 assert.truthy(errors.is_err(err)) 43 assert.truthy(errors.is_error(err))
44 end); 44 end);
45 end); 45 end);
46 46
47 describe("from_stanza", function () 47 describe("from_stanza", function ()
48 it("works", function () 48 it("works", function ()
49 local st = require "util.stanza"; 49 local st = require "util.stanza";
50 local m = st.message({ type = "chat" }); 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" }); 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); 52 local err = errors.from_stanza(e);
53 assert.truthy(errors.is_err(err)); 53 assert.truthy(errors.is_error(err));
54 assert.equal("modify", err.type); 54 assert.equal("modify", err.type);
55 assert.equal("bad-request", err.condition); 55 assert.equal("bad-request", err.condition);
56 assert.equal(e, err.context.stanza); 56 assert.equal(e, err.context.stanza);
57 assert.equal("error.example", err.context.by); 57 assert.equal("error.example", err.context.by);
58 assert.not_nil(err.extra.tag); 58 assert.not_nil(err.extra.tag);
185 local function test() 185 local function test()
186 return nil, errors.new({ type = "auth", condition = "forbidden" }); 186 return nil, errors.new({ type = "auth", condition = "forbidden" });
187 end 187 end
188 local ok, err = reg.coerce(test()); 188 local ok, err = reg.coerce(test());
189 assert.is_nil(ok); 189 assert.is_nil(ok);
190 assert.is_truthy(errors.is_err(err)); 190 assert.is_truthy(errors.is_error(err));
191 assert.equal("forbidden", err.condition); 191 assert.equal("forbidden", err.condition);
192 end); 192 end);
193 193
194 it("passes through successful return values", function () 194 it("passes through successful return values", function ()
195 local function test() 195 local function test()
206 local function test() 206 local function test()
207 return nil, "myerror"; 207 return nil, "myerror";
208 end 208 end
209 local ok, err = reg.coerce(test()); 209 local ok, err = reg.coerce(test());
210 assert.is_nil(ok); 210 assert.is_nil(ok);
211 assert.is_truthy(errors.is_err(err)); 211 assert.is_truthy(errors.is_error(err));
212 assert.equal("internal-server-error", err.condition); 212 assert.equal("internal-server-error", err.condition);
213 assert.equal("Oh no", err.text); 213 assert.equal("Oh no", err.text);
214 end); 214 end);
215 end); 215 end);
216 end); 216 end);