[gxml] GomElement: fixes on unit tests



commit 0515f2fb3b22cdb53700c3ce3826f74706c5366d
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Mar 20 10:49:50 2019 -0600

    GomElement: fixes on unit tests

 gxml/GomElement.vala           | 39 +++++++++++++++++++++++++++------------
 gxml/GomProperty.vala          |  4 ++--
 gxml/GomStringRef.vala         |  2 +-
 gxml/XParser.vala              | 20 ++++++++++++++++++--
 test/GomDocumentTest.vala      |  1 +
 test/GomElementTest.vala       | 10 ++++++++--
 test/GomSerializationTest.vala | 17 ++++++++++-------
 7 files changed, 67 insertions(+), 26 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 5c91216..9605d1a 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -313,6 +313,17 @@ public class GXml.GomElement : GomNode,
     _node_type = DomNode.NodeType.ELEMENT_NODE;
     _attributes = new Attributes (this);
     _local_name = "";
+    notify.connect ((pspec)=>{
+      if ("::" in pspec.get_nick ()) {
+        string name = pspec.get_nick ().replace ("::", "");
+        var p = _attributes.get (name.down ());
+        if (p == null) {
+          GomProperty prop = new GomStringRef (this, name);
+          _attributes.add (name, prop);
+          message ("Set: %s", name);
+        }
+      }
+    });
   }
   /**
    * Convenient function to initialize, at construction time, a {@link GomElement}
@@ -402,12 +413,12 @@ public class GXml.GomElement : GomNode,
       }
       string p = "";
       string ns = null;
-      string n = name;
+      string n = name.down ();
       if (":" in name) {
         string[] s = name.split (":");
         if (s.length > 2) return null;
         p = s[0];
-        n = s[1];
+        n = s[1].down ();
         if (p == "xml")
           ns = "http://www.w3.org/2000/xmlns/";;
         if (p == "xmlns")
@@ -415,7 +426,7 @@ public class GXml.GomElement : GomNode,
         if (p != "xmlns" && p != "xml")
           ns =  _element.lookup_namespace_uri (p);
       }
-      var prop = get (name);
+      var prop = get (n);
       string val = null;
       if (prop != null) {
           val = prop.value;
@@ -448,21 +459,21 @@ public class GXml.GomElement : GomNode,
       } else {
         prop = new GomString.with_string (node.node_value);
       }
-      set ((node as DomAttr).local_name, prop);
-      order.set (size, (node as DomAttr).local_name);
+      set ((node as DomAttr).local_name.down (), prop);
+      order.set (size, (node as DomAttr).local_name.down ());
       return new GomAttr (_element, (node as DomAttr).local_name, node.node_value);
     }
     public DomNode? remove_named_item (string name) throws GLib.Error {
       if (":" in name) return null;
       string val = null;
-      var prop = get (name);
+      var prop = get (name.down ());
       if (prop != null) {
           val = prop.value;
           prop.value = null;
       }
       var n = new GomAttr (_element, name, val);
-      unset (name);
       long i = index_of (name);
+      unset (name.down ());
       if (i < 0) {
         warning (_("No index found for attribute %s").printf (name));
       } else {
@@ -475,11 +486,11 @@ public class GXml.GomElement : GomNode,
       if (":" in local_name) return null;
       var nsp = _element.lookup_prefix (namespace_uri);
       if (nsp == null || nsp == "") return null;
-      var v = get (nsp+":"+local_name);
+      var v = get ((nsp+":"+local_name).down ());
       if (v == null) return null;
       string val = v.value;
       var n = new GomAttr.namespace (_element, namespace_uri, nsp, local_name, val);
-      string k = nsp+":"+local_name;
+      string k = (nsp+":"+local_name).down ();
       v.value = null;
       unset (k);
       long i = index_of (k);
@@ -495,7 +506,7 @@ public class GXml.GomElement : GomNode,
       if (":" in local_name) return null;
       var nsp = _element.lookup_prefix (namespace_uri);
       if (nsp == null) return null;
-      var v = get (nsp+":"+local_name);
+      var v = get ((nsp+":"+local_name).down ());
       if (v == null) return null;
       string val = v.value;
       var n = new GomAttr.namespace (_element, namespace_uri, nsp, local_name, val);
@@ -567,7 +578,7 @@ public class GXml.GomElement : GomNode,
       if ((node as DomAttr).prefix != null
           && (node as DomAttr).prefix != "")
         p = (node as DomAttr).prefix + ":";
-      string k = p+(node as DomAttr).local_name;
+      string k = (p+(node as DomAttr).local_name).down ();
       GomProperty prop = null;
       var pprop = (_element as GomObject).find_property_name ((node as DomAttr).local_name);
       if (pprop != null) {
@@ -594,11 +605,15 @@ public class GXml.GomElement : GomNode,
       }
       return -1;
     }
+    public void add (string name, GomProperty prop) {
+      set (name.down (), prop);
+      order.set (size, name);
+    }
   }
   public DomNamedNodeMap attributes { owned get { return (DomNamedNodeMap) _attributes; } }
   public string? get_attribute (string name) {
     string str = null;
-    var prop = _attributes.get (name);
+    var prop = _attributes.get (name.down ());
     if (prop != null) {
         str = prop.value;
     }
diff --git a/gxml/GomProperty.vala b/gxml/GomProperty.vala
index ccd1bd9..2a0d618 100644
--- a/gxml/GomProperty.vala
+++ b/gxml/GomProperty.vala
@@ -38,7 +38,7 @@ public interface GXml.GomProperty : Object
   /**
    * Takes a string and check if it is a valid value for property
    */
-  public abstract bool validate_value (string val);
+  public abstract bool validate_value (string? val);
 }
 
 /**
@@ -52,7 +52,7 @@ public abstract class GXml.GomBaseProperty : Object, GXml.GomProperty {
   /**
    * Takes a string and check if it can be valid for this property.
    */
-  public virtual bool validate_value (string val) { return true; }
+  public virtual bool validate_value (string? val) { return true; }
 }
 
 /**
diff --git a/gxml/GomStringRef.vala b/gxml/GomStringRef.vala
index d890de4..1a202c7 100644
--- a/gxml/GomStringRef.vala
+++ b/gxml/GomStringRef.vala
@@ -31,7 +31,7 @@ public class GXml.GomStringRef : Object, GomProperty {
         object.set_attribute (name, value);
     }
   }
-  public bool validate_value (string val) { return true; }
+  public bool validate_value (string? val) { return true; }
   public GomStringRef (GomObject obj, string name)  {
       object = obj;
       this.name = name;
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index aebdc8b..4eb6e83 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -116,7 +116,9 @@ public class GXml.XParser : Object, GXml.Parser {
     var b = new MemoryOutputStream.resizable ();
     b.splice (istream, 0);
     tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
+    _node.freeze_notify ();
     read_node (_node);
+    _node.thaw_notify ();
     tr = null;
   }
   public async void read_stream_async (GLib.InputStream istream,
@@ -126,7 +128,9 @@ public class GXml.XParser : Object, GXml.Parser {
     Idle.add (read_stream_async.callback);
     yield;
     tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
+    _node.freeze_notify ();
     read_node (_node);
+    _node.thaw_notify ();
     tr = null;
   }
 
@@ -459,8 +463,19 @@ public class GXml.XParser : Object, GXml.Parser {
         tw.flush ();
     }
     // DomElement attributes
-    foreach (string ak in (node as DomElement).attributes.keys) {
-      string v = ((node as DomElement).attributes as HashMap<string,string>).get (ak);
+    var keys = (node as DomElement).attributes.keys;
+    foreach (string ak in keys) {
+      var prop = ((node as DomElement).attributes as HashMap<string,GomProperty>).get (ak);
+      if (prop == null) {
+        continue;
+      }
+      if (prop is GomStringRef) {
+        continue;
+      }
+      string v = prop.value;
+      if (v == null) {
+        continue;
+      }
       if ("xmlns:" in ak) {
         string ns = (node as DomElement).namespace_uri;
         if (ns != null) {
@@ -621,3 +636,4 @@ public class GXml.XParser : Object, GXml.Parser {
     }
   }
 }
+
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index ddf6372..d27ccdf 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -714,6 +714,7 @@ class GomDocumentTest : GXmlTest {
                Test.add_func ("/gxml/gom-document/element-id", () => {
                        try {
                                var d = new GomDocument.from_string ("""<root><child id="id1"/><child 
id="id2"/></root>""") as DomDocument;
+                               message ("Serialize Document");
                                message ((d as GomDocument).write_string ());
                                var e = d.get_element_by_id ("id1");
                                assert (e != null);
diff --git a/test/GomElementTest.vala b/test/GomElementTest.vala
index 58a1e55..8dd74fb 100644
--- a/test/GomElementTest.vala
+++ b/test/GomElementTest.vala
@@ -38,7 +38,7 @@ class ObjectParent : GomElement {
        public ObjectProperty prop { get; set; }
        public class ObjectProperty : Object, GomProperty {
                public string? value { owned get; set; }
-               public bool validate_value (string val) {
+               public bool validate_value (string? val) {
                        return true;
                }
        }
@@ -336,7 +336,7 @@ class GomElementTest : GXmlTest  {
                                }
                                assert (elem != null);
                                assert (elem.attributes != null);
-                               assert (elem.attributes.size == 2);
+                               assert (elem.attributes.size == 3);
                                var n = doc.create_element ("node");
                                elem.append_child (n);
                                var child = doc.create_element ("child");
@@ -781,6 +781,12 @@ class GomElementTest : GXmlTest  {
                                assert (e.attributes.item (5).node_value == "http://www.gnome.org/gxml/test";);
                                assert (e.attributes.item (6).node_value == "prop1_test");
                                assert (e.attributes.item (7).node_value == "prop2_test");
+                               e.id = "di1";
+                               assert (e.id == "di1");
+                               assert (e.get_attribute ("id") == "di1");
+                               assert (e.attributes.length == 9);
+                               assert (e.attributes.item (8) != null);
+                               assert (e.attributes.item (8).node_value == "di1");
                        } catch (GLib.Error e) {
                    GLib.message ("Error: "+e.message);
                    assert_not_reached ();
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index fad183e..cdc6792 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -974,24 +974,18 @@ class GomSerializationTest : GXmlTest  {
       parser.read_string ("<book name=\"Loco\"/>", null);
       string s = parser.write_string ();
       assert (s != null);
-#if DEBUG
       GLib.message ("Doc:"+s);
-#endif
       assert (b != null);
       assert (b.child_nodes != null);
       assert (b.child_nodes.size == 0);
       assert (b.attributes != null);
-      assert (b.attributes.size == 0);
+      assert (b.attributes.size == 1);
       assert (b.name != null);
-#if DEBUG
       assert (b.name == "Loco");
-#endif
       s = parser.write_string ();
       assert (s != null);
       assert ("<Book Name=\"Loco\"/>" in s);
-#if DEBUG
       GLib.message ("Doc:"+s);
-#endif
       b.name = "My Book";
       assert (b.get_attribute ("name") == "My Book");
       s = b.to_string ();
@@ -1334,10 +1328,19 @@ class GomSerializationTest : GXmlTest  {
         bt.real_float = (float) 1.1;
         bt.real_double = 2.2;
         message (bt.write_string ());
+        assert (bt.attributes.size == 5);
+        assert (bt.unsigned_integer == 1);
+        assert (bt.get_attribute ("text") == "Text");
+        assert (bt.get_attribute ("integer") == "-1");
+        message ("realFloat = %s", bt.get_attribute ("realFloat"));
+        assert (bt.get_attribute ("realFloat") == "1.1");
+        message ("unsignedInteger = %s", bt.get_attribute ("unsignedInteger"));
+        assert (bt.get_attribute ("unsignedInteger") == "1");
         var bt2 = new GomBasicTypes ();
         bt2.read_from_string (bt.write_string ());
         assert (bt2.text == "Text");
         assert (bt2.integer == -1);
+        message (bt2.unsigned_integer.to_string ());
         assert (bt2.unsigned_integer == 1);
         assert (bt2.real_float == (float) 1.1);
         assert (bt2.real_double == 2.2);


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