Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "fixture.h"

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
{
Expand All @@ -34,6 +35,13 @@ int main(int argc, char *argv[])
TestFixture::printHelp();
return EXIT_SUCCESS;
}
if (!args.errors().empty()) {
for (const auto& error : args.errors())
{
std::cout << "error: " << error << '\n';
}
return EXIT_FAILURE;
}
const std::size_t failedTestsCount = TestFixture::runTests(args);
return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
31 changes: 22 additions & 9 deletions test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@
#include "options.h"

options::options(int argc, const char* const argv[])
: mArgs(argv + 1, argv + argc)
,mQuiet(mArgs.count("-q") != 0)
,mHelp(mArgs.count("-h") != 0 || mArgs.count("--help"))
,mSummary(mArgs.count("-n") == 0)
,mDryRun(mArgs.count("-d") != 0)
,mExcludeTests(mArgs.count("-x") != 0)
,mExe(argv[0])
: mExe(argv[0])
{
for (const auto& arg : mArgs) {
const std::set<std::string> args(argv + 1, argv + argc);
for (const auto& arg : args) {
if (arg.empty())
continue; // empty argument
if (arg[0] == '-')
if (arg[0] == '-') {
if (arg == "-q")
mQuiet = true;
else if (arg == "-h" || arg == "--help")
mHelp = true;
else if (arg == "-n")
mSummary = false;
else if (arg == "-d")
mDryRun = true;
else if (arg == "-x")
mExcludeTests = true;
else
mErrors.emplace_back("unknown option '" + arg + "'");
continue; // command-line switch
}
const auto pos = arg.find("::");
if (pos == std::string::npos) {
mWhichTests[arg] = {}; // run whole fixture
Expand Down Expand Up @@ -78,3 +86,8 @@ bool options::exclude_tests() const
{
return mExcludeTests;
}

const std::vector<std::string>& options::errors() const
{
return mErrors;
}
16 changes: 10 additions & 6 deletions test/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <map>
#include <set>
#include <string>
#include <vector>

/**
* @brief Class to parse command-line parameters for ./testrunner .
Expand All @@ -43,20 +44,23 @@ class options {
/** Which tests should be run. */
const std::map<std::string, std::set<std::string>>& which_tests() const;

/** Errors encountered during option processing. */
const std::vector<std::string>& errors() const;

const std::string& exe() const;

options() = delete;
options(const options&) = delete;
options& operator =(const options&) = delete;

private:
std::set<std::string> mArgs;
std::map<std::string, std::set<std::string>> mWhichTests;
const bool mQuiet;
const bool mHelp;
const bool mSummary;
const bool mDryRun;
const bool mExcludeTests;
std::vector<std::string> mErrors;
bool mQuiet{};
bool mHelp{};
bool mSummary{true};
bool mDryRun{};
bool mExcludeTests{};
std::string mExe;
};

Expand Down
43 changes: 31 additions & 12 deletions test/testoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class TestOptions : public TestFixture {
TEST_CASE(which_test);
TEST_CASE(which_test_method);
TEST_CASE(no_test_method);
TEST_CASE(not_quiet);
TEST_CASE(defaults);
TEST_CASE(quiet);
TEST_CASE(not_help);
TEST_CASE(help);
TEST_CASE(help_long);
TEST_CASE(multiple_testcases);
TEST_CASE(multiple_testcases_ignore_duplicates);
TEST_CASE(invalid_switches);
TEST_CASE(summary);
TEST_CASE(dry_run);
TEST_CASE(exclude_tests);
}


Expand All @@ -55,6 +55,7 @@ class TestOptions : public TestFixture {
{ "TestClass", {} }
};
ASSERT(expected == args.which_tests());
ASSERT(args.errors().empty());
}


Expand All @@ -65,6 +66,7 @@ class TestOptions : public TestFixture {
{ "TestClass", {"TestMethod"} }
};
ASSERT(expected == args.which_tests());
ASSERT(args.errors().empty());
}


Expand All @@ -73,40 +75,41 @@ class TestOptions : public TestFixture {
options args(getArrayLength(argv), argv);
const std::map<std::string, std::set<std::string>> expected{};
ASSERT(expected == args.which_tests());
ASSERT(args.errors().empty());
}


void not_quiet() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"};
void defaults() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(false, args.quiet());
ASSERT_EQUALS(false, args.help());
ASSERT_EQUALS(true, args.summary());
ASSERT_EQUALS(false, args.dry_run());
ASSERT_EQUALS(false, args.exclude_tests());
ASSERT(args.errors().empty());
}


void quiet() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-q"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(true, args.quiet());
ASSERT(args.errors().empty());
}

void not_help() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(false, args.help());
}


void help() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-h"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(true, args.help());
ASSERT(args.errors().empty());
}


void help_long() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "--help"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(true, args.help());
ASSERT(args.errors().empty());
}

void multiple_testcases() const {
Expand All @@ -116,6 +119,7 @@ class TestOptions : public TestFixture {
{ "TestClass", { "TestMethod", "AnotherTestMethod" } }
};
ASSERT(expected == args.which_tests());
ASSERT(args.errors().empty());
}

void multiple_testcases_ignore_duplicates() const {
Expand All @@ -125,6 +129,7 @@ class TestOptions : public TestFixture {
{ "TestClass", {} }
};
ASSERT(expected == args.which_tests());
ASSERT(args.errors().empty());
}

void invalid_switches() const {
Expand All @@ -135,18 +140,32 @@ class TestOptions : public TestFixture {
};
ASSERT(expected == args.which_tests());
ASSERT_EQUALS(true, args.quiet());
ASSERT_EQUALS(2, args.errors().size());
auto it = args.errors().cbegin();
ASSERT_EQUALS("unknown option '-a'", *it);
++it;
ASSERT_EQUALS("unknown option '-v'", *it);
}

void summary() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-n"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(false, args.summary());
ASSERT(args.errors().empty());
}

void dry_run() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-d"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(true, args.dry_run());
ASSERT(args.errors().empty());
}

void exclude_tests() const {
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-x"};
options args(getArrayLength(argv), argv);
ASSERT_EQUALS(true, args.exclude_tests());
ASSERT(args.errors().empty());
}
};

Expand Down
Loading