Enabling optional arguments

Adding tests to the OpenSpaceTest
This commit is contained in:
Alexander Bock
2016-09-13 20:27:29 +02:00
parent a71cccd58f
commit 45d034ad2a
5 changed files with 67 additions and 35 deletions

View File

@@ -26,6 +26,7 @@
#define __DOCUMENTATION_H__
#include <ghoul/misc/assert.h>
#include <ghoul/misc/boolean.h>
#include <ghoul/misc/dictionary.h>
#include <iterator>
@@ -43,6 +44,8 @@ struct TestResult {
std::vector<std::string> 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;

View File

@@ -72,7 +72,8 @@ template struct AnnotationVerifier<TableVerifier>;
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<Type>(key)) {
ghoul::Dictionary d = dict.value<Type>(key);
TestResult res = testSpecification(doc, d);

View File

@@ -58,6 +58,8 @@
//#include <test_iswamanager.inl>
#endif
#include <test_documentation.inl>
#include <openspace/engine/openspaceengine.h>
#include <openspace/engine/wrapper/windowwrapper.h>
#include <openspace/engine/configurationmanager.h>

View File

@@ -30,14 +30,6 @@
#include <string>
/*
* 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]);
//}