Add method to automatically test a dictionary and throw on error

This commit is contained in:
Alexander Bock
2016-09-15 13:40:39 +02:00
parent 1778a7cdb6
commit 43db84f620
5 changed files with 33 additions and 30 deletions

View File

@@ -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);

View File

@@ -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<std::string>(
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;

View File

@@ -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<ghoul::Dictionary>(KeyPaths);

View File

@@ -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);

View File

@@ -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;