Comparison

spec/util_stanza_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 8597:6e5fbeaca0f4
comparison
equal deleted inserted replaced
8235:7d9a2c200736 8236:4878e4159e12
1
2 local st = require "util.stanza";
3
4 describe("util.stanza", function()
5 describe("#preserialize()", function()
6 it("should work", function()
7 local stanza = st.stanza("message", { a = "a" });
8 local stanza2 = st.preserialize(stanza);
9 assert.is_string(stanza2 and stanza.name, "preserialize returns a stanza");
10 assert.is_nil(stanza2.tags, "Preserialized stanza has no tag list");
11 assert.is_nil(stanza2.last_add, "Preserialized stanza has no last_add marker");
12 assert.is_nil(getmetatable(stanza2), "Preserialized stanza has no metatable");
13 end);
14 end);
15
16 describe("#preserialize()", function()
17 it("should work", function()
18 local stanza = st.stanza("message", { a = "a" });
19 local stanza2 = st.deserialize(st.preserialize(stanza));
20 assert.is_string(stanza2 and stanza.name, "deserialize returns a stanza");
21 assert.is_table(stanza2.attr, "Deserialized stanza has attributes");
22 assert.are.equal(stanza2.attr.a, "a", "Deserialized stanza retains attributes");
23 assert.is_table(getmetatable(stanza2), "Deserialized stanza has metatable");
24 end);
25 end);
26
27 describe("#stanza()", function()
28 it("should work", function()
29 local s = st.stanza("foo", { xmlns = "myxmlns", a = "attr-a" });
30 assert.are.equal(s.name, "foo");
31 assert.are.equal(s.attr.xmlns, "myxmlns");
32 assert.are.equal(s.attr.a, "attr-a");
33
34 local s1 = st.stanza("s1");
35 assert.are.equal(s1.name, "s1");
36 assert.are.equal(s1.attr.xmlns, nil);
37 assert.are.equal(#s1, 0);
38 assert.are.equal(#s1.tags, 0);
39
40 s1:tag("child1");
41 assert.are.equal(#s1.tags, 1);
42 assert.are.equal(s1.tags[1].name, "child1");
43
44 s1:tag("grandchild1"):up();
45 assert.are.equal(#s1.tags, 1);
46 assert.are.equal(s1.tags[1].name, "child1");
47 assert.are.equal(#s1.tags[1], 1);
48 assert.are.equal(s1.tags[1][1].name, "grandchild1");
49
50 s1:up():tag("child2");
51 assert.are.equal(#s1.tags, 2, tostring(s1));
52 assert.are.equal(s1.tags[1].name, "child1");
53 assert.are.equal(s1.tags[2].name, "child2");
54 assert.are.equal(#s1.tags[1], 1);
55 assert.are.equal(s1.tags[1][1].name, "grandchild1");
56
57 s1:up():text("Hello world");
58 assert.are.equal(#s1.tags, 2);
59 assert.are.equal(#s1, 3);
60 assert.are.equal(s1.tags[1].name, "child1");
61 assert.are.equal(s1.tags[2].name, "child2");
62 assert.are.equal(#s1.tags[1], 1);
63 assert.are.equal(s1.tags[1][1].name, "grandchild1");
64 end);
65 end);
66
67 describe("#message()", function()
68 it("should work", function()
69 local m = st.message();
70 assert.are.equal(m.name, "message");
71 end);
72 end);
73
74 describe("#iq()", function()
75 it("should work", function()
76 local i = st.iq();
77 assert.are.equal(i.name, "iq");
78 end);
79 end);
80
81 describe("#iq()", function()
82 it("should work", function()
83 local p = st.presence();
84 assert.are.equal(p.name, "presence");
85 end);
86 end);
87
88 describe("#reply()", function()
89 it("should work for <s>", function()
90 -- Test stanza
91 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" })
92 :tag("child1");
93 -- Make reply stanza
94 local r = st.reply(s);
95 assert.are.equal(r.name, s.name);
96 assert.are.equal(r.id, s.id);
97 assert.are.equal(r.attr.to, s.attr.from);
98 assert.are.equal(r.attr.from, s.attr.to);
99 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
100 end);
101
102 it("should work for <iq get>", function()
103 -- Test stanza
104 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
105 :tag("child1");
106 -- Make reply stanza
107 local r = st.reply(s);
108 assert.are.equal(r.name, s.name);
109 assert.are.equal(r.id, s.id);
110 assert.are.equal(r.attr.to, s.attr.from);
111 assert.are.equal(r.attr.from, s.attr.to);
112 assert.are.equal(r.attr.type, "result");
113 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
114 end);
115
116 it("should work for <iq set>", function()
117 -- Test stanza
118 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "set" })
119 :tag("child1");
120 -- Make reply stanza
121 local r = st.reply(s);
122 assert.are.equal(r.name, s.name);
123 assert.are.equal(r.id, s.id);
124 assert.are.equal(r.attr.to, s.attr.from);
125 assert.are.equal(r.attr.from, s.attr.to);
126 assert.are.equal(r.attr.type, "result");
127 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
128 end);
129 end);
130
131 describe("#error_reply()", function()
132 it("should work for <s>", function()
133 -- Test stanza
134 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" })
135 :tag("child1");
136 -- Make reply stanza
137 local r = st.error_reply(s);
138 assert.are.equal(r.name, s.name);
139 assert.are.equal(r.id, s.id);
140 assert.are.equal(r.attr.to, s.attr.from);
141 assert.are.equal(r.attr.from, s.attr.to);
142 assert.are.equal(#r.tags, 1);
143 end);
144
145 it("should work for <iq get>", function()
146 -- Test stanza
147 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
148 :tag("child1");
149 -- Make reply stanza
150 local r = st.error_reply(s);
151 assert.are.equal(r.name, s.name);
152 assert.are.equal(r.id, s.id);
153 assert.are.equal(r.attr.to, s.attr.from);
154 assert.are.equal(r.attr.from, s.attr.to);
155 assert.are.equal(r.attr.type, "error");
156 assert.are.equal(#r.tags, 1);
157 end);
158 end);
159 end);