[gxml] Fixes for GElement unit test to pass



commit 156dc8fb99a2118bc75f5380720659fe6a968ff9
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Feb 8 11:21:14 2016 -0600

    Fixes for GElement unit test to pass

 gxml/GXmlElement.vala        |    5 ++
 gxml/GXmlHashMapAttr.vala    |    8 ++-
 gxml/GXmlListNamespaces.vala |    2 +
 test/GElementTest.vala       |  143 ++++++++++++++++++++++++++++++++++++++++++
 test/GXmlTest.vala           |    1 +
 test/Makefile.am             |    1 +
 6 files changed, 158 insertions(+), 2 deletions(-)
---
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index 360ff13..96cad72 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -70,4 +70,9 @@ public class GXml.GElement : GXml.GNode, GXml.Element
     }
   }
   public string tag_name { owned get { return _node->name.dup (); } }
+  public override string to_string () {
+    var buf = new Xml.Buffer ();
+    buf.node_dump (_node->doc, _node, 1, 0);
+    return buf.content ().dup ();
+  }
 }
diff --git a/gxml/GXmlHashMapAttr.vala b/gxml/GXmlHashMapAttr.vala
index 4dc7d8c..c0301aa 100644
--- a/gxml/GXmlHashMapAttr.vala
+++ b/gxml/GXmlHashMapAttr.vala
@@ -58,7 +58,8 @@ public class GXml.GHashMapAttr : Gee.AbstractMap<string,GXml.Node>
   }
   public override GXml.Node @get (string key) {
     if (_node == null) return null;
-    var p = _node->get_prop (key);
+    var p = _node->has_prop (key);
+    if (p == null) return  null;
     return new GAttribute (_doc, p);
   }
   public override bool has (string key, GXml.Node value) { return has_key (key); }
@@ -77,8 +78,11 @@ public class GXml.GHashMapAttr : Gee.AbstractMap<string,GXml.Node>
   }
   public override bool unset (string key, out GXml.Node value = null) {
     if (_node == null) return false;
+    var p = _node->has_prop (key);
+    if (p == null) return false;
+    p->remove ();
     value = null;
-    return (_node->set_prop (key, null)) != null;
+    return true;
   }
   public override Gee.Set<Gee.Map.Entry<string,GXml.Node>> entries {
     owned get {
diff --git a/gxml/GXmlListNamespaces.vala b/gxml/GXmlListNamespaces.vala
index 2484fd1..f22d6b6 100644
--- a/gxml/GXmlListNamespaces.vala
+++ b/gxml/GXmlListNamespaces.vala
@@ -40,6 +40,8 @@ public class GXml.GListNamespaces : Gee.AbstractList<GXml.Node>
       if (i == index) {
         return new GNamespace (ns);
       }
+      ns = ns->next;
+      i++;
     }
     return null;
   }
diff --git a/test/GElementTest.vala b/test/GElementTest.vala
new file mode 100644
index 0000000..06b2b61
--- /dev/null
+++ b/test/GElementTest.vala
@@ -0,0 +1,143 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* Notation.vala
+ *
+ * Copyright (C) 2016  Daniel Espinosa <esodan gmail com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using GXml;
+
+class GElementTest : GXmlTest  {
+       public static void add_tests () {
+               Test.add_func ("/gxml/gelement/namespace_uri", () => {
+                       try {
+                               GDocument doc = new GDocument.from_string ("<Potions><magic:Potion 
xmlns:magic=\"http://hogwarts.co.uk/magic\"; 
xmlns:products=\"http://diagonalley.co.uk/products\"/></Potions>");
+                               GXml.GNode root = (GXml.GNode) doc.root;
+                               assert (root != null);
+                               assert (root.name == "Potions");
+                               GXml.GNode node = (GXml.GNode) root.children[0];
+                               assert (node != null);
+                               assert (node.name == "Potion");
+                               assert (node.namespaces != null);
+                               assert (node.namespaces.size == 2);
+                               assert (node.namespaces[0].uri == "http://hogwarts.co.uk/magic";);
+                               assert (node.namespaces[0].prefix == "magic");
+                               assert (node.namespaces.get (1).prefix == "products");
+                               assert (node.namespaces.get (1).uri == "http://diagonalley.co.uk/products";);
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gelement/attributes", () => {
+                       try {
+                               GDocument doc = new GDocument.from_string ("<root />");
+                               assert (doc.root != null);
+                               GElement elem = (GElement) doc.create_element ("alphanumeric");
+                               doc.root.children.add (elem);
+                               assert (elem.attrs != null);
+                               assert (elem.attrs.size == 0);
+                               elem.set_attr ("alley", "Diagon");
+                               elem.set_attr ("train", "Hogwarts Express");
+                               assert (elem.attrs.size == 2);
+                               Test.message ("Getting attributes value alley... Node: "+doc.to_string ());
+                               assert (elem.attrs.get ("alley").value == "Diagon");
+                               assert (elem.attrs.get ("train").value == "Hogwarts Express");
+
+                               elem.set_attr ("owl", "");
+                               GAttribute attr = elem.get_attr ("owl") as GAttribute;
+                               assert (attr != null);
+                               attr.value = "Hedwig";
+
+                               assert (elem.attrs.size == 3);
+                               assert (elem.attrs.get ("owl").value == "Hedwig");
+
+                               elem.attrs.unset ("alley");
+                               assert (elem.attrs.get ("alley") == null);
+                               assert (elem.attrs.size == 2);
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gelement/to_string", () =>{
+                       try {
+                               GDocument doc = new GDocument.from_string ("<root />");
+                               var elem = doc.create_element ("country");
+                               var t = doc.create_text ("New Zealand");
+                               assert (t != null);
+                               elem.children.add (t);
+                               Test.message ("Elem1:"+elem.to_string ());
+                               assert (elem.to_string () == "<country>New Zealand</country>");
+                               var elem2 = doc.create_element ("messy");
+                               var t2 = doc.create_text ("&lt;<>&gt;");
+                               elem2.children.add (t2);
+                               Test.message ("Elem2:"+elem2.to_string ());
+                               assert (elem2.to_string () == "<messy>&amp;lt;&lt;&gt;&amp;gt;</messy>");
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gelement/content/set", () =>{
+                       try {
+                               var doc = new GDocument ();
+                               var root = (GElement) doc.create_element ("root");
+                               doc.children.add ((GNode) root);
+                               root.content = "TEXT1";
+                               assert (root.to_string () == "<root>TEXT1</root>");
+                               string s = doc.to_string ().split ("\n")[1];
+                               assert (s == "<root>TEXT1</root>");
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gelement/content/add_aside_child_nodes", () =>{
+                       try {
+                               var doc = new GDocument ();
+                               var root = (GElement) doc.create_element ("root");
+                               doc.children.add (root);
+                               var n = (GElement) doc.create_element ("child");
+                               root.children.add (n);
+                               root.content = "TEXT1";
+                               string s = doc.to_string ().split ("\n")[1];
+                               assert (s == "<root><child/>TEXT1</root>");
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gelement/content/keep_child_nodes", () =>{
+                       try {
+                               var doc = new GDocument ();
+                               var root = (GElement) doc.create_element ("root");
+                               doc.children.add (root);
+                               var n = (GElement) doc.create_element ("child");
+                               root.children.add (n);
+                               var t = (Text) doc.create_text ("TEXT1");
+                               root.children.add (t);
+                               string s = doc.to_string ().split ("\n")[1];
+                               assert (s == "<root><child/>TEXT1</root>");
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+       }
+}
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 7815542..005c3de 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -83,6 +83,7 @@ class GXmlTest {
                SerializablePropertyValueListTest.add_tests ();
                SerializablePropertyEnumTest.add_tests ();
                GDocumentTest.add_tests ();
+               GElementTest.add_tests ();
                HtmlDocumentTest.add_tests ();
 
                Test.run ();
diff --git a/test/Makefile.am b/test/Makefile.am
index 341ee24..f650314 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -61,6 +61,7 @@ sources = \
        TwCommentTest.vala \
        TwProcessingInstructionTest.vala \
        GDocumentTest.vala \
+       GElementTest.vala \
        HtmlDocumentTest.vala \
        $(NULL)
 


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