[gxml] GomElement: Added unit tests and API additions



commit da0d6571a8c82fd1fbd3d9631c067195879f2297
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Nov 1 17:49:33 2016 -0600

    GomElement: Added unit tests and API additions
    
    Added to_string() to GomNode.
    
    Added write_node() to Parser and XParser.

 gxml/GomDocument.vala    |   14 +++--
 gxml/GomNode.vala        |    5 ++
 gxml/Parser.vala         |    4 +
 gxml/XParser.vala        |    2 +
 test/GXmlTest.vala       |    1 +
 test/GomElementTest.vala |  140 ++++++++++++++++++++++++++++++++++++++++++++++
 test/Makefile.am         |    1 +
 7 files changed, 161 insertions(+), 6 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 07fe6f8..d99b030 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -35,7 +35,7 @@ public class GXml.GomDocument : GomNode,
   protected string _compat_mode;
   protected string _character_set;
   protected string _content_type;
-  protected Parser parser;
+  protected Parser _parser;
   protected GXml.DomEvent _constructor;
   public DomImplementation implementation { get { return _implementation; } }
   public string url { get { return _url; } }
@@ -60,6 +60,8 @@ public class GXml.GomDocument : GomNode,
     }
   }
 
+  public GXml.Parser parser { get { return _parser; } set { _parser = value; } }
+
   construct {
     _local_name = "#document";
     _node_type = DomNode.NodeType.DOCUMENT_NODE;
@@ -68,7 +70,7 @@ public class GXml.GomDocument : GomNode,
     _compat_mode = "";
     _character_set = "utf-8";
     _content_type = "application/xml";
-    parser = new XParser (this);
+    _parser = new XParser (this);
   }
   public GomDocument () {}
   public GomDocument.from_path (string path) throws GLib.Error {
@@ -81,20 +83,20 @@ public class GXml.GomDocument : GomNode,
   }
 
   public GomDocument.from_file (GLib.File file) throws GLib.Error {
-    parser.read (file, null);
+    _parser.read (file, null);
   }
 
   public GomDocument.from_stream (GLib.InputStream stream) throws GLib.Error {
-    parser.read_stream (stream, null);
+    _parser.read_stream (stream, null);
   }
 
   public GomDocument.from_string (string str) throws GLib.Error {
-    parser.read_string (str, null);
+    _parser.read_string (str, null);
   }
 
 
   public string to_string () {
-    return parser.write_string ();
+    return _parser.write_string ();
   }
 
   public DomElement create_element (string local_name) throws GLib.Error {
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index c253404..a000dee 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -283,6 +283,11 @@ public class GXml.GomNode : Object,
   { return; } // FIXME:
   public bool dispatch_event (DomEvent event)
   { return false; } // FIXME:
+
+  // To String
+  public string to_string () {
+    return (_document as GomDocument).parser.write_node (this);
+  }
 }
 
 /**
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 4d16ca3..dd0e4f8 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -49,6 +49,10 @@ public interface GXml.Parser : Object {
    */
   public abstract void write_stream (OutputStream stream,
                                     GLib.Cancellable? cancellable) throws GLib.Error;
+  /**
+   * Writes a {@link GXml.DomNode} to a string
+   */
+  public abstract string write_node (DomNode node) throws GLib.Error;
 
   /**
    * Read a {@link GXml.DomDocument} from a {@link GLib.File}
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 6944a91..f1adb6d 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -41,6 +41,8 @@ public class GXml.XParser : Object, GXml.Parser {
   public string write_string () throws GLib.Error  {
     return dump ();
   }
+  public string write_node (DomNode node) throws GLib.Error { return ""; }
+
   public void read_string (string str, GLib.Cancellable? cancellable) throws GLib.Error {
     if (str == "")
       throw new ParserError.INVALID_DATA_ERROR (_("Invalid document string, is empty is not allowed"));
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 2f7c86a..c0305ce 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -62,6 +62,7 @@ class GXmlTest {
                DomGDocumentTest.add_tests ();
                XPathTest.add_tests ();
                GomDocumentTest.add_tests ();
+               GomElementTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/GomElementTest.vala b/test/GomElementTest.vala
new file mode 100644
index 0000000..627c8d3
--- /dev/null
+++ b/test/GomElementTest.vala
@@ -0,0 +1,140 @@
+/* -*- 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 GomElementTest : GXmlTest  {
+       public static void add_tests () {
+               Test.add_func ("/gxml/gom-element/namespace_uri", () => {
+                       try {
+                               GomDocument doc = new GomDocument.from_string ("<Potions><magic:Potion 
xmlns:magic=\"http://hogwarts.co.uk/magic\"; 
xmlns:products=\"http://diagonalley.co.uk/products\"/></Potions>");
+                               GXml.GomNode root = (GXml.GomNode) doc.document_element;
+                               assert (root != null);
+                               assert (root.node_name == "Potions");
+                               GXml.GomNode node = (GXml.GomNode) root.child_nodes[0];
+                               assert (node != null);
+                               assert (node.node_name == "Potion");
+                               assert ((node as DomElement).namespace_uri == "http://hogwarts.co.uk/magic";);
+                               assert ((node as DomElement).prefix == "magic");
+                               assert (node.lookup_prefix ("http://diagonalley.co.uk/products";) == 
"products");
+                               assert (node.lookup_namespace_uri ("products") == 
"http://diagonalley.co.uk/products";);
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gom-element/attributes", () => {
+                       try {
+                               GomDocument doc = new GomDocument.from_string ("<root />");
+                               assert (doc.document_element != null);
+                               GomElement elem = (GomElement) doc.create_element ("alphanumeric");
+                               doc.document_element.child_nodes.add (elem);
+                               assert (elem.attributes != null);
+                               assert (elem.attributes.size == 0);
+                               elem.set_attribute ("alley", "Diagon");
+                               elem.set_attribute ("train", "Hogwarts Express");
+                               assert (elem.attributes.size == 2);
+                               Test.message ("Getting attributes value alley... Node: "+doc.to_string ());
+                               assert (elem.attributes.get_named_item ("alley").node_value == "Diagon");
+                               assert (elem.attributes.get_named_item ("train").node_value == "Hogwarts 
Express");
+
+                               elem.set_attribute ("owl", "");
+                               GomAttr attr = elem.attributes.get_named_item ("owl") as GomAttr;
+                               assert (attr != null);
+                               attr.node_value = "Hedwig";
+
+                               assert (elem.attributes.size == 3);
+                               assert (elem.attributes.get_named_item ("owl").node_value == "Hedwig");
+
+                               elem.attributes.remove_named_item ("alley");
+                               assert (elem.attributes.get_named_item ("alley") == null);
+                               assert (elem.attributes.size == 2);
+                               elem.remove_attribute ("owl");
+                               assert (elem.attributes.size == 1);
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gom-element/to_string", () =>{
+                       try {
+                               GomDocument doc = new GomDocument.from_string ("<root />");
+                               var elem = doc.create_element ("country");
+                               var t = doc.create_text_node ("New Zealand");
+                               assert (t != null);
+                               elem.child_nodes.add (t);
+                               Test.message ("Elem1:"+(elem as GomNode).to_string ());
+                               assert ((elem as GomElement).to_string () == "<country>New 
Zealand</country>");
+                               var elem2 = doc.create_element ("messy");
+                               var t2 = doc.create_text_node ("&lt;<>&gt;");
+                               elem2.child_nodes.add (t2);
+                               Test.message ("Elem2:"+(elem2 as GomNode).to_string ());
+                               assert ((elem2 as GomNode).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/gom-element/content/add_aside_child_nodes", () =>{
+                       try {
+                               var doc = new GomDocument ();
+                               var root = (GomElement) doc.create_element ("root");
+                               doc.child_nodes.add (root);
+                               var n = (GomElement) doc.create_element ("child");
+                               root.child_nodes.add (n);
+                               var t = doc.create_text_node ("TEXT1");
+                               root.child_nodes.add (t);
+                               string s = doc.to_string ().split ("\n")[1];
+                               Test.message ("root="+root.to_string ());
+                               assert (s == "<root><child/>TEXT1</root>");
+                       } catch (GLib.Error e) {
+                               Test.message (e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/gom-element/content/keep_child_nodes", () =>{
+                       try {
+                               var doc = new GomDocument ();
+                               var root = (GomElement) doc.create_element ("root");
+                               doc.child_nodes.add (root);
+                               var n = (GomElement) doc.create_element ("child");
+                               root.child_nodes.add (n);
+                               var t = doc.create_text_node ("TEXT1") as DomText;
+                               root.child_nodes.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 ();
+                       }
+               });
+               Test.add_func ("/gxml/gom-element/parent", () => {
+                       var doc = new GomDocument.from_string ("<root><child/></root>");
+                       assert (doc.document_element != null);
+                       assert (doc.document_element.parent_node is GXml.DomNode);
+                       assert (doc.document_element.parent_node is GXml.DomDocument);
+                       assert (doc.document_element.child_nodes[0] != null);
+                       assert (doc.document_element.child_nodes[0].parent_node != null);
+                       assert (doc.document_element.child_nodes[0].parent_node.node_name == "root");
+               });
+       }
+}
diff --git a/test/Makefile.am b/test/Makefile.am
index 11993dd..0344d87 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -59,6 +59,7 @@ sources = \
        DomGDocumentTest.vala \
        XPathTest.vala \
        GomDocumentTest.vala \
+       GomElementTest.vala \
        $(NULL)
 
 vala-stamp: $(sources)


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