Annotate

spec/util_stanza_spec.lua @ 11824:90a474aab2c1 0.11

Tag 0.11.10
author Kim Alvefur <zash@zash.se>
date Tue, 03 Aug 2021 11:44:31 +0200
parent 11205:9d1e21c23784
child 11206:f051394762ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 describe("util.stanza", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("#preserialize()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 it("should work", function()
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
7 local stanza = st.stanza("message", { type = "chat" }):text_tag("body", "Hello");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local stanza2 = st.preserialize(stanza);
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
9 assert.is_table(stanza2, "Preserialized stanza is a table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
10 assert.is_nil(getmetatable(stanza2), "Preserialized stanza has no metatable");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
11 assert.is_string(stanza2.name, "Preserialized stanza has a name field");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
12 assert.equal(stanza.name, stanza2.name, "Preserialized stanza has same name as the input stanza");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
13 assert.same(stanza.attr, stanza2.attr, "Preserialized stanza same attr table as input stanza");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 assert.is_nil(stanza2.tags, "Preserialized stanza has no tag list");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 assert.is_nil(stanza2.last_add, "Preserialized stanza has no last_add marker");
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
16 assert.is_table(stanza2[1], "Preserialized child element preserved");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
17 assert.equal("body", stanza2[1].name, "Preserialized child element name preserved");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
21 describe("#deserialize()", function()
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 it("should work", function()
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
23 local stanza = { name = "message", attr = { type = "chat" }, { name = "body", attr = { }, "Hello" } };
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local stanza2 = st.deserialize(st.preserialize(stanza));
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
25
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
26 assert.is_table(stanza2, "Deserialized stanza is a table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
27 assert.equal(st.stanza_mt, getmetatable(stanza2), "Deserialized stanza has stanza metatable");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
28 assert.is_string(stanza2.name, "Deserialized stanza has a name field");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
29 assert.equal(stanza.name, stanza2.name, "Deserialized stanza has same name as the input table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
30 assert.same(stanza.attr, stanza2.attr, "Deserialized stanza same attr table as input table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
31 assert.is_table(stanza2.tags, "Deserialized stanza has tag list");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
32 assert.is_table(stanza2[1], "Deserialized child element preserved");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
33 assert.equal("body", stanza2[1].name, "Deserialized child element name preserved");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 describe("#stanza()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local s = st.stanza("foo", { xmlns = "myxmlns", a = "attr-a" });
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 assert.are.equal(s.name, "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 assert.are.equal(s.attr.xmlns, "myxmlns");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 assert.are.equal(s.attr.a, "attr-a");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local s1 = st.stanza("s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 assert.are.equal(s1.name, "s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 assert.are.equal(s1.attr.xmlns, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 assert.are.equal(#s1, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 assert.are.equal(#s1.tags, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 s1:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 s1:tag("grandchild1"):up();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 assert.are.equal(s1.tags[1][1].name, "grandchild1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 s1:up():tag("child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 assert.are.equal(#s1.tags, 2, tostring(s1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 assert.are.equal(s1.tags[1][1].name, "grandchild1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 s1:up():text("Hello world");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 assert.are.equal(#s1.tags, 2);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 assert.are.equal(#s1, 3);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 assert.are.equal(s1.tags[1][1].name, "grandchild1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end);
8598
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
75 it("should work with unicode values", function ()
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
76 local s = st.stanza("Объект", { xmlns = "myxmlns", ["Объект"] = "&" });
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
77 assert.are.equal(s.name, "Объект");
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
78 assert.are.equal(s.attr.xmlns, "myxmlns");
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
79 assert.are.equal(s.attr["Объект"], "&");
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
80 end);
8641
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
81 it("should allow :text() with nil and empty strings", function ()
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
82 local s_control = st.stanza("foo");
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
83 assert.same(st.stanza("foo"):text(), s_control);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
84 assert.same(st.stanza("foo"):text(nil), s_control);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
85 assert.same(st.stanza("foo"):text(""), s_control);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
86 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 describe("#message()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 local m = st.message();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 assert.are.equal(m.name, "message");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 describe("#iq()", function()
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
97 it("should create an iq stanza", function()
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
98 local i = st.iq({ id = "foo" });
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
99 assert.are.equal("iq", i.name);
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
100 assert.are.equal("foo", i.attr.id);
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
101 end);
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
102
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
103 it("should reject stanzas with no id", function ()
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
104 assert.has.error_match(function ()
9308
21c2f3331c59 util.stanza tests: Remove unused variable #luacheck
Matthew Wild <mwild1@gmail.com>
parents: 9307
diff changeset
105 st.iq();
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
106 end, "id attribute");
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
107
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
108 assert.has.error_match(function ()
9308
21c2f3331c59 util.stanza tests: Remove unused variable #luacheck
Matthew Wild <mwild1@gmail.com>
parents: 9307
diff changeset
109 st.iq({ foo = "bar" });
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
110 end, "id attribute");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113
9302
57f8e41255fb util.stanza tests: Fix test name (copy/paste error?)
Matthew Wild <mwild1@gmail.com>
parents: 9217
diff changeset
114 describe("#presence()", function ()
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 local p = st.presence();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 assert.are.equal(p.name, "presence");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 describe("#reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" })
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 assert.are.equal(r.attr.to, s.attr.from);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 assert.are.equal(r.attr.from, s.attr.to);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 assert.are.equal(r.attr.to, s.attr.from);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 assert.are.equal(r.attr.from, s.attr.to);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 assert.are.equal(r.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 it("should work for <iq set>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "set" })
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 assert.are.equal(r.attr.to, s.attr.from);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 assert.are.equal(r.attr.from, s.attr.to);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 assert.are.equal(r.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 describe("#error_reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" })
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 -- Make reply stanza
8597
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
170 local r = st.error_reply(s, "cancel", "service-unavailable");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 assert.are.equal(r.attr.to, s.attr.from);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 assert.are.equal(r.attr.from, s.attr.to);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 assert.are.equal(#r.tags, 1);
8597
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
176 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 -- Make reply stanza
8597
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
184 local r = st.error_reply(s, "cancel", "service-unavailable");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 assert.are.equal(r.attr.to, s.attr.from);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 assert.are.equal(r.attr.from, s.attr.to);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 assert.are.equal(r.attr.type, "error");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 assert.are.equal(#r.tags, 1);
8597
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
191 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 end);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
194
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
195 describe("should reject #invalid", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
196 local invalid_names = {
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
197 ["empty string"] = "", ["characters"] = "<>";
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
198 }
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
199 local invalid_data = {
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
200 ["number"] = 1234, ["table"] = {};
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
201 ["utf8"] = string.char(0xF4, 0x90, 0x80, 0x80);
8641
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
202 ["nil"] = "nil"; ["boolean"] = true;
11205
9d1e21c23784 util.stanza: Reject ASCII control characters (fixes #1606)
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
203 ["control characters"] = "\0\1\2\3";
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
204 };
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
205
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
206 for value_type, value in pairs(invalid_names) do
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
207 it(value_type.." in tag names", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
208 assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
209 st.stanza(value);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
210 end, value_type);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
211 end);
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
212 it(value_type.." in attribute names", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
213 assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
214 st.stanza("valid", { [value] = "valid" });
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
215 end, value_type);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
216 end);
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
217 end
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
218 for value_type, value in pairs(invalid_data) do
8641
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
219 if value == "nil" then value = nil; end
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
220 it(value_type.." in tag names", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
221 assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
222 st.stanza(value);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
223 end, value_type);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
224 end);
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
225 it(value_type.." in attribute names", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
226 assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
227 st.stanza("valid", { [value] = "valid" });
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
228 end, value_type);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
229 end);
8641
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
230 if value ~= nil then
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
231 it(value_type.." in attribute values", function ()
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
232 assert.error_matches(function ()
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
233 st.stanza("valid", { valid = value });
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
234 end, value_type);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
235 end);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
236 it(value_type.." in text node", function ()
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
237 assert.error_matches(function ()
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
238 st.stanza("valid"):text(value);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
239 end, value_type);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
240 end);
c7734b59506f util.stanza: tests: Add more invalid data types and update for :text(nil) and :text("")
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
241 end
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
242 end
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
243 end);
8621
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
244
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
245 describe("#is_stanza", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
246 -- is_stanza(any) -> boolean
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
247 it("identifies stanzas as stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
248 assert.truthy(st.is_stanza(st.stanza("x")));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
249 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
250 it("identifies strings as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
251 assert.falsy(st.is_stanza(""));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
252 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
253 it("identifies numbers as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
254 assert.falsy(st.is_stanza(1));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
255 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
256 it("identifies tables as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
257 assert.falsy(st.is_stanza({}));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
258 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
259 end);
9000
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
260
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
261 describe("#remove_children", function ()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
262 it("should work", function ()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
263 local s = st.stanza("x", {xmlns="test"})
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
264 :tag("y", {xmlns="test"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
265 :tag("z", {xmlns="test2"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
266 :tag("x", {xmlns="test2"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
267
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
268 s:remove_children("x");
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
269 assert.falsy(s:get_child("x"))
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
270 assert.truthy(s:get_child("z","test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
271 assert.truthy(s:get_child("x","test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
272
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
273 s:remove_children(nil, "test2");
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
274 assert.truthy(s:get_child("y"))
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
275 assert.falsy(s:get_child(nil,"test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
276
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
277 s:remove_children();
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
278 assert.falsy(s.tags[1]);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
279 end);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
280 end);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
281
9216
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
282 describe("#maptags", function ()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
283 it("should work", function ()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
284 local s = st.stanza("test")
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
285 :tag("one"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
286 :tag("two"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
287 :tag("one"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
288 :tag("three"):up();
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
289
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
290 local function one_filter(tag)
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
291 if tag.name == "one" then
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
292 return nil;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
293 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
294 return tag;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
295 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
296 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
297 s:maptags(one_filter);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
298 assert.equal(2, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
299 end);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
300
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
301 it("should work with multiple consecutive text nodes", function ()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
302 local s = st.deserialize({
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
303 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
304 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
305 "away";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
306 name = "show";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
307 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
308 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
309 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
310 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
311 "I am away";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
312 name = "status";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
313 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
314 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
315 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
316 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
317 "0";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
318 name = "priority";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
319 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
320 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
321 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
322 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
323 name = "c";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
324 attr = {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
325 xmlns = "http://jabber.org/protocol/caps";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
326 node = "http://psi-im.org";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
327 hash = "sha-1";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
328 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
329 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
330 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
331 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
332 name = "presence";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
333 attr = {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
334 to = "user@example.com/jflsjfld";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
335 from = "room@chat.example.org/nick";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
336 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
337 });
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
338
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
339 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
340
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
341 s:maptags(function (tag) return tag; end);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
342 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
343
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
344 s:maptags(function (tag)
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
345 if tag.name == "c" then
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
346 return nil;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
347 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
348 return tag;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
349 end);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
350 assert.equal(3, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
351 end);
9217
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
352 it("errors on invalid data - #981", function ()
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
353 local s = st.message({}, "Hello");
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
354 s.tags[1] = st.clone(s.tags[1]);
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
355 assert.has_error_match(function ()
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
356 s:maptags(function () end);
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
357 end, "Invalid stanza");
7df29c5fbb9b util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
Matthew Wild <mwild1@gmail.com>
parents: 9216
diff changeset
358 end);
9216
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
359 end);
9630
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
360
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
361 describe("#clone", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
362 it("works", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
363 local s = st.message({type="chat"}, "Hello"):reset();
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
364 local c = st.clone(s);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
365 assert.same(s, c);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
366 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
367
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
368 it("works", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
369 assert.has_error(function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
370 st.clone("this is not a stanza");
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
371 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
372 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
373 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
374 end);