Annotate

spec/util_stanza_spec.lua @ 12215:33a93d0a9a45

mod_pubsub: Allow configuring summary templates Enables generation of summaries for more than Atom without additional modules.
author Kim Alvefur <zash@zash.se>
date Mon, 24 Jan 2022 23:06:45 +0100
parent 12141:3ac801630b4b
child 12687:5b69ecaf3427
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";
10503
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
3 local errors = require "util.error";
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("util.stanza", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 describe("#preserialize()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 it("should work", function()
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
8 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
9 local stanza2 = st.preserialize(stanza);
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
10 assert.is_table(stanza2, "Preserialized stanza is a table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
11 assert.is_nil(getmetatable(stanza2), "Preserialized stanza has no metatable");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
12 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
13 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
14 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
15 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
16 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
17 assert.is_table(stanza2[1], "Preserialized child element preserved");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
18 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
19 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
22 describe("#deserialize()", function()
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 it("should work", function()
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
24 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
25 local stanza2 = st.deserialize(st.preserialize(stanza));
9673
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
26
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
27 assert.is_table(stanza2, "Deserialized stanza is a table");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
28 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
29 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
30 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
31 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
32 assert.is_table(stanza2.tags, "Deserialized stanza has tag list");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
33 assert.is_table(stanza2[1], "Deserialized child element preserved");
e7e75b091c96 util.stanza: Improve tests
Kim Alvefur <zash@zash.se>
parents: 9630
diff changeset
34 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
35 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 describe("#stanza()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 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
41 assert.are.equal(s.name, "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 assert.are.equal(s.attr.xmlns, "myxmlns");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 assert.are.equal(s.attr.a, "attr-a");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local s1 = st.stanza("s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 assert.are.equal(s1.name, "s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 assert.are.equal(s1.attr.xmlns, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 assert.are.equal(#s1, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 assert.are.equal(#s1.tags, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 s1:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 s1:tag("grandchild1"):up();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 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
60
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 s1:up():tag("child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 assert.are.equal(#s1.tags, 2, tostring(s1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 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
67
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 s1:up():text("Hello world");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 assert.are.equal(#s1.tags, 2);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 assert.are.equal(#s1, 3);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 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
75 end);
8598
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8597
diff changeset
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 end);
12139
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
88 it("validates names", function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
89 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
90 st.stanza("invalid\0name");
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
91 end, "invalid tag name:")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
92 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
93 st.stanza("name", { ["foo\1\2\3bar"] = "baz" });
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
94 end, "invalid attribute name: contains control characters")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
95 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
96 st.stanza("name", { ["foo"] = "baz\1\2\3\255moo" });
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
97 end, "invalid attribute value: contains control characters")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
98 end)
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
99 it("validates types", function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
100 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
101 st.stanza(1);
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
102 end, "invalid tag name: expected string, got number")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
103 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
104 st.stanza("name", "string");
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
105 end, "invalid attributes: expected table, got string")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
106 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
107 st.stanza("name",{1});
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
108 end, "invalid attribute name: expected string, got number")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
109 assert.has_error_match(function ()
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
110 st.stanza("name",{foo=1});
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
111 end, "invalid attribute value: expected string, got number")
7d6497294d92 util.stanza: Increase test coverage to cover validation errors
Kim Alvefur <zash@zash.se>
parents: 11786
diff changeset
112 end)
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 describe("#message()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 local m = st.message();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 assert.are.equal(m.name, "message");
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 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 describe("#iq()", function()
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
123 it("should create an iq stanza", function()
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
124 local i = st.iq({ type = "get", id = "foo" });
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
125 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
126 assert.are.equal("foo", i.attr.id);
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
127 assert.are.equal("get", i.attr.type);
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
128 end);
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
129
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
130 it("should reject stanzas with no attributes", function ()
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
131 assert.has.error_match(function ()
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
132 st.iq();
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
133 end, "attributes");
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
134 end);
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
135
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
136
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
137 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
138 assert.has.error_match(function ()
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
139 st.iq({ type = "get" });
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
140 end, "id attribute");
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
141 end);
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
142
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
143 it("should reject stanzas with no type", function ()
9307
feaef6215bb8 util.stanza: Don't automatically generate ids for iq stanzas
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
144 assert.has.error_match(function ()
9732
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
145 st.iq({ id = "foo" });
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
146 end, "type attribute");
51583ea2b4fd util.stanza: Require a type attribute for iq stanzas
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
147
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150
9302
57f8e41255fb util.stanza tests: Fix test name (copy/paste error?)
Matthew Wild <mwild1@gmail.com>
parents: 9217
diff changeset
151 describe("#presence()", function ()
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 local p = st.presence();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 assert.are.equal(p.name, "presence");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 describe("#reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 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
162 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 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
168 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
169 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
170 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 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
175 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 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
181 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
182 assert.are.equal(r.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 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
184 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 it("should work for <iq set>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 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
189 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 -- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 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
195 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
196 assert.are.equal(r.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 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
198 end);
10442
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
199
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
200 it("should reject not-stanzas", function ()
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
201 assert.has.error_match(function ()
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
202 st.reply(not "a stanza");
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
203 end, "expected stanza");
22db763c510c util.stanza: Check that argument to reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 9924
diff changeset
204 end);
10443
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
205
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
206 it("should reject not-stanzas", function ()
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
207 assert.has.error_match(function ()
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
208 st.reply({name="x"});
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
209 end, "expected stanza");
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
210 end);
f28718f46196 util.stanza: Remove redundant check for attrs
Kim Alvefur <zash@zash.se>
parents: 10442
diff changeset
211
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 describe("#error_reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 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
218 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 -- Make reply stanza
10446
5c2d1b13537c util.stanza: Support the 'by' attribute on errors
Kim Alvefur <zash@zash.se>
parents: 10445
diff changeset
220 local r = st.error_reply(s, "cancel", "service-unavailable", nil, "host");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 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
224 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
225 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
226 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
10446
5c2d1b13537c util.stanza: Support the 'by' attribute on errors
Kim Alvefur <zash@zash.se>
parents: 10445
diff changeset
227 assert.are.equal(r.tags[1].attr.by, "host");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 -- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 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
233 :tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 -- 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
235 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
236 assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 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
239 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
240 assert.are.equal(r.attr.type, "error");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 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
242 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
243 end);
10444
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
244
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
245 it("should reject not-stanzas", function ()
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
246 assert.has.error_match(function ()
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
247 st.error_reply(not "a stanza", "modify", "bad-request");
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
248 end, "expected stanza");
4eab1f5a4f3b util.stanza: Check that argument to error_reply is a stanza
Kim Alvefur <zash@zash.se>
parents: 10443
diff changeset
249 end);
10445
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
250
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
251 it("should reject stanzas of type error", function ()
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
252 assert.has.error_match(function ()
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
253 st.error_reply(st.message({type="error"}), "cancel", "conflict");
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
254 end, "got stanza of type error");
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
255 assert.has.error_match(function ()
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
256 st.error_reply(st.error_reply(st.message({type="chat"}), "modify", "forbidden"), "cancel", "service-unavailable");
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
257 end, "got stanza of type error");
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
258 end);
f53c03ab4357 util.stanza: Check that argument to error_reply is NOT a stanza of type error
Kim Alvefur <zash@zash.se>
parents: 10444
diff changeset
259
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
260 describe("util.error integration", function ()
10503
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
261 it("should accept util.error objects", function ()
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
262 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello");
11085
5705d151ea11 util.stanza: Get 'by' from context instead
Kim Alvefur <zash@zash.se>
parents: 11084
diff changeset
263 local e = errors.new({ type = "modify", condition = "not-acceptable", text = "Bork bork bork" }, { by = "this.test" });
10503
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
264 local r = st.error_reply(s, e);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
265
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
266 assert.are.equal(r.name, s.name);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
267 assert.are.equal(r.id, s.id);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
268 assert.are.equal(r.attr.to, s.attr.from);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
269 assert.are.equal(r.attr.from, s.attr.to);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
270 assert.are.equal(r.attr.type, "error");
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
271 assert.are.equal(r.tags[1].name, "error");
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
272 assert.are.equal(r.tags[1].attr.type, e.type);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
273 assert.are.equal(r.tags[1].tags[1].name, e.condition);
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
274 assert.are.equal(r.tags[1].tags[2]:get_text(), e.text);
11083
4d12a6785531 util.stanza: Support getting 'by' from util.error object
Kim Alvefur <zash@zash.se>
parents: 10717
diff changeset
275 assert.are.equal("this.test", r.tags[1].attr.by);
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
276 end);
11084
5e09a3389adb util.stanza: Support inclusion of <gone> URI from util.error object
Kim Alvefur <zash@zash.se>
parents: 11083
diff changeset
277
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
278 it("should accept util.error objects with an URI", function ()
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
279 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello");
11084
5e09a3389adb util.stanza: Support inclusion of <gone> URI from util.error object
Kim Alvefur <zash@zash.se>
parents: 11083
diff changeset
280 local gone = errors.new({ condition = "gone", extra = { uri = "file:///dev/null" } })
5e09a3389adb util.stanza: Support inclusion of <gone> URI from util.error object
Kim Alvefur <zash@zash.se>
parents: 11083
diff changeset
281 local gonner = st.error_reply(s, gone);
5e09a3389adb util.stanza: Support inclusion of <gone> URI from util.error object
Kim Alvefur <zash@zash.se>
parents: 11083
diff changeset
282 assert.are.equal("gone", gonner.tags[1].tags[1].name);
5e09a3389adb util.stanza: Support inclusion of <gone> URI from util.error object
Kim Alvefur <zash@zash.se>
parents: 11083
diff changeset
283 assert.are.equal("file:///dev/null", gonner.tags[1].tags[1][1]);
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
284 end);
11086
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
285
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
286 it("should accept util.error objects with application specific error", function ()
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
287 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello");
11086
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
288 local e = errors.new({ condition = "internal-server-error", text = "Namespaced thing happened",
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
289 extra = {namespace="xmpp:example.test", condition="this-happened"} })
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
290 local r = st.error_reply(s, e);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
291 assert.are.equal("xmpp:example.test", r.tags[1].tags[3].attr.xmlns);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
292 assert.are.equal("this-happened", r.tags[1].tags[3].name);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
293
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
294 local e2 = errors.new({ condition = "internal-server-error", text = "Namespaced thing happened",
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
295 extra = {tag=st.stanza("that-happened", { xmlns = "xmpp:example.test", ["another-attribute"] = "here" })} })
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
296 local r2 = st.error_reply(s, e2);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
297 assert.are.equal("xmpp:example.test", r2.tags[1].tags[3].attr.xmlns);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
298 assert.are.equal("that-happened", r2.tags[1].tags[3].name);
2846b6226a8e util.stanza: Support Application-Specific Conditions in util.error
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
299 assert.are.equal("here", r2.tags[1].tags[3].attr["another-attribute"]);
10503
e25a1a9a6e7e util.stanza: Accept util.error object to error_reply
Kim Alvefur <zash@zash.se>
parents: 10446
diff changeset
300 end);
11087
cdd4684992f1 spec.stanza spec: Split up util.error related tests
Kim Alvefur <zash@zash.se>
parents: 11086
diff changeset
301 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
302 end);
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
303
11088
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
304 describe("#get_error()", function ()
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
305 describe("basics", function ()
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
306 local s = st.message();
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
307 local e = st.error_reply(s, "cancel", "not-acceptable", "UNACCEPTABLE!!!! ONE MILLION YEARS DUNGEON!")
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
308 :tag("dungeon", { xmlns = "urn:uuid:c9026187-5b05-4e70-b265-c3b6338a7d0f", period="1000000years"});
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
309 local typ, cond, text, extra = e:get_error();
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
310 assert.equal("cancel", typ);
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
311 assert.equal("not-acceptable", cond);
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
312 assert.equal("UNACCEPTABLE!!!! ONE MILLION YEARS DUNGEON!", text);
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
313 assert.not_nil(extra)
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
314 end)
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
315 end)
1f84d0e4d0c4 util.stanza: Extract Application-Specific Condition from errors
Kim Alvefur <zash@zash.se>
parents: 11087
diff changeset
316
8626
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
317 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
318 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
319 ["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
320 }
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
321 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
322 ["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
323 ["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
324 ["nil"] = "nil"; ["boolean"] = true;
11205
9d1e21c23784 util.stanza: Reject ASCII control characters (fixes #1606)
Kim Alvefur <zash@zash.se>
parents: 9673
diff changeset
325 ["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
326 };
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
327
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
328 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
329 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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 end
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8621
diff changeset
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364 end
8599
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8598
diff changeset
365 end);
8621
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
366
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
367 describe("#is_stanza", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
368 -- is_stanza(any) -> boolean
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
369 it("identifies stanzas as stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
370 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
371 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
372 it("identifies strings as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
373 assert.falsy(st.is_stanza(""));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
374 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
375 it("identifies numbers as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
376 assert.falsy(st.is_stanza(1));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
377 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
378 it("identifies tables as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
379 assert.falsy(st.is_stanza({}));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
380 end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8599
diff changeset
381 end);
9000
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
382
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
383 describe("#remove_children", function ()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
384 it("should work", function ()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
385 local s = st.stanza("x", {xmlns="test"})
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
386 :tag("y", {xmlns="test"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
387 :tag("z", {xmlns="test2"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
388 :tag("x", {xmlns="test2"}):up()
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
389
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
390 s:remove_children("x");
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
391 assert.falsy(s:get_child("x"))
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
392 assert.truthy(s:get_child("z","test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
393 assert.truthy(s:get_child("x","test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
394
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
395 s:remove_children(nil, "test2");
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
396 assert.truthy(s:get_child("y"))
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
397 assert.falsy(s:get_child(nil,"test2"));
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
398
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
399 s:remove_children();
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
400 assert.falsy(s.tags[1]);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
401 end);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
402 end);
4d64ff0719a6 util.stanza: Brief tests for :remove_children
Kim Alvefur <zash@zash.se>
parents: 8641
diff changeset
403
9216
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
404 describe("#maptags", function ()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
405 it("should work", function ()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
406 local s = st.stanza("test")
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
407 :tag("one"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
408 :tag("two"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
409 :tag("one"):up()
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
410 :tag("three"):up();
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
411
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
412 local function one_filter(tag)
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
413 if tag.name == "one" then
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
414 return nil;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
415 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
416 return tag;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
417 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
418 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
419 s:maptags(one_filter);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
420 assert.equal(2, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
421 end);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
422
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
423 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
424 local s = st.deserialize({
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
425 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
426 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
427 "away";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
428 name = "show";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
429 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
430 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
431 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
432 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
433 "I am away";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
434 name = "status";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
435 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
436 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
437 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
438 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
439 "0";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
440 name = "priority";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
441 attr = {};
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
442 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
443 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
444 {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
445 name = "c";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
446 attr = {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
447 xmlns = "http://jabber.org/protocol/caps";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
448 node = "http://psi-im.org";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
449 hash = "sha-1";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
450 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
451 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
452 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
453 "\n";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
454 name = "presence";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
455 attr = {
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
456 to = "user@example.com/jflsjfld";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
457 from = "room@chat.example.org/nick";
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
458 };
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
459 });
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
460
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
461 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
462
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
463 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
464 assert.equal(4, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
465
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
466 s:maptags(function (tag)
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
467 if tag.name == "c" then
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
468 return nil;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
469 end
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
470 return tag;
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
471 end);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
472 assert.equal(3, #s.tags);
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
473 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
474 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
475 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
476 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
477 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
478 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
479 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
480 end);
9216
ba38a947020e util.stanza tests: Add tests for maptags() method
Matthew Wild <mwild1@gmail.com>
parents: 9000
diff changeset
481 end);
9630
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
482
11786
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
483 describe("get_child_with_attr", function ()
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
484 local s = st.message({ type = "chat" })
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
485 :text_tag("body", "Hello world", { ["xml:lang"] = "en" })
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
486 :text_tag("body", "Bonjour le monde", { ["xml:lang"] = "fr" })
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
487 :text_tag("body", "Hallo Welt", { ["xml:lang"] = "de" })
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
488
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
489 it("works", function ()
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
490 assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "en"):get_text(), "Hello world");
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
491 assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "de"):get_text(), "Hallo Welt");
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
492 assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "fr"):get_text(), "Bonjour le monde");
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
493 assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "FR"));
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
494 assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "es"));
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
495 end);
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
496
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
497 it("supports normalization", function ()
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
498 assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "EN", string.upper):get_text(), "Hello world");
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
499 assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "ES", string.upper));
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
500 end);
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
501 end);
39164ea2ab9e util.stanza: Add :get_child_with_attr() + tests
Matthew Wild <mwild1@gmail.com>
parents: 11206
diff changeset
502
9630
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
503 describe("#clone", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
504 it("works", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
505 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
506 local c = st.clone(s);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
507 assert.same(s, c);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
508 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
509
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
510 it("works", function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
511 assert.has_error(function ()
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
512 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
513 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
514 end);
bff66c3faceb util.stanza: Validate input to clone() (with brief tests)
Kim Alvefur <zash@zash.se>
parents: 9308
diff changeset
515 end);
9924
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
516
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
517 describe("top_tag", function ()
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
518 local xml_parse = require "util.xml".parse;
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
519 it("works", function ()
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
520 local s = st.message({type="chat"}, "Hello");
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
521 local top_tag = s:top_tag();
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
522 assert.is_string(top_tag);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
523 assert.not_equal("/>", top_tag:sub(-2, -1));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
524 assert.equal(">", top_tag:sub(-1, -1));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
525 local s2 = xml_parse(top_tag.."</message>");
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
526 assert(st.is_stanza(s2));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
527 assert.equal("message", s2.name);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
528 assert.equal(0, #s2);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
529 assert.equal(0, #s2.tags);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
530 assert.equal("chat", s2.attr.type);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
531 end);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
532
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
533 it("works with namespaced attributes", function ()
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
534 local s = xml_parse[[<message foo:bar='true' xmlns:foo='my-awesome-ns'/>]];
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
535 local top_tag = s:top_tag();
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
536 assert.is_string(top_tag);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
537 assert.not_equal("/>", top_tag:sub(-2, -1));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
538 assert.equal(">", top_tag:sub(-1, -1));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
539 local s2 = xml_parse(top_tag.."</message>");
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
540 assert(st.is_stanza(s2));
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
541 assert.equal("message", s2.name);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
542 assert.equal(0, #s2);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
543 assert.equal(0, #s2.tags);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
544 assert.equal("true", s2.attr["my-awesome-ns\1bar"]);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
545 end);
5a2e53bef031 util.stanza: Fix :top_tag() handling of namespaced attributes
Matthew Wild <mwild1@gmail.com>
parents: 9732
diff changeset
546 end);
10717
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
547
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
548 describe("indent", function ()
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
549 local s = st.stanza("foo"):text("\n"):tag("bar"):tag("baz"):up():text_tag("cow", "moo");
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
550 assert.equal("<foo>\n\t<bar>\n\t\t<baz/>\n\t\t<cow>moo</cow>\n\t</bar>\n</foo>", tostring(s:indent()));
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
551 assert.equal("<foo>\n <bar>\n <baz/>\n <cow>moo</cow>\n </bar>\n</foo>", tostring(s:indent(1, " ")));
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
552 assert.equal("<foo>\n\t\t<bar>\n\t\t\t<baz/>\n\t\t\t<cow>moo</cow>\n\t\t</bar>\n\t</foo>", tostring(s:indent(2, "\t")));
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
553 end);
05e4645fc9b3 util.stanza: Add method returning stanza with added indentation
Kim Alvefur <zash@zash.se>
parents: 10503
diff changeset
554
12141
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
555 describe("find", function()
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
556 it("works", function()
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
557 local s = st.stanza("root", { attr = "value" }):tag("child",
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
558 { xmlns = "urn:example:not:same"; childattr = "thisvalue" }):text_tag("nested", "text"):reset();
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
559 assert.equal("value", s:find("@attr"), "finds attr")
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
560 assert.equal(s:get_child("child", "urn:example:not:same"), s:find("{urn:example:not:same}child"),
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
561 "equivalent to get_child")
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
562 assert.equal("thisvalue", s:find("{urn:example:not:same}child@childattr"), "finds child attr")
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
563 assert.equal("text", s:find("{urn:example:not:same}child/nested#"), "finds nested text")
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
564 assert.is_nil(s:find("child"), "respects namespaces")
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
565 end);
3ac801630b4b util.stanza: Cover :find method in tests
Kim Alvefur <zash@zash.se>
parents: 12139
diff changeset
566 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
567 end);