# HG changeset patch # User Matthew Wild # Date 1457221461 0 # Node ID d0296db76ccdb3b73ee1fc4ff128dc97d87cda01 # Parent bf8aa0d1951cf2392b3ed42e6c2f1d0bb37238cf# Parent 5e7797822f19320bc1a403bab84d968f9fd4dfef Merge 0.10->trunk diff -r bf8aa0d1951c -r d0296db76ccd tests/test.lua --- a/tests/test.lua Sat Mar 05 23:37:56 2016 +0000 +++ b/tests/test.lua Sat Mar 05 23:44:21 2016 +0000 @@ -25,6 +25,8 @@ dotest "util.throttle" dotest "util.uuid" dotest "util.random" + dotest "util.xml" + dotest "util.xmppstream" dosingletest("test_sasl.lua", "latin1toutf8"); dosingletest("test_utf8.lua", "valid"); diff -r bf8aa0d1951c -r d0296db76ccd tests/test_util_xml.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_util_xml.lua Sat Mar 05 23:44:21 2016 +0000 @@ -0,0 +1,12 @@ +function parse(parse) + local x = +[[ + + + + + +]] + local stanza = parse(x); + assert_equal(stanza.tags[2].attr.xmlns, "b"); +end diff -r bf8aa0d1951c -r d0296db76ccd tests/test_util_xmppstream.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_util_xmppstream.lua Sat Mar 05 23:44:21 2016 +0000 @@ -0,0 +1,83 @@ +function new(new_stream, _M) + local function test(xml, expect_success, ex) + local stanzas = {}; + local session = { notopen = true }; + local callbacks = { + stream_ns = "streamns"; + stream_tag = "stream"; + default_ns = "stanzans"; + streamopened = function (_session) + assert_equal(session, _session); + assert_equal(session.notopen, true); + _session.notopen = nil; + return true; + end; + handlestanza = function (_session, stanza) + assert_equal(session, _session); + assert_equal(_session.notopen, nil); + table.insert(stanzas, stanza); + end; + streamclosed = function (_session) + assert_equal(session, _session); + assert_equal(_session.notopen, nil); + _session.notopen = nil; + end; + } + if type(ex) == "table" then + for k, v in pairs(ex) do + if k ~= "_size_limit" then + callbacks[k] = v; + end + end + end + local stream = new_stream(session, callbacks, size_limit); + local ok, err = pcall(function () + assert(stream:feed(xml)); + end); + + if ok and type(expect_success) == "function" then + expect_success(stanzas); + end + assert_equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure")); + end + + local function test_stanza(stanza, expect_success, ex) + return test([[]]..stanza, expect_success, ex); + end + + test([[]], true); + test([[]], true); + + test([[]], false); + test([[]], false); + test("<>", false); + + test_stanza("", function (stanzas) + assert_equal(#stanzas, 1); + assert_equal(stanzas[1].name, "message"); + end); + test_stanza("< message>>>>/>\n", false); + + test_stanza([[ + + + + + ]], function (stanzas) + assert_equal(#stanzas, 1); + local s = stanzas[1]; + assert_equal(s.name, "x"); + assert_equal(#s.tags, 2); + + assert_equal(s.tags[1].name, "y"); + assert_equal(s.tags[1].attr.xmlns, nil); + + assert_equal(s.tags[1].tags[1].name, "z"); + assert_equal(s.tags[1].tags[1].attr.xmlns, "c"); + + assert_equal(s.tags[2].name, "z"); + assert_equal(s.tags[2].attr.xmlns, "b"); + + assert_equal(s.namespaces, nil); + end); +end