[gxml] Added documentation to SerializableProperty and unit tests to SerializableInt



commit e0f0aa12317d52344970bf6aeb666c94b5e66ad1
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Oct 22 10:04:40 2015 -0500

    Added documentation to SerializableProperty and unit tests to SerializableInt
    
    * Documentation to SerializableInt
    * Removed SerializableInt.to_string, use SerializableInt.get_value().to_string()
      instead
    * Unit tests for SerializableInt covers deserialization

 gxml/SerializableInt.vala             |   19 ++++++++----
 gxml/SerializableProperty.vala        |   11 ++++++-
 test/SerializablePropertyIntTest.vala |   50 +++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 7 deletions(-)
---
diff --git a/gxml/SerializableInt.vala b/gxml/SerializableInt.vala
index 4244f57..9a4b121 100644
--- a/gxml/SerializableInt.vala
+++ b/gxml/SerializableInt.vala
@@ -22,15 +22,22 @@
 
 using Gee;
 /**
- * Represent any boolean property to be added as a { link GXml.Attr} to a { link GXml.Element}
- *
+ * Represent any boolean property to be added as a { link GXml.Attr} to a { link GXml.Element} 
  */
 public class GXml.SerializableInt : GXml.SerializableDouble
 {
+  /**
+   * Parse the stored value, from the XML property, to a { link int}. This parsing
+   * may is different from the actual stored string.
+   *
+   * The stored value, is parsed using { lilnk GLib.ascii_strtod} and then
+   * casted to an integer before return, this make flexible on stored values
+   * in XML and parsed without errors, but they could defere from the value
+   * returned by this method.
+   */
   public new int get_value () { return (int) double.parse (_val); }
+  /**
+   * Given integer is parsed to string and then stored.
+   */
   public new void set_value (int val) { _val = val.to_string (); }
-  public override string to_string () {
-    if (_val != null) return ((int) double.parse (_val)).to_string ();
-    return "";
-  }
 }
\ No newline at end of file
diff --git a/gxml/SerializableProperty.vala b/gxml/SerializableProperty.vala
index 128301d..c29ff2f 100644
--- a/gxml/SerializableProperty.vala
+++ b/gxml/SerializableProperty.vala
@@ -29,6 +29,11 @@ using Gee;
  * { link GXml.Serializable.serialize_property} instead of { link GXml.Serializable.serialize}
  * to fill automatically { link GXml.SerializableProperty.serializable_property_name} and
  * { link GXml.SerializableProperty.serializable_property_value}.
+ *
+ * The actual value stored and returned by { link GXml.SerializableProperty.get_serializable_property_value}
+ * is the actual string in the XML property, this means may the value could differ from the spected value
+ * on some implementations like { link GXml.SerializableInt}. Take a look in each implementations about
+ * retured values.
  */
 public interface GXml.SerializableProperty : Object, Serializable
 {
@@ -40,7 +45,11 @@ public interface GXml.SerializableProperty : Object, Serializable
   * Set value to be set to a { link GXml.Attr}, to be added to a { link GXml.Element}
   *
   * If value is set to @null then the property will be ignored by default and no
-  * property will be set to given { link GXml.Element}
+  * property will be set to given { link GXml.Element}.
+  *
+  * Some implementations stores the value without any convertion at all; then if the value,
+  * from XML property, makes no sense for the property type, you should take care
+  * to use the provided API from them to convert it.
   */
   public abstract void set_serializable_property_value (string? val);
   /**
diff --git a/test/SerializablePropertyIntTest.vala b/test/SerializablePropertyIntTest.vala
index b617413..c837e3c 100644
--- a/test/SerializablePropertyIntTest.vala
+++ b/test/SerializablePropertyIntTest.vala
@@ -97,5 +97,55 @@ class SerializablePropertyIntTest : GXmlTest {
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/Int/deserialize",
+    () => {
+      try {
+        var doc1 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <IntNode IntegerValue="1416"/>""");
+        var i = new IntNode ();
+        i.deserialize (doc1);
+        Test.message ("Actual value: "+i.integer.get_serializable_property_value ());
+        assert (i.integer.get_serializable_property_value () == "1416");
+        Test.message ("Actual value parse: "+i.integer.get_value ().to_string ());
+        int iv = i.integer.get_value ();
+        Test.message ("Use from int, value:"+iv.to_string ());
+        assert (i.integer.get_value () == 1416);
+      } catch (GLib.Error e) {
+        Test.message (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
+    Test.add_func ("/gxml/serializable/Int/deserialize/bad-value",
+    () => {
+      try {
+        var doc1 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <IntNode IntegerValue="a"/>""");
+        var i1 = new IntNode ();
+        i1.deserialize (doc1);
+        Test.message ("Actual value: "+i1.integer.get_serializable_property_value ());
+        assert (i1.integer.get_serializable_property_value () == "a");
+        Test.message ("Actual value parse: "+i1.integer.get_value ().to_string ());
+        assert (i1.integer.get_value () == 0);
+        var doc2 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <IntNode IntegerValue="1.1"/>""");
+        var i2 = new IntNode ();
+        i2.deserialize (doc2);
+        Test.message ("Actual value: "+i2.integer.get_serializable_property_value ());
+        assert (i2.integer.get_serializable_property_value () == "1.1");
+        Test.message ("Actual value parse: "+i2.integer.get_value ().to_string ());
+        assert (i2.integer.get_value () == 1);
+        var doc3 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <IntNode IntegerValue="1a"/>""");
+        var i3 = new IntNode ();
+        i3.deserialize (doc3);
+        Test.message ("Actual value: "+i3.integer.get_serializable_property_value ());
+        assert (i3.integer.get_serializable_property_value () == "1a");
+        Test.message ("Actual value parse: "+i3.integer.get_value ().to_string ());
+        assert (i3.integer.get_value () == 1);
+      } catch (GLib.Error e) {
+        Test.message (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
   }
 }


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