[gxml] Adding TwDocument tests: creation and root



commit 8d6ea4c1bcf94530c1b9084b62697d0c2b41f710
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue May 5 11:37:09 2015 -0500

    Adding TwDocument tests: creation and root
    
    * Fixes on GXml.TwNode, TwElement and TwDocument
    * Removing non-debug messages and invalid cast
    * Added TwDocumentTest suite

 configure.ac                |    1 +
 gxml/Document.vala          |    4 ++--
 gxml/TwAttribute.vala       |    2 --
 gxml/TwDocument.vala        |   22 ++++++++++++++++++++--
 gxml/TwElement.vala         |    4 ++--
 gxml/TwNode.vala            |    6 ++++--
 gxml/libxml-BackedNode.vala |    2 ++
 test/ElementTest.vala       |    2 +-
 test/GXmlTest.vala          |    1 +
 test/Makefile.am            |    2 ++
 10 files changed, 35 insertions(+), 11 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6434f24..cb5f720 100644
--- a/configure.ac
+++ b/configure.ac
@@ -287,6 +287,7 @@ test/test_invalid.xml
 test/test_out_path_expected.xml
 test/test_out_stream_expected.xml
 test/test_with_ns.xml
+test/tests-config.vala
 docs/Makefile
 docs/valadoc/Makefile
 docs/valadoc/devhelp/Makefile
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 17489cf..f943471 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -109,13 +109,13 @@ public interface GXml.Document : Object, GXml.Node
   /**
    * Save this { link GXml.Document} to { link GXml.Document.file}
    */
-  public abstract bool save (GLib.Cancellable? cancellable) throws GLib.Error;
+  public abstract bool save (GLib.Cancellable? cancellable = null) throws GLib.Error;
   /**
    * Save this { link GXml.Document} to given { link GLib.File}
    *
    * This overrides actual { link GXml.Document.file}
    */
-  public virtual bool save_as (GLib.File f, GLib.Cancellable? cancellable) throws GLib.Error
+  public virtual bool save_as (GLib.File f, GLib.Cancellable? cancellable = null) throws GLib.Error
   {
     if (f.query_exists ()) {
       f = file;
diff --git a/gxml/TwAttribute.vala b/gxml/TwAttribute.vala
index 7d6adb4..d97500d 100644
--- a/gxml/TwAttribute.vala
+++ b/gxml/TwAttribute.vala
@@ -23,8 +23,6 @@ using Gee;
 
 public class GXml.TwAttribute : GXml.TwNode, GXml.Attribute
 {
-  private string _name = null;
-  private string _value = null;
   public TwAttribute (GXml.Document d, string name, string value)
     requires (d is TwDocument)
   {
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 20eec46..3b07d04 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -58,8 +58,26 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     return t;
   }
   public GLib.File file { get; set; }
-  public GXml.Node root { get { return _root; } }
-  public bool save (GLib.Cancellable? cancellable)
+  public GXml.Node root {
+    get {
+      if (_root == null) {
+        int found = 0;
+        for (int i = 0; i < childs.size; i++) {
+          GXml.Node n = childs.get (i);
+          if (n is GXml.Element) {
+            found++;
+            if (found == 1)
+              _root = (GXml.Element) n;
+          }
+        }
+        if (found > 1) {
+          GLib.warning ("Document have more than one GXmlElement. Using first found");
+        }
+      }
+      return _root;
+    }
+  }
+  public bool save (GLib.Cancellable? cancellable = null)
   {
     var tw = new Xml.TextWriter.filename (file.get_path ());
     tw.start_document ();
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
index a400c73..1f7ad91 100644
--- a/gxml/TwElement.vala
+++ b/gxml/TwElement.vala
@@ -23,12 +23,12 @@ using Gee;
 
 public class GXml.TwElement : GXml.TwNode, GXml.Element
 {
-  private string _content = null;
   public TwElement (GXml.Document d, string name)
     requires (d is TwDocument)
   {
     _doc = d;
     ((TwDocument) document).tw = ((TwDocument) d).tw;
+    _name = name;
   }
   // GXml.Element
   public void set_attr (string name, string value)
@@ -39,6 +39,6 @@ public class GXml.TwElement : GXml.TwNode, GXml.Element
   public GXml.Node get_attr (string name) { return attrs.get (name); }
   public void normalize () {}
   public string content {
-    owned get { return _content; } set { } }
+    owned get { return _value; } set { _value = value; } }
   public string tag_name { get { return name; } }
 }
diff --git a/gxml/TwNode.vala b/gxml/TwNode.vala
index 03a37ab..b0480d0 100644
--- a/gxml/TwNode.vala
+++ b/gxml/TwNode.vala
@@ -25,6 +25,8 @@ public abstract class GXml.TwNode : Object, GXml.Node
 {
   Gee.HashMap<string,GXml.Node> _attrs = new Gee.HashMap<string,GXml.Node> ();
   Gee.ArrayList<GXml.Node> _childs = new Gee.ArrayList<GXml.Node> ();
+  protected string _name = null;
+  protected string _value = null;
   protected GXml.Document _doc;
   internal Xml.TextWriter *tw;
 
@@ -49,8 +51,8 @@ public abstract class GXml.TwNode : Object, GXml.Node
   public virtual Gee.Map<string,GXml.Node> attrs { get { return _attrs; } }
   public virtual Gee.BidirList<GXml.Node> childs { get { return _childs; } }
   public virtual GXml.Document document { get { return _doc; } }
-  public virtual string name { get { return ""; } }
+  public virtual string name { get { return _name; } }
   public virtual Gee.List<GXml.Namespace> namespaces { get { return document.namespaces; } }
   public virtual GXml.NodeType type_node { get { return GXml.NodeType.DOCUMENT; } }
-  public virtual string value { get { return ""; } set  {} }
+  public virtual string value { get { return _value; } set  { _value = value; } }
 }
diff --git a/gxml/libxml-BackedNode.vala b/gxml/libxml-BackedNode.vala
index e9177b7..e17d1dd 100644
--- a/gxml/libxml-BackedNode.vala
+++ b/gxml/libxml-BackedNode.vala
@@ -103,7 +103,9 @@ namespace GXml {
                 */
                public override bool set_namespace (string uri, string namespace_prefix)
                {
+#if DEBUG
                        GLib.message ("Setting a new Namespace...");
+#endif
                        if (node == null) return false;
                        Xml.Ns* ns = node->doc->search_ns_by_href (node, uri);
                        if (ns != null) {
diff --git a/test/ElementTest.vala b/test/ElementTest.vala
index f131305..3af36d6 100644
--- a/test/ElementTest.vala
+++ b/test/ElementTest.vala
@@ -445,7 +445,7 @@ class ElementTest : GXmlTest  {
                        var n = (xElement) doc.create_element ("child");
                        root.append_child (n);
                        // This will remove all child nodes
-                       var t = (xElement) doc.create_text_node ("TEXT1");
+                       var t = (xText) doc.create_text_node ("TEXT1");
                        root.append_child (t);
                        string d = """<?xml version="1.0"?>
 <root><child/>TEXT1</root>
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 197bb6d..bf21008 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -69,6 +69,7 @@ class GXmlTest {
                SerializableBasicTypeTest.add_tests ();
                SerializableEnumerationTest.add_tests ();
                Performance.add_tests ();
+               TwDocumentTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 0045ff6..bf504d1 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -25,6 +25,7 @@ noinst_PROGRAMS = $(TEST_PROGS)
 TEST_PROGS += gxml_test
 
 sources = \
+       tests-config.vala \
        AttrTest.vala \
        CharacterDataTest.vala \
        DocumentTest.vala \
@@ -46,6 +47,7 @@ sources = \
        SerializableGeeCollectionsTest.vala \
        SerializableBasicTypesTest.vala \
        gxml-performance.vala \
+       TwDocumentTest.vala \
        $(NULL)
 
 gxml_test.vala.stamp: $(sources)


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