From 4ce3be9e8cea2f4c5c8f13b21ec23cda0d329797 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 27 Oct 2016 15:50:29 +0200 Subject: [PATCH] Add a Verifier for a list of Strings to replace a more cumbersome expression --- include/openspace/documentation/verifier.h | 13 +++++ src/documentation/verifier.cpp | 8 +++ src/engine/configurationmanager_doc.inl | 14 ++---- tests/test_documentation.inl | 57 ++++++++++++++++++++++ 4 files changed, 82 insertions(+), 10 deletions(-) diff --git a/include/openspace/documentation/verifier.h b/include/openspace/documentation/verifier.h index c28cbd030e..bc8dd451b7 100644 --- a/include/openspace/documentation/verifier.h +++ b/include/openspace/documentation/verifier.h @@ -199,6 +199,19 @@ struct TableVerifier : public TemplateVerifier { Exhaustive exhaustive; }; +/** + * A Verifier that checks whether all values contained in a Table are of type \c string. + */ +struct StringListVerifier : public TableVerifier { + /** + * Constructor for a StringListVerifier. + * \param elementDocumentation The documentation for each string in the list + */ + StringListVerifier(std::string elementDocumentation = ""); + + std::string type() const override; +}; + //---------------------------------------------------------------------------------------- // Vector verifiers //---------------------------------------------------------------------------------------- diff --git a/src/documentation/verifier.cpp b/src/documentation/verifier.cpp index 540f9b9439..28b83f13d1 100644 --- a/src/documentation/verifier.cpp +++ b/src/documentation/verifier.cpp @@ -198,6 +198,14 @@ std::string TableVerifier::type() const { return "Table"; } +StringListVerifier::StringListVerifier(std::string elementDocumentation) + : TableVerifier({{ "*", new StringVerifier, std::move(elementDocumentation) }}) +{} + +std::string StringListVerifier::type() const { + return "List of strings"; +} + ReferencingVerifier::ReferencingVerifier(std::string id) : identifier(std::move(id)) { diff --git a/src/engine/configurationmanager_doc.inl b/src/engine/configurationmanager_doc.inl index ea60c236cf..4e06d9dcef 100644 --- a/src/engine/configurationmanager_doc.inl +++ b/src/engine/configurationmanager_doc.inl @@ -51,9 +51,7 @@ Documentation ConfigurationManager::Documentation() { }, { ConfigurationManager::KeyPaths, - new TableVerifier({ - { "*", new StringVerifier } - }), + new StringListVerifier, "A list of paths that are automatically registered with the file system. " "If a key X is used in the table, it is then useable by referencing ${X} " "in all other configuration files or scripts.", @@ -61,9 +59,7 @@ Documentation ConfigurationManager::Documentation() { }, { ConfigurationManager::KeyFonts, - new TableVerifier({ - { "*", new StringVerifier, "Font paths loadable by FreeType" } - }), + new StringListVerifier("Font paths loadable by FreeType"), "A list of all fonts that will automatically be loaded on startup. Each " "key-value pair contained in the table will become the name and the file " "for a font.", @@ -76,7 +72,7 @@ Documentation ConfigurationManager::Documentation() { ConfigurationManager::PartLogLevel, new StringInListVerifier( // List from logmanager.cpp::levelFromString - {"Debug", "Info", "Warning", "Error", "Fatal", "None" } + { "Debug", "Info", "Warning", "Error", "Fatal", "None" } ), "The severity of log messages that will be displayed. Only " "messages of the selected level or higher will be displayed. All " @@ -312,9 +308,7 @@ Documentation ConfigurationManager::Documentation() { ConfigurationManager::KeyDownloadRequestURL, new OrVerifier( new StringVerifier, - new TableVerifier({ - { "*", new StringVerifier } - }) + new StringListVerifier ), "The URL from which files will be downloaded by the Launcher. This can " "either be a single URL or a list of possible URLs from which the " diff --git a/tests/test_documentation.inl b/tests/test_documentation.inl index 07e256f068..c14af8d1df 100644 --- a/tests/test_documentation.inl +++ b/tests/test_documentation.inl @@ -336,6 +336,63 @@ TEST_F(DocumentationTest, TableVerifierType) { EXPECT_EQ(TestResult::Offense::Reason::MissingKey, negativeRes.offenses[0].reason); } +TEST_F(DocumentationTest, StringListVerifierType) { + using namespace openspace::documentation; + using namespace std::string_literals; + + Documentation doc { + { { "StringList", new StringListVerifier } } + }; + + ghoul::Dictionary positive { + { + "StringList", + ghoul::Dictionary { + { "1", "a"s }, + { "2", "b"s }, + { "3", "c"s } + } + } + }; + TestResult positiveRes = testSpecification(doc, positive); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenses.size()); + + ghoul::Dictionary negative { + { "StringList", 0 } + }; + TestResult negativeRes = testSpecification(doc, negative); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenses.size()); + EXPECT_EQ("StringList", negativeRes.offenses[0].offender); + EXPECT_EQ(TestResult::Offense::Reason::WrongType, negativeRes.offenses[0].reason); + + ghoul::Dictionary negative2 { + { + "StringList", + ghoul::Dictionary { + { "1", "a"s }, + { "2", "b"s }, + { "3", 2.0 } + } + } + }; + negativeRes = testSpecification(doc, negative2); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenses.size()); + EXPECT_EQ("StringList.3", negativeRes.offenses[0].offender); + EXPECT_EQ(TestResult::Offense::Reason::WrongType, negativeRes.offenses[0].reason); + + ghoul::Dictionary negativeExist { + { "StringList2", ghoul::Dictionary{} } + }; + negativeRes = testSpecification(doc, negativeExist); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenses.size()); + EXPECT_EQ("StringList", negativeRes.offenses[0].offender); + EXPECT_EQ(TestResult::Offense::Reason::MissingKey, negativeRes.offenses[0].reason); +} + TEST_F(DocumentationTest, MixedVerifiers) { using namespace openspace::documentation; using namespace std::string_literals;