From 0fd65daf57959fe5409a624bffcd5998fc55d332 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Sun, 18 Jan 2015 16:52:54 +0100 Subject: improved Document validation process and added some test cases. Further tests are required, though. --- test/core/model/DocumentTest.cpp | 88 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp index 4b0447d..d8ccef1 100644 --- a/test/core/model/DocumentTest.cpp +++ b/test/core/model/DocumentTest.cpp @@ -31,7 +31,7 @@ namespace ousia { namespace model { -TEST(Document, testDocumentConstruction) +TEST(Document, construct) { // Construct Manager TerminalLogger logger{std::cerr, true}; @@ -109,5 +109,91 @@ TEST(Document, testDocumentConstruction) } } } + +TEST(Document, validate) +{ + // Let's start with a trivial domain and a trivial document. + TerminalLogger logger{std::cerr, true}; + Manager mgr{1}; + Rooted sys{new SystemTypesystem(mgr)}; + Rooted domain{new Domain(mgr, sys, "trivial")}; + Cardinality single; + single.merge({1}); + // Set up the "root" StructuredClass. + Rooted rootClass{new StructuredClass( + mgr, "root", domain, single, {nullptr}, {nullptr}, false, true)}; + + // set up a document for it. + { + // first an invalid one, which is empty. + Rooted doc{new Document(mgr, "myDoc.oxd")}; + doc->addDomain(domain); + ASSERT_FALSE(doc->validate(logger)); + // then add a root, which should make it valid. + Rooted root = + buildRootStructuredEntity(doc, logger, {"root"}); + ASSERT_TRUE(doc->validate(logger)); + } + + // now let's extend the rootClass with a default field. + Rooted rootField{new FieldDescriptor(mgr, rootClass)}; + // and add a child class for it. + Rooted childClass{ + new StructuredClass(mgr, "child", domain, single)}; + rootField->addChild(childClass); + { + /* + * now check again: Because the child has the cardinality {1} our + * document should be invalid again. + */ + Rooted doc{new Document(mgr, "myDoc.oxd")}; + doc->addDomain(domain); + Rooted root = + buildRootStructuredEntity(doc, logger, {"root"}); + ASSERT_FALSE(doc->validate(logger)); + // but it should get valid if we add a proper child. + buildStructuredEntity(doc, logger, root, {"child"}); + ASSERT_TRUE(doc->validate(logger)); + // and it should get invalid again if we add one more child. + buildStructuredEntity(doc, logger, root, {"child"}); + ASSERT_FALSE(doc->validate(logger)); + } + /* + * Add a further extension to the domain: We add a subclass to child. + */ + Rooted childSubClass{new StructuredClass( + mgr, "childSub", domain, single, {nullptr}, childClass)}; + { + /* + * A document with one instance of the Child subclass should be valid. + */ + Rooted doc{new Document(mgr, "myDoc.oxd")}; + doc->addDomain(domain); + Rooted root = + buildRootStructuredEntity(doc, logger, {"root"}); + buildStructuredEntity(doc, logger, root, {"childSub"}); + ASSERT_TRUE(doc->validate(logger)); + } + /* + * Make it even more complicated: child gets a field for further child + * instances now. + */ + Rooted childField{new FieldDescriptor(mgr, childClass)}; + childField->addChild(childClass); + { + /* + * Now a document with one instance of the Child subclass should be + * invalid, because it has no children of itself. + */ + Rooted doc{new Document(mgr, "myDoc.oxd")}; + doc->addDomain(domain); + Rooted root = + buildRootStructuredEntity(doc, logger, {"root"}); + buildStructuredEntity(doc, logger, root, {"childSub"}); + ASSERT_FALSE(doc->validate(logger)); + } + // TODO: Override child field in childSub such that an empty childSub is + // valid. +} } } -- cgit v1.2.3