Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches
main.cpp

Example of how to run tests, without any test cases.

Example of how to run tests, without any test cases.

Note
This also has command line argument checking.
This also adjust the fail count for the tests that JonTest needs to have fail in order to test itself.
#include "ExpectedFailureLogger.h"
#include "JonTest/Count.h"
#include "JonTest/Logger.h"
#include <array>
#include <iostream>
#include <vector>
namespace
{
void doHelp(
const std::string& program,
const std::string& error = std::string(""),
const std::string& errorInfo = std::string("")
)
{
std::cout
<< program << " -- Run Unit Tests\n"
<< "\n"
<< "Runs the automatically build=defined unit tests.\n"
<< "\tExit code is number of failed tests, or 1 for program error.\n"
<< "\n"
<< "Usage: " << program << " [<options>] [<test>...]\n"
<< "\n"
<< "Options:\t\t(alias)\n"
<< "\t--help\t\t-h\tGets this help information\n"
<< "\t--verbose\t-v\tEnables verbose output\n"
<< "\t--list\t\t-l\tList available Test Suites\n"
<< "\t--cases\t\t-c\tList available Test Cases\n"
<< "Note: aliases may NOT be concatenated, such as \"-hv\"\n"
<< "\n"
<< "A <test> is one of:\n"
<< "\t<suite>\t\tname of Test Suite to run\n"
<< "\t<suite>:<case>\tname of Test Case in Test Suite to run\n"
;
if(!error.empty())
{
std::cout << "\n Error: " << error;
if(!errorInfo.empty())
{
std::cout << ": " << errorInfo;
}
std::cout << "\n";
}
}
template<typename S, typename T, long unsigned int length>
bool
contains(
std::array<S, length> matches,
const T& value
)
{
for(auto const match : matches)
{
if(value == match)
{
return true;
}
}
return false;
}
}
int
main(
int argc,
char**argv
)
{
bool verbose = false;
std::vector<std::string> tests;
{
const std::string program(argv[0]);
for(int i = 1; i < argc; ++i)
{
const std::string arg(argv[i]);
if(contains(std::array{"--verbose", "-v"}, arg)) { verbose = true; }
else if(contains(std::array{"--help", "-h"}, arg)) { doHelp(program); exit(0); }
else if(contains(std::array{"--list", "-l"}, arg)) { JonTest::TestRunner::get().listTestSuites(std::cout); exit(0); }
else if(contains(std::array{"--cases", "-c"}, arg)) { JonTest::TestRunner::get().listTestCases(std::cout); exit(0); }
else if(0 == arg.find("-")) { doHelp(program, "Unknown option", arg.c_str()); exit(1); }
else if(JonTest::TestRunner::get().isValid(arg)) { tests.push_back(arg); }
else { doHelp(program, "Unknown test", arg.c_str()); exit(1); }
}
}
ExpectedFailureLogger logger(std::cout, verbose);
const JonTest::Count counts = JonTest::TestRunner::get().run(logger, tests);
const int fails = counts.fails - logger.getExpectedFailuresFailed();
return fails;
}
Count of tests run, and fails.
Definition Count.h:10
int fails
Number of tests that have failed.
Definition Count.h:18
Count run(Logger &logger) const
Run all tests that have been add()ed.
void listTestSuites(std::ostream &out) const
List available Test Suites to out.
static TestRunner & get()
Get the singleton.
void listTestCases(std::ostream &out) const
List available Test Cases to out.