[gxml] BackedNode.set_namespace bug fix. Serializable from string fixes.



commit c8980a88ee7d7ce770512695d02e968719aedb34
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Feb 6 10:09:18 2015 -0600

    BackedNode.set_namespace bug fix. Serializable from string fixes.
    
    * BackedNode Checks for namespaces on parent node
    * Reimplemented Serializable.string_to_gvalue to use more
      reliable double.parse()
    * Added unit tests for Serializable.string_to_gvalue for integers

 gxml/BackedNode.vala                 |   18 ++++++-
 gxml/Serializable.vala               |   81 +++++++++++++++++-----------------
 test/SerializableBasicTypesTest.vala |   41 +++++++++++++++++
 3 files changed, 97 insertions(+), 43 deletions(-)
---
diff --git a/gxml/BackedNode.vala b/gxml/BackedNode.vala
index 53724c6..03f5189 100644
--- a/gxml/BackedNode.vala
+++ b/gxml/BackedNode.vala
@@ -102,6 +102,7 @@ namespace GXml {
                 */
                public override bool set_namespace (string uri, string prefix)
                {
+                       if (node == null) return false;
                        if (node->ns_def != null) {
                                for (Xml.Ns *cur = node->ns_def; cur != null; cur = cur->next) {
                                        if ((string) cur->prefix == prefix && (string) cur->href == uri) {
@@ -110,15 +111,26 @@ namespace GXml {
                                        }
                                }
                        }
-                       // Not found in this node, searching on root element
-                       if (owner_document.document_element.node->ns_def != null) {
-                               for (Xml.Ns *cur = owner_document.document_element.node->ns_def; cur != null; 
cur = cur->next) {
+                       // Not found search on parent
+                       if (node->parent != null) {
+                               for (Xml.Ns *cur = node->parent->ns_def; cur != null; cur = cur->next) {
                                        if ((string) cur->prefix == prefix && (string) cur->href == uri) {
                                                this.node->set_ns (cur);
                                                return true;
                                        }
                                }
                        }
+                       // Not found in this node, searching on root element
+                       if (owner_document == null) return false;
+                       if (owner_document.document_element == null) return false;
+                       if (owner_document.document_element.node == null) return false;
+                       if (owner_document.document_element.node->ns_def == null) return false;
+                       for (Xml.Ns *cur = owner_document.document_element.node->ns_def; cur != null; cur = 
cur->next) {
+                               if ((string) cur->prefix == prefix && (string) cur->href == uri) {
+                                       this.node->set_ns (cur);
+                                       return true;
+                               }
+                       }
                        return false;
                }
                /**
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index e39b2cc..854f313 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -466,66 +466,67 @@ namespace GXml {
       GLib.Value dest2 = Value (t);
       bool ret = false;
 
-      if (t == typeof (int64)) {
+      if (t == typeof (int64)
+        || t == typeof (uint64)) {
         int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_int64 (val);
-        }
+        val = (int64) double.parse (str);
+        dest2.set_int64 (val);
+        ret = true;
       } else if (t == typeof (int)) {
-        int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_int ((int)val);
-        }
+        int val;
+        val = (int) double.parse (str);
+        dest2.set_int (val);
+        ret = true;
       } else if (t == typeof (long)) {
-        int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_long ((long)val);
-        }
+        long val;
+        val = (long) double.parse (str);
+        dest2.set_long (val);
+        ret = true;
       } else if (t == typeof (uint)) {
-        uint64 val;
-        ;
-        if (ret = uint64.try_parse (str, out val)) {
-          dest2.set_uint ((uint)val);
-        }
+        uint val;
+        val = (uint) double.parse (str);
+        dest2.set_uint (val);
+        ret = true;
       } else if (t == typeof (ulong)) {
-        uint64 val;
-        if (ret = uint64.try_parse (str, out val)) {
-          dest2.set_ulong ((ulong)val);
-        }
+        ulong val;
+        val = (ulong) double.parse (str);
+        dest2.set_ulong (val);
+        ret = true;
       } else if (t == typeof (bool)) {
         bool val;
         if (ret = bool.try_parse (str.down (), out val)) {
           dest2.set_boolean (val);
         }
       } else if (t == typeof (float)) {
-        double val;
-        if (ret = double.try_parse (str, out val)) {
-          dest2.set_float ((float)val);
-        }
+        float val;
+        val = (float) double.parse (str);
+        dest2.set_float (val);
+        ret = true;
       } else if (t == typeof (double)) {
         double val;
-        if (ret = double.try_parse (str, out val)) {
-          dest2.set_double (val);
-        }
+        val = (double) double.parse (str);
+        dest2.set_double (val);
+        ret = true;
       } else if (t == typeof (string)) {
         dest2.set_string (str);
         ret = true;
       } else if (t == typeof (char)) {
-        int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_char ((char)val);
-        }
+        char val;
+        val = (char) double.parse (str);
+        dest2.set_char (val);
+        ret = true;
       } else if (t == typeof (uchar)) {
-        int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_uchar ((uchar)val);
-        }
+        uchar val;
+        val = (uchar) double.parse (str);
+        dest2.set_uchar (val);
+        ret = true;
       } else if (t == Type.BOXED) {
+        // TODO: Boxed type transformation
       } else if (t.is_enum ()) {
-        int64 val;
-        if (ret = int64.try_parse (str, out val)) {
-          dest2.set_enum ((int)val);
-        }
+        int val;
+        val = (int) double.parse (str);
+        dest2.set_enum (val);
+        ret = true;
       } else if (t.is_flags ()) {
       } else if (t.is_object ()) {
       } else {
diff --git a/test/SerializableBasicTypesTest.vala b/test/SerializableBasicTypesTest.vala
index b3655ef..7ac0ad5 100644
--- a/test/SerializableBasicTypesTest.vala
+++ b/test/SerializableBasicTypesTest.vala
@@ -28,6 +28,9 @@ class SerializableBasicTypeTest : GXmlTest {
   public class BasicType : SerializableObjectModel
   {
     public bool boolean { get; set; }
+    public int  integer { get; set; }
+    public float  floatval { get; set; }
+    public double  doubleval { get; set; }
     public override string node_name () { return "basictype"; }
     public override string to_string () { return get_type ().name (); }
   }
@@ -73,5 +76,43 @@ class SerializableBasicTypeTest : GXmlTest {
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/basic_types/integer",
+    () => {
+      try {
+        var bt = new BasicType ();
+        bt.boolean = true;
+        var doc = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="156"/>""");
+        bt.deserialize (doc);
+        assert (bt.integer == 156);
+        var doc1 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="a156"/>""");
+        bt.deserialize (doc1);
+        assert (bt.integer == 0);
+        var doc2 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="156b"/>""");
+        bt.deserialize (doc2);
+        assert (bt.integer == 156);
+        var doc3 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="156.0"/>""");
+        bt.deserialize (doc3);
+        assert (bt.integer == 156);
+        var doc4 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="0.156"/>""");
+        bt.deserialize (doc4);
+        assert (bt.integer == 0);
+        var doc5 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="a156.156"/>""");
+        bt.deserialize (doc5);
+        assert (bt.integer == 0);
+        var doc6 = new Document.from_string ("""<?xml version="1.0"?>
+<basictype integer="156.156b"/>""");
+        bt.deserialize (doc6);
+        assert (bt.integer == 156);
+      } catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
   }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]