Merge 0.10->trunk
[prosody.git] / tests / test_util_stanza.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 function preserialize(preserialize, st)
11         local stanza = st.stanza("message", { a = "a" });
12         local stanza2 = preserialize(stanza);
13         assert_is(stanza2 and stanza.name, "preserialize returns a stanza");
14         assert_is_not(stanza2.tags, "Preserialized stanza has no tag list");
15         assert_is_not(stanza2.last_add, "Preserialized stanza has no last_add marker");
16         assert_is_not(getmetatable(stanza2), "Preserialized stanza has no metatable");
17 end
18
19 function deserialize(deserialize, st)
20         local stanza = st.stanza("message", { a = "a" });
21
22         local stanza2 = deserialize(st.preserialize(stanza));
23         assert_is(stanza2 and stanza.name, "deserialize returns a stanza");
24         assert_table(stanza2.attr, "Deserialized stanza has attributes");
25         assert_equal(stanza2.attr.a, "a", "Deserialized stanza retains attributes");
26         assert_table(getmetatable(stanza2), "Deserialized stanza has metatable");
27 end
28
29 function stanza(stanza)
30         local s = stanza("foo", { xmlns = "myxmlns", a = "attr-a" });
31         assert_equal(s.name, "foo");
32         assert_equal(s.attr.xmlns, "myxmlns");
33         assert_equal(s.attr.a, "attr-a");
34
35         local s1 = stanza("s1");
36         assert_equal(s1.name, "s1");
37         assert_equal(s1.attr.xmlns, nil);
38         assert_equal(#s1, 0);
39         assert_equal(#s1.tags, 0);
40         
41         s1:tag("child1");
42         assert_equal(#s1.tags, 1);
43         assert_equal(s1.tags[1].name, "child1");
44
45         s1:tag("grandchild1"):up();
46         assert_equal(#s1.tags, 1);
47         assert_equal(s1.tags[1].name, "child1");
48         assert_equal(#s1.tags[1], 1);
49         assert_equal(s1.tags[1][1].name, "grandchild1");
50         
51         s1:up():tag("child2");
52         assert_equal(#s1.tags, 2, tostring(s1));
53         assert_equal(s1.tags[1].name, "child1");
54         assert_equal(s1.tags[2].name, "child2");
55         assert_equal(#s1.tags[1], 1);
56         assert_equal(s1.tags[1][1].name, "grandchild1");
57
58         s1:up():text("Hello world");
59         assert_equal(#s1.tags, 2);
60         assert_equal(#s1, 3);
61         assert_equal(s1.tags[1].name, "child1");
62         assert_equal(s1.tags[2].name, "child2");
63         assert_equal(#s1.tags[1], 1);
64         assert_equal(s1.tags[1][1].name, "grandchild1");
65 end
66
67 function message(message)
68         local m = message();
69         assert_equal(m.name, "message");
70 end
71
72 function iq(iq)
73         local i = iq();
74         assert_equal(i.name, "iq");
75 end
76
77 function presence(presence)
78         local p = presence();
79         assert_equal(p.name, "presence");
80 end
81
82 function reply(reply, _M)
83         -- Test stanza
84         local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
85                 :tag("child1");
86         -- Make reply stanza
87         local r = reply(s);
88         assert_equal(r.name, s.name);
89         assert_equal(r.id, s.id);
90         assert_equal(r.attr.to, s.attr.from);
91         assert_equal(r.attr.from, s.attr.to);
92         assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
93
94         -- Test stanza
95         local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
96                 :tag("child1");
97         -- Make reply stanza
98         local r = reply(s);
99         assert_equal(r.name, s.name);
100         assert_equal(r.id, s.id);
101         assert_equal(r.attr.to, s.attr.from);
102         assert_equal(r.attr.from, s.attr.to);
103         assert_equal(r.attr.type, "result");
104         assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
105
106         -- Test stanza
107         local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "set" })
108                 :tag("child1");
109         -- Make reply stanza
110         local r = reply(s);
111         assert_equal(r.name, s.name);
112         assert_equal(r.id, s.id);
113         assert_equal(r.attr.to, s.attr.from);
114         assert_equal(r.attr.from, s.attr.to);
115         assert_equal(r.attr.type, "result");
116         assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
117 end
118
119 function error_reply(error_reply, _M)
120         -- Test stanza
121         local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
122                 :tag("child1");
123         -- Make reply stanza
124         local r = error_reply(s);
125         assert_equal(r.name, s.name);
126         assert_equal(r.id, s.id);
127         assert_equal(r.attr.to, s.attr.from);
128         assert_equal(r.attr.from, s.attr.to);
129         assert_equal(#r.tags, 1);
130         
131         -- Test stanza
132         local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
133                 :tag("child1");
134         -- Make reply stanza
135         local r = error_reply(s);
136         assert_equal(r.name, s.name);
137         assert_equal(r.id, s.id);
138         assert_equal(r.attr.to, s.attr.from);
139         assert_equal(r.attr.from, s.attr.to);
140         assert_equal(r.attr.type, "error");
141         assert_equal(#r.tags, 1);
142 end