[gxml/serialization] Added Unit Test for serialize Enumeration
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml/serialization] Added Unit Test for serialize Enumeration
- Date: Fri, 8 Nov 2013 00:04:13 +0000 (UTC)
commit d976efc34e485bc709711e5a0accc46084505e84
Author: Daniel Espinosa <esodan gmail com>
Date: Thu Nov 7 16:47:42 2013 -0600
Added Unit Test for serialize Enumeration
gxml/SerializableObjectModel.vala | 60 ++++++++++++++++++++++-----------
test/SerializableObjectModelTest.vala | 42 ++++++++++++++++++++++-
2 files changed, 80 insertions(+), 22 deletions(-)
---
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index afe34fe..ab7e571 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -139,20 +139,30 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
if (obj != null)
return obj.serialize (element);
}
- Value oval = Value (prop.value_type);
+ Value oval;
+ if (prop.value_type.is_a (Type.ENUM))
+ oval = Value (typeof (int));
+ else
+ oval = Value (prop.value_type);
get_property (prop.name, ref oval);
string val = "";
- if (!transform_to_string (oval, ref val)) {
- if (Value.type_transformable (prop.value_type, typeof (string)))
- {
- Value rval = Value (typeof (string));
- oval.transform (ref rval);
- val = rval.dup_string ();
- }
- else {
- Node node = null;
- this.serialize_unknown_property (element, prop, out node);
- return node;
+ if (prop.value_type.is_a (Type.ENUM)) {
+ val = Enumeration.get_nick_camelcase (prop.value_type, oval.get_int ());
+ }
+ else
+ {
+ if (!transform_to_string (oval, ref val)) {
+ if (Value.type_transformable (prop.value_type, typeof (string)))
+ {
+ Value rval = Value (typeof (string));
+ oval.transform (ref rval);
+ val = rval.dup_string ();
+ }
+ else {
+ Node node = null;
+ this.serialize_unknown_property (element, prop, out node);
+ return node;
+ }
}
}
string attr_name;
@@ -249,16 +259,26 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
return true;
}
else {
- Value val = Value (prop.value_type);
+ Value val;
+ if (prop.value_type == Type.ENUM)
+ val = Value (typeof (int));
+ else
+ val = Value (prop.value_type);
if (property_node is GXml.Attr)
{
- if (!transform_from_string (property_node.node_value, ref val)) {
- Value ptmp = Value (typeof (string));
- ptmp.set_string (property_node.node_value);
- if (Value.type_transformable (typeof (string), prop.value_type))
- ret = ptmp.transform (ref val);
- else
- ret = string_to_gvalue (property_node.node_value, ref val);
+ if (prop.value_type.is_a (Type.ENUM)) {
+ var env = Enumeration.parse (prop.value_type,
property_node.node_value);
+ val.set_enum (env.value);
+ }
+ else {
+ if (!transform_from_string (property_node.node_value, ref val)) {
+ Value ptmp = Value (typeof (string));
+ ptmp.set_string (property_node.node_value);
+ if (Value.type_transformable (typeof (string),
prop.value_type))
+ ret = ptmp.transform (ref val);
+ else
+ ret = string_to_gvalue (property_node.node_value, ref
val);
+ }
}
set_property (prop.name, val);
return ret;
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 5adeb5f..a46ad3f 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -268,7 +268,7 @@ public enum OptionsEnum
NORMAL_OPERATION
}
-class Options : Object
+class Options : ObjectModel
{
public string test { get; set; }
public OptionsEnum options { get; set; }
@@ -907,7 +907,6 @@ class SerializableObjectModelTest : GXmlTest
});
Test.add_func ("/gxml/serializable/object_model/enumeration",
() => {
- var doc = new Document ();
var e = new Options ();
try {
e.test = "t1";
@@ -953,6 +952,45 @@ class SerializableObjectModelTest : GXmlTest
stdout.printf (@"ERROR PARSING NormalOperation: $(e.message)");
assert_not_reached ();
}
+ var env = Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
+ Value v = Value (typeof (int));
+ v.set_int (env.value);
+ e.options = (OptionsEnum) v.get_int ();
+ if (e.options != OptionsEnum.NORMAL_OPERATION) {
+ stdout.printf (@"ERROR: setting NormalOperation: $(e.options)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/serializable/object_model/enumeration-serialize",
+ () => {
+ var doc = new Document ();
+ var options = new Options ();
+ options.options = OptionsEnum.NORMAL_OPERATION;
+ try {
+ options.serialize (doc);
+ if (doc.document_element == null) {
+ stdout.printf (@"ERROR: No root node found");
+ assert_not_reached ();
+ }
+ if (doc.document_element.node_name != "options") {
+ stdout.printf (@"ERROR: bad root name:\n$(doc)");
+ assert_not_reached ();
+ }
+ Element element = doc.document_element;
+ var op = element.get_attribute_node ("options");
+ if (op == null) {
+ stdout.printf (@"ERROR: attribute options not found:\n$(doc)");
+ assert_not_reached ();
+ }
+ if (op.node_value != "NormalOperation") {
+ stdout.printf (@"ERROR: attribute options value invalid:
$(op.node_value)\n$(doc)");
+ 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]