2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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");
19 function deserialize(deserialize, st)
20 local stanza = st.stanza("message", { a = "a" });
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");
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");
35 local s1 = stanza("s1");
36 assert_equal(s1.name, "s1");
37 assert_equal(s1.attr.xmlns, nil);
39 assert_equal(#s1.tags, 0);
42 assert_equal(#s1.tags, 1);
43 assert_equal(s1.tags[1].name, "child1");
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");
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");
58 s1:up():text("Hello world");
59 assert_equal(#s1.tags, 2);
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");
67 function message(message)
69 assert_equal(m.name, "message");
74 assert_equal(i.name, "iq");
77 function presence(presence)
79 assert_equal(p.name, "presence");
82 function reply(reply, _M)
84 local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
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");
95 local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
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");
107 local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "set" })
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");
119 function error_reply(error_reply, _M)
121 local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
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);
132 local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
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);