[gxml] StreamReader: prepraration for serialization tests
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] StreamReader: prepraration for serialization tests
- Date: Sun, 28 Jul 2019 04:04:34 +0000 (UTC)
commit b1007d94d6a3e49165d2e676ef77abc2fed3b168
Author: Daniel Espinosa <esodan gmail com>
Date: Tue Jul 23 18:38:16 2019 -0500
StreamReader: prepraration for serialization tests
gxml/StreamReader.vala | 10 ++-
test/StreamReaderTest.vala | 162 ++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 169 insertions(+), 3 deletions(-)
---
diff --git a/gxml/StreamReader.vala b/gxml/StreamReader.vala
index c431fd6..b14cc5b 100644
--- a/gxml/StreamReader.vala
+++ b/gxml/StreamReader.vala
@@ -62,9 +62,16 @@ public class GXml.StreamReader : GLib.Object {
private inline uint8 cur_byte () {
return buf[0];
}
-
public DomDocument read () throws GLib.Error {
_document = new Document ();
+ internal_read ();
+ return document;
+ }
+ public void read_document (DomDocument doc) throws GLib.Error {
+ _document = doc;
+ internal_read ();
+ }
+ private void internal_read () throws GLib.Error {
read_byte ();
if (cur_char () != '<') {
throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: should start with '<'"));
@@ -85,7 +92,6 @@ public class GXml.StreamReader : GLib.Object {
}
var re = read_root_element ();
document.append_child (re);
- return document;
}
public GXml.Element read_root_element () throws GLib.Error {
return read_element (true);
diff --git a/test/StreamReaderTest.vala b/test/StreamReaderTest.vala
index be638b2..75a95d8 100644
--- a/test/StreamReaderTest.vala
+++ b/test/StreamReaderTest.vala
@@ -21,6 +21,115 @@
*/
using GXml;
+class ContentNode : GXml.Element {
+ string _val = null;
+ public string val {
+ get {
+ _val = text_content;
+ return _val;
+ }
+ set {
+ text_content = _val;
+ }
+ }
+}
+
+class Name : ContentNode {
+ construct {
+ try {
+ initialize ("Name");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+}
+class Email : ContentNode {
+ construct {
+ try {
+ initialize ("Email");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+}
+
+class Author : GXml.Element {
+ public Name name { get; set; }
+ public Email email { get; set; }
+ construct {
+ try {
+ initialize ("Author");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+ public class Collection : GXml.ArrayList {
+ construct {
+ try {
+ initialize (typeof (Author));
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+ }
+}
+
+class Authors : GXml.Element {
+ public Author.Collection authors { get; set; }
+ construct {
+ try {
+ initialize ("Authors");
+ set_instance_property ("authors");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+}
+
+class Book : GXml.Element {
+ [Description(nick="::year")]
+ public int year { get; set; }
+ [Description(nick="::ISBN")]
+ public string ISBN { get; set; }
+ construct {
+ try {
+ initialize ("Book");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+ public class Collection : GXml.ArrayList {
+ construct {
+ try {
+ initialize (typeof (Book));
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+ }
+}
+
+class BookStore : GXml.Element {
+ public Book.Collection books { get; set; }
+ construct {
+ try {
+ initialize ("BookStore");
+ set_instance_property ("books");
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ }
+}
+class Library : GXml.Document {
+ [Description(nick="::ROOT")]
+ public BookStore store { get; set; }
+ public void read (string str) throws GLib.Error {
+ var istream = new MemoryInputStream.from_data (str.data, null);
+ var sr = new StreamReader (istream);
+ sr.read_document (this);
+ }
+}
+
class GXmlTest {
public static int main (string[] args) {
Test.init (ref args);
@@ -106,7 +215,58 @@ class GXmlTest {
(doc.document_element as
GXml.Element).parse_buffer.end (res);
message (doc.write_string ());
assert ((doc.document_element as
GXml.Element).read_buffer == null);
- //assert
((doc.document_element.child_nodes.item (0) as GXml.Element).read_buffer == null);
+ loop.quit ();
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ });
+ } catch (GLib.Error e) {
+ warning ("Error while reading stream: %s", e.message);
+ }
+ return Source.REMOVE;
+ });
+ loop.run ();
+ });
+ Test.add_func ("/gxml/stream-reader/serialization", () => {
+ var loop = new GLib.MainLoop (null);
+ Idle.add (()=>{
+ string str = """<?xml version="1.0"?>
+<BookStore>
+ <book year="2014" isbn="ISBN83763550019---11">
+ <Authors>
+ <Author>
+ <Name>Fred</Name>
+ <Email>fweasley hogwarts co uk</Email>
+ </Author>
+ <Author>
+ <Name>George</Name>
+ <Email>gweasley hogwarts co uk</Email>
+ </Author>
+ </Authors>
+ <name>Book1</name>
+ </book>
+ <book year="2014" isbn="ISBN83763550019---11">
+ <Authors>
+ <Author>
+ <Name>Fred</Name>
+ <Email>fweasley hogwarts co uk</Email>
+ </Author>
+ <Author>
+ <Name>George</Name>
+ <Email>gweasley hogwarts co uk</Email>
+ </Author>
+ </Authors>
+ <name>Book1</name>
+ </book>
+</BookStore>""";
+ var doc = new Library ();
+ try {
+ doc.read (str);
+ (doc.document_element as GXml.Element).parse_buffer.begin ((obj,
res)=>{
+ try {
+ (doc.document_element as
GXml.Element).parse_buffer.end (res);
+ message (doc.write_string ());
+ assert ((doc.document_element as
GXml.Element).read_buffer == null);
loop.quit ();
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]