diff --git a/include/openspace/documentation/documentation.h b/include/openspace/documentation/documentation.h index f8d86556e1..de85ca0c0d 100644 --- a/include/openspace/documentation/documentation.h +++ b/include/openspace/documentation/documentation.h @@ -273,6 +273,30 @@ struct AnnotationVerifier : public T { std::string annotation; }; +// Boolean Verifiers + +struct AndVerifier : public Verifier { + AndVerifier(Verifier* a, Verifier* b); + + bool test(const ghoul::Dictionary& dict, const std::string& key) const override; + + std::string documentation() const override; + + std::shared_ptr a; + std::shared_ptr b; +}; + +struct OrVerifier : public Verifier { + OrVerifier(Verifier* a, Verifier* b); + + bool test(const ghoul::Dictionary& dict, const std::string& key) const override; + + std::string documentation() const override; + + std::shared_ptr a; + std::shared_ptr b; +}; + using IntLessVerifier = LessVerifier; using DoubleLessVerifier = LessVerifier; using IntLessEqualVerifier = LessEqualVerifier; diff --git a/src/documentation/documentation.cpp b/src/documentation/documentation.cpp index 25cba94fca..9cc663a924 100644 --- a/src/documentation/documentation.cpp +++ b/src/documentation/documentation.cpp @@ -235,5 +235,30 @@ std::string TableVerifier::documentation() const { return "Type: Table" + '\n' + generateDocumentation(doc); } +AndVerifier::AndVerifier(Verifier* a, Verifier* b) + : a(a) + , b(b) {} + +bool AndVerifier::test(const ghoul::Dictionary& dict, const std::string& key) const { + return a->test(dict, key) && b->test(dict, key); +} + +std::string AndVerifier::documentation() const { + return a->documentation() + " and " + b->documentation(); +} + +OrVerifier::OrVerifier(Verifier* a, Verifier* b) + : a(a) + , b(b) +{} + +bool OrVerifier::test(const ghoul::Dictionary& dict, const std::string& key) const { + return a->test(dict, key) || b->test(dict, key); +} + +std::string OrVerifier::documentation() const { + return a->documentation() + " or " + b->documentation(); +} + } // namespace documentation } // namespace openspace diff --git a/tests/test_documentation.inl b/tests/test_documentation.inl index 0ee05b4dfe..67e4ca87f9 100644 --- a/tests/test_documentation.inl +++ b/tests/test_documentation.inl @@ -1648,3 +1648,68 @@ TEST_F(DocumentationTest, WildcardMixed) { EXPECT_EQ("a", negativeRes.offenders[0]); EXPECT_EQ("c", negativeRes.offenders[1]); } + +TEST_F(DocumentationTest, AndOperator) { + using namespace openspace::documentation; + + Documentation doc { + { "a", new AndVerifier( + new IntGreaterEqualVerifier(2), new IntLessEqualVerifier(5) + ) + } + }; + + ghoul::Dictionary positive { + { "a", 4 } + }; + TestResult positiveRes = testSpecification(doc, positive); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenders.size()); + + ghoul::Dictionary negative { + { "a", 0 } + }; + TestResult negativeRes = testSpecification(doc, negative); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("a", negativeRes.offenders[0]); + + ghoul::Dictionary negative2 { + { "a", 8 } + }; + negativeRes = testSpecification(doc, negative2); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("a", negativeRes.offenders[0]); +} + +TEST_F(DocumentationTest, OrOperator) { + using namespace openspace::documentation; + using namespace std::string_literals; + + Documentation doc { + { "a", new OrVerifier(new StringVerifier, new IntVerifier)} + }; + + ghoul::Dictionary positive { + { "a", ""s} + }; + TestResult positiveRes = testSpecification(doc, positive); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenders.size()); + + ghoul::Dictionary positive2 { + { "a", 1 } + }; + positiveRes = testSpecification(doc, positive2); + EXPECT_TRUE(positiveRes.success); + EXPECT_EQ(0, positiveRes.offenders.size()); + + ghoul::Dictionary negative { + { "a", false } + }; + TestResult negativeRes = testSpecification(doc, negative); + EXPECT_FALSE(negativeRes.success); + ASSERT_EQ(1, negativeRes.offenders.size()); + EXPECT_EQ("a", negativeRes.offenders[0]); +}