diff options
-rw-r--r-- | xmpp-vala/src/core/stanza_node.vala | 26 | ||||
-rw-r--r-- | xmpp-vala/tests/stanza.vala | 20 |
2 files changed, 40 insertions, 6 deletions
diff --git a/xmpp-vala/src/core/stanza_node.vala b/xmpp-vala/src/core/stanza_node.vala index 44a70d8d..54c14956 100644 --- a/xmpp-vala/src/core/stanza_node.vala +++ b/xmpp-vala/src/core/stanza_node.vala @@ -98,15 +98,29 @@ public class StanzaNode : StanzaEntry { } public int get_attribute_int(string name, int def = -1, string? ns_uri = null) { - string? res = get_attribute(name, ns_uri); - if (res == null) return def; - return int.parse((!)res); + string? attribute_str = get_attribute(name, ns_uri); + if (attribute_str == null) return def; + + int res = def; + bool parse_success = int.try_parse(attribute_str, out res); + if (!parse_success) { + info("Could not parse int attribute %s: %s", name, attribute_str); + return def; + } + return res; } public uint get_attribute_uint(string name, uint def = 0, string? ns_uri = null) { - string? res = get_attribute(name, ns_uri); - if (res == null) return def; - return (uint) long.parse((!)res); + string? attribute_str = get_attribute(name, ns_uri); + if (attribute_str == null) return def; + + uint res = def; + bool parse_success = uint.try_parse(attribute_str, out res); + if (!parse_success) { + info("Could not parse uint attribute %s: %s", name, attribute_str); + return def; + } + return res; } public bool get_attribute_bool(string name, bool def = false, string? ns_uri = null) { diff --git a/xmpp-vala/tests/stanza.vala b/xmpp-vala/tests/stanza.vala index cd374912..fa1ccb41 100644 --- a/xmpp-vala/tests/stanza.vala +++ b/xmpp-vala/tests/stanza.vala @@ -7,6 +7,7 @@ class StanzaTest : Gee.TestCase { add_async_test("node_one", (cb) => { test_node_one.begin(cb); }); add_async_test("typical_stream", (cb) => { test_typical_stream.begin(cb); }); add_async_test("ack_stream", (cb) => { test_ack_stream.begin(cb); }); + add_test("get_attribute_(u)int", test_get_attribute_int); } private async void test_node_one(Gee.TestFinishedCallback cb) { @@ -112,6 +113,25 @@ class StanzaTest : Gee.TestCase { cb(); } + private void test_get_attribute_int() { + var stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "42"); + assert(stanza_node.get_attribute_int("bar", -2) == 42); + assert(stanza_node.get_attribute_uint("bar", 3) == 42); + + stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "-42"); + assert(stanza_node.get_attribute_int("bar", -2) == -42); + assert(stanza_node.get_attribute_uint("bar", 3) == 3); + + stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns(); + assert(stanza_node.get_attribute_int("bar", -2) == -2); + assert(stanza_node.get_attribute_uint("bar", 3) == 3); + + stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "str"); + assert(stanza_node.get_attribute_int("bar", -2) == -2); + assert(stanza_node.get_attribute_uint("bar", 3) == 3); + + } + } } |