From 45d034ad2a3a6a7bcdf0451d51182f1aaadf2509 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Tue, 13 Sep 2016 20:27:29 +0200 Subject: [PATCH] Enabling optional arguments Adding tests to the OpenSpaceTest --- ext/ghoul | 2 +- include/openspace/util/documentation.h | 5 +- src/util/documentation.cpp | 16 ++++-- tests/main.cpp | 2 + tests/test_documentation.inl | 77 ++++++++++++++++---------- 5 files changed, 67 insertions(+), 35 deletions(-) diff --git a/ext/ghoul b/ext/ghoul index 5120a2d60b..e10dc7cc5f 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 5120a2d60b61303369c305e66c4fcd334c249a02 +Subproject commit e10dc7cc5fbfd5c4d74b525f55ade7bc339e5bc6 diff --git a/include/openspace/util/documentation.h b/include/openspace/util/documentation.h index a049e785cc..d3470dfdc6 100644 --- a/include/openspace/util/documentation.h +++ b/include/openspace/util/documentation.h @@ -26,6 +26,7 @@ #define __DOCUMENTATION_H__ #include +#include #include #include @@ -43,6 +44,8 @@ struct TestResult { std::vector offenders; }; +using Optional = ghoul::Boolean; + struct Verifier { virtual TestResult operator()(const ghoul::Dictionary& dict, const std::string& key) const; @@ -54,7 +57,7 @@ struct Verifier { struct DocumentationEntry { - DocumentationEntry(std::string key, Verifier* t, bool optional = false, + DocumentationEntry(std::string key, Verifier* t, Optional optional = Optional::No, std::string doc = ""); std::string key; diff --git a/src/util/documentation.cpp b/src/util/documentation.cpp index c4fd5ca8a6..affa1d73ed 100644 --- a/src/util/documentation.cpp +++ b/src/util/documentation.cpp @@ -72,7 +72,8 @@ template struct AnnotationVerifier; TestResult Verifier::operator()(const ghoul::Dictionary& dict, - const std::string& key) const { + const std::string& key) const +{ bool testSuccess = test(dict, key); if (testSuccess) { return{ testSuccess, {} }; @@ -87,17 +88,22 @@ bool Verifier::test(const ghoul::Dictionary& dict, const std::string& key) const }; DocumentationEntry::DocumentationEntry(std::string key, Verifier* t, - bool optional, std::string doc) + Optional optional, std::string doc) : key(std::move(key)) , tester(std::move(t)) , optional(optional) , documentation(std::move(doc)) {} -TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& dictionary) { +TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& dictionary){ TestResult result; result.success = true; for (const auto& p : d) { + if (p.optional && !dictionary.hasKey(p.key)) { + // If the key is optional and it doesn't exist, we don't need to check it + // if the key exists, it has to be correct, however + continue; + } Verifier& verifier = *(p.tester); TestResult res = verifier(dictionary, p.key); if (!res.success) { @@ -177,7 +183,9 @@ TableVerifier::TableVerifier(Documentation d) : doc(std::move(d)) {} -TestResult TableVerifier::operator()(const ghoul::Dictionary& dict, const std::string& key) const { +TestResult TableVerifier::operator()(const ghoul::Dictionary& dict, + const std::string& key) const +{ if (dict.hasKeyAndValue(key)) { ghoul::Dictionary d = dict.value(key); TestResult res = testSpecification(doc, d); diff --git a/tests/main.cpp b/tests/main.cpp index 0cbe2b6040..354d923c9c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -58,6 +58,8 @@ //#include #endif +#include + #include #include #include diff --git a/tests/test_documentation.inl b/tests/test_documentation.inl index ea48200ef1..4d002cbdba 100644 --- a/tests/test_documentation.inl +++ b/tests/test_documentation.inl @@ -30,14 +30,6 @@ #include -/* - * To test: - * optional values - * - * to add: - * external template instantiations -*/ - class DocumentationTest : public testing::Test {}; TEST_F(DocumentationTest, Constructor) { @@ -506,6 +498,54 @@ TEST_F(DocumentationTest, NestedTables) { EXPECT_EQ("Outer_Table2.Inner_Table.Inner_Inner_Int", negativeRes.offenders[2]); } +TEST_F(DocumentationTest, Optional) { + using namespace openspace::documentation; + + Documentation doc { + { "Bool_Force", new BoolVerifier, Optional::No }, + { "Bool_Optional", new BoolVerifier, Optional::Yes } + }; + + ghoul::Dictionary positive { + { "Bool_Force", true }, + }; + TestResult positiveRes = testSpecification(doc, positive); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenders.size()); + + ghoul::Dictionary positive2 { + { "Bool_Force", true }, + { "Bool_Optional", true } + }; + positiveRes = testSpecification(doc, positive); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenders.size()); + + ghoul::Dictionary negative { + }; + TestResult negativeRes = testSpecification(doc, negative); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("Bool_Force", negativeRes.offenders[0]); + + ghoul::Dictionary negative2 { + { "Bool_Optional", true } + }; + negativeRes = testSpecification(doc, negative2); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("Bool_Force", negativeRes.offenders[0]); + + ghoul::Dictionary negative3 { + { "Bool_Force", true }, + { "Bool_Optional", 1 } + }; + negativeRes = testSpecification(doc, negative3); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("Bool_Optional", negativeRes.offenders[0]); +} + TEST_F(DocumentationTest, LessInt) { using namespace openspace::documentation; @@ -1421,24 +1461,3 @@ TEST_F(DocumentationTest, NotInRangeDouble) { ASSERT_EQ(1, negativeRes.offenders.size()); EXPECT_EQ("Double", negativeRes.offenders[0]); } - - -//TEST_F(DocumentationTest, LessBool) { -// using namespace openspace::documentation; -// -// Documentation doc { -// }; -// -// ghoul::Dictionary positive { -// }; -// TestResult positiveRes = testSpecification(doc, positive); -// EXPECT_TRUE(positiveRes.success); -// EXPECT_EQ(0, positiveRes.offenders.size()); -// -// ghoul::Dictionary negative { -// }; -// TestResult negativeRes = testSpecification(doc, negative); -// EXPECT_FALSE(negativeRes.success); -// ASSERT_EQ(1, negativeRes.offenders.size()); -// EXPECT_EQ("", negativeRes.offenders[0]); -//} \ No newline at end of file