[gxml/serialization] SerializableObjectModel: fixed use_nick property.



commit 47b1d68995b303731c6a75789aaa3bac0a3a15a3
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Oct 28 16:52:07 2013 -0600

    SerializableObjectModel: fixed use_nick property.
    
    * SerializableObjectModel.serializable_property_use_nick = false
      by default
    * Fixed when property use nick as GXml.Element property name
    * Added test case for property using nick

 gxml/SerializableObjectModel.vala     |   10 +++-
 test/SerializableObjectModelTest.vala |   85 +++++++++++++++++++++++++++++----
 test/SerializationTest.vala           |    2 +-
 3 files changed, 84 insertions(+), 13 deletions(-)
---
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 2b7f284..e1cdb97 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -32,7 +32,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
 
        public SerializableObjectModel ()
        {
-               serializable_property_use_nick = true;
+               serializable_property_use_nick = false;
                serialized_xml_node_value = null;
                serializable_node_name = get_type().name().down();
        }
@@ -104,7 +104,13 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                        Value rval = Value (typeof (string));
                        oval.transform (ref rval);
                        val = rval.dup_string ();
-                       string attr_name = prop.name.down ();
+                       string attr_name;
+                       if (serializable_property_use_nick &&
+                           prop.get_nick () != null &&
+                           prop.get_nick () != "")
+                               attr_name = prop.get_nick ();
+                       else
+                               attr_name = prop.get_name ();
                        var attr = element.get_attribute_node (attr_name);
                        if (attr == null) {
                                //GLib.message (@"New Attr to add... $(attr_name)");
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 35532bb..b2cffb9 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -21,7 +21,7 @@ const string XML_PACKAGE_FILE =
 <tag>Printer</tag><tag>Partner</tag><tag>Support</tag>
 </PACKAGE>""";
 
-const string XML_PACKAGE_UNKNOWN_FILE =
+const string XML_PACKAGE_UNKNOWN_NODES_FILE =
 """<?xml version="1.0"?>
 <PACKAGE source="Mexico/North" destiny="Brazil" Hope="2/4.04">
   <manual document="Sales Card" pages="1">Selling Card Specification</manual>
@@ -100,6 +100,7 @@ public class Package : ObjectModel
 
        public Package ()
        {
+               serializable_property_use_nick = true;
                computer = new Computer ();
                manual = new Manual ();
                source = "Mexico";
@@ -123,16 +124,13 @@ public class Package : ObjectModel
                });
                ((Serializable) this).deserialize_unknown_property.connect ( (element, prop) => {
                        //GLib.message (@"Deserializing Unknown Property: $(prop.name) | $(prop.get_nick 
())");
-                       if (prop.get_nick () == "tag")
-                       {
-                               try {
-                                       if (element.node_name == "tag") {
-                                                       tags.append_val (((Element) element).content);
-                                       }
-                               } catch (GLib.Error e) {
-                                       GLib.message (e.message);
-                                       assert_not_reached ();
+                       try {
+                               if (element.node_name == "tag") {
+                                               tags.append_val (((Element) element).content);
                                }
+                       } catch (GLib.Error e) {
+                               GLib.message (e.message);
+                               assert_not_reached ();
                        }
                });
                //GLib.message ("PACKAGE: Properties.");
@@ -153,6 +151,20 @@ public class Package : ObjectModel
        }
 }
 
+public class Monitor : ObjectModel
+{
+       public string resolution { get; set; }
+       [Description (nick="AcPower")]
+       public int ac_power { get; set; }
+       [Description (nick="DcPower")]
+       public int dc_power { get; set; }
+       public Monitor ()
+       {
+               serializable_property_use_nick = true;
+       }
+}
+
+
 class SerializableObjectModelTest : GXmlTest
 {
        public static void add_tests ()
@@ -430,6 +442,59 @@ class SerializableObjectModelTest : GXmlTest
                        }
                }
                );
+               Test.add_func ("/gxml/serializable/object_model/serialize_property_nick",
+               () => {
+                       var doc = new Document ();
+                       var monitor = new Monitor ();
+                       try {
+                               monitor.resolution = "1204x720";
+                               monitor.ac_power = 120;
+                               monitor.dc_power = 125;
+                               monitor.serialize (doc);
+                               //stdout.printf (@"DOC: [$(doc)]");
+                               if (doc.document_element == null) {
+                                       stdout.printf ("ERROR MONITOR: No root Element");
+                                       assert_not_reached ();
+                               }
+                               Element element = doc.document_element;
+                               if (element.node_name != "monitor") {
+                                       stdout.printf (@"ERROR MONITOR: root Element $(element.node_name)");
+                                       assert_not_reached ();
+                               }
+                               var ac = element.get_attribute_node ("AcPower");
+                               if (ac == null) {
+                                       stdout.printf (@"ERROR MONITOR: attribute AcPower not found");
+                                       assert_not_reached ();
+                               }
+                               if (ac.node_value != "120") {
+                                       stdout.printf (@"ERROR MONITOR: AcPower value $(ac.node_value)");
+                                       assert_not_reached ();
+                               }
+                               var dc = element.get_attribute_node ("DcPower");
+                               if (dc == null) {
+                                       stdout.printf (@"ERROR MONITOR: attribute DcPower not found");
+                                       assert_not_reached ();
+                               }
+                               if (dc.node_value != "125") {
+                                       stdout.printf (@"ERROR MONITOR: AcPower value $(dc.node_value)");
+                                       assert_not_reached ();
+                               }
+                               var r = element.get_attribute_node ("resolution");
+                               if (r == null) {
+                                       stdout.printf (@"ERROR MONITOR: attribute resolution not found");
+                                       assert_not_reached ();
+                               }
+                               if (r.node_value != "1204x720") {
+                                       stdout.printf (@"ERROR MONITOR: resolution value $(r.node_value)");
+                                       assert_not_reached ();
+                               }
+                       }
+                       catch (GLib.Error e) {
+                               stdout.printf (@"Error: $(e.message)");
+                               assert_not_reached ();
+                       }
+               }
+               );
        }
        static void serialize_manual_check (Element element, Manual manual)
        {
diff --git a/test/SerializationTest.vala b/test/SerializationTest.vala
index 929db95..747a774 100644
--- a/test/SerializationTest.vala
+++ b/test/SerializationTest.vala
@@ -371,7 +371,7 @@ class SerializationTest : GXmlTest {
                                        Test.message ("Expected SerializationError.UNKNOWN_PROPERTY to be 
thrown for property 'badname' in object 'Fruit' :(  Did not happen.");
                                        assert_not_reached ();
                                } catch (GLib.Error e) {
-                                       stdout.printf (@"Cought Error: $(e.message)");
+                                       //stdout.printf (@"Cought Error: $(e.message)");
                                        if (e is GXml.SerializationError.UNKNOWN_PROPERTY) {
                                                // pass
                                        } else {


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