[gxml/serialization] Added Test cases and parse with options for Document



commit 4751274858c6e99839a42db36431a61ccf856e7b
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Dec 4 23:40:06 2013 -0600

    Added Test cases and parse with options for Document

 gxml/BackedNode.vala                   |    3 +-
 gxml/Document.vala                     |   25 ++++++++---
 test/DocumentTest.vala                 |    2 +-
 test/ElementTest.vala                  |    6 ++-
 test/SerializableGeeArrayListTest.vala |   51 ++++++++++++++++++++++
 test/SerializableGeeHashMapTest.vala   |   74 ++++++++++++++++++++++++++++++++
 test/SerializableObjectModelTest.vala  |    2 +-
 7 files changed, 151 insertions(+), 12 deletions(-)
---
diff --git a/gxml/BackedNode.vala b/gxml/BackedNode.vala
index 95e5c7f..e0a4cf0 100644
--- a/gxml/BackedNode.vala
+++ b/gxml/BackedNode.vala
@@ -308,7 +308,7 @@ namespace GXml {
                        // TODO: add a better test, as we weren't realising this was just a stub; test for 
memory usage too
                }
 
-               public override string to_string (bool format = false, int level = 0) {
+               public override string to_string (bool format = true, int level = 0) {
                        /* TODO: change from xmlNodeDump and xmlBuffer
                         * to xmlBuf and xmlBufNodeDump; the former
                         * are 'somehow deprecated' (see xmlNodeDump
@@ -333,7 +333,6 @@ namespace GXml {
                        buffer = new Xml.Buffer ();
                        buffer->node_dump (this.owner_document.xmldoc, this.node, level, format ? 1 : 0);
                        str = buffer->content ();
-
                        return str;
                }
        }
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 62ddcbd..78ee005 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -497,14 +497,27 @@ namespace GXml {
                 */
                public Document.from_string (string xml) {
                        Xml.Doc *doc;
-
-                       /* TODO: consider breaking API to support
-                        * xmlParserOptions, encoding, and base URL
-                        * from xmlReadMemory */
-
                        doc = Xml.Parser.parse_memory (xml, (int)xml.length);
                        this.from_libxml2 (doc);
                }
+               /**
+                * Creates a Document from data found in memory using options.
+                *
+                * @xml: A string representing an XML document
+                * @url: the base URL to use for the document
+                * @encoding: the document encoding
+                * @options: a combination of { link Xml.ParserOption}
+                *
+                * @return A new { link GXml.Document} from `memory`; this must be freed with { link 
GLib.Object.unref}
+                */
+               public Document.from_string_with_options (string xml, string? url = null,
+                                                         string? encoding = null,
+                                                         int options = 0)
+               {
+                 Xml.Doc *doc;
+                 doc = Xml.Parser.read_memory (xml, (int)xml.length, url, encoding, options);
+                 this.from_libxml2 (doc);
+               }
 
                /**
                 * Creates an empty document.
@@ -912,7 +925,7 @@ namespace GXml {
                /**
                 * { inheritDoc}
                 */
-               public override string to_string (bool format = false, int level = 0) {
+               public override string to_string (bool format = true, int level = 0) {
                        string str;
                        int len;
 
diff --git a/test/DocumentTest.vala b/test/DocumentTest.vala
index a43cc8c..90eae7b 100644
--- a/test/DocumentTest.vala
+++ b/test/DocumentTest.vala
@@ -200,7 +200,7 @@ class DocumentTest : GXmlTest {
 
                                        doc.save_to_stream (outstream);
                                        assert_not_reached ();
-                               } catch (GXml.Error e) {
+                               } catch (GLib.Error e) {
                                        assert (e is GXml.Error.WRITER);
                                }
                                test_error (DomException.X_OTHER);
diff --git a/test/ElementTest.vala b/test/ElementTest.vala
index a1b27b7..0ad3939 100644
--- a/test/ElementTest.vala
+++ b/test/ElementTest.vala
@@ -369,8 +369,10 @@ class ElementTest : GXmlTest  {
                                Document doc, doc2;
                                Element elem = get_elem_new_doc ("country", out doc);
                                elem.append_child (elem.owner_document.create_text_node ("New Zealand"));
-                               assert (elem.to_string () == "<country>New Zealand</country>");
-
+                               if (elem.to_string () != "<country>New Zealand</country>") {
+                                 stdout.printf (@"ERROR: Element to_string() fail. Expected <country>New 
Zealand</country> got: $(elem.to_string ())\n");
+                                 assert_not_reached ();
+                               }
                                // during stringification, we don't want to confuse XML <> with text <>
                                Element elem2 = get_elem_new_doc ("messy", out doc2);
                                elem2.append_child (elem.owner_document.create_text_node ("&lt;<>&gt;"));
diff --git a/test/SerializableGeeArrayListTest.vala b/test/SerializableGeeArrayListTest.vala
index 6c95504..ee2f85c 100644
--- a/test/SerializableGeeArrayListTest.vala
+++ b/test/SerializableGeeArrayListTest.vala
@@ -111,5 +111,56 @@ class SerializableGeeArrayListTest : GXmlTest
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/serializable_array_list/deserialize-serialize",
+    () => {
+      try {
+        var idoc = new Document.from_string ("""<?xml version="1.0"?>
+    <root>
+      <aelement name="Big"/>
+      <aelement name="Small"/>
+      <aelement name="Wall">FAKE1</aelement>
+    </root>""");
+        var iroot = idoc.document_element;
+        var ic = new SerializableArrayList<AElement> ();
+        ic.deserialize (iroot);
+        var doc = new Document.from_string ("""<?xml version="1.0"?><root />""");
+        var root = doc.document_element;
+        ic.serialize (root);
+        var c = new SerializableArrayList<AElement> ();
+        c.deserialize (root);
+        if (c.size != 3) {
+          stdout.printf (@"ERROR: incorrect counted. Expected 3, got $(c.size)");
+          assert_not_reached ();
+        }
+        int i = 0;
+        foreach (AElement e in c)
+          i++;
+        if (i != 3) {
+          stdout.printf (@"ERROR: incorrect counted. Expected 3, got $i");
+          assert_not_reached ();
+        }
+        string[] s = {"Big","Small","Wall"};
+        for (int j = 0; j < c.size; j++) {
+          var e = c.get (j);
+          if (s[j] != e.name) {
+            stdout.printf (@"ERROR: incorrect name. Expected $(s[j]), got: $(c.get (j))");
+            assert_not_reached ();
+          }
+          if (e.name == "Wall") {
+            if (e.serialized_xml_node_value != "FAKE1") {
+              string nc = "";
+              if (e.serialized_xml_node_value != null)
+                nc = e.serialized_xml_node_value;
+              stdout.printf (@"ERROR: incorrect node content. Expected 'FAKE1', got: $(nc)");
+              assert_not_reached ();
+            }
+          }
+        }
+      }
+      catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
   }
 }
diff --git a/test/SerializableGeeHashMapTest.vala b/test/SerializableGeeHashMapTest.vala
index 1631a17..b6e9a97 100644
--- a/test/SerializableGeeHashMapTest.vala
+++ b/test/SerializableGeeHashMapTest.vala
@@ -8,6 +8,8 @@ class SerializableGeeHashMapTest : GXmlTest
     public string get_map_key () { return name; }
     public string name { get; set; }
     public Space.named (string name) { this.name = name; }
+    public void set_value (string str) { serialized_xml_node_value = str; }
+    public string get_value () { return serialized_xml_node_value; }
     public override string node_name () { return "space"; }
     public override string to_string () { return name; }
   }
@@ -83,7 +85,9 @@ class SerializableGeeHashMapTest : GXmlTest
       try {
         var c = new SerializableHashMap<string,Space> ();
         var o1 = new Space.named ("Big");
+        o1.set_value ("FAKE TEXT");
         var o2 = new Space.named ("Small");
+        o2.set_value ("FAKE TEXT");
         c.set (o1.name, o1);
         c.set (o2.name, o2);
         var doc = new Document ();
@@ -103,6 +107,16 @@ class SerializableGeeHashMapTest : GXmlTest
               if (name.node_value == "Big") found1 = true;
               if (name.node_value == "Small") found2 = true;
             }
+            if (n.has_child_nodes ()) {
+              foreach (GXml.Node nd in n.child_nodes) {
+                if (nd is Text) {
+                  if (nd.node_value != "FAKE TEXT") {
+                    stdout.printf (@"ERROR: node content don't much. Expected 'FAKE TEXT', got: 
$(nd.node_value)\n$(doc)\n");
+                    assert_not_reached ();
+                  }
+                }
+              }
+            }
           }
         }
         if (!found1) {
@@ -234,5 +248,65 @@ class SerializableGeeHashMapTest : GXmlTest
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/serializable_hash_map/deserialize-serialize",
+    () => {
+      try {
+        var idoc = new Document.from_string ("""<?xml version="1.0"?>
+    <spacecontainer owner="Earth">
+      <space name="Big"/>
+      <space name="Big">FAKE1</space>
+      <space name="Small"/>
+      <space name="Yum1">FAKE2</space>
+      <space name="Yum2">FAKE3</space>
+      <space name="Yum3">FAKE4</space>
+      <space name="Yum3">FAKE5</space>
+    </spacecontainer>""");
+        var isc = new SpaceContainer ();
+        isc.deserialize (idoc);
+        var doc = new Document ();
+        isc.serialize (doc);
+        var sc = new SpaceContainer ();
+        sc.deserialize (doc);
+        if (sc.storage.size != 5) {
+          stdout.printf (@"ERROR: wrong storage size. Expected 5, got: $(sc.storage.size)");
+          assert_not_reached ();
+        }
+        int i = 0;
+        foreach (Space s in sc.storage.values)
+          i++;
+        if (i != 5) {
+          stdout.printf (@"ERROR: wrong storage size counted. Expected 5, got: $(sc.storage.size)");
+          assert_not_reached ();
+        }
+        var s1 = sc.storage.get ("Big");
+        if (s1.get_value () != "FAKE1") {
+          stdout.printf (@"ERROR: wrong s1 text. Expected 'FAKE1', got: $(s1.get_value ())");
+          assert_not_reached ();
+        }
+        var s2 = sc.storage.get ("Small");
+        if (s2.get_value () != null) {
+          stdout.printf (@"ERROR: wrong s2 text. Expected '', got: $(s2.get_value ())");
+          assert_not_reached ();
+        }
+        var s3 = sc.storage.get ("Yum1");
+        if (s3.get_value () != "FAKE2") {
+          stdout.printf (@"ERROR: wrong s3 text. Expected 'FAKE2', got: $(s3.get_value ())");
+          assert_not_reached ();
+        }
+        var s4 = sc.storage.get ("Yum2");
+        if (s4.get_value () != "FAKE3") {
+          stdout.printf (@"ERROR: wrong s4 text. Expected 'FAKE3', got: $(s4.get_value ())");
+          assert_not_reached ();
+        }
+        var s5 = sc.storage.get ("Yum3");
+        if (s5.get_value () != "FAKE5") {
+          stdout.printf (@"ERROR: wrong s5 text. Expected 'FAKE5', got: $(s5.get_value ())");
+          assert_not_reached ();
+        }
+      } catch (GLib.Error e) {
+        stdout.printf (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
   }
 }
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 504582e..e4a6bc2 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -887,7 +887,7 @@ class SerializableObjectModelTest : GXmlTest
           assert_not_reached ();
         }
         // TODO: serialized_xml_node_value have more text than expected, may be a bug in Document.to_string 
()
-        if (!unknown_property.serialized_xml_node_value.contains ("FAKE TEXT")) {
+        if (unknown_property.serialized_xml_node_value == "FAKE TEXT") {
           stdout.printf (@"ERROR: UNKNOWN_ATTRIBUTE: SERIALIZATION: Bad UnknownAttribute node's content text 
$(unknown_property.serialized_xml_node_value)");
           assert_not_reached ();
         }


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