From 928d989aef13e72064f31e3d6cad64f46c56bfdc Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 10 Feb 2015 20:57:16 +0100 Subject: added a document parsing test. --- test/plugins/xml/XmlParserTest.cpp | 91 +++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) (limited to 'test/plugins') diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp index af1ef56..067214c 100644 --- a/test/plugins/xml/XmlParserTest.cpp +++ b/test/plugins/xml/XmlParserTest.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -303,12 +304,98 @@ TEST(XmlParser, domainParsing) checkFieldDescriptor(heading, paragraph, {text, comment}); } +static void checkStructuredEntity( + Handle s, Handle expectedParent, Handle strct, + const Variant::mapType &expectedAttributes = Variant::mapType{}, + const std::string &expectedName = "") +{ + ASSERT_FALSE(s == nullptr); + ASSERT_TRUE(s->isa(&RttiTypes::StructuredEntity)); + Rooted entity = s.cast(); + ASSERT_EQ(expectedParent, entity->getParent()); + ASSERT_EQ(strct, entity->getDescriptor()); + ASSERT_EQ(expectedAttributes, entity->getAttributes()); + ASSERT_EQ(expectedName, entity->getName()); +} + +static void checkStructuredEntity( + Handle s, Handle expectedParent, Handle doc, + const std::string &className, + const Variant::mapType &expectedAttributes = Variant::mapType{}, + const std::string &expectedName = "") +{ + auto res = doc->resolve(&RttiTypes::StructuredClass, className); + if (res.size() != 1) { + throw OusiaException("resolution error!"); + } + Handle sc = res[0].node.cast(); + checkStructuredEntity(s, expectedParent, sc, expectedAttributes, + expectedName); +} + +static void checkText(Handle p, Handle expectedParent, + Handle doc, Variant expected) +{ + checkStructuredEntity(p, expectedParent, doc, "paragraph"); + Rooted par = p.cast(); + ASSERT_EQ(1, par->getField().size()); + checkStructuredEntity(par->getField()[0], par, doc, "text"); + Rooted text = par->getField()[0].cast(); + ASSERT_EQ(1, text->getField().size()); + + Handle d = text->getField()[0]; + ASSERT_FALSE(d == nullptr); + ASSERT_TRUE(d->isa(&RttiTypes::DocumentPrimitive)); + Rooted prim = d.cast(); + ASSERT_EQ(text, prim->getParent()); + ASSERT_EQ(expected, prim->getContent()); +} + TEST(XmlParser, documentParsing) { XmlStandaloneEnvironment env(logger); - Rooted book_domain_node = + Rooted book_document_node = env.parse("simple_book.oxd", "", "", RttiSet{&RttiTypes::Document}); - //TODO: Check result + ASSERT_FALSE(book_document_node == nullptr); + ASSERT_TRUE(book_document_node->isa(&RttiTypes::Document)); + Rooted doc = book_document_node.cast(); + ASSERT_TRUE(doc->validate(logger)); + checkStructuredEntity(doc->getRoot(), doc, doc, "book"); + { + Rooted book = doc->getRoot(); + ASSERT_EQ(2, book->getField().size()); + checkText(book->getField()[0], book, doc, + "This might be some introductory text or a dedication."); + checkStructuredEntity(book->getField()[1], book, doc, "chapter", + Variant::mapType{}, "myFirstChapter"); + { + Rooted chapter = + book->getField()[1].cast(); + ASSERT_EQ(3, chapter->getField().size()); + checkText(chapter->getField()[0], chapter, doc, + "Here we might have an introduction to the chapter."); + checkStructuredEntity(chapter->getField()[1], chapter, doc, + "section", Variant::mapType{}, + "myFirstSection"); + { + Rooted section = + chapter->getField()[1].cast(); + ASSERT_EQ(1, section->getField().size()); + checkText(section->getField()[0], section, doc, + "Here we might find the actual section content."); + } + checkStructuredEntity(chapter->getField()[2], chapter, doc, + "section", Variant::mapType{}, + "mySndSection"); + { + Rooted section = + chapter->getField()[2].cast(); + ASSERT_EQ(1, section->getField().size()); + checkText(section->getField()[0], section, doc, + "Here we might find the actual section content."); + } + } + } } } -- cgit v1.2.3