[gxml] GomDocument: fixes on constructs and unit tests



commit e2440464244f79868cbc31a1782a0900d20e6630
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Oct 31 15:13:28 2016 -0600

    GomDocument: fixes on constructs and unit tests

 gxml/GomAttr.vala         |    2 +-
 gxml/GomDocument.vala     |   35 ++++++++++-------------------------
 gxml/Parser.vala          |    4 +++-
 gxml/XParser.vala         |    8 +++++---
 test/GomDocumentTest.vala |   42 ++++++++++++++++++++++--------------------
 5 files changed, 41 insertions(+), 50 deletions(-)
---
diff --git a/gxml/GomAttr.vala b/gxml/GomAttr.vala
index 419a34a..89ca0ff 100644
--- a/gxml/GomAttr.vala
+++ b/gxml/GomAttr.vala
@@ -24,7 +24,7 @@ using GXml;
 using Gee;
 
 
-public class GXml.GomAttr : GXml.GomNode {
+public class GXml.GomAttr : GXml.GomNode, GXml.DomAttr {
   protected string _namespace_uri;
   protected string _prefix;
   public string local_name { owned get { return _local_name; } }
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 36eecd2..ee3c751 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -71,45 +71,30 @@ public class GXml.GomDocument : GomNode,
     parser = new XParser (this);
   }
   public GomDocument () {}
-  public GomDocument.from_path (string path) {
+  public GomDocument.from_path (string path) throws GLib.Error {
     var file = GLib.File.new_for_path (path);
-    if (!file.query_exists ()) return;
-    try { parser.read (file, null); }
-    catch (GLib.Error e) {
-      GLib.warning (_("Can't read document from path: ")+e.message);
-    }
-
+    from_file (file);
   }
 
-  public GomDocument.from_uri (string uri) {
+  public GomDocument.from_uri (string uri) throws GLib.Error {
     this.from_file (File.new_for_uri (uri));
   }
 
-  public GomDocument.from_file (GLib.File file) {
-    if (!file.query_exists ()) return;
-    try { parser.read (file, null); }
-    catch (GLib.Error e) {
-      GLib.warning (_("Can't read document from file: ")+e.message);
-    }
+  public GomDocument.from_file (GLib.File file) throws GLib.Error {
+    parser.read (file, null);
   }
 
-  public GomDocument.from_stream (GLib.InputStream stream) {
-    try { parser.read_stream (stream, null); }
-    catch (GLib.Error e) {
-      GLib.warning (_("Can't read document from stream: ")+e.message);
-    }
+  public GomDocument.from_stream (GLib.InputStream stream) throws GLib.Error {
+    parser.read_stream (stream, null);
   }
 
-  public GomDocument.from_string (string str) {
-    try { parser.read_string (str, null); }
-    catch (GLib.Error e) {
-      GLib.warning (_("Can't read document from string: ")+e.message);
-    }
+  public GomDocument.from_string (string str) throws GLib.Error {
+    parser.read_string (str, null);
   }
 
 
   public DomElement create_element (string local_name) throws GLib.Error {
-      return new GomElement (this, local_name);
+    return new GomElement (this, local_name);
   }
   public DomElement create_element_ns (string? ns, string qualified_name) throws GLib.Error
   {
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index ae27ad5..4d16ca3 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -22,7 +22,9 @@
 using Gee;
 
 public errordomain GXml.ParserError {
-  INVALID_DATA
+  INVALID_DATA_ERROR,
+  INVALID_FILE_ERROR,
+  INVALID_STREAM_ERROR
 }
 
 /**
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index ec838f6..58a61a8 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -42,6 +42,8 @@ public class GXml.XParser : Object, GXml.Parser {
     return dump ();
   }
   public void read_string (string str, GLib.Cancellable? cancellable) throws GLib.Error {
+    if (str == "")
+      throw new ParserError.INVALID_DATA_ERROR (_("Invalid document string, is empty is not allowed"));
     StringBuilder s = new StringBuilder (str);
     var stream = new GLib.MemoryInputStream.from_data (str.data);
     read_stream (stream, cancellable);
@@ -50,7 +52,7 @@ public class GXml.XParser : Object, GXml.Parser {
 
   public void read (GLib.File file,  GLib.Cancellable? cancellable) throws GLib.Error {
     if (!file.query_exists ())
-      throw new GXml.DocumentError.INVALID_FILE (_("File doesn't exist"));
+      throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
     read_stream (file.read (), cancellable);
   }
 
@@ -79,7 +81,7 @@ public class GXml.XParser : Object, GXml.Parser {
 #endif
     int res = tr.read ();
     if (res == -1)
-      throw new ParserError.INVALID_DATA (_("Can't read node data"));
+      throw new ParserError.INVALID_DATA_ERROR (_("Can't read node data"));
 #if DEBUG
     if (res == 0)
       GLib.message ("ReadNode: No more nodes");
@@ -93,7 +95,7 @@ public class GXml.XParser : Object, GXml.Parser {
 #endif
       res = tr.read ();
       if (res == -1)
-        throw new ParserError.INVALID_DATA (_("Can't read node data"));
+        throw new ParserError.INVALID_DATA_ERROR (_("Can't read node data"));
       break;
     case Xml.ReaderType.ELEMENT:
       bool isempty = (tr.is_empty_element () == 1);
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index fa2b5f7..2b0a5c4 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -45,17 +45,17 @@ class GomDocumentTest : GXmlTest {
                                try {
                                GLib.Test.message ("invalid file...");
                                        // file does not exist
-                                       doc = new GDocument.from_path ("/tmp/asdfjlkansdlfjl");
+                                       doc = new GomDocument.from_path ("/tmp/asdfjlkansdlfjl");
                                        assert_not_reached ();
                                } catch {}
 
                                try {
                                        // file exists, but is not XML (it's a directory!)
-                                       doc = new GDocument.from_path ("/tmp/");
+                                       doc = new GomDocument.from_path ("/tmp/");
                                        assert_not_reached ();
                                } catch  {}
                                try {
-                                       doc = new GDocument.from_path ("test_invalid.xml");
+                                       doc = new GomDocument.from_path ("test_invalid.xml");
                                        assert_not_reached ();
                                } catch {}
                        });
@@ -64,7 +64,7 @@ class GomDocumentTest : GXmlTest {
                                assert (fin.query_exists ());
                                try {
                                        var instream = fin.read (null);
-                                       var doc = new GDocument.from_stream (instream);
+                                       var doc = new GomDocument.from_stream (instream);
                                        assert (doc != null);
                                        // TODO: CHECKS
                                } catch (GLib.Error e) {
@@ -82,7 +82,7 @@ class GomDocumentTest : GXmlTest {
                                Test.message ("Saving to file: "+f.get_uri ()+d.to_string ());
                                d.save_as (f);
                                assert (f.query_exists ());
-                               var d2 = new GDocument.from_file (f);
+                               var d2 = new GomDocument.from_file (f);
                                assert (d2 != null);
                                assert (d2.document_element != null);
                                assert (d2.document_element.node_name == "document_element");
@@ -132,7 +132,7 @@ class GomDocumentTest : GXmlTest {
 
                                try {
                                        fin = File.new_tmp ("gxml.XXXXXX", out iostream);
-                                       doc = new GDocument.from_stream (iostream.input_stream);
+                                       doc = new GomDocument.from_stream (iostream.input_stream);
                                        GLib.message ("Passed parse error stream");
                                        assert_not_reached ();
                                } catch  {}
@@ -161,7 +161,7 @@ class GomDocumentTest : GXmlTest {
                                GXml.DomNode document_element;
 
                                xml = """<?xml version="1.0"?>""";
-                               doc = new GDocument.from_string (xml);
+                               doc = new GomDocument.from_string (xml);
                                assert_not_reached ();
                        } catch {}
                        });
@@ -172,8 +172,9 @@ class GomDocumentTest : GXmlTest {
                                GXml.DomNode document_element;
 
                                xml = "";
-                               doc = new GDocument.from_string (xml);
-                       } catch { assert_not_reached (); }
+                               doc = new GomDocument.from_string (xml);
+                               assert_not_reached ();
+                       } catch {}
                        });
                Test.add_func ("/gxml/gom-document/save", () => {
                                DomDocument doc;
@@ -202,19 +203,20 @@ class GomDocumentTest : GXmlTest {
 
                Test.add_func ("/gxml/gom-document/create_element", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<document_element />");
-                               GElement elem = null;
-
-                               elem = (GElement) doc.create_element ("Banana");
+                               DomDocument doc = new GomDocument.from_string ("<document_element />");
+                               DomElement elem = null;
+                               elem = (DomElement) doc.create_element ("Banana");
+                               assert (elem is DomElement);
+                               assert (elem is GomElement);
                                assert (elem.tag_name == "Banana");
                                assert (elem.tag_name != "banana");
 
-                               elem = (GElement) doc.create_element ("ØÏØÏدÏØÏ  ²øœ³¤ïØ£");
+                               elem = (DomElement) doc.create_element ("ØÏØÏدÏØÏ  ²øœ³¤ïØ£");
                        } catch { assert_not_reached (); }
                });
                Test.add_func ("/gxml/gom-document/create_text_node", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<document_element />");
+                               DomDocument doc = new GomDocument.from_string ("<document_element />");
                                DomText text = (DomText) doc.create_text_node ("Star of my dreams");
                                assert (text is GomText);
                                assert (text is DomText);
@@ -225,7 +227,7 @@ class GomDocumentTest : GXmlTest {
                });
                Test.add_func ("/gxml/gom-document/create_comment", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<document_element />");
+                               DomDocument doc = new GomDocument.from_string ("<document_element />");
                                DomComment comment = (GXml.DomComment) doc.create_comment ("Ever since the 
day we promised.");
 
                                assert (comment.node_name == "#comment");
@@ -244,15 +246,15 @@ class GomDocumentTest : GXmlTest {
                                GLib.message ("Dat:"+instruction.node_value);
                                assert (instruction.data == "data");
                                assert (instruction.target != null);
-                               assert_not_reached ();
                                assert (instruction.target == "target");
                        } catch { assert_not_reached (); }
                });
                Test.add_func ("/gxml/gom-document/create_attribute", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<document_element />");
+                               DomDocument doc = new GomDocument.from_string ("<document_element />");
                                assert (doc.document_element != null);
                                ((DomElement) doc.document_element).set_attribute ("attrname", "attrvalue");
+                               assert_not_reached ();
                                //Test.message ("DOC:"+doc.to_string ());
                                var attr = ((DomElement) doc.document_element).get_attribute ("attrname");
                                Test.message ("Attr value: "+attr);
@@ -264,7 +266,7 @@ class GomDocumentTest : GXmlTest {
                });
                Test.add_func ("/gxml/gom-document/to_string/basic", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<?xml version=\"1.0\"?>
+                               DomDocument doc = new GomDocument.from_string ("<?xml version=\"1.0\"?>
 <Sentences><Sentence lang=\"en\">I like the colour blue.</Sentence><Sentence lang=\"de\">Ich liebe die 
T&#xFC;r.</Sentence><Authors><Author><Name>Fred</Name><Email>fweasley hogwarts co 
uk</Email></Author><Author><Name>George</Name><Email>gweasley hogwarts co 
uk</Email></Author></Authors></Sentences>");
                                string s1 = "";//doc.to_string ();
                                string[] cs1 = s1.split ("\n");
@@ -297,7 +299,7 @@ class GomDocumentTest : GXmlTest {
                });
                Test.add_func ("/gxml/gom-document/namespace", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string 
("<document_element><child/></document_element>");
+                               DomDocument doc = new GomDocument.from_string 
("<document_element><child/></document_element>");
                                doc.document_element.set_attribute_ns 
("http://www.gnome.org/GXml","xmlns:gxml","http://www.gnome.org/GXml";);
                                assert (doc.document_element != null);
                                assert (doc.document_element.namespace_uri != null);


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