Comparison

tests/test_util_jid.lua @ 7296:1859e07ae082

util.jid+tests: Add simple helpers... node(), host() and resource() for extracting specific parts of a JID
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2016 23:15:08 +0000
parent 5776:bd0ff8ae98a8
child 7473:a558fef9d018
comparison
equal deleted inserted replaced
7294:5f4d0753c818 7296:1859e07ae082
69 assert_equal(compare("user@host/resource", "host"), true, "host should match"); 69 assert_equal(compare("user@host/resource", "host"), true, "host should match");
70 assert_equal(compare("user@host/resource", "user@host"), true, "user and host should match"); 70 assert_equal(compare("user@host/resource", "user@host"), true, "user and host should match");
71 assert_equal(compare("user@other-host", "host"), false, "host should not match"); 71 assert_equal(compare("user@other-host", "host"), false, "host should not match");
72 assert_equal(compare("user@other-host", "user@host"), false, "host should not match"); 72 assert_equal(compare("user@other-host", "user@host"), false, "host should not match");
73 end 73 end
74
75 function node(node)
76 local function test(jid, expected_node)
77 assert_equal(node(jid), expected_node, "Unexpected node for "..tostring(jid));
78 end
79
80 test("example.com", nil);
81 test("foo.example.com", nil);
82 test("foo.example.com/resource", nil);
83 test("foo.example.com/some resource", nil);
84 test("foo.example.com/some@resource", nil);
85
86 test("foo@foo.example.com/some@resource", "foo");
87 test("foo@example/some@resource", "foo");
88
89 test("foo@example/@resource", "foo");
90 test("foo@example@resource", nil);
91 test("foo@example", "foo");
92 test("foo", nil);
93
94 test(nil, nil);
95 end
96
97 function host(host)
98 local function test(jid, expected_host)
99 assert_equal(host(jid), expected_host, "Unexpected host for "..tostring(jid));
100 end
101
102 test("example.com", "example.com");
103 test("foo.example.com", "foo.example.com");
104 test("foo.example.com/resource", "foo.example.com");
105 test("foo.example.com/some resource", "foo.example.com");
106 test("foo.example.com/some@resource", "foo.example.com");
107
108 test("foo@foo.example.com/some@resource", "foo.example.com");
109 test("foo@example/some@resource", "example");
110
111 test("foo@example/@resource", "example");
112 test("foo@example@resource", nil);
113 test("foo@example", "example");
114 test("foo", "foo");
115
116 test(nil, nil);
117 end
118
119 function resource(resource)
120 local function test(jid, expected_resource)
121 assert_equal(resource(jid), expected_resource, "Unexpected resource for "..tostring(jid));
122 end
123
124 test("example.com", nil);
125 test("foo.example.com", nil);
126 test("foo.example.com/resource", "resource");
127 test("foo.example.com/some resource", "some resource");
128 test("foo.example.com/some@resource", "some@resource");
129
130 test("foo@foo.example.com/some@resource", "some@resource");
131 test("foo@example/some@resource", "some@resource");
132
133 test("foo@example/@resource", "@resource");
134 test("foo@example@resource", nil);
135 test("foo@example", nil);
136 test("foo", nil);
137 test("/foo", nil);
138 test("@x/foo", nil);
139 test("@/foo", nil);
140
141 test(nil, nil);
142 end
143