Comparison

spec/util_xmppstream_spec.lua @ 8236:4878e4159e12

Port tests to the `busted` test runner
author Waqas Hussain <waqas20@gmail.com>
date Fri, 15 Sep 2017 17:07:57 -0400
child 9021:548ba4090012
comparison
equal deleted inserted replaced
8235:7d9a2c200736 8236:4878e4159e12
1
2 local xmppstream = require "util.xmppstream";
3
4 describe("util.xmppstream", function()
5 describe("#new()", function()
6 it("should work", function()
7 local function test(xml, expect_success, ex)
8 local stanzas = {};
9 local session = { notopen = true };
10 local callbacks = {
11 stream_ns = "streamns";
12 stream_tag = "stream";
13 default_ns = "stanzans";
14 streamopened = function (_session)
15 assert.are.equal(session, _session);
16 assert.are.equal(session.notopen, true);
17 _session.notopen = nil;
18 return true;
19 end;
20 handlestanza = function (_session, stanza)
21 assert.are.equal(session, _session);
22 assert.are.equal(_session.notopen, nil);
23 table.insert(stanzas, stanza);
24 end;
25 streamclosed = function (_session)
26 assert.are.equal(session, _session);
27 assert.are.equal(_session.notopen, nil);
28 _session.notopen = nil;
29 end;
30 }
31 if type(ex) == "table" then
32 for k, v in pairs(ex) do
33 if k ~= "_size_limit" then
34 callbacks[k] = v;
35 end
36 end
37 end
38 local stream = xmppstream.new(session, callbacks, size_limit);
39 local ok, err = pcall(function ()
40 assert(stream:feed(xml));
41 end);
42
43 if ok and type(expect_success) == "function" then
44 expect_success(stanzas);
45 end
46 assert.are.equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure"));
47 end
48
49 local function test_stanza(stanza, expect_success, ex)
50 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex);
51 end
52
53 test([[<stream:stream xmlns:stream="streamns"/>]], true);
54 test([[<stream xmlns="streamns"/>]], true);
55
56 test([[<stream1 xmlns="streamns"/>]], false);
57 test([[<stream xmlns="streamns1"/>]], false);
58 test("<>", false);
59
60 test_stanza("<message/>", function (stanzas)
61 assert.are.equal(#stanzas, 1);
62 assert.are.equal(stanzas[1].name, "message");
63 end);
64 test_stanza("< message>>>>/>\n", false);
65
66 test_stanza([[<x xmlns:a="b">
67 <y xmlns:a="c">
68 <a:z/>
69 </y>
70 <a:z/>
71 </x>]], function (stanzas)
72 assert.are.equal(#stanzas, 1);
73 local s = stanzas[1];
74 assert.are.equal(s.name, "x");
75 assert.are.equal(#s.tags, 2);
76
77 assert.are.equal(s.tags[1].name, "y");
78 assert.are.equal(s.tags[1].attr.xmlns, nil);
79
80 assert.are.equal(s.tags[1].tags[1].name, "z");
81 assert.are.equal(s.tags[1].tags[1].attr.xmlns, "c");
82
83 assert.are.equal(s.tags[2].name, "z");
84 assert.are.equal(s.tags[2].attr.xmlns, "b");
85
86 assert.are.equal(s.namespaces, nil);
87 end);
88 end);
89 end);
90 end);