diff options
author | Marvin W <git@larma.de> | 2017-05-13 17:43:51 +0200 |
---|---|---|
committer | Marvin W <git@larma.de> | 2017-08-12 11:59:38 +0200 |
commit | 6904bda756321c67bea0108342fa9ba859dd66e9 (patch) | |
tree | 24517bf54d6188af401afedd343c42b6ece3d1c6 /xmpp-vala/tests | |
parent | dd88db7556a20707f6fe3c81b3c58df42a0f5224 (diff) | |
download | dino-6904bda756321c67bea0108342fa9ba859dd66e9.tar.gz dino-6904bda756321c67bea0108342fa9ba859dd66e9.zip |
xmpp-vala: improve namespace handling, add some tests
Diffstat (limited to 'xmpp-vala/tests')
-rw-r--r-- | xmpp-vala/tests/common.vala | 93 | ||||
-rw-r--r-- | xmpp-vala/tests/stanza.vala | 105 | ||||
-rw-r--r-- | xmpp-vala/tests/testcase.vala | 80 |
3 files changed, 278 insertions, 0 deletions
diff --git a/xmpp-vala/tests/common.vala b/xmpp-vala/tests/common.vala new file mode 100644 index 00000000..b393ba79 --- /dev/null +++ b/xmpp-vala/tests/common.vala @@ -0,0 +1,93 @@ +namespace Xmpp.Test { + +int main(string[] args) { + GLib.Test.init(ref args); + GLib.Test.set_nonfatal_assertions(); + TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite()); + return GLib.Test.run(); +} + +bool fail_if(bool exp, string? reason = null) { + if (exp) { + if (reason != null) GLib.Test.message(reason); + GLib.Test.fail(); + return true; + } + return false; +} + +void fail_if_reached(string? reason = null) { + fail_if(true, reason); +} + +delegate void ErrorFunc() throws Error; + +void fail_if_not_error_code(ErrorFunc func, int expectedCode, string? reason = null) { + try { + func(); + fail_if_reached(@"$(reason + ": " ?? "")no error thrown"); + } catch (Error e) { + fail_if_not_eq_int(e.code, expectedCode, @"$(reason + ": " ?? "")catched unexpected error"); + } +} + +bool fail_if_not(bool exp, string? reason = null) { + return fail_if(!exp, reason); +} + +bool fail_if_eq_int(int left, int right, string? reason = null) { + return fail_if(left == right, @"$(reason + ": " ?? "")$left == $right"); +} + +bool fail_if_not_eq_node(Core.StanzaNode left, Core.StanzaNode right, string? reason = null) { + if (fail_if_not_eq_str(left.name, right.name, @"$(reason + ": " ?? "")name mismatch")) return true; + if (fail_if_not_eq_str(left.val, right.val, @"$(reason + ": " ?? "")val mismatch")) return true; + if (left.name == "#text") return false; + if (fail_if_not_eq_str(left.ns_uri, right.ns_uri, @"$(reason + ": " ?? "")ns_uri mismatch")) return true; + if (fail_if_not_eq_int(left.sub_nodes.size, right.sub_nodes.size, @"$(reason + ": " ?? "")sub node count mismatch")) return true; + if (fail_if_not_eq_int(left.attributes.size, right.attributes.size, @"$(reason + ": " ?? "")attributes count mismatch")) return true; + for (var i = 0; i < left.sub_nodes.size; i++) { + if (fail_if_not_eq_node(left.sub_nodes[i], right.sub_nodes[i], @"$(reason + ": " ?? "")$(i+1)th subnode mismatch")) return true; + } + for (var i = 0; i < left.attributes.size; i++) { + if (fail_if_not_eq_attr(left.attributes[i], right.attributes[i], @"$(reason + ": " ?? "")$(i+1)th attribute mismatch")) return true; + } + return false; +} + +bool fail_if_not_eq_attr(Core.StanzaAttribute left, Core.StanzaAttribute right, string? reason = null) { + if (fail_if_not_eq_str(left.name, right.name, @"$(reason + ": " ?? "")name mismatch")) return true; + if (fail_if_not_eq_str(left.val, right.val, @"$(reason + ": " ?? "")val mismatch")) return true; + if (fail_if_not_eq_str(left.ns_uri, right.ns_uri, @"$(reason + ": " ?? "")ns_uri mismatch")) return true; + return false; +} + +bool fail_if_not_eq_int(int left, int right, string? reason = null) { + return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right"); +} + +bool fail_if_not_eq_str(string? left, string? right, string? reason = null) { + bool nullcheck = (left == null || right == null) && (left != null && right != null); + if (left == null) left = "(null)"; + if (right == null) right = "(null)"; + return fail_if_not(!nullcheck && left == right, @"$(reason + ": " ?? "")$left != $right"); +} + +bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) { + if (fail_if_not_eq_int(left.length, right.length, @"$(reason + ": " ?? "")array length not equal")) return true; + return fail_if_not_eq_str(Base64.encode(left), Base64.encode(right), reason); +} + +bool fail_if_not_zero_int(int zero, string? reason = null) { + return fail_if_not_eq_int(zero, 0, reason); +} + +bool fail_if_zero_int(int zero, string? reason = null) { + return fail_if_eq_int(zero, 0, reason); +} + +bool fail_if_null(void* what, string? reason = null) { + return fail_if(what == null || (size_t)what == 0, reason); +} + +} diff --git a/xmpp-vala/tests/stanza.vala b/xmpp-vala/tests/stanza.vala new file mode 100644 index 00000000..1662c9bc --- /dev/null +++ b/xmpp-vala/tests/stanza.vala @@ -0,0 +1,105 @@ +using Xmpp.Core; + +namespace Xmpp.Test { + +class StanzaTest : Gee.TestCase { + public StanzaTest() { + base("Stanza"); + + add_test("node_one", test_node_one); + add_test("typical_stream", test_typical_stream); + add_test("ack_stream", test_ack_stream); + } + + private void test_node_one() { + var node1 = new StanzaNode.build("test", "ns1_uri") + .add_self_xmlns() + .put_attribute("ns2", "ns2_uri", XMLNS_URI) + .put_attribute("bla", "blub") + .put_node(new StanzaNode.build("testaa", "ns2_uri") + .put_attribute("ns3", "ns3_uri", XMLNS_URI)) + .put_node(new StanzaNode.build("testbb", "ns3_uri") + .add_self_xmlns()); + + var xml1 = node1.to_xml(); + var node2 = new StanzaReader.for_string(xml1).read_node(); + fail_if_not(node1.equals(node2)); + fail_if_not_eq_str(node1.to_string(), node2.to_string()); + } + + private void test_typical_stream() { + var stream = """ + <?xml version='1.0' encoding='UTF-8'?> + <stream:stream + to='example.com' + xmlns='jabber:client' + xmlns:stream='http://etherx.jabber.org/streams' + version='1.0'> + <message from='laurence@example.net/churchyard' + to='juliet@example.com' + xml:lang='en'> + <body>I'll send a friar with speed, to Mantua, with my letters to thy lord.</body> + </message> + </stream:stream> + """; + var root_node_cmp = new StanzaNode.build("stream", "http://etherx.jabber.org/streams") + .put_attribute("to", "example.com") + .put_attribute("xmlns", "jabber:client") + .put_attribute("stream", "http://etherx.jabber.org/streams", XMLNS_URI) + .put_attribute("version", "1.0"); + var node_cmp = new StanzaNode.build("message") + .put_attribute("from", "laurence@example.net/churchyard") + .put_attribute("to", "juliet@example.com") + .put_attribute("lang", "en", XML_URI) + .put_node(new StanzaNode.build("body") + .put_node(new StanzaNode.text("I'll send a friar with speed, to Mantua, with my letters to thy lord."))); + + var reader = new StanzaReader.for_string(stream); + fail_if_not_eq_node(root_node_cmp, reader.read_root_node()); + fail_if_not_eq_node(node_cmp, reader.read_node()); + reader.read_node(); + fail_if_not_error_code(() => reader.read_node(), 3, "end of stream should be reached"); + } + + private void test_ack_stream() { + var stream = """ + <?xml version='1.0' encoding='UTF-8'?> + <stream:stream + to='example.com' + xmlns='jabber:client' + xmlns:stream='http://etherx.jabber.org/streams' + xmlns:ack='http://jabber.org/protocol/ack' + version='1.0'> + <stream:features> + <ack:ack/> + <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> + <required/> + </bind> + </stream:features> + <ack:r/> + </stream:stream> + """; + var root_node_cmp = new StanzaNode.build("stream", "http://etherx.jabber.org/streams") + .put_attribute("to", "example.com") + .put_attribute("xmlns", "jabber:client") + .put_attribute("stream", "http://etherx.jabber.org/streams", XMLNS_URI) + .put_attribute("ack", "http://jabber.org/protocol/ack", XMLNS_URI) + .put_attribute("version", "1.0"); + var node_cmp = new StanzaNode.build("features", XmppStream.NS_URI) + .put_node(new StanzaNode.build("ack", "http://jabber.org/protocol/ack")) + .put_node(new StanzaNode.build("bind", "urn:ietf:params:xml:ns:xmpp-bind") + .add_self_xmlns() + .put_node(new StanzaNode.build("required", "urn:ietf:params:xml:ns:xmpp-bind"))); + var node2_cmp = new StanzaNode.build("r", "http://jabber.org/protocol/ack"); + + var reader = new StanzaReader.for_string(stream); + fail_if_not_eq_node(root_node_cmp, reader.read_root_node()); + fail_if_not_eq_node(node_cmp, reader.read_node()); + fail_if_not_eq_node(node2_cmp, reader.read_node()); + reader.read_node(); + fail_if_not_error_code(() => reader.read_node(), 3, "end of stream should be reached"); + } + +} + +} diff --git a/xmpp-vala/tests/testcase.vala b/xmpp-vala/tests/testcase.vala new file mode 100644 index 00000000..dd1fdefd --- /dev/null +++ b/xmpp-vala/tests/testcase.vala @@ -0,0 +1,80 @@ +/* testcase.vala + * + * Copyright (C) 2009 Julien Peeters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Julien Peeters <contact@julienpeeters.fr> + */ + +public abstract class Gee.TestCase : Object { + + private GLib.TestSuite suite; + private Adaptor[] adaptors = new Adaptor[0]; + + public delegate void TestMethod (); + + public TestCase (string name) { + this.suite = new GLib.TestSuite (name); + } + + public void add_test (string name, owned TestMethod test) { + var adaptor = new Adaptor (name, (owned)test, this); + this.adaptors += adaptor; + + this.suite.add (new GLib.TestCase (adaptor.name, + adaptor.set_up, + adaptor.run, + adaptor.tear_down )); + } + + public virtual void set_up () { + } + + public virtual void tear_down () { + } + + public GLib.TestSuite get_suite () { + return this.suite; + } + + private class Adaptor { + [CCode (notify = false)] + public string name { get; private set; } + private TestMethod test; + private TestCase test_case; + + public Adaptor (string name, + owned TestMethod test, + TestCase test_case) { + this.name = name; + this.test = (owned)test; + this.test_case = test_case; + } + + public void set_up (void* fixture) { + this.test_case.set_up (); + } + + public void run (void* fixture) { + this.test (); + } + + public void tear_down (void* fixture) { + this.test_case.tear_down (); + } + } +} |