From 493acd119d730207524cd69fa25868c978bdf0f9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 18 Feb 2015 10:45:40 +0100 Subject: Added test for bug with structs that do not posses a field --- test/formats/osml/OsmlParserTest.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/formats') diff --git a/test/formats/osml/OsmlParserTest.cpp b/test/formats/osml/OsmlParserTest.cpp index 3472e5f..4cda20a 100644 --- a/test/formats/osml/OsmlParserTest.cpp +++ b/test/formats/osml/OsmlParserTest.cpp @@ -158,5 +158,19 @@ TEST(OsmlParser, structureInheritance) ASSERT_TRUE(node != nullptr); ASSERT_TRUE(node->isa(&RttiTypes::Domain)); } + +TEST(OsmlParser, structWithNoField) +{ + OsmlStandaloneEnvironment env(logger); + logger.reset(); + + Rooted node = env.parse("struct_with_no_field.osml", "", "", + RttiSet{&RttiTypes::Node}); + ASSERT_FALSE(logger.hasError()); + + ASSERT_TRUE(node != nullptr); + ASSERT_TRUE(node->isa(&RttiTypes::Document)); +} + } -- cgit v1.2.3 From 2de08643afdb4771ef8d1f6dd836ded20db244cf Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 18 Feb 2015 11:32:23 +0100 Subject: Fix for issue #85 -- only allowing explicit fields if no structure elements or data have been given beforehand. Added unit tests. --- src/core/parser/ParserScope.hpp | 9 ++++- src/core/parser/stack/DocumentHandler.cpp | 47 ++++++++++++++++++++---- test/formats/osml/OsmlParserTest.cpp | 27 ++++++++++++++ testdata/osmlparser/explicit_fields.osml | 16 ++++++++ testdata/osmlparser/invalid_explicit_fields.osml | 16 ++++++++ 5 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 testdata/osmlparser/explicit_fields.osml create mode 100644 testdata/osmlparser/invalid_explicit_fields.osml (limited to 'test/formats') diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp index fa78c17..e27c81e 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -286,7 +286,13 @@ enum class ParserFlag { * Set to the boolean value "true" if the head section of a file has passed. * This happens once the first non-import tag is reached. */ - POST_HEAD + POST_HEAD, + + /** + * Set to the boolean value "true" if explicit fields may no longer be + * defined inside a structure element. + */ + POST_EXPLICIT_FIELDS }; /** @@ -797,6 +803,7 @@ public: bool resolveFieldDescriptor(const std::string &name, Handle owner, Logger &logger, ResolutionResultCallback resultCallback); + /** * Tries to resolve all currently deferred resolution steps. The list of * pending deferred resolutions is cleared after this function has run. diff --git a/src/core/parser/stack/DocumentHandler.cpp b/src/core/parser/stack/DocumentHandler.cpp index e959b9a..1df3cb3 100644 --- a/src/core/parser/stack/DocumentHandler.cpp +++ b/src/core/parser/stack/DocumentHandler.cpp @@ -98,6 +98,9 @@ void DocumentChildHandler::createPath(const NodeVector &path, manager(), scope().getLeaf(), parent->getDescriptor()->getFieldDescriptorIndex(), true)}; scope().push(field); + + // Generally allow explicit fields in the new field + scope().setFlag(ParserFlag::POST_EXPLICIT_FIELDS, false); } void DocumentChildHandler::createPath(const size_t &firstFieldIdx, @@ -113,6 +116,9 @@ void DocumentChildHandler::createPath(const size_t &firstFieldIdx, parent = static_cast(transparent.get()); createPath(path, parent, 2); + + // Generally allow explicit fields in the new field + scope().setFlag(ParserFlag::POST_EXPLICIT_FIELDS, false); } bool DocumentChildHandler::start(Variant::mapType &args) @@ -170,12 +176,25 @@ bool DocumentChildHandler::start(Variant::mapType &args) ssize_t newFieldIdx = parent->getDescriptor()->getFieldDescriptorIndex(name()); if (newFieldIdx != -1) { - Rooted field{new DocumentField( - manager(), parentNode, newFieldIdx, false)}; - field->setLocation(location()); - scope().push(field); - isExplicitField = true; - return true; + // Check whether explicit fields are allowed here, if not + if (scope().getFlag(ParserFlag::POST_EXPLICIT_FIELDS)) { + logger().note( + std::string( + "Data or structure commands have already been " + "given, command \"") + + name() + std::string( + "\" is not interpreted explicit " + "field. Move explicit field " + "references to the beginning."), + location()); + } else { + Rooted field{new DocumentField( + manager(), parentNode, newFieldIdx, false)}; + field->setLocation(location()); + scope().push(field); + isExplicitField = true; + return true; + } } } @@ -218,11 +237,17 @@ bool DocumentChildHandler::start(Variant::mapType &args) parent->getDescriptor()->getFieldDescriptorIndex(); } // create the entity for the new element at last. - //TODO: REMOVE + // TODO: REMOVE strct_name = strct->getName(); entity = parent->createChildStructuredEntity(strct, lastFieldIdx, args, nameAttr); } + + // We're past the region in which explicit fields can be defined in the + // parent structure element + scope().setFlag(ParserFlag::POST_EXPLICIT_FIELDS, true); + + // Bush the entity onto the stack entity->setLocation(location()); scope().push(entity); return true; @@ -271,6 +296,10 @@ bool DocumentChildHandler::fieldStart(bool &isDefault, size_t fieldIdx) new DocumentField(manager(), parentNode, fieldIdx, false)}; field->setLocation(location()); scope().push(field); + + // Generally allow explicit fields in the new field + scope().setFlag(ParserFlag::POST_EXPLICIT_FIELDS, false); + return true; } @@ -334,6 +363,10 @@ bool DocumentChildHandler::convertData(Handle field, bool DocumentChildHandler::data(Variant &data) { + // We're past the region in which explicit fields can be defined in the + // parent structure element + scope().setFlag(ParserFlag::POST_EXPLICIT_FIELDS, true); + Rooted parentField = scope().getLeaf(); assert(parentField->isa(&RttiTypes::DocumentField)); diff --git a/test/formats/osml/OsmlParserTest.cpp b/test/formats/osml/OsmlParserTest.cpp index 4cda20a..5127b32 100644 --- a/test/formats/osml/OsmlParserTest.cpp +++ b/test/formats/osml/OsmlParserTest.cpp @@ -172,5 +172,32 @@ TEST(OsmlParser, structWithNoField) ASSERT_TRUE(node->isa(&RttiTypes::Document)); } +TEST(OsmlParser, invalidExplicitFields) +{ + OsmlStandaloneEnvironment env(logger); + logger.reset(); + + ASSERT_FALSE(logger.hasError()); + Rooted node = env.parse("invalid_explicit_fields.osml", "", "", + RttiSet{&RttiTypes::Node}); + ASSERT_TRUE(logger.hasError()); + + ASSERT_TRUE(node != nullptr); + ASSERT_TRUE(node->isa(&RttiTypes::Document)); +} + +TEST(OsmlParser, explicitFields) +{ + OsmlStandaloneEnvironment env(logger); + logger.reset(); + + Rooted node = env.parse("explicit_fields.osml", "", "", + RttiSet{&RttiTypes::Node}); + ASSERT_FALSE(logger.hasError()); + + ASSERT_TRUE(node != nullptr); + ASSERT_TRUE(node->isa(&RttiTypes::Document)); +} + } diff --git a/testdata/osmlparser/explicit_fields.osml b/testdata/osmlparser/explicit_fields.osml new file mode 100644 index 0000000..a9ba1a3 --- /dev/null +++ b/testdata/osmlparser/explicit_fields.osml @@ -0,0 +1,16 @@ +\document + +\domain#test + \struct#a[isRoot=true] + \primitive#b[type=string,isSubtree=true] + \primitive#c[type=string,isSubtree=true] + \primitive#d[type=string,isSubtree=false] + + +\a{! + \b{test} + \c{test} + test + test + test +} diff --git a/testdata/osmlparser/invalid_explicit_fields.osml b/testdata/osmlparser/invalid_explicit_fields.osml new file mode 100644 index 0000000..9986204 --- /dev/null +++ b/testdata/osmlparser/invalid_explicit_fields.osml @@ -0,0 +1,16 @@ +\document + +\domain#test + \struct#a[isRoot=true] + \primitive#b[type=string,isSubtree=true] + \primitive#c[type=string,isSubtree=true] + \primitive#d[type=string,isSubtree=false] + + +\a{! + \b{test} + test + \c{test} + test + test +} -- cgit v1.2.3 From 2623e21ca411b29cee9e98eb1234fac87a74d04f Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 18 Feb 2015 16:34:05 +0100 Subject: added complex document parsing test --- test/formats/osxml/OsxmlParserTest.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'test/formats') diff --git a/test/formats/osxml/OsxmlParserTest.cpp b/test/formats/osxml/OsxmlParserTest.cpp index 5cc0669..3bf4a47 100644 --- a/test/formats/osxml/OsxmlParserTest.cpp +++ b/test/formats/osxml/OsxmlParserTest.cpp @@ -66,15 +66,6 @@ TEST(OsxmlParser, mismatchedTag) ASSERT_TRUE(logger.hasError()); } -TEST(OsxmlParser, generic) -{ - XmlStandaloneEnvironment env(logger); - env.parse("generic.osxml", "", "", RttiSet{&RttiTypes::Node}); -#ifdef MANAGER_GRAPHVIZ_EXPORT - env.manager.exportGraphviz("xmlDocument.dot"); -#endif -} - static void checkAttributes(Handle expected, Handle desc) { @@ -347,6 +338,7 @@ static void checkText(Handle p, Handle expectedParent, TEST(OsxmlParser, documentParsing) { + logger.reset(); XmlStandaloneEnvironment env(logger); Rooted book_document_node = env.parse("simple_book.osxml", "", "", RttiSet{&RttiTypes::Document}); @@ -391,5 +383,21 @@ TEST(OsxmlParser, documentParsing) } } } + + +TEST(OsxmlParser, complexDocumentParsing) +{ + logger.reset(); + XmlStandaloneEnvironment env(logger); + Rooted book_document_node = + env.parse("complex_book.osxml", "", "", RttiSet{&RttiTypes::Document}); + ASSERT_FALSE(logger.hasError()); + 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)); + ASSERT_FALSE(logger.hasError()); +} + } -- cgit v1.2.3