Slow working on bringing the old code up to standard.
- Documentation updates - most of the old files use non-Doxygen or no/minimal header comments. - Rework SimpleSuite to be more useful. - Coverity-surfaced fixes.
This commit is contained in:
@@ -22,16 +22,96 @@
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
|
||||
#include <sctest/Report.h>
|
||||
|
||||
|
||||
|
||||
namespace sctest {
|
||||
|
||||
|
||||
_Report::_Report()
|
||||
: Failing (0), Total(0), Start(std::chrono::steady_clock::now()),
|
||||
End(std::chrono::steady_clock::now()), Duration(0) {}
|
||||
Report::Report()
|
||||
{
|
||||
this->Reset(0);
|
||||
}
|
||||
|
||||
|
||||
} // end namespace test
|
||||
size_t
|
||||
Report::Failing() const
|
||||
{
|
||||
return this->failing;
|
||||
}
|
||||
|
||||
|
||||
size_t
|
||||
Report::Total() const
|
||||
{
|
||||
return this->total;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Report::Failed()
|
||||
{
|
||||
this->failing++;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Report::AddTest(size_t testCount)
|
||||
{
|
||||
this->total += testCount;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Report::Reset(size_t testCount)
|
||||
{
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
this->total = testCount;
|
||||
this->failing = 0;
|
||||
|
||||
this->Start();
|
||||
this->end = now;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
Report::Start()
|
||||
{
|
||||
this->start = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Report::End()
|
||||
{
|
||||
this->end = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
|
||||
std::chrono::duration<double, std::milli>
|
||||
Report::Elapsed() const
|
||||
{
|
||||
return this->end - this->start;
|
||||
}
|
||||
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream &os, const Report &report)
|
||||
{
|
||||
auto elapsed = report.Elapsed();
|
||||
|
||||
os << report.Total() - report.Failing() << "/"
|
||||
<< report.Total() << " tests passed in "
|
||||
<< std::setw(3) << elapsed.count() << "ms";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // end namespace sctest
|
||||
@@ -20,7 +20,6 @@
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
///
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
#include <sctest/SimpleSuite.h>
|
||||
@@ -39,14 +38,22 @@ SimpleSuite::SimpleSuite()
|
||||
: quiet(false), fnSetup(stub), fnTeardown(stub), tests(),
|
||||
report(), hasRun(false)
|
||||
{
|
||||
this->Reset();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SimpleSuite::Silence()
|
||||
{
|
||||
// Silence will fall.
|
||||
quiet = true;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SimpleSuite::AddTest(std::string name, std::function<bool()> test)
|
||||
{
|
||||
TestCase test_case = {name, test};
|
||||
UnitTest const test_case = {name, test};
|
||||
tests.push_back(test_case);
|
||||
}
|
||||
|
||||
@@ -55,7 +62,7 @@ void
|
||||
SimpleSuite::AddFailingTest(std::string name, std::function<bool()> test)
|
||||
{
|
||||
// auto ntest = [&test]() { return !test(); };
|
||||
TestCase test_case = {name, [&test]() { return !test(); }};
|
||||
UnitTest test_case = {name, [&test]() { return !test(); }};
|
||||
tests.push_back(test_case);
|
||||
}
|
||||
|
||||
@@ -63,38 +70,55 @@ SimpleSuite::AddFailingTest(std::string name, std::function<bool()> test)
|
||||
bool
|
||||
SimpleSuite::Run()
|
||||
{
|
||||
report.Start = std::chrono::steady_clock::now();
|
||||
report.Reset(this->tests.size());
|
||||
|
||||
unless(quiet) { std::cout << "Setting up the tests.\n"; }
|
||||
unless(fnSetup()) { return false; }
|
||||
|
||||
// Reset the failed test counts.
|
||||
report.Failing = 0;
|
||||
this->hasRun = true;
|
||||
this->hasPassed = true;
|
||||
|
||||
bool result = true;
|
||||
hasRun = true;
|
||||
report.Total = tests.size();
|
||||
for (size_t i = 0; i < report.Total && result; i++) {
|
||||
TestCase tc = tests.at(i);
|
||||
for (size_t i = 0; i < this->report.Total() && this->hasPassed; i++) {
|
||||
const UnitTest testCase = this->tests.at(i);
|
||||
unless(quiet) {
|
||||
std::cout << "[" << i + 1 << "/" << report.Total << "] Running test " << tc.name << ": ";
|
||||
std::cout << "[" << i + 1 << "/"
|
||||
<< this -> report.Total()
|
||||
<< "] Running test "
|
||||
<< testCase.name << ": ";
|
||||
}
|
||||
|
||||
result = tc.test();
|
||||
this->hasPassed = testCase.test();
|
||||
if (quiet) { continue; }
|
||||
|
||||
if (result) {
|
||||
if (this->hasPassed) {
|
||||
std::cout << "[PASS]";
|
||||
} else {
|
||||
std::cout << "[FAIL]";
|
||||
report.Failing++;
|
||||
report.Failed();
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
unless(quiet) { std::cout << "Tearing down the tests.\n"; }
|
||||
unless(fnTeardown()) { return false; }
|
||||
report.End = std::chrono::steady_clock::now();
|
||||
return result;
|
||||
report.End();
|
||||
return this->hasPassed;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SimpleSuite::Reset()
|
||||
{
|
||||
this->report.Reset(0);
|
||||
this->hasRun = false;
|
||||
this->hasPassed = false;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SimpleSuite::HasRun() const
|
||||
{
|
||||
return this->hasRun;
|
||||
}
|
||||
|
||||
|
||||
@@ -105,4 +129,17 @@ SimpleSuite::GetReport()
|
||||
}
|
||||
|
||||
|
||||
std::ostream &
|
||||
operator<<(std::ostream &os, SimpleSuite &suite)
|
||||
{
|
||||
if (suite.HasRun()) {
|
||||
os << "OK: " << suite.GetReport();
|
||||
} else {
|
||||
os << "Test suite hasn't run.";
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
} // end namespace sctest
|
||||
Reference in New Issue
Block a user