[gxml/serialization] Added Unit Test for deserialize Enumeration



commit ff9df581d879b58ff868cf33499085279a3742ed
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 7 17:57:53 2013 -0600

    Added Unit Test for deserialize Enumeration
    
    * Fixed use of camelcase representation of a enum property in a
      GXml.Attr
    * Fixed: no serialize enumeration with invalid value
    * Fixed: no deseralize enumeration with invalid GXml.Attr value
    * Changed SerializableEnumError to EnumerationError and INVALID_NAME to
      to INVALID_TEXT

 gxml/Enumeration.vala                 |   13 ++++--
 gxml/SerializableObjectModel.vala     |   12 ++++-
 test/SerializableObjectModelTest.vala |   75 +++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 8 deletions(-)
---
diff --git a/gxml/Enumeration.vala b/gxml/Enumeration.vala
index 3188a0b..6c292f8 100644
--- a/gxml/Enumeration.vala
+++ b/gxml/Enumeration.vala
@@ -71,7 +71,7 @@ namespace GXml {
                        EnumClass enumc = (EnumClass) enumeration.class_ref ();
                        EnumValue? enumv = enumc.get_value (val);
                        if (enumv == null)
-                               throw new SerializableEnumError.INVALID_VALUE ("value is invalid");
+                               throw new EnumerationError.INVALID_VALUE ("value is invalid");
                        if (use_nick && enumv.value_nick != null)
                                return enumv.value_nick;
                        if (camelcase && enumv.value_nick != null) {
@@ -105,11 +105,14 @@ namespace GXml {
                                        enumv = ev;
                                if (val == ev.value_nick)
                                        enumv = ev;
-                               if (val == get_nick_camelcase (enumeration, ev.value))
+                               string nick = get_nick_camelcase (enumeration, ev.value);
+                               if (val == nick)
+                                       enumv = ev;
+                               if (val.down () == nick.down ())
                                        enumv = ev;
                        }
                        if (enumv == null)
-                               throw new SerializableEnumError.INVALID_NAME ("text is not valid");
+                               throw new EnumerationError.INVALID_TEXT ("text is not valid");
                        return enumv;
                }
                /**
@@ -129,7 +132,7 @@ namespace GXml {
                        return enumc.values;
                }
        }
-       public errordomain SerializableEnumError
+       public errordomain EnumerationError
        {
                /**
                 * Given value is invalid in enumeration, when transform to string.
@@ -138,6 +141,6 @@ namespace GXml {
                /**
                 * Given text to transform to an enumeration's value.
                 */
-               INVALID_NAME
+               INVALID_TEXT
        }
 }
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index ab7e571..177ead1 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -147,7 +147,9 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                get_property (prop.name, ref oval);
                string val = "";
                if (prop.value_type.is_a (Type.ENUM)) {
-                       val = Enumeration.get_nick_camelcase (prop.value_type, oval.get_int ());
+                       try {
+                               val = Enumeration.get_nick_camelcase (prop.value_type, oval.get_int ());
+                       } catch (EnumerationError e) { val = null; }
                }
                else
                {
@@ -267,8 +269,12 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
                        if (property_node is GXml.Attr)
                        {
                                if (prop.value_type.is_a (Type.ENUM)) {
-                                       var env = Enumeration.parse (prop.value_type, 
property_node.node_value);
-                                       val.set_enum (env.value);
+                                       EnumValue env;
+                                       try {
+                                               env = Enumeration.parse (prop.value_type, 
property_node.node_value);
+                                               val.set_enum (env.value);
+                                       }
+                                       catch (EnumerationError e) {}
                                }
                                else {
                                        if (!transform_from_string (property_node.node_value, ref val)) {
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index a46ad3f..5eeda02 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -991,6 +991,81 @@ class SerializableObjectModelTest : GXmlTest
                                        stdout.printf (@"ERROR: attribute options value invalid: 
$(op.node_value)\n$(doc)");
                                        assert_not_reached ();
                                }
+                               options.options = (OptionsEnum) (-1); // invaliding this property. Avoids 
serialize it.
+                               var doc2 = new Document ();
+                               options.serialize (doc2);
+                               var opts = doc2.document_element.get_attribute_node ("options");
+                               if (opts != null) {
+                                       stdout.printf (@"ERROR: attribute options must not be 
present:\n$(doc)");
+                                       assert_not_reached ();
+                               }
+                       }
+                       catch (GLib.Error e) {
+                               stdout.printf (@"Error: $(e.message)");
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/serializable/object_model/enumeration-deserialize",
+               () => {
+                       var options = new Options ();
+                       try {
+                               var doc = new Document.from_string ("""<?xml version="1.0"?>
+<options options="NormalOperation"/>""");
+                               options.deserialize (doc);
+                               if (options.options != OptionsEnum.NORMAL_OPERATION)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc)");
+                                       assert_not_reached ();
+                               }
+                               var doc2 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="normal-operation"/>""");
+                               options.deserialize (doc2);
+                               if (options.options != OptionsEnum.NORMAL_OPERATION)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc2)");
+                                       assert_not_reached ();
+                               }
+                               var doc3 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="selectbefore"/>""");
+                               options.deserialize (doc3);
+                               if (options.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc3)");
+                                       assert_not_reached ();
+                               }
+                               var doc4 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="OPTIONS_ENUM_SelectBefore"/>""");
+                               options.deserialize (doc4);
+                               if (options.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc4)");
+                                       assert_not_reached ();
+                               }
+                               var doc5 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="SelectBefore"/>""");
+                               options.deserialize (doc5);
+                               if (options.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc5)");
+                                       assert_not_reached ();
+                               }
+                               var doc6 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="SELECTBEFORE"/>""");
+                               options.deserialize (doc6);
+                               if (options.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc6)");
+                                       assert_not_reached ();
+                               }
+                               var doc7 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="NORMAL_OPERATION"/>""");
+                               options.deserialize (doc7);
+                               if (options.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(options.options)\n$(doc7)");
+                                       assert_not_reached ();
+                               }
+                               var op2 = new Options ();
+                               var doc8 = new Document.from_string ("""<?xml version="1.0"?>
+<options options="INVALID"/>""");
+                               op2.deserialize (doc8);
+                               if (op2.options != OptionsEnum.SelectBefore)  {
+                                       stdout.printf (@"ERROR: Bad value to options property: 
$(op2.options)\n$(doc8)");
+                                       assert_not_reached ();
+                               }
                        }
                        catch (GLib.Error e) {
                                stdout.printf (@"Error: $(e.message)");


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