[gxml] StreamReader: adding unit tests



commit bedb17e14672e706f1f40e5815cceb0736969292
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 22 12:36:02 2019 -0500

    StreamReader: adding unit tests

 gxml/StreamReader.vala     | 81 ++++++++++++++++++++++++++++++++++------------
 test/StreamReaderTest.vala | 41 +++++++++++++++++++++++
 test/meson.build           | 13 ++++++++
 3 files changed, 114 insertions(+), 21 deletions(-)
---
diff --git a/gxml/StreamReader.vala b/gxml/StreamReader.vala
index af391da..2611404 100644
--- a/gxml/StreamReader.vala
+++ b/gxml/StreamReader.vala
@@ -37,8 +37,16 @@ public class GXml.StreamReader : GLib.Object {
   public Cancellable? cancellable { get; set; }
   public bool has_xml_dec { get; set; }
   public bool has_doc_type_dec { get; set; }
+  public bool has_misc { get; set; }
   public bool has_root { get; set; }
-  public void read () throws GLib.Error {
+  public DomDocument document { get; }
+
+  public StreamReader (InputStream istream) {
+    _stream = new DataInputStream (istream);
+  }
+
+  public DomDocument read () throws GLib.Error {
+    _document = new Document ();
     char buf[2] = {0, 0};
     int64 pos = -1;
     buf[0] = (char) stream.read_byte (cancellable);
@@ -60,22 +68,30 @@ public class GXml.StreamReader : GLib.Object {
         read_misc (buf[0]);
       }
     }
+    if (!has_xml_dec && !has_doc_type_dec && !has_misc) {
+      stream.seek (-2, SeekType.CUR, cancellable);
+    }
     while (!has_root) {
       buf[0] = (char) stream.read_byte (cancellable);
+      message ("Current: '%c' - Pos: %ld", buf[0], (long) stream.tell ());
       if (buf[0] != '<') {
         throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: expected '<' character"));
       }
       buf[0] = (char) stream.read_byte (cancellable);
+      message ("Current: '%c' - Pos: %ld", buf[0], (long) stream.tell ());
       if (buf[0] == '!' || buf[0] == '?') {
         read_misc (buf[0]);
       } else {
         has_root = true;
       }
     }
+    message ("Break Found Root");
     if (is_space (buf[0])) {
       throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: unexpected character"));
     }
-    read_element ();
+    string res = read_element ();
+    message ("Root string: %s", res);
+    return document;
   }
   public bool read_misc (char c) throws GLib.Error {
     char buf[2] = {0, 0};
@@ -125,37 +141,60 @@ public class GXml.StreamReader : GLib.Object {
       root_pos_start = (size_t) (stream.tell () - 1);
     }
     stream.seek (-1, SeekType.CUR, cancellable);
-    string name = stream.read_upto (" ", -1, null, cancellable);
-    string atts = stream.read_upto (">", -1, null, cancellable);
-    stream.read_byte (cancellable);
-    str = "<"+name+atts+">";
-    if (is_root) {
+    buf[0] = (char) stream.read_byte (cancellable);
+    string name = "";
+    while (buf[0] != '>') {
+      if (is_space (buf[0])) {
+        break;
+      }
+      name += (string) buf;
+      buf[0] = (char) stream.read_byte (cancellable);
+    }
+    message ("Element's name found: %s", name);
+    string atts = "";
+    while (buf[0] != '>') {
+      atts += (string) buf;
+      buf[0] = (char) stream.read_byte (cancellable);
+    }
+    message ("Element's attributes found: %s", atts);
+    str = "<"+name+atts;
+    if (atts[atts.length - 1] == '/') {
+     str += "/>";
+     return str;
+    } else {
+      str += ">";
+    }
+    message ("Element's declaration head: %s", str);
+    message ("Current: %s", (string) buf);
+    while (true) {
       string content = stream.read_upto ("<", -1, null, cancellable);
       buf[0] = (char) stream.read_byte (cancellable);
       str += content;
+      message ("Current Element's content: '%s'", content);
       buf[0] = (char) stream.read_byte (cancellable);
       if (buf[0] == '/') {
         string closetag = stream.read_upto (">", -1, null, cancellable);
         buf[0] = (char) stream.read_byte (cancellable);
-        root_pos_end = (size_t) stream.tell ();
-        str += closetag + ">";
+        if (is_root) {
+          root_pos_end = (size_t) stream.tell ();
+        }
+        message ("CloseTAG: %s", closetag);
+        if (closetag == name) {
+          str = str + "</"+closetag+">";
+          return str;
+        }
       } else {
-        read_element (false);
-      }
-    } else {
-      string cltag = "</"+name;
-      string content = stream.read_upto (cltag, -1, null, cancellable);
-      str += content;
-      for (int i = 0; i < cltag.data.length; i++) {
-        stream.read_byte (cancellable);
+        string nnode = read_element (false);
+        if (is_root) {
+          message ("Child Element read: \n%s", nnode);
+        } else {
+          str += nnode;
+        }
       }
-      root_pos_end = (size_t) stream.tell ();
-      str += cltag;
     }
-    return str;
   }
   public bool is_space (char c) {
-    return c == 0x20 || c == 0x9 || c == 0xA;
+    return c == 0x20 || c == 0x9 || c == 0xA || c == ' ' || c == '\t' || c == '\n';
   }
   public bool validate_xml_definition () throws GLib.Error {
     return true;
diff --git a/test/StreamReaderTest.vala b/test/StreamReaderTest.vala
new file mode 100644
index 0000000..106e5b9
--- /dev/null
+++ b/test/StreamReaderTest.vala
@@ -0,0 +1,41 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* GXmlTest.vala
+ *
+ * Copyright (C) 2019  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:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+using GXml;
+
+class GXmlTest {
+       public static int main (string[] args) {
+               Test.init (ref args);
+               Test.add_func ("/gxml/stream-reader", () => {
+                       string str = """<root p1="a" p2="b" ><child>ContentChild</child></root>""";
+                       var istream = new MemoryInputStream.from_data (str.data, null);
+                       var sr = new StreamReader (istream);
+                       try {
+                               sr.read ();
+                       } catch (GLib.Error e) {
+                               warning ("Error: %s", e.message);
+                       }
+               });
+               Test.run ();
+
+               return 0;
+       }
+}
diff --git a/test/meson.build b/test/meson.build
index 1cc39c0..baeb084 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -28,6 +28,19 @@ t = executable('tests', files_tests + configvapi + configtestvapi,
 
 test ('tests', t)
 
+stream_tests = files ([
+               'StreamReaderTest.vala',
+       ])
+
+tstream = executable('stream-reader', stream_tests + configvapi + configtestvapi,
+       vala_args : [],
+       c_args: tests_cargs,
+       dependencies : [ libgxml_deps, inc_libh_dep, testdirs_dep, inc_rooth_dep],
+       link_with: libgxml
+)
+
+test ('stream-reader', tstream)
+
 feedreader_files = ([
        'feedreader-test.vala'
 ])


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