From b1007d94d6a3e49165d2e676ef77abc2fed3b168 Mon Sep 17 00:00:00 2001 From: Daniel Espinosa Date: Tue, 23 Jul 2019 18:38:16 -0500 Subject: [PATCH] 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 c431fd6f..b14cc5b5 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 be638b2f..75a95d8d 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 = """ + + + + + Fred + fweasley@hogwarts.co.uk + + + George + gweasley@hogwarts.co.uk + + + Book1 + + + + + Fred + fweasley@hogwarts.co.uk + + + George + gweasley@hogwarts.co.uk + + + Book1 + +"""; + 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); -- GitLab