From 7d22aac0c7be52381822abdd0cb6860deaf01096 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Sun, 4 Jan 2015 19:30:56 +0100 Subject: fixed a nasty bug in the getField method of DocumentEntity. Also corrected some issues in the TestDocument and TestDomain. --- test/core/model/DocumentTest.cpp | 62 +++++++++++++++++++++++++++++++++++++++- test/core/model/TestDocument.hpp | 26 +++++++++++------ test/core/model/TestDomain.hpp | 15 ++++++++-- 3 files changed, 91 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp index 9e3229c..a671d2c 100644 --- a/test/core/model/DocumentTest.cpp +++ b/test/core/model/DocumentTest.cpp @@ -37,8 +37,68 @@ TEST(Document, testDocumentConstruction) // Construct the document. Rooted doc = constructBookDocument(mgr, domain); - // If that works we are happy already. + // Check the document content. ASSERT_FALSE(doc.isNull()); + // get root node. + Rooted root = doc->getRoot(); + ASSERT_FALSE(root.isNull()); + ASSERT_EQ("book", root->getDescriptor()->getName()); + ASSERT_TRUE(root->hasField()); + ASSERT_EQ(2, root->getField().size()); + // get foreword (paragraph) + { + Rooted foreword = root->getField()[0]; + ASSERT_FALSE(foreword.isNull()); + ASSERT_EQ("paragraph", foreword->getDescriptor()->getName()); + // it should contain one text node + ASSERT_TRUE(foreword->hasField()); + ASSERT_EQ(1, foreword->getField().size()); + // which in turn should have a primitive content field containing the + // right text. + { + Rooted text = foreword->getField()[0]; + ASSERT_FALSE(text.isNull()); + ASSERT_EQ("text", text->getDescriptor()->getName()); + ASSERT_TRUE(text->hasField()); + ASSERT_EQ(1, text->getField().size()); + ASSERT_TRUE(text->getField()[0]->isa(typeOf())); + Variant content = + text->getField()[0].cast()->getContent(); + ASSERT_EQ("Some introductory text", content.asString()); + } + } + // get section + { + Rooted section = root->getField()[1]; + ASSERT_FALSE(section.isNull()); + ASSERT_EQ("section", section->getDescriptor()->getName()); + // it should contain one paragraph + ASSERT_TRUE(section->hasField()); + ASSERT_EQ(1, section->getField().size()); + { + Rooted par = section->getField()[0]; + ASSERT_FALSE(par.isNull()); + ASSERT_EQ("paragraph", par->getDescriptor()->getName()); + // it should contain one text node + ASSERT_TRUE(par->hasField()); + ASSERT_EQ(1, par->getField().size()); + // which in turn should have a primitive content field containing + // the + // right text. + { + Rooted text = par->getField()[0]; + ASSERT_FALSE(text.isNull()); + ASSERT_EQ("text", text->getDescriptor()->getName()); + ASSERT_TRUE(text->hasField()); + ASSERT_EQ(1, text->getField().size()); + ASSERT_TRUE( + text->getField()[0]->isa(typeOf())); + Variant content = + text->getField()[0].cast()->getContent(); + ASSERT_EQ("Some actual text", content.asString()); + } + } + } } } } diff --git a/test/core/model/TestDocument.hpp b/test/core/model/TestDocument.hpp index a1a3434..6b0267a 100644 --- a/test/core/model/TestDocument.hpp +++ b/test/core/model/TestDocument.hpp @@ -50,13 +50,18 @@ static Rooted constructBookDocument(Manager &mgr, return {nullptr}; } // Add its text. - Variant text{std::map{ - {"content", Variant("Some introductory text")}}}; - Rooted foreword_text = - DocumentPrimitive::buildEntity(foreword, text, "text"); + Rooted foreword_text = + StructuredEntity::buildEntity(foreword, {bookDomain}, "text"); if (foreword_text.isNull()) { return {nullptr}; } + // And its primitive content + Variant text{"Some introductory text"}; + Rooted foreword_primitive = + DocumentPrimitive::buildEntity(foreword_text, text, "content"); + if (foreword_primitive.isNull()) { + return {nullptr}; + } // Add a section. Rooted section = StructuredEntity::buildEntity(root, {bookDomain}, "section"); @@ -67,13 +72,18 @@ static Rooted constructBookDocument(Manager &mgr, return {nullptr}; } // Add its text. - text = Variant{std::map{ - {"content", Variant("Some introductory text")}}}; - Rooted main_text = - DocumentPrimitive::buildEntity(foreword, text, "text"); + Rooted main_text = + StructuredEntity::buildEntity(main, {bookDomain}, "text"); if (main_text.isNull()) { return {nullptr}; } + // And its primitive content + text = Variant{"Some actual text"}; + Rooted main_primitive = + DocumentPrimitive::buildEntity(main_text, text, "content"); + if (main_primitive.isNull()) { + return {nullptr}; + } return doc; } diff --git a/test/core/model/TestDomain.hpp b/test/core/model/TestDomain.hpp index d55bff7..f457531 100644 --- a/test/core/model/TestDomain.hpp +++ b/test/core/model/TestDomain.hpp @@ -81,11 +81,20 @@ static Rooted constructBookDomain(Manager &mgr, Logger &logger) section_field->getChildren().push_back(paragraph); book_field->getChildren().push_back(paragraph); domain->getStructureClasses().push_back(paragraph); + // And the field of it. + Rooted paragraph_field{new FieldDescriptor(mgr, paragraph)}; + paragraph->getFieldDescriptors().push_back(paragraph_field); + + // Finally we add the "text" node, which is transparent as well. + Rooted text{new StructuredClass( + mgr, "text", domain, any, {nullptr}, {nullptr}, true)}; + paragraph_field->getChildren().push_back(text); + domain->getStructureClasses().push_back(text); // ... and has a primitive field. - Rooted paragraph_field{new FieldDescriptor( - mgr, paragraph, domain->getTypesystems()[0]->getTypes()[1], "text", + Rooted text_field{new FieldDescriptor( + mgr, text, domain->getTypesystems()[0]->getTypes()[0], "content", false)}; - paragraph->getFieldDescriptors().push_back(paragraph_field); + text->getFieldDescriptors().push_back(text_field); return domain; } -- cgit v1.2.3 From 319ad738f677a20403cc27192f1df7bb65ce8c0e Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Sun, 4 Jan 2015 19:40:21 +0100 Subject: corrected first draft of DemoOutput. Still some TODOs remain, but for the easy test document everything works. --- CMakeLists.txt | 13 +++++++++- src/plugins/html/DemoOutput.cpp | 26 +++++++++---------- test/plugins/html/DemoOutputTest.cpp | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 test/plugins/html/DemoOutputTest.cpp (limited to 'test') diff --git a/CMakeLists.txt b/CMakeLists.txt index ddb087e..10e43ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -241,6 +241,16 @@ IF(TEST) ousia_css ) + ADD_EXECUTABLE(ousia_test_html + test/plugins/html/DemoOutputTest + ) + + TARGET_LINK_LIBRARIES(ousia_test_html + ${GTEST_LIBRARIES} + ousia_core + ousia_html + ) + ADD_EXECUTABLE(ousia_test_xml test/plugins/xml/XmlParserTest ) @@ -264,8 +274,9 @@ IF(TEST) # Register the unit tests ADD_TEST(ousia_test_core ousia_test_core) ADD_TEST(ousia_test_boost ousia_test_boost) - ADD_TEST(ousia_test_xml ousia_test_xml) ADD_TEST(ousia_test_css ousia_test_css) + ADD_TEST(ousia_test_html ousia_test_html) + ADD_TEST(ousia_test_xml ousia_test_xml) # ADD_TEST(ousia_test_mozjs ousia_test_mozjs) ENDIF() diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp index 6b9b3a4..463a5d2 100644 --- a/src/plugins/html/DemoOutput.cpp +++ b/src/plugins/html/DemoOutput.cpp @@ -31,7 +31,7 @@ void DemoHTMLTransformer::writeHTML(Handle doc, out << "\n"; out << "\n"; out << "\t\n"; - out << "\t\tTest HTML Output for" << doc->getName() << "\n"; + out << "\t\tTest HTML Output for " << doc->getName() << "\n"; out << "\t\n"; out << "\t\n"; @@ -76,10 +76,9 @@ void DemoHTMLTransformer::writeSection(Handle sec, // if the input node is no section, we ignore it. return; } - // get the fields. - std::vector> &fields = sec->getFields(); // check if we have a heading. - if (fields.size() > 1 && fields[1].size() > 0) { + if (sec->hasField("heading")) { + Rooted heading = sec->getField("heading")[0]; out << "\t\t"; switch (type) { case SectionType::BOOK: @@ -99,7 +98,7 @@ void DemoHTMLTransformer::writeSection(Handle sec, break; } // the second field marks the heading. So let's write it. - writeParagraph(fields[1][0], out, false); + writeParagraph(heading, out, false); // close the heading tag. switch (type) { case SectionType::BOOK: @@ -122,7 +121,8 @@ void DemoHTMLTransformer::writeSection(Handle sec, } // then write the section content recursively. - for (auto &n : fields[0]) { + NodeVector mainField = sec->getField(); + for (auto &n : mainField) { /* * Strictly speaking this is the wrong mechanism, because we would have * to make an "isa" call here because we can not rely on our knowledge @@ -149,15 +149,13 @@ void DemoHTMLTransformer::writeParagraph(Handle par, if (par->getDescriptor()->getName() != "paragraph") { throw OusiaException("Expected paragraph!"); } - // get the fields. - std::vector> &fields = par->getFields(); - // write heading if its there. // check if we have a heading. - if (fields.size() > 1 && fields[1].size() > 0) { + if (par->hasField("heading")) { + Rooted heading = par->getField("heading")[0]; // start the heading tag out << "\t\t
"; // the second field marks the heading. So let's write it. - writeParagraph(fields[1][0], out, false); + writeParagraph(heading, out, false); // close the heading tag. out << "
\n"; } @@ -167,13 +165,13 @@ void DemoHTMLTransformer::writeParagraph(Handle par, } // write content // TODO: What about emphasis? - for (auto &text : fields[0]) { + for (auto &text : par->getField()) { if (text->getDescriptor()->getName() != "text") { throw OusiaException("Expected text!"); } Handle primitive = - text->getFields()[0][0].cast(); - if (primitive == nullptr) { + text->getField()[0].cast(); + if (primitive.isNull()) { throw OusiaException("Text field is not primitive!"); } out << primitive->getContent().asString(); diff --git a/test/plugins/html/DemoOutputTest.cpp b/test/plugins/html/DemoOutputTest.cpp new file mode 100644 index 0000000..b81a001 --- /dev/null +++ b/test/plugins/html/DemoOutputTest.cpp @@ -0,0 +1,49 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +#include + +#include +#include + +#include +#include + +namespace ousia { +namespace html { + +TEST(DemoHTMLTransformer, writeHTML) +{ + // Construct Manager + Logger logger; + Manager mgr{1}; + // Get the domain. + Rooted domain = model::constructBookDomain(mgr, logger); + // Construct the document. + Rooted doc = model::constructBookDocument(mgr, domain); + + // print it + DemoHTMLTransformer transformer; + transformer.writeHTML(doc, std::cout); +} +} +} -- cgit v1.2.3