98c85f4aba05c209bd63a698736dcdc85bb5daae
[prosody.git] / tests / test_util_jid.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 function split(split)
23         function test(input_jid, expected_node, expected_server, expected_resource)
24                 local rnode, rserver, rresource = split(input_jid);
25                 assert_equal(expected_node, rnode, "split("..tostring(input_jid)..") failed");
26                 assert_equal(expected_server, rserver, "split("..tostring(input_jid)..") failed");
27                 assert_equal(expected_resource, rresource, "split("..tostring(input_jid)..") failed");
28         end
29         test("node@server",             "node", "server", nil           );
30         test("node@server/resource",    "node", "server", "resource"    );
31         test("server",                  nil,    "server", nil           );
32         test("server/resource",         nil,    "server", "resource"    );
33         test(nil,                       nil,    nil     , nil           );
34
35         test("node@/server", nil, nil, nil , nil );
36         test("@server",      nil, nil, nil , nil );
37         test("@server/resource",nil,nil,nil, nil );
38 end
39
40 function bare(bare)
41         assert_equal(bare("user@host"), "user@host", "bare JID remains bare");
42         assert_equal(bare("host"), "host", "Host JID remains host");
43         assert_equal(bare("host/resource"), "host", "Host JID with resource becomes host");
44         assert_equal(bare("user@host/resource"), "user@host", "user@host JID with resource becomes user@host");
45         assert_equal(bare("user@/resource"), nil, "invalid JID is nil");
46         assert_equal(bare("@/resource"), nil, "invalid JID is nil");
47         assert_equal(bare("@/"), nil, "invalid JID is nil");
48         assert_equal(bare("/"), nil, "invalid JID is nil");
49         assert_equal(bare(""), nil, "invalid JID is nil");
50         assert_equal(bare("@"), nil, "invalid JID is nil");
51         assert_equal(bare("user@"), nil, "invalid JID is nil");
52         assert_equal(bare("user@@"), nil, "invalid JID is nil");
53         assert_equal(bare("user@@host"), nil, "invalid JID is nil");
54         assert_equal(bare("user@@host/resource"), nil, "invalid JID is nil");
55         assert_equal(bare("user@host/"), nil, "invalid JID is nil");
56 end