[gxml/serialization] Serializable Gee.TreeMap added more Test cases



commit 7b8abd852cc56dc7f109666b8b9e1ab6396d91f2
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 14 10:41:16 2013 -0600

    Serializable Gee.TreeMap added more Test cases
    
    * SerializableTreeMap now rize an error on unsupported type on deserialize
    * Added a test case (and example) on how to implement a class with a
      SerializableTreeMap property

 gxml/SerializableGeeTreeMap.vala     |    6 +-
 test/SerializableGeeTreeMapTest.vala |  129 +++++++++++++++++++++++++++++++---
 2 files changed, 123 insertions(+), 12 deletions(-)
---
diff --git a/gxml/SerializableGeeTreeMap.vala b/gxml/SerializableGeeTreeMap.vala
index 6ede0c9..db3e062 100644
--- a/gxml/SerializableGeeTreeMap.vala
+++ b/gxml/SerializableGeeTreeMap.vala
@@ -111,9 +111,11 @@ public class GXml.SerializableTreeMap<K,V> : Gee.TreeMap<K,V>, Serializable
   }
   public GXml.Node? default_deserialize (GXml.Node node)
                     throws GLib.Error
-                    requires (value_type.is_a (typeof (GXml.Serializable)))
-                    requires (value_type.is_a (typeof (SerializableMapId)))
   {
+    if (!(value_type.is_a (typeof (GXml.Serializable)) &&
+        value_type.is_a (typeof (SerializableMapId)))) {
+      throw new SerializableError.UNSUPPORTED_TYPE ("Value type '%s' is unsupported", value_type.name ());
+    }
     if (node is Element) {
       foreach (GXml.Node n in node.child_nodes) {
         var obj = Object.new (value_type);
diff --git a/test/SerializableGeeTreeMapTest.vala b/test/SerializableGeeTreeMapTest.vala
index 07bd614..fed64cb 100644
--- a/test/SerializableGeeTreeMapTest.vala
+++ b/test/SerializableGeeTreeMapTest.vala
@@ -1,16 +1,41 @@
 using GXml;
 using Gee;
 
-class Space : SerializableObjectModel, SerializableMapId<string>
-{
-  public string id () { return name; }
-  public string name { get; set; }
-  public Space.named (string name) { this.name = name; }
-  public override string to_string () { return name; }
-}
-
 class SerializableGeeTreeMapTest : GXmlTest
 {
+  class Space : SerializableObjectModel, SerializableMapId<string>
+  {
+    public string id () { return name; }
+    public string name { get; set; }
+    public Space.named (string name) { this.name = name; }
+    public override string node_name () { return "space"; }
+    public override string to_string () { return name; }
+  }
+
+  class SpaceContainer : SerializableObjectModel
+  {
+    public string owner { get; set; }
+    public SpaceCollection storage { get; set; }
+    public override string node_name () { return "spacecontainer"; }
+    public override string to_string () { return owner; }
+    public override GXml.Node? deserialize (GXml.Node node)
+                                    throws GLib.Error
+    {
+      Element element;
+      if (node is Document)
+        element = ((Document) node).document_element;
+      else
+        element = (Element) node;
+      if (element.has_child_nodes ()) {
+        if (storage == null)
+          storage = new SpaceCollection ();
+        storage.deserialize (element);
+      }
+      return default_deserialize (node);
+    }
+    public class SpaceCollection : SerializableTreeMap<string,Space> {}
+  }
+
   public static void add_tests ()
   {
     Test.add_func ("/gxml/serializable/serializable_tree_map/api",
@@ -81,11 +106,11 @@ class SerializableGeeTreeMapTest : GXmlTest
           }
         }
         if (!found1) {
-          stdout.printf (@"ERROR: Big space node is not found\n");
+          stdout.printf (@"ERROR: Big space node is not found\n$(doc)\n");
           assert_not_reached ();
         }
         if (!found2) {
-          stdout.printf (@"ERROR: Small space node is not found\n");
+          stdout.printf (@"ERROR: Small space node is not found\n$(doc)\n");
           assert_not_reached ();
         }
       }
@@ -125,5 +150,89 @@ class SerializableGeeTreeMapTest : GXmlTest
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/serializable_tree_map/container_class/deserialize",
+    () => {
+      try {
+        var doc = new Document.from_string ("""<?xml version="1.0"?>
+  <spacecontainer owner="Earth"><space name="Big"/><space name="Small"/></spacecontainer>""");
+        var c = new SpaceContainer ();
+        c.deserialize (doc);
+        if (c.owner != "Earth") {
+          stdout.printf (@"ERROR: owner must be 'Earth' got: $(c.owner)\n$(doc)\n");
+          assert_not_reached ();
+        }
+        if (c.storage.size != 2) {
+          stdout.printf (@"ERROR: Size must be 2 got: $(c.storage.size)\n$(doc)\n");
+          assert_not_reached ();
+        }
+        bool found1 = false;
+        bool found2 = false;
+        foreach (string k in c.storage.keys) {
+          if ((c storage  get (k)).name == "Big") found1 = true;
+          if ((c storage  get (k)).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 ();
+      }
+    });
+    Test.add_func ("/gxml/serializable/serializable_tree_map/containder_class/serialize",
+    () => {
+      try {
+        var c = new SpaceContainer ();
+        var o1 = new Space.named ("Big");
+        var o2 = new Space.named ("Small");
+        c.storage = new SpaceContainer.SpaceCollection ();
+        c.storage.set (o1.name, o1);
+        c.storage.set (o2.name, o2);
+        var doc = new Document ();
+        c.serialize (doc);
+        if (doc.document_element == null) {
+          stdout.printf (@"ERROR: doc have no root node\n$(doc)\n");
+          assert_not_reached ();
+        }
+        if (doc.document_element.node_name != "spacecontainer") {
+          stdout.printf (@"ERROR: bad doc root node's name: $(doc.document_element.node_name)\n$(doc)\n");
+          assert_not_reached ();
+        }
+        var root = doc.document_element;
+        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 == "space") {
+            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$(doc)\n");
+          assert_not_reached ();
+        }
+        if (!found2) {
+          stdout.printf (@"ERROR: Small space node is not found\n$(doc)\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]