Comparison

spec/util_jid_spec.lua @ 8236:4878e4159e12

Port tests to the `busted` test runner
author Waqas Hussain <waqas20@gmail.com>
date Fri, 15 Sep 2017 17:07:57 -0400
child 11055:5fb95410f89c
comparison
equal deleted inserted replaced
8235:7d9a2c200736 8236:4878e4159e12
1
2 local jid = require "util.jid";
3
4 describe("util.jid", function()
5 describe("#join()", function()
6 it("should work", function()
7 assert.are.equal(jid.join("a", "b", "c"), "a@b/c", "builds full JID");
8 assert.are.equal(jid.join("a", "b", nil), "a@b", "builds bare JID");
9 assert.are.equal(jid.join(nil, "b", "c"), "b/c", "builds full host JID");
10 assert.are.equal(jid.join(nil, "b", nil), "b", "builds bare host JID");
11 assert.are.equal(jid.join(nil, nil, nil), nil, "invalid JID is nil");
12 assert.are.equal(jid.join("a", nil, nil), nil, "invalid JID is nil");
13 assert.are.equal(jid.join(nil, nil, "c"), nil, "invalid JID is nil");
14 assert.are.equal(jid.join("a", nil, "c"), nil, "invalid JID is nil");
15 end);
16 end);
17 describe("#split()", function()
18 it("should work", function()
19 local function test(input_jid, expected_node, expected_server, expected_resource)
20 local rnode, rserver, rresource = jid.split(input_jid);
21 assert.are.equal(expected_node, rnode, "split("..tostring(input_jid)..") failed");
22 assert.are.equal(expected_server, rserver, "split("..tostring(input_jid)..") failed");
23 assert.are.equal(expected_resource, rresource, "split("..tostring(input_jid)..") failed");
24 end
25
26 -- Valid JIDs
27 test("node@server", "node", "server", nil );
28 test("node@server/resource", "node", "server", "resource" );
29 test("server", nil, "server", nil );
30 test("server/resource", nil, "server", "resource" );
31 test("server/resource@foo", nil, "server", "resource@foo" );
32 test("server/resource@foo/bar", nil, "server", "resource@foo/bar");
33
34 -- Always invalid JIDs
35 test(nil, nil, nil, nil);
36 test("node@/server", nil, nil, nil);
37 test("@server", nil, nil, nil);
38 test("@server/resource", nil, nil, nil);
39 test("@/resource", nil, nil, nil);
40 end);
41 end);
42
43
44 describe("#bare()", function()
45 it("should work", function()
46 assert.are.equal(jid.bare("user@host"), "user@host", "bare JID remains bare");
47 assert.are.equal(jid.bare("host"), "host", "Host JID remains host");
48 assert.are.equal(jid.bare("host/resource"), "host", "Host JID with resource becomes host");
49 assert.are.equal(jid.bare("user@host/resource"), "user@host", "user@host JID with resource becomes user@host");
50 assert.are.equal(jid.bare("user@/resource"), nil, "invalid JID is nil");
51 assert.are.equal(jid.bare("@/resource"), nil, "invalid JID is nil");
52 assert.are.equal(jid.bare("@/"), nil, "invalid JID is nil");
53 assert.are.equal(jid.bare("/"), nil, "invalid JID is nil");
54 assert.are.equal(jid.bare(""), nil, "invalid JID is nil");
55 assert.are.equal(jid.bare("@"), nil, "invalid JID is nil");
56 assert.are.equal(jid.bare("user@"), nil, "invalid JID is nil");
57 assert.are.equal(jid.bare("user@@"), nil, "invalid JID is nil");
58 assert.are.equal(jid.bare("user@@host"), nil, "invalid JID is nil");
59 assert.are.equal(jid.bare("user@@host/resource"), nil, "invalid JID is nil");
60 assert.are.equal(jid.bare("user@host/"), nil, "invalid JID is nil");
61 end);
62 end);
63
64 describe("#compare()", function()
65 it("should work", function()
66 assert.are.equal(jid.compare("host", "host"), true, "host should match");
67 assert.are.equal(jid.compare("host", "other-host"), false, "host should not match");
68 assert.are.equal(jid.compare("other-user@host/resource", "host"), true, "host should match");
69 assert.are.equal(jid.compare("other-user@host", "user@host"), false, "user should not match");
70 assert.are.equal(jid.compare("user@host", "host"), true, "host should match");
71 assert.are.equal(jid.compare("user@host/resource", "host"), true, "host should match");
72 assert.are.equal(jid.compare("user@host/resource", "user@host"), true, "user and host should match");
73 assert.are.equal(jid.compare("user@other-host", "host"), false, "host should not match");
74 assert.are.equal(jid.compare("user@other-host", "user@host"), false, "host should not match");
75 end);
76 end);
77
78 it("should work with nodes", function()
79 local function test(_jid, expected_node)
80 assert.are.equal(jid.node(_jid), expected_node, "Unexpected node for "..tostring(_jid));
81 end
82
83 test("example.com", nil);
84 test("foo.example.com", nil);
85 test("foo.example.com/resource", nil);
86 test("foo.example.com/some resource", nil);
87 test("foo.example.com/some@resource", nil);
88
89 test("foo@foo.example.com/some@resource", "foo");
90 test("foo@example/some@resource", "foo");
91
92 test("foo@example/@resource", "foo");
93 test("foo@example@resource", nil);
94 test("foo@example", "foo");
95 test("foo", nil);
96
97 test(nil, nil);
98 end);
99
100 it("should work with hosts", function()
101 local function test(_jid, expected_host)
102 assert.are.equal(jid.host(_jid), expected_host, "Unexpected host for "..tostring(_jid));
103 end
104
105 test("example.com", "example.com");
106 test("foo.example.com", "foo.example.com");
107 test("foo.example.com/resource", "foo.example.com");
108 test("foo.example.com/some resource", "foo.example.com");
109 test("foo.example.com/some@resource", "foo.example.com");
110
111 test("foo@foo.example.com/some@resource", "foo.example.com");
112 test("foo@example/some@resource", "example");
113
114 test("foo@example/@resource", "example");
115 test("foo@example@resource", nil);
116 test("foo@example", "example");
117 test("foo", "foo");
118
119 test(nil, nil);
120 end);
121
122 it("should work with resources", function()
123 local function test(_jid, expected_resource)
124 assert.are.equal(jid.resource(_jid), expected_resource, "Unexpected resource for "..tostring(_jid));
125 end
126
127 test("example.com", nil);
128 test("foo.example.com", nil);
129 test("foo.example.com/resource", "resource");
130 test("foo.example.com/some resource", "some resource");
131 test("foo.example.com/some@resource", "some@resource");
132
133 test("foo@foo.example.com/some@resource", "some@resource");
134 test("foo@example/some@resource", "some@resource");
135
136 test("foo@example/@resource", "@resource");
137 test("foo@example@resource", nil);
138 test("foo@example", nil);
139 test("foo", nil);
140 test("/foo", nil);
141 test("@x/foo", nil);
142 test("@/foo", nil);
143
144 test(nil, nil);
145 end);
146 end);