[gxml] TwDocument, TwElement fixes and Unit Tests
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] TwDocument, TwElement fixes and Unit Tests
- Date: Tue, 12 May 2015 00:01:13 +0000 (UTC)
commit 1d8b1a4940fffd714c01439a145ff394abcaa74b
Author: Daniel Espinosa <esodan gmail com>
Date: Mon May 11 18:51:56 2015 -0500
TwDocument, TwElement fixes and Unit Tests
* Added TwDocument Collection tests
* Added TwDocument tests
* Added TwElement tests
* Improved tests in ObjectModel
* TwElement contents is calculated from GXml.Text nodes, setting
its contents, removes all of them and adds just one with new
content
* Fixed TwDocument's TwElement's contents writing
* TwDocument's node names defaults to #document
* Improve configure resume
* Disabled performance tests by default --enable-performance-tests
to enable them
configure.ac | 23 +-
gxml/SerializableObjectModel.vala | 4 +
gxml/TwDocument.vala | 22 +-
gxml/TwElement.vala | 43 ++-
gxml/TwText.vala | 3 +
gxml/libxml-Element.vala | 10 +-
test/GXmlTest.vala | 2 +
test/Makefile.am | 2 +
test/SerializableGeeCollections-Tw-Test.vala | 578 ++++++++++++++++++++++++++
test/SerializableObjectModel-Tw-Test.vala | 29 +-
test/SerializableObjectModelTest.vala | 2 +-
test/TwDocumentTest.vala | 44 ++-
test/TwElementTest.vala | 99 +++++
test/gxml-performance.vala | 2 +
14 files changed, 812 insertions(+), 51 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5cd8bde..2ebed63 100644
--- a/configure.ac
+++ b/configure.ac
@@ -241,6 +241,13 @@ AC_ARG_ENABLE(debug,
AM_CONDITIONAL(DEBUG, [test $debug = yes])
+performance=no
+AC_ARG_ENABLE(performance-tests,
+ AS_HELP_STRING([--enable-performance-tests], [Enable Performance tests (slow down Unit Tests
processing) [default=no]]),
+ [performance=$enableval], [performance="no"])
+
+AM_CONDITIONAL(ENABLE_PERFORMANCE_TESTS, [test $performance = yes])
+
dnl Check Cross Compile
dnl ******************************
dnl Check for Operating System
@@ -308,11 +315,13 @@ AC_OUTPUT
# Print configuration summary
echo ""
echo " Configuration summary for GXml-$GXML_VERSION"
-echo " Installation prefix: $prefix"
-echo " GObject Introspection: $found_introspection"
-echo " Documentation: ${enable_docs}"
-echo " DevHelp Doc: `if test x${devhelpdocs} = xyes; then echo yes; else echo no; fi`"
-echo " Gtk-Doc: `if test x${gtkdocs} = xyes; then echo yes; else echo no; fi`"
-echo " GIR Documented: $girdocs"
-echo " Platform: $host"
+echo " Installation prefix: $prefix"
+echo " GObject Introspection: $found_introspection"
+echo " Documentation: ${enable_docs}"
+echo " DevHelp Doc: `if test x${devhelpdocs} = xyes; then echo yes; else echo no; fi`"
+echo " Gtk-Doc: `if test x${gtkdocs} = xyes; then echo yes; else echo no; fi`"
+echo " GIR Documented: $girdocs"
+echo " Enable Performance Test: $performance"
+echo " Debug: $debug"
+echo " Platform: $host"
echo ""
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 5ae1aff..202c432 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -130,6 +130,7 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
public GXml.Node? default_serialize (GXml.Node node) throws GLib.Error
{
+ assert (node.name != null);
#if DEBUG
stdout.printf (@"$(get_type ().name ()): Serializing on node: $(node.name)\n");
#endif
@@ -181,6 +182,9 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
if (serialize_use_xml_node_value ()) {
// Set un empty string if no value is set for node contents
string t = "";
+#if DEBUG
+ stdout.printf (@"SET CONTENT FOR: $(get_type ().name ()): $(element.name)\n");
+#endif
if (serialized_xml_node_value != null)
t = serialized_xml_node_value;
element.content = t;
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index fd03105..7caab90 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -25,6 +25,9 @@ using Xml;
public class GXml.TwDocument : GXml.TwNode, GXml.Document
{
GXml.Element _root = null;
+ construct {
+ _name = "#document";
+ }
public TwDocument () {}
public TwDocument.for_path (string file)
{
@@ -102,10 +105,6 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
#endif
start_node (tw, root);
#if DEBUG
- GLib.message ("Writting Document Root node's contents");
-#endif
- tw.write_string (root.value);
-#if DEBUG
GLib.message ("Ending writting Document Root node");
#endif
tw.end_element ();
@@ -166,12 +165,16 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
GLib.message (@"Starting Child Element: writting Node '$(n.name)'");
#endif
start_node (tw, n);
- if (n.value != null)
- tw.write_string (n.value);
size += tw.end_element ();
if (size > 1500)
tw.flush ();
}
+ if (n is GXml.Text) {
+ //GLib.message ("Writting Element's contents");
+ size += tw.write_string (node.value);
+ if (size > 1500)
+ tw.flush ();
+ }
}
}
if (node is GXml.Comment) {
@@ -179,11 +182,6 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
if (size > 1500)
tw.flush ();
}
- if (node is GXml.Text) {
- size += tw.write_string (node.value);
- if (size > 1500)
- tw.flush ();
- }
}
public override string to_string ()
{
@@ -191,7 +189,7 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
GLib.message ("TwDocument: to_string ()");
#endif
Xml.Doc doc = new Xml.Doc ();
- Xml.TextWriter* tw = Xmlx.new_text_writer_doc (ref doc);
+ Xml.TextWriter tw = Xmlx.new_text_writer_doc (ref doc);
write_document (tw);
string str;
int size;
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
index bc47a22..85c8fec 100644
--- a/gxml/TwElement.vala
+++ b/gxml/TwElement.vala
@@ -23,12 +23,22 @@ using Gee;
public class GXml.TwElement : GXml.TwNode, GXml.Element
{
+ private string _content = null;
public TwElement (GXml.Document d, string name)
requires (d is TwDocument)
{
_doc = d;
_name = name;
}
+ // GXml.Node
+ public override string value
+ {
+ get {
+ calculate_content ();
+ return _content;
+ }
+ set { update_content (value); }
+ }
// GXml.Element
public void set_attr (string name, string value)
{
@@ -38,6 +48,37 @@ public class GXml.TwElement : GXml.TwNode, GXml.Element
public GXml.Node get_attr (string name) { return attrs.get (name); }
public void normalize () {}
public string content {
- owned get { return _value; } set { _value = value; } }
+ owned get {
+ calculate_content ();
+ return _content;
+ }
+ set {
+ update_content (value);
+ }
+ }
public string tag_name { get { return name; } }
+ private void calculate_content ()
+ {
+ _content = "";
+ foreach (GXml.Node n in childs) {
+ if (n is Text) {
+ _content += n.value;
+ }
+ }
+ }
+ private void update_content (string? val)
+ {
+ // Remove all GXml.Text elements
+ for (int i = 0; i < childs.size; i++) {
+ var n = childs.get (i);
+ if (n is Text) {
+ //GLib.message (@"Removing Text at: $i");
+ childs.remove_at (i);
+ }
+ }
+ if (val != null) {
+ var t = document.create_text (val);
+ this.childs.add (t);
+ }
+ }
}
diff --git a/gxml/TwText.vala b/gxml/TwText.vala
index 9ea5e56..cd3f513 100644
--- a/gxml/TwText.vala
+++ b/gxml/TwText.vala
@@ -24,6 +24,9 @@ using Gee;
public class GXml.TwText : GXml.TwNode, GXml.Text
{
private string _str = null;
+ construct {
+ _name = "#text";
+ }
public TwText (GXml.Document d, string text)
requires (d is GXml.TwDocument)
{
diff --git a/gxml/libxml-Element.vala b/gxml/libxml-Element.vala
index d7be782..65c7c9a 100644
--- a/gxml/libxml-Element.vala
+++ b/gxml/libxml-Element.vala
@@ -450,14 +450,20 @@ namespace GXml {
_content = "";
foreach (xNode n in child_nodes) {
if (n is xText) {
- if (_content == null) _content = "";
_content += n.value;
}
}
return _content;
}
set {
- if (value != null && value != "") {
+ if (value != null) {
+ // Remove all GXml.Text elements by just one with given content
+ for (int i = 0; i < childs.size; i++) {
+ var n = childs.get (i);
+ if (n is Text) {
+ childs.remove_at (i);
+ }
+ }
var t = owner_document.create_text_node (value);
this.append_child (t);
}
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 9d59dc3..e733641 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -67,9 +67,11 @@ class GXmlTest {
SerializableGeeDualKeyMapTest.add_tests ();
SerializableGeeArrayListTest.add_tests ();
SerializableGeeCollectionsTest.add_tests ();
+ SerializableGeeCollectionsTwTest.add_tests ();
SerializableBasicTypeTest.add_tests ();
SerializableEnumerationTest.add_tests ();
Performance.add_tests ();
+ TwElementTest.add_tests ();
TwDocumentTest.add_tests ();
Test.run ();
diff --git a/test/Makefile.am b/test/Makefile.am
index cbe5173..095463a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -46,8 +46,10 @@ sources = \
SerializableGeeArrayListTest.vala \
SerializableGeeHashMapTest.vala \
SerializableGeeCollectionsTest.vala \
+ SerializableGeeCollections-Tw-Test.vala \
SerializableBasicTypesTest.vala \
gxml-performance.vala \
+ TwElementTest.vala \
TwDocumentTest.vala \
$(NULL)
diff --git a/test/SerializableGeeCollections-Tw-Test.vala b/test/SerializableGeeCollections-Tw-Test.vala
new file mode 100644
index 0000000..7e4f6db
--- /dev/null
+++ b/test/SerializableGeeCollections-Tw-Test.vala
@@ -0,0 +1,578 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/**
+ *
+ * GXml.Serializable.GeeCollectionsTest
+ *
+ * Authors:
+ *
+ * Daniel Espinosa <esodan gmail com>
+ *
+ *
+ * Copyright (c) 2013-2015 Daniel Espinosa
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+using GXml;
+using Gee;
+
+class SerializableGeeCollectionsTwTest : GXmlTest
+{
+ class Citizen : SerializableObjectModel
+ {
+ public string ctype { get; set; }
+ public string get_value () { return serialized_xml_node_value; }
+ public void set_value (string val) { serialized_xml_node_value = val; }
+ //Enable set xElement content
+ public override bool serialize_use_xml_node_value () { return true; }
+ public override string to_string () { return @"Citizen: $ctype"; }
+ public override string node_name () { return "citizen"; }
+
+ public class Array : SerializableArrayList<Citizen> {}
+ }
+ class Asteroid : SerializableObjectModel
+ {
+ public int size { get; set; }
+ public Asteroid.with_size (int size) { this.size = size; }
+ public string get_value () { return serialized_xml_node_value; }
+ public void set_value (string val) { serialized_xml_node_value = val; }
+ public override string to_string () { return @"$size"; }
+ public override string node_name () { return "asteroid"; }
+
+ public class Array : SerializableArrayList<Asteroid> {}
+ }
+ class Planet : SerializableContainer, SerializableMapKey<string>
+ {
+ public string get_map_key () { return name; }
+ public string name { get; set; }
+ public Citizen.Array citizens { get; set; }
+ public Planet.named (string name) { this.name = name; }
+ public override string node_name () { return "planet"; }
+ public override string to_string () { return name; }
+
+ public override void init_containers ()
+ {
+ if (citizens == null)
+ citizens = new Citizen.Array ();
+ }
+
+ public class Collection : SerializableTreeMap<string,Planet> {}
+ }
+ class Space : SerializableContainer, SerializableMapKey<string>
+ {
+ public string get_map_key () { return name; }
+ public string name { get; set; }
+ public Planet.Collection planets { get; set; }
+ public Asteroid.Array asteroids { get; set; }
+
+ public Space.named (string name) { this.name = name; }
+
+ public override string node_name () { return "space"; }
+ public override string to_string () { return name; }
+ public override void init_containers ()
+ {
+ if (asteroids == null)
+ asteroids = new Asteroid.Array ();
+ if (planets == null)
+ planets = new Planet.Collection ();
+ }
+ public class Collection : SerializableTreeMap<string,Space> {}
+ }
+ class Refaction : SerializableContainer, SerializableMapDualKey<string,string>
+ {
+ public string model { get; set; }
+ public string manufacturer { get; set; }
+ public SpaceShip.Collection spaceships { get; set; }
+
+ public Refaction.named (string manufacturer, string model)
+ {
+ this.manufacturer = manufacturer;
+ this.model = model;
+ }
+
+ public string get_map_primary_key () { return manufacturer; }
+ public string get_map_secondary_key () { return model; }
+
+ public override string node_name () { return "refaction"; }
+ public override string to_string () { return model; }
+ public override bool get_enable_unknown_serializable_property () { return true; }
+
+ public override void init_containers ()
+ {
+ if (spaceships == null)
+ spaceships = new SpaceShip.Collection ();
+ }
+
+ public class Collection : SerializableDualKeyMap<string,string,Refaction> {}
+ }
+ class SpaceShip : SerializableContainer, SerializableMapDualKey<string,string>
+ {
+ public string model { get; set; }
+ public string manufacturer { get; set; }
+ public Space.Collection spaces { get; set; }
+
+ public SpaceShip.named (string manufacturer, string model)
+ {
+ this.manufacturer = manufacturer;
+ this.model = model;
+ }
+
+ public string get_map_primary_key () { return manufacturer; }
+ public string get_map_secondary_key () { return model; }
+
+ public override string node_name () { return "ship"; }
+ public override string to_string () { return model; }
+ public override bool get_enable_unknown_serializable_property () { return true; }
+
+ public override void init_containers ()
+ {
+ if (spaces == null)
+ spaces = new Space.Collection ();
+ }
+
+ public class Collection : SerializableDualKeyMap<string,string,SpaceShip> {}
+ }
+
+ class ChargeZone : SerializableContainer
+ {
+ public string name { get; set; }
+ public SpaceShip.Collection spaceships { get; set; }
+
+ public override string node_name () { return "ChargeZone"; }
+ public override string to_string () { return name; }
+ public override void init_containers ()
+ {
+ if (spaceships == null)
+ spaceships = new SpaceShip.Collection ();
+ }
+ }
+ class Storage : SerializableContainer
+ {
+ public string name { get; set; }
+ public Refaction.Collection refactions { get; set; }
+
+ public override string node_name () { return "storage"; }
+ public override string to_string () { return name; }
+ public override void init_containers ()
+ {
+ if (refactions == null)
+ refactions = new Refaction.Collection ();
+ }
+ }
+
+ class SpaceBase : SerializableObjectModel
+ {
+ public string name { get; set; }
+ public ChargeZone charge_zone { get; set; }
+ public Storage storage { get; set; }
+ public override string node_name () { return "base"; }
+ public override string to_string () { return name; }
+ }
+
+ public static void add_tests ()
+ {
+ Test.add_func ("/gxml/tw/serializable/convined_gee_containers/de-se-deserialize",
+ () => {
+ try {
+ var doc = new xDocument.from_string ("""<?xml version="1.0"?>
+<base name="AlphaOne" >
+ <chargezone name="A1-1">
+ <ship manufacturer="MacToy" model="A1234">
+ <space name="Alpha Centaury">
+ <planet name="Earth">
+ <citizen ctype="Human">1M</citizen>
+ <citizen ctype="Ghost">10M</citizen>
+ </planet>
+ <planet name="Platon" />
+ </space>
+ <space name="Galax">
+ <planet name="Saminon">
+ <citizen ctype="Humanes">100M</citizen>
+ <citizen ctype="Jeties">1000M</citizen>
+ </planet>
+ <planet name="Platon" />
+ </space>
+ </ship>
+ <ship manufacturer="Memphis" model="AB1">
+ <space name="Beta Centaury">
+ <planet name="Tronex">
+ <citizen ctype="Human">10000M</citizen>
+ <citizen ctype="Cat">100000M</citizen>
+ </planet>
+ <planet name="Palax" />
+ </space>
+ </ship>
+ </chargezone>
+ <storage name="B4-A4">
+ <refaction manufacturer="MacToy" model="Fly045">
+ <ship manufacturer="MacToy" model="A1234" />
+ <ship manufacturer="MegaTrench" model="G045-1" />
+ </refaction>
+ </storage>
+</base>""");
+ var sb = new SpaceBase ();
+ // TODO: implement deserialize TwDocument
+ sb.deserialize (doc);
+ assert (sb.charge_zone != null);
+ assert (sb.charge_zone.spaceships != null);
+ assert (sb.charge_zone.spaceships.size == 2);
+ var sbsh1 = sb.charge_zone.spaceships.get ("MacToy", "A1234");
+ assert (sbsh1 != null);
+ assert (sbsh1.spaces != null);
+ assert (sbsh1.spaces.size == 2);
+ // Check serialize
+ var ndoc = new TwDocument ();
+ sb.serialize (ndoc);
+ assert (ndoc.root != null);
+ assert (ndoc.root.name == "base");
+ assert (ndoc.root.childs.size == 2);
+ var cz = ndoc.root.childs.get (0);
+ assert (cz != null);
+ assert (cz.name.down () == "chargezone");
+ assert (cz.attrs.get ("name").value == "A1-1");
+ assert (cz.childs.size == 2);
+ bool fmactoy = false;
+ bool fmemphis = false;
+ foreach (GXml.Node sh in cz.childs) {
+ if (sh.name == "ship" && sh.attrs.get ("manufacturer").value == "MacToy") {
+ fmactoy = true;
+ assert (sh.attrs.get ("manufacturer").value == "MacToy");
+ assert (sh.attrs.get ("model").value == "A1234");
+ assert (sh.childs.size == 2);
+ bool falphac = false;
+ bool fgalax = false;
+ foreach (GXml.Node s in sh.childs){
+ if (s.name == "space" && s.attrs.get ("name").value == "Alpha Centaury") {
+ falphac = true;
+ assert (s.attrs.get ("name").value == "Alpha Centaury");
+ assert (s.childs.size == 2);
+ bool fearth = false;
+ bool fplaton = false;
+ foreach (GXml.Node p1 in s.childs) {
+ if (p1.name == "planet" && p1.attrs.get ("name").value == "Earth") {
+ fearth = true;
+ assert (p1.name == "planet");
+ assert (p1.attrs.get ("name").value == "Earth");
+ assert (p1.childs.size == 2);
+ var c1 = p1.childs.get (0);
+ assert (c1 != null);
+ assert (c1.name == "citizen");
+ assert (c1.attrs.get ("ctype").value == "Human");
+ assert (((Element)c1).content == "1M");
+ assert (c1.childs.size == 1);
+ var c2 = p1.childs.get (1);
+ assert (c2 != null);
+ assert (c2.name == "citizen");
+ assert (c2.attrs.get ("ctype").value == "Ghost");
+ assert (((Element) c2).content == "10M");
+ }
+ if (p1.name == "planet" && p1.attrs.get ("name").value == "Platon") {
+ fplaton = true;
+ assert (p1.name == "planet");
+ assert (p1.attrs.get ("name").value == "Platon");
+ assert (p1.childs.size == 0);
+ }
+ }
+ assert (fearth);
+ assert (fplaton);
+ }
+ if (s.name == "space" && s.attrs.get ("name").value == "Galax") {
+ fgalax = true;
+ assert (s.attrs.get ("name").value == "Galax");
+ assert (s.childs.size == 2);
+ bool fsaminon = false;
+ foreach (GXml.Node p in s.childs) {
+ if (p.name == "planet" && p.attrs.get ("name").value == "Saminon") {
+ fsaminon = true;
+ var h = p.childs.get (0);
+ assert (h != null);
+ assert (h.name == "citizen");
+ assert (h.attrs.get ("ctype").value == "Humanes");
+ assert (h.childs.size == 1);
+ assert (((Element) h).content == "100M");
+ var j = p.childs.get (1);
+ assert (j != null);
+ assert (j.name == "citizen");
+ assert (j.attrs.get ("ctype").value == "Jeties");
+ assert (j.childs.size == 1);
+ assert (((Element) j).content == "1000M");
+ }
+ }
+ assert (fsaminon);
+ }
+ }
+ assert (falphac);
+ }
+ if (sh.name == "ship" && sh.attrs.get ("manufacturer").value == "Memphis") {
+ fmemphis = true;
+ assert (sh.name == "ship");
+ assert (sh.attrs.get ("manufacturer").value == "Memphis");
+ assert (sh.attrs.get ("model").value == "AB1");
+ assert (sh.childs.size == 1);
+ bool fbetac = false;
+ foreach (GXml.Node s in sh.childs){
+ if (s.name == "space" && s.attrs.get ("name").value == "Beta Centaury") {
+ fbetac = true;
+ assert (s.attrs.get ("name").value == "Beta Centaury");
+ assert (s.childs.size == 2);
+ bool ftronex = false;
+ bool fpalax = false;
+ foreach (GXml.Node p in s.childs) {
+ if (p.name == "planet" && p.attrs.get ("name").value == "Tronex") {
+ ftronex = true;
+ assert (p.name == "planet");
+ assert (p.attrs.get ("name").value == "Tronex");
+ assert (p.childs.size == 2);
+ var cp = p.childs.get (0);
+ assert (cp.name == "citizen");
+ assert (cp.attrs.get ("ctype").value == "Human");
+ assert (((Element)cp).content == "10000M");
+ var cp2 = p.childs.get (1);
+ assert (cp2.name == "citizen");
+ assert (cp2.attrs.get ("ctype").value == "Cat");
+ assert (((Element)cp2).content == "100000M");
+ }
+ if (p.name == "planet" && p.attrs.get ("name").value == "Palax") {
+ fpalax = true;
+ assert (p.name == "planet");
+ assert (p.attrs.get ("name").value == "Palax");
+ assert (p.childs.size == 0);
+ }
+ }
+ assert (ftronex);
+ assert (fpalax);
+ }
+ assert (fbetac);
+ }
+ }
+ }
+ assert (fmactoy);
+ assert (fmemphis);
+ var st = ndoc.root.childs.get (1);
+ assert (st != null);
+ assert (st.name.down () == "storage");
+ assert (st.attrs.get ("name").value == "B4-A4");
+ assert (st.childs.size == 1);
+ bool fr = false;
+ foreach (GXml.Node r in st.childs) {
+ if (r.name == "refaction" && r.attrs.get ("manufacturer").value == "MacToy") {
+ fr = true;
+ assert (r.name == "refaction");
+ assert (r.attrs.get ("manufacturer").value == "MacToy");
+ assert (r.attrs.get ("model").value == "Fly045");
+ assert (r.childs.size == 2);
+ bool frmactoy = false;
+ bool frmega = false;
+ foreach (GXml.Node rsh in r.childs) {
+ if (rsh.name == "ship" && rsh.attrs.get ("manufacturer").value == "MacToy") {
+ frmactoy = true;
+ assert (rsh.attrs.get ("manufacturer").value == "MacToy");
+ assert (rsh.attrs.get ("model").value == "A1234");
+ assert (rsh.childs.size == 0);
+ }
+ if (rsh.name == "ship" && rsh.attrs.get ("manufacturer").value == "MegaTrench") {
+ frmega = true;
+ assert (rsh.attrs.get ("manufacturer").value == "MegaTrench");
+ assert (rsh.attrs.get ("model").value == "G045-1");
+ assert (rsh.childs.size == 0);
+ }
+ }
+ assert (frmactoy);
+ assert (frmega);
+ }
+ }
+ assert (fr);
+#if DEBUG
+ ndoc.indent = true;
+ stdout.printf (@"$ndoc");
+#endif
+ }
+ catch (GLib.Error e) {
+#if DEBUG
+ stdout.printf (@"ERROR: $(e.message)");
+#endif
+ assert_not_reached ();
+ }
+ });/*
+ Test.add_func ("/gxml/serializable/convined_gee_containers/se-deserialize-unknowns",
+ () => {
+ try {
+ var org_doc = new xDocument.from_string ("""<?xml version="1.0"?>
+<base name="AlphaOne" >
+ <chargezone name="A1-1">
+ <ship manufacturer="MacToy" model="A1234">
+ <space name="Alpha Centaury">
+ <planet name="Earth">
+ <citizen ctype="Human">1M</citizen>
+ <citizen ctype="Ghost">10M</citizen>
+ </planet>
+ <planet name="Galiumt" />
+ </space>
+ <space name="Galax">
+ <planet name="Saminon">
+ <citizen ctype="Humanes">100M</citizen>
+ <citizen ctype="Jeties">1000M</citizen>
+ </planet>
+ <planet name="Keymanth" />
+ </space>
+ </ship>
+ <ship manufacturer="Memphis" model="AB1">
+ <space name="Proximity">
+ <planet name="Pluton">
+ <citizen ctype="Ork">10000M</citizen>
+ <citizen ctype="Gyk">100000M</citizen>
+ </planet>
+ <planet name="Mercury" />
+ </space>
+ </ship>
+ </chargezone>
+ <storage name="B4-A4">
+ <refaction manufacturer="MacToy" model="Fly045">
+ <ship manufacturer="MacToy" model="A1234" />
+ <ship manufacturer="MegaTrench" model="G045-1" unknown="UNKNOWN ATTR">TEST_TEXT</ship>
+ <UnknownAttribute name="nothing" />
+ </refaction>
+ </storage>
+</base>""");
+ var s = new SpaceBase ();
+ s.deserialize (org_doc);
+ if (s.charge_zone == null) {
+ stdout.printf (@"ERROR: No charge Zone for $(s.name)\n");
+ assert_not_reached ();
+ }
+ if (s.charge_zone.spaceships.size != 2) {
+ stdout.printf (@"ERROR: Bad SpaceShip size: $(s.charge_zone.spaceships.size)\n");
+ assert_not_reached ();
+ }
+ var mssh = s charge_zone spaceships get ("MacToy","A1234");
+ if (mssh == null) {
+ stdout.printf (@"ERROR: No spaceship MacToy/A1234\n");
+ assert_not_reached ();
+ }
+
+ if (s.storage == null) {
+ stdout.printf (@"ERROR: No storage\n");
+ assert_not_reached ();
+ }
+ if (mssh.spaces.size != 2) {
+ stdout.printf (@"ERROR: Bad spaces number for MacToy/A1234: $(mssh.spaces.size)\n");
+ assert_not_reached ();
+ }
+ if (s.storage == null) {
+ stdout.printf (@"ERROR: No storage\n");
+ assert_not_reached ();
+ }
+ if (s.storage.refactions.size != 1) {
+ stdout.printf (@"ERROR: Bad number of refactions: got $(s.storage.refactions.size)\n");
+ assert_not_reached ();
+ }
+ var refaction = s storage refactions get ("MacToy","Fly045");
+ if (refaction == null) {
+ stdout.printf (@"ERROR: No Refaction MacToy/Fly045 found!\n");
+ assert_not_reached ();
+ }
+ assert (refaction.unknown_serializable_properties != null);
+ assert (refaction.unknown_serializable_properties.size == 0);
+ assert (refaction.unknown_serializable_nodes.size == 1);
+ var doc = new xDocument ();
+ s.serialize (doc);
+ if (doc.document_element.node_name != "base") {
+ stdout.printf (@"ERROR: bad root node name\n");
+ assert_not_reached ();
+ }
+ //stdout.printf (@"$doc\n");
+ foreach (GXml.xNode n in doc.document_element.child_nodes) {
+ if (n is xElement) {
+ if (n.node_name == "ChargeZone") {
+
+ }
+ if (n.node_name == "storage") {
+ bool unkfound = false;
+ bool tfound = false;
+ bool attrfound = false;
+ foreach (GXml.xNode sn in n.child_nodes) {
+ if (sn is xElement) {
+ if (sn.node_name == "refaction") {
+ foreach (GXml.xNode rn in sn.child_nodes) {
+ if (rn is xElement) {
+ //stdout.printf (@"Refaction current node: '$(rn.node_name)'\n");
+ if (rn.node_name == "ship") {
+ var atr = ((xElement) rn).get_attribute_node ("manufacturer");
+ if (atr == null) {
+ stdout.printf (@"ERROR: No attribute manufacturer for Ship\n");
+ assert_not_reached ();
+ }
+ if (atr.node_value == "MegaTrench") {
+ var shanattr = ((xElement) rn).get_attribute_node ("unknown");
+ if (shanattr != null) {
+ attrfound = true;
+ if (shanattr.node_value != "UNKNOWN ATTR") {
+ stdout.printf (@"ERROR: Invalid Text Node Value for ship MegaTrench:
$(shanattr.node_value)\n");
+ assert_not_reached ();
+ }
+ }
+ foreach (GXml.xNode shn in rn.child_nodes) {
+ //stdout.printf (@"Refaction: Ship MegaTrench: Node: $(shn.node_name)\n");
+ if (shn is Text) {
+ tfound = true;
+ if (shn.node_value != "TEST_TEXT") {
+ stdout.printf (@"ERROR: Invalid Text Node Value for ship MegaTrench:
$(shn.node_value)\n");
+ assert_not_reached ();
+ }
+ }
+ }
+ }
+ }
+ if (rn.node_name == "UnknownAttribute") {
+ unkfound = true;
+ var nattr = ((xElement) rn).get_attribute_node ("name");
+ if (nattr == null) {
+ stdout.printf (@"ERROR: No Unknown Attribute Node with attribute name\n");
+ assert_not_reached ();
+ }
+ if (nattr.node_value != "nothing") {
+ stdout.printf (@"ERROR: Invalid unknown attribute node's attribute name value:
found $(nattr.node_value)\n");
+ assert_not_reached ();
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ if (!attrfound) {
+ stdout.printf (@"ERROR: No Unknown AttributeText found for ship MegaTrench\n");
+ assert_not_reached ();
+ }
+ if (!tfound) {
+ stdout.printf (@"ERROR: No Text Node Value found for ship MegaTrench\n");
+ assert_not_reached ();
+ }
+ if (!unkfound) {
+ stdout.printf (@"ERROR: No Unknown Attribute Node found for storage\n");
+ assert_not_reached ();
+ }
+ }
+ }
+ if (n is Text) {
+ stdout.printf (@"ROOT NODE VALUE: '$(n.node_value)'\n");
+ }
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR: $(e.message)");
+ assert_not_reached ();
+ }
+ });*/
+ }
+}
diff --git a/test/SerializableObjectModel-Tw-Test.vala b/test/SerializableObjectModel-Tw-Test.vala
index 477b44e..735a0ee 100644
--- a/test/SerializableObjectModel-Tw-Test.vala
+++ b/test/SerializableObjectModel-Tw-Test.vala
@@ -56,14 +56,14 @@ class SerializableObjectModelTwTest : GXmlTest
() => {
var doc = new TwDocument ();
var manual = new Manual ();
+ assert (manual.document == "MANUAL DOCUMENTATION");
+ assert (manual.pages == 3);
+ assert (manual.get_contents () == "TEXT INTO THE MANUAL DOCUMENT");
try {
+ GLib.message ("Before Serialize...");
manual.serialize (doc);
- if (doc.root.name != "manual") {
- stdout.printf (@"ERROR MANUAL: xElement: $(doc.root.name)\n");
- assert_not_reached ();
- }
+ GLib.message ("After Serialize...");
Element element = (Element) doc.root;
- serialize_manual_check (element, manual);
} catch (GLib.Error e) {
stdout.printf (@"$(e.message)");
assert_not_reached ();
@@ -579,20 +579,11 @@ Test.add_func ("/gxml/tw/serializable/object_model/override_deserialize",
static void serialize_manual_check (Element element, Manual manual)
{
var document = element.attrs.get ("document");
- if (document == null) assert_not_reached ();
- if (document.value != manual.document) {
- stdout.printf (@"ERROR MANUAL: document: $(document.value)\n");
- assert_not_reached ();
- }
+ assert (document != null);
+ assert (document.value == manual.document);
var pages = element.attrs.get ("pages");
- if (pages == null) assert_not_reached ();
- if (int.parse (pages.value) != manual.pages) {
- stdout.printf (@"ERROR MANUAL: pages: $(pages.value)\n");
- assert_not_reached ();
- }
- if (element.content != manual.get_contents ()) {
- stdout.printf (@"ERROR MANUAL: content: Expected $(manual.get_contents ()): got:
$(element.content)\n");
- assert_not_reached ();
- }
+ assert (pages != null);
+ assert (int.parse (pages.value) == manual.pages);
+ assert (element.content == manual.get_contents ());
}
}
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 67733d8..8e54884 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -883,8 +883,8 @@ class SerializableObjectModelTest : GXmlTest
GLib.message ("Prepare to Serialize...");
#endif
unknown_property.serialize (doc2);
- GLib.message ("After Serialize...");
#if DEBUG
+ GLib.message ("After Serialize...");
GLib.message ("Serialized back document: \n"+doc2.to_string ());
#endif
if (doc2.root == null) {
diff --git a/test/TwDocumentTest.vala b/test/TwDocumentTest.vala
index a9bfa4a..9465fa2 100644
--- a/test/TwDocumentTest.vala
+++ b/test/TwDocumentTest.vala
@@ -26,6 +26,23 @@ using GXml;
class TwDocumentTest : GXmlTest {
public static void add_tests () {
+ Test.add_func ("/gxml/tw-document", () => {
+ try {
+ var d = new TwDocument ();
+ assert (d.name == "#document");
+ assert (d.root == null);
+ assert (d.childs != null);
+ assert (d.attrs != null);
+ assert (d.childs.size == 0);
+ assert (d.value == null);
+ }
+ catch (GLib.Error e) {
+#if DEBUG
+ GLib.message (@"ERROR: $(e.message)");
+#endif
+ assert_not_reached ();
+ }
+ });
Test.add_func ("/gxml/tw-document/root", () => {
try {
var f = GLib.File.new_for_path (GXmlTestConfig.TEST_SAVE_DIR+"/tw-test.xml");
@@ -36,7 +53,7 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "root");
- assert (d.root.value == null);
+ assert (d.root.value == "");
}
catch (GLib.Error e) {
#if DEBUG
@@ -44,7 +61,7 @@ class TwDocumentTest : GXmlTest {
#endif
assert_not_reached ();
}
- });
+ });
Test.add_func ("/gxml/tw-document/save/root", () => {
try {
var f = GLib.File.new_for_path
(GXmlTestConfig.TEST_SAVE_DIR+"/tw-test.xml");
@@ -55,7 +72,7 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "root");
- assert (d.root.value == null);
+ assert (d.root.value == "");
d.save ();
var istream = f.read ();
uint8[] buffer = new uint8[2048];
@@ -81,7 +98,7 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "root");
- assert (d.root.value == null);
+ assert (d.root.value == "");
var root = (GXml.Element) d.root;
root.set_attr ("Pos","on");
var at1 = root.get_attr ("Pos");
@@ -118,9 +135,15 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "root");
- assert (d.root.value == null);
+ assert (d.root.value == "");
var root = (GXml.Element) d.root;
root.content = "GXml TwDocument Test";
+ assert (root.childs.size == 1);
+ assert (root.content == "GXml TwDocument Test");
+ var t = root.childs.get (0);
+ assert (t.value == "GXml TwDocument Test");
+ assert (t is GXml.Text);
+ //GLib.message (@"$d");
d.save ();
var istream = f.read ();
uint8[] buffer = new uint8[2048];
@@ -147,14 +170,17 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "root");
- assert (d.root.value == null);
+ assert (d.root.value == "");
var root = (GXml.Element) d.root;
var e1 = (GXml.Element) d.create_element ("child");
e1.set_attr ("name","Test1");
+ assert (e1.childs.size == 0);
root.childs.add (e1);
var e2 = (GXml.Element) d.create_element ("child");
e2.set_attr ("name","Test2");
+ assert (e2.childs.size == 0);
root.childs.add (e2);
+ assert (root.childs.size == 2);
d.save ();
var istream = f.read ();
uint8[] buffer = new uint8[2048];
@@ -192,7 +218,7 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "bookstore");
- assert (d.root.value == null);
+ assert (d.root.value == "");
var r = (GXml.Element) d.root;
r.set_attr ("name","The Great Book");
#if DEBUG
@@ -255,7 +281,7 @@ class TwDocumentTest : GXmlTest {
assert (d.childs.size == 1);
assert (d.root != null);
assert (d.root.name == "bookstore");
- assert (d.root.value == null);
+ assert (d.root.value == "");
var r = (GXml.Element) d.root;
r.set_attr ("name","The Great Book");
#if DEBUG
@@ -310,10 +336,10 @@ class TwDocumentTest : GXmlTest {
#if DEBUG
GLib.message (@"$(doc)");
#endif
- GLib.message (@"\n$(doc)");
string str = doc.to_string ();
assert ("<?xml version=\"1.0\"?>" in str);
assert ("<root/>" in str);
+ assert ("<root/>" in doc.to_string ());
});
}
}
diff --git a/test/TwElementTest.vala b/test/TwElementTest.vala
new file mode 100644
index 0000000..c333eed
--- /dev/null
+++ b/test/TwElementTest.vala
@@ -0,0 +1,99 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* Notation.vala
+ *
+ * Copyright (C) 2011-2013 Richard Schwarting <aquarichy gmail com>
+ * Copyright (C) 2011-2015 Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Richard Schwarting <aquarichy gmail com>
+ * Daniel Espinosa <esodan gmail com>
+ */
+
+using GXml;
+
+class TwElementTest : GXmlTest {
+ public static void add_tests () {
+ Test.add_func ("/gxml/tw-element/api", () => {
+ var d = new TwDocument ();
+ var e = (Element) d.create_element ("element");
+ d.childs.add (e);
+ assert (d.childs.size == 1);
+ assert (d.root.name == "element");
+ e.set_attr ("attr1","val1");
+ assert (d.root.attrs.get ("attr1") != null);
+ assert (d.root.attrs.get ("attr1").value == "val1");
+ assert (e.attrs.size == 1);
+ assert (e.childs.size == 0);
+ var child = (Element) d.create_element ("child");
+ assert (child != null);
+ e.childs.add (child);
+ assert (e.childs.size == 1);
+ child.set_attr ("cattr1", "cval1");
+ var c = (Element) e.childs.get (0);
+ assert (c != null);
+ assert (c.name == "child");
+ assert (c.attrs.get ("cattr1") != null);
+ assert (c.attrs.get ("cattr1").value == "cval1");
+ assert (c.content == "");
+ c.content = "";
+ assert (c.content == "");
+ assert (c.childs.size == 1);
+ c.content = "HELLO CONTENT";
+ assert (c.childs.size == 1);
+ assert (c.content == "HELLO CONTENT");
+ });
+ Test.add_func ("/gxml/tw-element/content", () => {
+ var d = new TwDocument ();
+ var e = (Element) d.create_element ("element");
+ d.childs.add (e);
+ assert (d.childs.size == 1);
+ assert (d.root.name == "element");
+ e.content = "HELLO";
+ assert (e.content == "HELLO");
+ assert (d.root.childs.size == 1);
+ e.content = "TIME";
+ assert (d.root.childs.size == 1);
+ assert (e.content == "TIME");
+ var t = d.create_text (" OTHER");
+ e.childs.add (t);
+ assert (e.childs.size == 2);
+ assert (d.root.childs.size == 2);
+ assert (e.content == "TIME OTHER");
+ e.childs.clear ();
+ assert (e.childs.size == 0);
+ assert (e.content == "");
+ var c = d.create_element ("child");
+ e.childs.add (c);
+ e.content = "KNOW";
+ assert (e.childs.size == 2);
+ assert (e.content == "KNOW");
+ e.content = "";
+ assert (e.childs.size == 2);
+ e.childs.clear ();
+ assert (e.content == "");
+ var t1 = d.create_text ("TEXT1");
+ var c1 = d.create_element ("child2");
+ var t2 = d.create_text ("TEXT2");
+ e.childs.add (t1);
+ e.childs.add (c1);
+ e.childs.add (t2);
+ assert (e.childs.size == 3);
+ assert (e.content == "TEXT1TEXT2");
+ e.content = null;
+ assert (e.childs.size == 1);
+ });
+ }
+}
diff --git a/test/gxml-performance.vala b/test/gxml-performance.vala
index 1d28c63..eb1e855 100644
--- a/test/gxml-performance.vala
+++ b/test/gxml-performance.vala
@@ -121,6 +121,7 @@ public class Performance
{
public static void add_tests ()
{
+#if ENABLE_PERFORMANCE_TESTS
Test.add_func ("/gxml/performance/document",
() => {
try {
@@ -235,5 +236,6 @@ public class Performance
assert_not_reached ();
}
});
+#endif
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]