spec/util_error_spec.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 17 Mar 2024 10:10:24 +0000
changeset 13464 a688947fab1e
parent 13084 031382b207ec
permissions -rw-r--r--
mod_bosh: Set base_type on session This fixes a traceback with mod_saslauth. Ideally we move this to util.session at some point, though.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local errors = require "util.error"
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
describe("util.error", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
	describe("new()", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
		it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
			local err = errors.new("bork", "bork bork");
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
			assert.not_nil(err);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
			assert.equal("cancel", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
			assert.equal("undefined-condition", err.condition);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
			assert.same("bork bork", err.context);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
		describe("templates", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
			it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
				local templates = {
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
					["fail"] = {
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
						type = "wait",
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
						condition = "internal-server-error",
10369
744ca71a49f7 util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents: 10105
diff changeset
    19
						code = 555;
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
					};
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
				};
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
				local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
				assert.equal("wait", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
				assert.equal("internal-server-error", err.condition);
10369
744ca71a49f7 util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents: 10105
diff changeset
    25
				assert.equal(555, err.code);
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
				assert.same({ traceback = "in some file, somewhere" }, err.context);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
			end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
	describe("is_err()", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
		it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
			assert.truthy(errors.is_err(errors.new()));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
			assert.falsy(errors.is_err("not an error"));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
	end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
	describe("coerce", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
		it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
			local ok, err = errors.coerce(nil, "it dun goofed");
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
			assert.is_nil(ok);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
			assert.truthy(errors.is_err(err))
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
	end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
	describe("from_stanza", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
		it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
			local st = require "util.stanza";
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
			local m = st.message({ type = "chat" });
11096
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11093
diff changeset
    51
			local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" });
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
			local err = errors.from_stanza(e);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
			assert.truthy(errors.is_err(err));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
			assert.equal("modify", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
			assert.equal("bad-request", err.condition);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
			assert.equal(e, err.context.stanza);
11093
35d2260644d9 util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
    57
			assert.equal("error.example", err.context.by);
11096
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11093
diff changeset
    58
			assert.not_nil(err.extra.tag);
13084
031382b207ec util.error: Add test for #1805
Kim Alvefur <zash@zash.se>
parents: 11225
diff changeset
    59
			assert.not_has_error(function ()
031382b207ec util.error: Add test for #1805
Kim Alvefur <zash@zash.se>
parents: 11225
diff changeset
    60
				errors.from_stanza(st.message())
031382b207ec util.error: Add test for #1805
Kim Alvefur <zash@zash.se>
parents: 11225
diff changeset
    61
			end);
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
	end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
	describe("__tostring", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
		it("doesn't throw", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
			assert.has_no.errors(function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
				-- See 6f317e51544d
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
				tostring(errors.new());
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
			end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
		end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
	end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
11085
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    74
	describe("extra", function ()
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    75
		it("keeps some extra fields", function ()
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    76
			local err = errors.new({condition="gone",text="Sorry mate, it's all gone",extra={uri="file:///dev/null"}});
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    77
			assert.is_table(err.extra);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    78
			assert.equal("file:///dev/null", err.extra.uri);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    79
		end);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    80
	end)
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    81
11101
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    82
	describe("init", function()
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    83
		it("basics works", function()
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    84
			local reg = errors.init("test", {
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    85
				broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    86
				nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    87
			});
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    88
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    89
			local broke = reg.new("broke");
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    90
			assert.equal("cancel", broke.type);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    91
			assert.equal("internal-server-error", broke.condition);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    92
			assert.equal("It broke :(", broke.text);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    93
			assert.equal("test", broke.source);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    94
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    95
			local nope = reg.new("nope");
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    96
			assert.equal("auth", nope.type);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    97
			assert.equal("not-authorized", nope.condition);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    98
			assert.equal("Can't let you do that Dave", nope.text);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
    99
		end);
11104
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   100
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   101
		it("compact mode works", function()
11106
5a0ff475ecfd util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents: 11105
diff changeset
   102
			local reg = errors.init("test", "spec", {
11104
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   103
				broke = {"cancel"; "internal-server-error"; "It broke :("};
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   104
				nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   105
			});
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   106
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   107
			local broke = reg.new("broke");
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   108
			assert.equal("cancel", broke.type);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   109
			assert.equal("internal-server-error", broke.condition);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   110
			assert.equal("It broke :(", broke.text);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   111
			assert.is_nil(broke.extra);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   112
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   113
			local nope = reg.new("nope");
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   114
			assert.equal("auth", nope.type);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   115
			assert.equal("not-authorized", nope.condition);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   116
			assert.equal("Can't let you do that Dave", nope.text);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   117
			assert.equal("spec", nope.extra.namespace);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   118
			assert.equal("sorry-dave", nope.extra.condition);
3aa06cdd2dc8 util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents: 11101
diff changeset
   119
		end);
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   120
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   121
		it("registry looks the same regardless of syntax", function()
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   122
			local normal = errors.init("test", {
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   123
				broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   124
				nope = {
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   125
					type = "auth";
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   126
					condition = "not-authorized";
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   127
					text = "Can't let you do that Dave";
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   128
					extra = {namespace = "spec"; condition = "sorry-dave"};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   129
				};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   130
			});
11106
5a0ff475ecfd util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents: 11105
diff changeset
   131
			local compact1 = errors.init("test", "spec", {
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   132
				broke = {"cancel"; "internal-server-error"; "It broke :("};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   133
				nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   134
			});
11106
5a0ff475ecfd util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents: 11105
diff changeset
   135
			local compact2 = errors.init("test", {
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   136
				broke = {"cancel"; "internal-server-error"; "It broke :("};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   137
				nope = {"auth"; "not-authorized"; "Can't let you do that Dave"};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   138
			});
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   139
			assert.same(normal.registry, compact1.registry);
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   140
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   141
			assert.same({
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   142
				broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   143
				nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
11106
5a0ff475ecfd util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents: 11105
diff changeset
   144
			}, compact2.registry);
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
   145
		end);
11225
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   146
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   147
		describe(".wrap", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   148
			local reg = errors.init("test", "spec", {
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   149
				myerror = { "cancel", "internal-server-error", "Oh no" };
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   150
			});
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   151
			it("is exposed", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   152
				assert.is_function(reg.wrap);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   153
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   154
			it("returns errors according to the registry", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   155
				local e = reg.wrap("myerror");
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   156
				assert.equal("cancel", e.type);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   157
				assert.equal("internal-server-error", e.condition);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   158
				assert.equal("Oh no", e.text);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   159
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   160
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   161
			it("passes through existing errors", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   162
				local e = reg.wrap(reg.new({ type = "auth", condition = "forbidden" }));
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   163
				assert.equal("auth", e.type);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   164
				assert.equal("forbidden", e.condition);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   165
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   166
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   167
			it("wraps arbitrary values", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   168
				local e = reg.wrap(123);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   169
				assert.equal("cancel", e.type);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   170
				assert.equal("undefined-condition", e.condition);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   171
				assert.equal(123, e.context.wrapped_error);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   172
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   173
		end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   174
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   175
		describe(".coerce", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   176
			local reg = errors.init("test", "spec", {
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   177
				myerror = { "cancel", "internal-server-error", "Oh no" };
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   178
			});
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   179
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   180
			it("is exposed", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   181
				assert.is_function(reg.coerce);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   182
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   183
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   184
			it("passes through existing errors", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   185
				local function test()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   186
					return nil, errors.new({ type = "auth", condition = "forbidden" });
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   187
				end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   188
				local ok, err = reg.coerce(test());
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   189
				assert.is_nil(ok);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   190
				assert.is_truthy(errors.is_err(err));
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   191
				assert.equal("forbidden", err.condition);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   192
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   193
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   194
			it("passes through successful return values", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   195
				local function test()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   196
					return 1, 2, 3, 4;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   197
				end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   198
				local one, two, three, four = reg.coerce(test());
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   199
				assert.equal(1, one);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   200
				assert.equal(2, two);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   201
				assert.equal(3, three);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   202
				assert.equal(4, four);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   203
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   204
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   205
			it("wraps non-error objects", function ()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   206
				local function test()
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   207
					return nil, "myerror";
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   208
				end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   209
				local ok, err = reg.coerce(test());
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   210
				assert.is_nil(ok);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   211
				assert.is_truthy(errors.is_err(err));
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   212
				assert.equal("internal-server-error", err.condition);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   213
				assert.equal("Oh no", err.text);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   214
			end);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11106
diff changeset
   215
		end);
11101
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
   216
	end);
f23cf8e2e2ff util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
   217
10105
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   218
end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   219