spec/util_stanza_spec.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 18 Mar 2018 11:32:00 +0000
changeset 8629 20532f191f8d
parent 8624 e3e9479d526e
child 8644 c7734b59506f
permissions -rw-r--r--
util.stanza: Switch from asserts to if's, improve performance, errors and tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8239
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()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
			local stanza = st.stanza("message", { a = "a" });
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     8
			local stanza2 = st.preserialize(stanza);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
			assert.is_string(stanza2 and stanza.name, "preserialize returns a stanza");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
			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
    11
			assert.is_nil(stanza2.last_add, "Preserialized stanza has no last_add marker");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
			assert.is_nil(getmetatable(stanza2), "Preserialized stanza has no metatable");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    16
	describe("#preserialize()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
		it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
			local stanza = st.stanza("message", { a = "a" });
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
			local stanza2 = st.deserialize(st.preserialize(stanza));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
			assert.is_string(stanza2 and stanza.name, "deserialize returns a stanza");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
			assert.is_table(stanza2.attr, "Deserialized stanza has attributes");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
			assert.are.equal(stanza2.attr.a, "a", "Deserialized stanza retains attributes");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
			assert.is_table(getmetatable(stanza2), "Deserialized stanza has metatable");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    24
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    25
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
	describe("#stanza()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    28
		it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
			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
    30
			assert.are.equal(s.name, "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    31
			assert.are.equal(s.attr.xmlns, "myxmlns");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    32
			assert.are.equal(s.attr.a, "attr-a");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    33
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
			local s1 = st.stanza("s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    35
			assert.are.equal(s1.name, "s1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    36
			assert.are.equal(s1.attr.xmlns, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    37
			assert.are.equal(#s1, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    38
			assert.are.equal(#s1.tags, 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    39
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
			s1:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
			assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    42
			assert.are.equal(s1.tags[1].name, "child1");
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
			s1:tag("grandchild1"):up();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
			assert.are.equal(#s1.tags, 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
			assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    47
			assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    48
			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
    49
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    50
			s1:up():tag("child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    51
			assert.are.equal(#s1.tags, 2, tostring(s1));
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
			assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
			assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
			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
    56
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
			s1:up():text("Hello world");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    58
			assert.are.equal(#s1.tags, 2);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    59
			assert.are.equal(#s1, 3);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    60
			assert.are.equal(s1.tags[1].name, "child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    61
			assert.are.equal(s1.tags[2].name, "child2");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    62
			assert.are.equal(#s1.tags[1], 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    63
			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
    64
		end);
8601
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8600
diff changeset
    65
		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: 8600
diff changeset
    66
			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: 8600
diff changeset
    67
			assert.are.equal(s.name, "Объект");
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8600
diff changeset
    68
			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: 8600
diff changeset
    69
			assert.are.equal(s.attr["Объект"], "&");
282d544d48f4 util.stanza: Add tests ensuring support for unicode in tag/attr names
Matthew Wild <mwild1@gmail.com>
parents: 8600
diff changeset
    70
		end);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    71
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    72
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    73
	describe("#message()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    74
		it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    75
			local m = st.message();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    76
			assert.are.equal(m.name, "message");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    77
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    78
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    79
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    80
	describe("#iq()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    81
		it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    82
			local i = st.iq();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    83
			assert.are.equal(i.name, "iq");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    84
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    85
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    86
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    87
	describe("#iq()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    88
		it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    89
			local p = st.presence();
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    90
			assert.are.equal(p.name, "presence");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    91
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    92
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    93
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    94
	describe("#reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    95
		it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    96
			-- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
			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
    98
				:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
			-- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
			local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   101
			assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
			assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   103
			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
   104
			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
   105
			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
   106
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   107
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
		it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
			-- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
			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
   111
				:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   112
			-- Make reply stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   113
			local r = st.reply(s);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
			assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   115
			assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   116
			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
   117
			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
   118
			assert.are.equal(r.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   119
			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
   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
		it("should work for <iq set>", 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("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
   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.attr.type, "result");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   133
			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
   134
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   135
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   136
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   137
	describe("#error_reply()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   138
		it("should work for <s>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   139
			-- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   140
			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
   141
				:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   142
			-- Make reply stanza
8600
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   143
			local r = st.error_reply(s, "cancel", "service-unavailable");
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   144
			assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   145
			assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   146
			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
   147
			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
   148
			assert.are.equal(#r.tags, 1);
8600
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   149
			assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   150
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   151
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   152
		it("should work for <iq get>", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   153
			-- Test stanza
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   154
			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
   155
				:tag("child1");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   156
			-- Make reply stanza
8600
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   157
			local r = st.error_reply(s, "cancel", "service-unavailable");
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   158
			assert.are.equal(r.name, s.name);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   159
			assert.are.equal(r.id, s.id);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   160
			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
   161
			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
   162
			assert.are.equal(r.attr.type, "error");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   163
			assert.are.equal(#r.tags, 1);
8600
6e5fbeaca0f4 util.stanza: Fix tests to call error_reply() correctly, and add tests to ensure it vaguely works
Matthew Wild <mwild1@gmail.com>
parents: 8239
diff changeset
   164
			assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   165
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   166
	end);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   167
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   168
	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: 8624
diff changeset
   169
		local invalid_names = {
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   170
			["empty string"] = "", ["characters"] = "<>";
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   171
		}
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   172
		local invalid_data = {
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   173
			["number"] = 1234, ["table"] = {};
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   174
			["utf8"] = string.char(0xF4, 0x90, 0x80, 0x80);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   175
		};
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   176
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   177
		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: 8624
diff changeset
   178
			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: 8624
diff changeset
   179
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   180
					st.stanza(value);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   181
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   182
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   183
			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: 8624
diff changeset
   184
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   185
					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: 8624
diff changeset
   186
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   187
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   188
		end
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   189
		for value_type, value in pairs(invalid_data) do
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   190
			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: 8624
diff changeset
   191
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   192
					st.stanza(value);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   193
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   194
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   195
			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: 8624
diff changeset
   196
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   197
					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: 8624
diff changeset
   198
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   199
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   200
			it(value_type.." in attribute values", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   201
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   202
					st.stanza("valid", { valid = value });
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   203
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   204
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   205
			it(value_type.." in text node", function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   206
				assert.error_matches(function ()
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   207
					st.stanza("valid"):text(value);
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   208
				end, value_type);
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   209
			end);
8629
20532f191f8d util.stanza: Switch from asserts to if's, improve performance, errors and tests
Matthew Wild <mwild1@gmail.com>
parents: 8624
diff changeset
   210
		end
8602
62bfc85a53c8 util.stanza: Add stricter validation for data passed to stanza builder API
Matthew Wild <mwild1@gmail.com>
parents: 8601
diff changeset
   211
	end);
8624
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   212
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   213
	describe("#is_stanza", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   214
		-- is_stanza(any) -> boolean
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   215
		it("identifies stanzas as stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   216
			assert.truthy(st.is_stanza(st.stanza("x")));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   217
		end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   218
		it("identifies strings as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   219
			assert.falsy(st.is_stanza(""));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   220
		end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   221
		it("identifies numbers as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   222
			assert.falsy(st.is_stanza(1));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   223
		end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   224
		it("identifies tables as not stanzas", function ()
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   225
			assert.falsy(st.is_stanza({}));
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   226
		end);
e3e9479d526e util.stanza: Test coverage of is_stanza()
Kim Alvefur <zash@zash.se>
parents: 8602
diff changeset
   227
	end);
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   228
end);