Skip to content

Commit

Permalink
Add option to run test scripts (will use for script-based testing later)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed May 30, 2018
1 parent b3387cb commit f496af6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions geometrize/cli/commandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include "script/chaiscriptcreator.h"
#include "script/scriptrunner.h"
#include "task/taskutil.h"
#include "test/functionaltestrunner.h"

namespace
{
const QString scriptFileFlag{"script_file"};
const QString scriptSourceFlag{"script_inline"};
const QString localeOverrideFlag{"locale_override"};
const QString selfTestsFlag{"functional_tests"};

/**
* @brief setupCommandLineParser Sets up a command line parser to handle application arguments.
Expand All @@ -35,6 +37,7 @@ namespace
parser.addOption(QCommandLineOption(scriptFileFlag, "Executes the ChaiScript script file at the given file path", "File path to ChaiScript script file"));
parser.addOption(QCommandLineOption(scriptSourceFlag, "Executes the inline ChaiScript source code unmodified", "Inline ChaiScript source code"));
parser.addOption(QCommandLineOption(localeOverrideFlag, "Overrides the locale and translation that the application launches with", "Locale code"));
parser.addOption(QCommandLineOption(selfTestsFlag, "Executes the Geometrize functional tests suite", "Flag to additional test scripts folder"));

if(!parser.parse(arguments)) {
assert(0 && "Failed to parse command line arguments");
Expand All @@ -60,6 +63,9 @@ namespace

std::unique_ptr<chaiscript::ChaiScript> engine{geometrize::script::createImageTaskEngine()};
geometrize::script::runScript(code, *engine);
} else if(parser.isSet(selfTestsFlag)) {
const std::string testScriptsDirectory{parser.value(selfTestsFlag).toStdString()};
geometrize::test::runSelfTests(testScriptsDirectory);
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions geometrize/test/functionaltestrunner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "test/functionaltestrunner.h"

#include <cassert>
#include <string>
#include <vector>

#include "common/util.h"
#include "script/scriptrunner.h"

namespace geometrize
{

namespace test
{

void runSelfTests(const std::string& testScriptsDirectory)
{
if(!geometrize::util::directoryExists(testScriptsDirectory)) {
assert(0 && "Given test scripts directory does not exist");
return;
}

const std::vector<std::string> testScripts{geometrize::util::getScriptsForPath(testScriptsDirectory)};

if(testScripts.empty()) {
assert(0 && "Did not find any test scripts in the given test directory");
return;
}

for(const auto& scriptPath : testScripts) {
const std::string scriptCode = geometrize::util::readFileAsString(scriptPath);
if(scriptCode.empty()) {
assert(0 && "Failed to read script file or it was empty");
}
geometrize::script::runScript(scriptCode);
}
}

}

}
15 changes: 15 additions & 0 deletions geometrize/test/functionaltestrunner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <string>

namespace geometrize
{

namespace test
{

void runSelfTests(const std::string& testScriptsDirectory);

}

}

0 comments on commit f496af6

Please sign in to comment.