[gxml] Fixed stored string parsing and added unit tests for SerializableBool



commit 7249b5b6ed5101049d50481bbcf41c977e775c07
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Oct 22 10:43:46 2015 -0500

    Fixed stored string parsing and added unit tests for SerializableBool

 gxml/SerializableBool.vala             |   30 +++++++++++++++++++-----------
 gxml/SerializableProperty.vala         |    4 ++++
 test/SerializablePropertyBoolTest.vala |   26 ++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 11 deletions(-)
---
diff --git a/gxml/SerializableBool.vala b/gxml/SerializableBool.vala
index dc63996..17fb971 100644
--- a/gxml/SerializableBool.vala
+++ b/gxml/SerializableBool.vala
@@ -28,17 +28,25 @@ using Gee;
 public class GXml.SerializableBool : SerializableObjectModel, SerializableProperty
 {
   private string _val = null;
-  public bool get_value () { return bool.parse (_val); }
+  /**
+   * Parse the stored value, from the XML property, to a { link int}. This parsing
+   * may is different from the actual stored string. If can't be parsed to a valid
+   * boolean, this method will always return { link false}
+   */
+  public bool get_value () {
+    if (_val.down () == "true") return true;
+    if (_val.down () == "false") return false;
+    return false;
+  }
+  /**
+   * Given boolean value is parsed to string and then stored.
+   */
   public void set_value (bool val) { _val = val.to_string (); }
+  // SerializableProperty implementations
   public string get_serializable_property_value () { return _val; }
-  public void set_serializable_property_value (string? val) {
-    if (val == null)
-      _val = val;
-    else
-      _val = (bool.parse (val)).to_string ();
-  }
-  public override string to_string () {
-    if (_val != null) return (bool.parse (_val)).to_string ();
-    return false.to_string ();
-  }
+  public void set_serializable_property_value (string? val) { _val = val; }
+  /**
+   * Parse actual stored string to a boolean and returns the result. See { link get_value}
+   */
+  public override string to_string () { return get_value ().to_string (); }
 }
diff --git a/gxml/SerializableProperty.vala b/gxml/SerializableProperty.vala
index c29ff2f..865abff 100644
--- a/gxml/SerializableProperty.vala
+++ b/gxml/SerializableProperty.vala
@@ -34,6 +34,10 @@ using Gee;
  * 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.
+ *
+ * Implementations of { link GXml.SerializableProperty}, could be used to provide more flexibility
+ * when parsing { link GXml.Attr} properties values and to exclude to be serialized if they have not
+ * been created in the holding objects.
  */
 public interface GXml.SerializableProperty : Object, Serializable
 {
diff --git a/test/SerializablePropertyBoolTest.vala b/test/SerializablePropertyBoolTest.vala
index 53158e5..7f690b9 100644
--- a/test/SerializablePropertyBoolTest.vala
+++ b/test/SerializablePropertyBoolTest.vala
@@ -114,5 +114,31 @@ class SerializablePropertyBoolTest : GXmlTest {
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/Bool/deserialize/invalid",
+    () => {
+      try {
+        var doc1 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <BooleanNode boolean="c"/>""");
+        var b1 = new BoolNode ();
+        b1.deserialize (doc1);
+        assert (b1.boolean.get_serializable_property_value () == "c");
+        assert (b1.boolean.get_value () == false);
+        var doc2 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <BooleanNode boolean="TRUE"/>""");
+        var b2 = new BoolNode ();
+        b2.deserialize (doc2);
+        assert (b2.boolean.get_serializable_property_value () == "TRUE");
+        assert (b2.boolean.get_value () == true);
+        var doc3 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <BooleanNode boolean="FALSE"/>""");
+        var b3 = new BoolNode ();
+        b3.deserialize (doc3);
+        assert (b3.boolean.get_serializable_property_value () == "FALSE");
+        assert (b3.boolean.get_value () == false);
+      } 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]