[gxml/serialization] Added Serializable Gee.ArrayList and Unit Tests



commit 1e9fa24e7f03e8cc0b25de5774ece095d6fe1f43
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Nov 13 18:54:33 2013 -0600

    Added Serializable Gee.ArrayList and Unit Tests

 gxml/Makefile.am                       |    1 +
 gxml/SerializableGeeArrayList.vala     |  137 ++++++++++++++++++++++++++++++++
 test/GXmlTest.vala                     |    1 +
 test/Makefile.am                       |    1 +
 test/SerializableGeeArrayListTest.vala |  115 ++++++++++++++++++++++++++
 5 files changed, 255 insertions(+), 0 deletions(-)
---
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index a691a97..25c2e20 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -69,6 +69,7 @@ libgxml_la_SOURCES = \
        SerializableMapId.vala \
        SerializableGeeDualKeyMap.vala \
        SerializableMapDualId.vala \
+       SerializableGeeArrayList.vala \
        $(NULL)
 
 gxml.vala.stamp: $(libgxml_la_SOURCES)
diff --git a/gxml/SerializableGeeArrayList.vala b/gxml/SerializableGeeArrayList.vala
new file mode 100644
index 0000000..36c547c
--- /dev/null
+++ b/gxml/SerializableGeeArrayList.vala
@@ -0,0 +1,137 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* SerializableGeeTreeModel.vala
+ *
+ * Copyright (C) 2013  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;
+using Gee;
+
+public class GXml.SerializableArrayList<G> : Gee.ArrayList<G>, Serializable
+{
+  protected ParamSpec[] properties { get; set; }
+  public GLib.HashTable<string,GLib.ParamSpec> ignored_serializable_properties { get; protected set; }
+  public string? serialized_xml_node_value { get; protected set; default=null; }
+  public GLib.HashTable<string,GXml.Node> unknown_serializable_property { get; protected set; }
+
+  public virtual bool property_use_nick () { return false; }
+
+  public virtual string node_name ()
+  {
+    return "";
+  }
+  public string default_node_name ()
+  {
+    return get_type().name().down();
+  }
+
+  public virtual GLib.ParamSpec? find_property_spec (string property_name)
+  {
+    return default_find_property_spec (property_name);
+  }
+
+  public virtual void init_properties ()
+  {
+    default_init_properties ();
+  }
+
+  public virtual GLib.ParamSpec[] list_serializable_properties ()
+  {
+    return default_list_serializable_properties ();
+  }
+
+  public virtual void get_property_value (GLib.ParamSpec spec, ref Value val)
+  {
+    default_get_property_value (spec, ref val);
+  }
+
+  public virtual void set_property_value (GLib.ParamSpec spec, GLib.Value val)
+  {
+    default_set_property_value (spec, val);
+  }
+
+  public virtual bool transform_from_string (string str, ref GLib.Value dest)
+  {
+    return false;
+  }
+
+  public virtual bool transform_to_string (GLib.Value val, ref string str)
+  {
+    return false;
+  }
+
+  public virtual GXml.Node? serialize (GXml.Node node)
+                              throws GLib.Error
+                              requires (node is Element)
+  {
+    return default_serialize (node);
+  }
+  public GXml.Node? default_serialize (GXml.Node node)
+                              throws GLib.Error
+                              requires (node is Element)
+  {
+    if (element_type.is_a (typeof (Serializable))) {
+      foreach (G e in this) {
+       ((GXml.Serializable) e).serialize (node);
+      }
+    }
+    return node;
+  }
+  public virtual GXml.Node? serialize_property (GXml.Element element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    return default_serialize_property (element, prop);
+  }
+  public GXml.Node? default_serialize_property (GXml.Element element,
+                                        GLib.ParamSpec prop)
+                                        throws GLib.Error
+  {
+    return element;
+  }
+  public virtual GXml.Node? deserialize (GXml.Node node)
+                                    throws GLib.Error
+                                    requires (node_name () != null)
+  {
+    return default_deserialize (node);
+  }
+  public GXml.Node? default_deserialize (GXml.Node node)
+                    throws GLib.Error
+                    requires (element_type.is_a (typeof (GXml.Serializable)))
+  {
+    if (node is Element) {
+      foreach (GXml.Node n in node.child_nodes) {
+        var obj = (Serializable) Object.new (element_type);
+        obj.deserialize (n);
+        add (obj);
+      }
+    }
+    return node;
+  }
+  public virtual bool deserialize_property (GXml.Node property_node)
+                                            throws GLib.Error
+  {
+    return default_deserialize_property (property_node);
+  }
+  public bool default_deserialize_property (GXml.Node property_node)
+                                            throws GLib.Error
+  {
+    return true;
+  }
+}
+
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 8811ca5..eac03f3 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -40,6 +40,7 @@ class GXmlTest {
                SerializableObjectModelTest.add_tests ();
                SerializableGeeTreeMapTest.add_tests ();
                SerializableGeeDualKeyMapTest.add_tests ();
+               SerializableGeeArrayListTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 4296024..b98600e 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -40,6 +40,7 @@ gxml_test_SOURCES = \
        SerializableObjectModelTest.vala \
        SerializableGeeTreeMapTest.vala \
        SerializableGeeDualKeyMapTest.vala \
+       SerializableGeeArrayListTest.vala \
        $(NULL)
 
 gxml_test.vala.stamp: $(gxml_test_SOURCES)
diff --git a/test/SerializableGeeArrayListTest.vala b/test/SerializableGeeArrayListTest.vala
new file mode 100644
index 0000000..6c95504
--- /dev/null
+++ b/test/SerializableGeeArrayListTest.vala
@@ -0,0 +1,115 @@
+using GXml;
+using Gee;
+
+class AElement : SerializableObjectModel
+{
+  public string name { get; set; }
+  public AElement.named (string name) { this.name = name; }
+  public override string to_string () { return name; }
+}
+
+class SerializableGeeArrayListTest : GXmlTest
+{
+  public static void add_tests ()
+  {
+    Test.add_func ("/gxml/serializable/serializable_array_list/api",
+    () => {
+      try {
+        var c = new SerializableArrayList<AElement> ();
+        var o1 = new AElement.named ("Big");
+        var o2 = new AElement.named ("Small");
+        c.add (o1);
+        c.add (o2);
+        bool found1 = false;
+        bool found2 = false;
+        foreach (AElement o in c) {
+          if (o.name == "Big") found1 = true;
+          if (o.name == "Small") found2 = true;
+        }
+        if (!found1) {
+          stdout.printf (@"Big is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"Small is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+      }
+    });
+    Test.add_func ("/gxml/serializable/serializable_array_list/serialize",
+    () => {
+      try {
+        var c = new SerializableArrayList<AElement> ();
+        var o1 = new AElement.named ("Big");
+        var o2 = new AElement.named ("Small");
+        c.add (o1);
+        c.add (o2);
+        var doc = new Document ();
+        var root = doc.create_element ("root");
+        doc.append_child (root);
+        c.serialize (root);
+        if (!root.has_child_nodes ()) {
+          stdout.printf (@"ERROR: root node have no childs $(doc)\n");
+          assert_not_reached ();
+        }
+        bool found1 = false;
+        bool found2 = false;
+        foreach (GXml.Node n in root.child_nodes) {
+          if (n is Element && n.node_name == "aelement") {
+            var name = ((Element) n).get_attribute_node ("name");
+            if (name != null) {
+              if (name.node_value == "Big") found1 = true;
+              if (name.node_value == "Small") found2 = true;
+            }
+          }
+        }
+        if (!found1) {
+          stdout.printf (@"ERROR: Big space node is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"ERROR: Small space node is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
+    Test.add_func ("/gxml/serializable/serializable_array_list/deserialize",
+    () => {
+      try {
+        var doc = new Document.from_string ("""<?xml version="1.0"?>
+  <root><aelement name="Big"/><aelement name="Small"/></root>""");
+        var c = new SerializableArrayList<AElement> ();
+        c.deserialize (doc.document_element);
+        if (c.size != 2) {
+          stdout.printf (@"ERROR: incorrect size must be 2 got: $(c.size)\n");
+          assert_not_reached ();
+        }
+        bool found1 = false;
+        bool found2 = false;
+        foreach (AElement o in c) {
+          if (o.name == "Big") found1 = true;
+          if (o.name == "Small") found2 = true;
+        }
+        if (!found1) {
+          stdout.printf (@"ERROR: Big key value is not found\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"ERROR: Small key value is not found\n");
+          assert_not_reached ();
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
+  }
+}


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