[gxml] Fixes on DomElement with Unit Tests



commit 02512bdbdaecf152ba753a33651774f2bb976024
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 18 17:41:57 2016 -0500

    Fixes on DomElement with Unit Tests

 gxml/GXmlElement.vala      |   55 ++++++++++++++------
 test/DomGDocumentTest.vala |  122 ++++++++++++++++++++++++++++++++++++++++++++
 test/GXmlTest.vala         |    1 +
 test/Makefile.am           |    1 +
 4 files changed, 162 insertions(+), 17 deletions(-)
---
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index a184547..60be4c0 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -193,9 +193,15 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   }
 
   public DomNamedNodeMap attributes { owned get { return (DomNamedNodeMap) attrs; } }
-  public string? get_attribute (string name) { return attrs.get (name).value; }
+  public string? get_attribute (string name) {
+    var p = attrs.get (name);
+    if (p == null) return null;
+    return p.value;
+  }
   public string? get_attribute_ns (string? namespace, string local_name) {
-    return get_ns_attr (local_name, namespace).value;
+    var p = get_ns_attr (local_name, namespace);
+    if (p == null) return null;
+    return p.value;
   }
   public void set_attribute (string name, string? value) { set_attr (name, value); }
   public void set_attribute_ns (string? namespace, string name, string? value) {
@@ -221,6 +227,7 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
       if (!(n is GXml.DomElement)) continue;
       if (n.node_name == local_name)
         l.add ((DomElement) n);
+      l.add_all ((n as DomElement).get_elements_by_tag_name (local_name));
     }
     return l;
   }
@@ -243,27 +250,41 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   }
   public DomHTMLCollection get_elements_by_class_name (string class_names) {
     var l = new GDomHTMLCollection ();
+    if (class_names == "") return l;
+    string[] cs = {};
+    if (" " in class_names) {
+      cs = class_names.split (" ");
+    } else
+      cs += class_names;
     foreach (GXml.DomElement n in children) {
-      if (!(n is GXml.DomElement)) continue;
-      if (!n.attributes.has_key ("class")) continue;
-      if (" " in class_names) {
-        string[] cs = class_names.split (" ");
-        bool cl = true;
-        foreach (string s in cs) {
-          if (!(s in n.attributes.get ("class").node_value)) cl = false;
+      l.add_all (n.get_elements_by_class_name (class_names));
+      string cls = n.get_attribute ("class");
+      if (cls == null) continue;
+      foreach (string s in cs) {
+        if (" " in cls) {
+          string[] ncls = cls.split (" ");
+          foreach (string snc in ncls) {
+            if (snc == s) l.add (n);
+          }
         }
-        if (cl)
-          l.add ((DomElement) n);
-      } else
-        if (n.attributes.get ("class").node_value == class_names)
-          l.add ((DomElement) n);
+        else
+          if (s == cls) l.add (n);
+      }
     }
     return l;
   }
   // DomParentNode
-  public new DomHTMLCollection children { owned get { return children; } }
-  public DomElement? first_element_child { owned get { return children.first (); } }
-  public DomElement? last_element_child { owned get { return children.last (); } }
+  public new DomHTMLCollection children {
+    owned get {
+      var l = new DomElementList ();
+      foreach (GXml.DomNode n in child_nodes) {
+        if (n is DomElement) l.add ((DomElement) n);
+      }
+      return l;
+    }
+  }
+  public DomElement? first_element_child { owned get { return (DomElement) children.first (); } }
+  public DomElement? last_element_child { owned get { return (DomElement) children.last (); } }
   public ulong child_element_count { get { return (ulong) children.size; } }
 
   public DomElement? query_selector (string selectors) throws GLib.Error {
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
new file mode 100644
index 0000000..cbb965d
--- /dev/null
+++ b/test/DomGDocumentTest.vala
@@ -0,0 +1,122 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* DomGDocumentTest.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 DomGDocumentTest : GXmlTest {
+
+static const string STRDOC = "<?xml version=\"1.0\"?>
+<!-- Comment -->
+<Sentences>
+  <Sentence lang=\"en\">I like the colour blue.</Sentence>
+  <Sentence lang=\"de\">Ich liebe die T&#xFC;r.</Sentence>
+  <Authors>
+    <Author>
+      <Name>Fred</Name>
+      <Email>fweasley hogwarts co uk</Email>
+    </Author>
+    <Author>
+      <Name>George</Name>
+      <Email>gweasley hogwarts co uk</Email>
+    </Author>
+  </Authors>
+</Sentences>
+";
+
+static const string HTMLDOC ="
+<html>
+<body>
+<p class=\"black\">Text content</p>
+<p id=\"p01\">p01 p id</p>
+<p class=\"black block\">Two classes</p>
+</body>
+</html>
+";
+
+       public static void add_tests () {
+               Test.add_func ("/gxml/dom/document/children", () => {
+                       GLib.message ("Doc: "+STRDOC);
+                       GDocument doc = new GDocument.from_string (STRDOC);
+                       DomElement root = doc.document_element;
+                       assert (root != null);
+                       assert (root is DomElement);
+                       assert (root.node_name == "Sentences");
+                       assert (root.has_child_nodes ());
+                       assert (root.children.size == 3);
+                       assert (root.children[0].node_name == "Sentence");
+                       assert (root.children[0].get_attribute ("lang") == "en");
+                       assert (root.children[0].node_value == "I like the colour blue.");
+               });
+               Test.add_func ("/gxml/dom/document/child_nodes", () => {
+                       GLib.message ("Doc: "+STRDOC);
+                       GDocument doc = new GDocument.from_string (STRDOC);
+                       assert (doc is DomDocument);
+                       assert (doc.child_nodes != null);
+                       assert (doc.child_nodes.size == 2);
+                       assert (doc.child_nodes[0] is DomComment);
+                       assert (doc.child_nodes[1] is DomElement);
+                       assert (doc.child_nodes[1].node_name == "Sentences");
+                       assert (doc.document_element != null);
+                       assert (doc.document_element is DomElement);
+                       var auths = doc.document_element.children[2];
+                       assert (auths.node_name == "Authors");
+                       assert (auths.child_nodes.size == 5);
+                       assert (auths.child_nodes[4] is DomText);
+               });
+               Test.add_func ("/gxml/dom/document/element_collections", () => {
+                       GLib.message ("Doc: "+STRDOC);
+                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       assert (doc is DomDocument);
+                       var le = doc.get_elements_by_tag_name ("p");
+                       assert (le.size == 3);
+                       assert (le[0].get_attribute ("class") == "black");
+                       assert (le[1].get_attribute ("id") == "p01");
+                       var lc = doc.get_elements_by_class_name ("black");
+                       assert (lc.size == 2);
+                       assert (lc[0].node_name == "p");
+                       assert (lc[0].get_attribute ("class") == "black");
+                       assert (lc[1].node_name == "p");
+                       assert (lc[1].get_attribute ("class") == "black block");
+                       var nid = doc.get_element_by_id ("p01");
+                       assert (nid != null);
+                       assert (nid.node_name == "p");
+                       assert (nid.get_attribute ("id") == "p01");
+               });
+               Test.add_func ("/gxml/dom/element/element_collections", () => {
+                       GLib.message ("Doc: "+HTMLDOC);
+                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       assert (doc is DomDocument);
+                       assert (doc.document_element.children.size == 1);
+                       var le = doc.document_element.get_elements_by_tag_name ("p");
+                       assert (le.size == 3);
+                       assert (le[0].get_attribute ("class") == "black");
+                       assert (le[1].get_attribute ("id") == "p01");
+                       var lc = doc.document_element.get_elements_by_class_name ("black");
+                       GLib.message("size"+lc.size.to_string ());
+                       assert (lc.size == 2);
+                       assert (lc[0].node_name == "p");
+                       assert (lc[0].get_attribute ("class") == "black");
+                       assert (lc[1].node_name == "p");
+                       assert (lc[1].get_attribute ("class") == "black block");
+               });
+       }
+}
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 998ed0c..afadd01 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -85,6 +85,7 @@ class GXmlTest {
                GElementTest.add_tests ();
                GAttributeTest.add_tests ();
                HtmlDocumentTest.add_tests ();
+               DomGDocumentTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/Makefile.am b/test/Makefile.am
index e264a77..e3a8dfb 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -63,6 +63,7 @@ sources = \
        GElementTest.vala \
        GAttributeTest.vala \
        HtmlDocumentTest.vala \
+       DomGDocumentTest.vala \
        $(NULL)
 
 vala-stamp: $(sources)


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