[gxml] SerializableDouble implemented use of set_fraction()



commit 461d0a4e666012de5c28e9ed816c12ad1bde216f
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Sep 8 19:00:11 2016 -0500

    SerializableDouble implemented use of set_fraction()
    
    When serialize a SerializableDouble and set_fraction() is used
    any call to to_string() will generated a string representation
    using the required number of fractions.

 gxml/SerializableDouble.vala             |   11 +++++++----
 test/SerializablePropertyDoubleTest.vala |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)
---
diff --git a/gxml/SerializableDouble.vala b/gxml/SerializableDouble.vala
index 0fc386e..c452ee1 100644
--- a/gxml/SerializableDouble.vala
+++ b/gxml/SerializableDouble.vala
@@ -22,10 +22,12 @@
 
 using Gee;
 /**
- * Represent any boolean property to be added as a {@link GXml.Attribute} to a {@link GXml.Element}
+ * Represent any real double value property to be added as a
+ * {@link GXml.Attribute} to a {@link GXml.Element}
  *
- * This object can be used when the property could be removed from serialization, specially if missing
- * on XML have some missing and avoidable.
+ * This object can be used when the property could be removed from
+ * serialization, specially if missing on XML have some
+ * missing and avoidable.
  */
 public class GXml.SerializableDouble : Object, SerializableProperty
 {
@@ -45,7 +47,8 @@ public class GXml.SerializableDouble : Object, SerializableProperty
   public string get_serializable_property_value () { return _val; }
   public void set_serializable_property_value (string? val) { _val = val; }
   public string to_string () {
-    if (_val != null) return (double.parse (_val)).to_string ();
+    string f = "%."+_fraction.to_string ()+"f";
+    if (_val != null) return (f.printf(double.parse (_val)).to_string ());
     return "";
   }
   public string format (string f)
diff --git a/test/SerializablePropertyDoubleTest.vala b/test/SerializablePropertyDoubleTest.vala
index 6cffb4c..c21a1d6 100644
--- a/test/SerializablePropertyDoubleTest.vala
+++ b/test/SerializablePropertyDoubleTest.vala
@@ -135,5 +135,27 @@ class SerializablePropertyDoubleTest : GXmlTest {
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/Double/deserialize/fraction",
+    () => {
+      try {
+        var doc1 = new GDocument.from_string ("""<?xml version="1.0"?>
+                       <DoubleNode DoubleValue="3.02"/>""");
+        var d = new DoubleNode ();
+        d.deserialize (doc1);
+        assert (d.double_value != null);
+        d.double_value.set_fraction (2);
+        Test.message ("Actual value: "+d.double_value.get_serializable_property_value ());
+        assert (d.double_value.get_serializable_property_value () == "3.02");
+        Test.message ("Actual value parse: "+"%2.4f".printf (double.parse 
(d.double_value.get_serializable_property_value ())));
+        assert ("%2.2f".printf (double.parse (d.double_value.get_serializable_property_value ())) == "3.02");
+        GLib.message ("Value to string: "+d.double_value.to_string ());
+        assert ("3.02" == d.double_value.to_string ());
+        d.double_value.set_fraction (4);
+        assert ("3.0200" == d.double_value.to_string ());
+      } 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]