From 43db84f62077d78a9850f2546846bfee359177b4 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 15 Sep 2016 13:40:39 +0200 Subject: [PATCH] Add method to automatically test a dictionary and throw on error --- .../openspace/documentation/documentation.h | 2 ++ src/documentation/documentation.cpp | 32 +++++++++++++------ src/engine/configurationmanager.cpp | 9 ++---- src/scene/scene.cpp | 9 ++---- src/scene/scenegraphnode.cpp | 11 ++----- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/include/openspace/documentation/documentation.h b/include/openspace/documentation/documentation.h index 3f1074adcb..2d18c37249 100644 --- a/include/openspace/documentation/documentation.h +++ b/include/openspace/documentation/documentation.h @@ -73,6 +73,8 @@ struct Documentation { TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& dictionary); +void testSpecificationAndThrow(const Documentation& doc, + const ghoul::Dictionary& dictionary, std::string component); std::string generateDocumentation(const Documentation& d); diff --git a/src/documentation/documentation.cpp b/src/documentation/documentation.cpp index 71b026b66e..7c1a917adf 100644 --- a/src/documentation/documentation.cpp +++ b/src/documentation/documentation.cpp @@ -42,8 +42,7 @@ namespace documentation { SpecificationError::SpecificationError(TestResult result, std::string component) : ghoul::RuntimeError("Error in specification", std::move(component)) - , result(std::move(result)) -{} + , result(std::move(result)) {} DocumentationEntry::DocumentationEntry(std::string key, Verifier* t, std::string doc, @@ -51,19 +50,16 @@ DocumentationEntry::DocumentationEntry(std::string key, Verifier* t, std::string : key(std::move(key)) , tester(std::move(t)) , documentation(std::move(doc)) - , optional(optional) -{} + , optional(optional) {} Documentation::Documentation(std::string name, DocumentationEntries entries) : name(std::move(name)) - , entries(std::move(entries)) -{} + , entries(std::move(entries)) {} Documentation::Documentation(DocumentationEntries entries) - : Documentation("", std::move(entries)) -{} + : Documentation("", std::move(entries)) {} -TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& dictionary){ +TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& dictionary) { TestResult result; result.success = true; @@ -108,11 +104,27 @@ TestResult testSpecification(const Documentation& d, const ghoul::Dictionary& di ); result.offenders = std::vector( uniqueOffenders.begin(), uniqueOffenders.end() - ); + ); return result; } +void testSpecificationAndThrow(const Documentation& doc, + const ghoul::Dictionary& dictionary, std::string component) + +{ + // Perform testing against the documentation/specification + using namespace openspace::documentation; + TestResult testResult = testSpecification( + doc, + dictionary + ); + if (!testResult.success) { + throw SpecificationError(std::move(testResult), std::move(component)); + } +} + + std::string generateDocumentation(const Documentation& d) { using namespace std::string_literals; std::string result; diff --git a/src/engine/configurationmanager.cpp b/src/engine/configurationmanager.cpp index f903f36f25..c0bfb1fc1d 100644 --- a/src/engine/configurationmanager.cpp +++ b/src/engine/configurationmanager.cpp @@ -113,14 +113,11 @@ void ConfigurationManager::loadFromFile(const string& filename) { ghoul::lua::loadDictionaryFromFile(filename, *this); // Perform testing against the documentation/specification - using namespace openspace::documentation; - TestResult result = testSpecification( + openspace::documentation::testSpecificationAndThrow( ConfigurationManager::Documentation(), - *this + *this, + "ConfigurationManager" ); - if (!result.success) { - throw SpecificationError(result, "ConfigurationManager"); - } // Register all the paths ghoul::Dictionary dictionary = value(KeyPaths); diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index 7181a1d49d..c10b2ca2dd 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -190,14 +190,11 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { ); // Perform testing against the documentation/specification - using namespace openspace::documentation; - TestResult result = testSpecification( + openspace::documentation::testSpecificationAndThrow( Scene::Documentation(), - dictionary + dictionary, + "Scene" ); - if (!result.success) { - throw SpecificationError(result, "Scene"); - } _graph.loadFromFile(sceneDescriptionFilePath); diff --git a/src/scene/scenegraphnode.cpp b/src/scene/scenegraphnode.cpp index bf00e499ad..e3334e178d 100644 --- a/src/scene/scenegraphnode.cpp +++ b/src/scene/scenegraphnode.cpp @@ -69,16 +69,11 @@ const std::string SceneGraphNode::KeyParentName = "Parent"; const std::string SceneGraphNode::KeyDependencies = "Dependencies"; SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& dictionary){ - // Perform testing against the documentation/specification - using namespace openspace::documentation; - TestResult testResult = testSpecification( + openspace::documentation::testSpecificationAndThrow( SceneGraphNode::Documentation(), - dictionary + dictionary, + "SceneGraphNode" ); - if (!testResult.success) { - throw SpecificationError(testResult, "SceneGraphNode"); - } - SceneGraphNode* result = new SceneGraphNode;