Commit 7e7871c4 authored by Matthias Vallentin's avatar Matthias Vallentin Committed by Marian Triebe

Add ability to register command line arguments.

By adding a delimeter "--" into the sequence of arguments, it is now
possible to partition the command line arguments into two halves.
The unit test binary consumes everything before "--" and the unit test
engine registers anything after "--".

For example,

    caf-test -n -- foo bar

passes "-n" to the caf-test parser to disable colors, whereas user tests
can now access the remaining two arguments "foo" and "bar" via
test::engine::argc() and test::engine::argv().
parent a12892f7
......@@ -184,6 +184,25 @@ enum color_value {
*/
class engine {
public:
/**
* Sets external command line arguments.
* @param argc The argument counter.
* @param argv The argument vectors.
*/
static void args(int argc, char** argv);
/**
* Retrieves the argument counter.
* @returns The number of arguments set via ::args or 0.
*/
static int argc();
/**
* Retrieves the argument vector.
* @returns The argument vector set via ::args or `nullptr`.
*/
static char** argv();
/**
* Adds a test to the engine.
* @param name The name of the suite.
......@@ -236,6 +255,8 @@ class engine {
static std::string render(std::chrono::microseconds t);
int m_argc = 0;
char** m_argv = nullptr;
const char* m_reset = "\033[0m";
const char* m_black = "\033[30m";
const char* m_red = "\033[31m";
......
......@@ -221,6 +221,19 @@ logger::logger() : m_console(std::cerr) {
// nop
}
void engine::args(int argc, char** argv) {
instance().m_argc = argc;
instance().m_argv = argv;
}
int engine::argc() {
return instance().m_argc;
}
char** engine::argv() {
return instance().m_argv;
}
void engine::add(const char* name, std::unique_ptr<test> t) {
auto& suite = instance().m_suites[std::string{name ? name : ""}];
for (auto& x : suite) {
......@@ -542,8 +555,18 @@ int main(int argc, char** argv) {
std::string not_suites;
std::string tests = ".*";
std::string not_tests;
// use all arguments after '--' for the test engine.
std::string delimiter = "--";
auto divider = argc;
auto cli_argv = argv + 1;
for (auto i = 1; i < argc; ++i) {
if (delimiter == argv[i]) {
divider = i;
break;
}
}
// our simple command line parser.
auto res = message_builder(argv + 1, argv + argc).extract_opts({
auto res = message_builder(cli_argv, cli_argv + divider - 1).extract_opts({
{"no-colors,n", "disable coloring"},
{"log-file,l", "set output file", log_file},
{"console-verbosity,v", "set verbosity level of console (1-5)",
......@@ -574,6 +597,9 @@ int main(int argc, char** argv) {
return 1;
}
auto colorize = res.opts.count("no-colors") == 0;
if (divider < argc) {
test::engine::args(argc - divider - 1, argv + divider + 1);
}
auto result = test::engine::run(colorize, log_file, verbosity_console,
verbosity_file, max_runtime, suites,
not_suites, tests, not_tests);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment