mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-06-26 10:54:28 +02:00
Refactor UnitTest class
- Use UnitTest::ok in UnitTest::notok - Extract report_* functions - Extract common color function and constants - Use templates to reduce code duplication
This commit is contained in:
parent
9c49d4df2d
commit
67c5b7de6c
2 changed files with 127 additions and 264 deletions
369
test/test.cpp
369
test/test.cpp
|
@ -30,6 +30,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <test.h>
|
#include <test.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -59,7 +60,9 @@ UnitTest::~UnitTest ()
|
||||||
{
|
{
|
||||||
float percentPassed = 0.0;
|
float percentPassed = 0.0;
|
||||||
if (_planned > 0)
|
if (_planned > 0)
|
||||||
|
{
|
||||||
percentPassed = (100.0 * _passed) / std::max (_planned, _passed + _failed + _skipped);
|
percentPassed = (100.0 * _passed) / std::max (_planned, _passed + _failed + _skipped);
|
||||||
|
}
|
||||||
|
|
||||||
if (_counter < _planned)
|
if (_counter < _planned)
|
||||||
{
|
{
|
||||||
|
@ -72,11 +75,13 @@ UnitTest::~UnitTest ()
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (_counter > _planned)
|
else if (_counter > _planned)
|
||||||
|
{
|
||||||
std::cout << "# "
|
std::cout << "# "
|
||||||
<< _counter
|
<< _counter
|
||||||
<< " tests were run, but only "
|
<< " tests were run, but only "
|
||||||
<< _planned
|
<< _planned
|
||||||
<< " were planned.\n";
|
<< " were planned.\n";
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "# "
|
std::cout << "# "
|
||||||
<< _passed
|
<< _passed
|
||||||
|
@ -116,231 +121,63 @@ void UnitTest::ok (bool expression, const std::string& name)
|
||||||
|
|
||||||
if (expression)
|
if (expression)
|
||||||
{
|
{
|
||||||
++_passed;
|
report_success (name);
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
++_failed;
|
report_failure (name);
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::notok (bool expression, const std::string& name)
|
void UnitTest::notok (bool expression, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
ok (!expression, name);
|
||||||
|
|
||||||
if (!expression)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (bool actual, bool expected, const std::string& name)
|
void UnitTest::is (bool actual, bool expected, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <bool> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (size_t actual, size_t expected, const std::string& name)
|
void UnitTest::is (size_t actual, size_t expected, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <size_t> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (int actual, int expected, const std::string& name)
|
void UnitTest::is (int actual, int expected, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <int> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (double actual, double expected, const std::string& name)
|
void UnitTest::is (double actual, double expected, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <double> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (double actual, double expected, double tolerance, const std::string& name)
|
void UnitTest::is (double actual, double expected, double tolerance, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
++_counter;
|
||||||
|
|
||||||
if (fabs (actual - expected) <= tolerance)
|
if (fabs (actual - expected) <= tolerance)
|
||||||
{
|
{
|
||||||
++_passed;
|
report_success (name);
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
++_failed;
|
report_failure (create_message<double> (actual, expected, name));
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::is (unsigned char actual, unsigned char expected, const std::string& name)
|
void UnitTest::is (unsigned char actual, unsigned char expected, const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <unsigned char> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: "
|
|
||||||
<< expected
|
|
||||||
<< "\n# got: "
|
|
||||||
<< actual
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -349,32 +186,7 @@ void UnitTest::is (
|
||||||
const std::string& expected,
|
const std::string& expected,
|
||||||
const std::string& name)
|
const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
_is <const std::string&> (actual, expected, name);
|
||||||
if (actual == expected)
|
|
||||||
{
|
|
||||||
++_passed;
|
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++_failed;
|
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< "\n# expected: '"
|
|
||||||
<< expected
|
|
||||||
<< "'"
|
|
||||||
<< "\n# got: '"
|
|
||||||
<< actual
|
|
||||||
<< "'\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -384,30 +196,30 @@ void UnitTest::is (
|
||||||
const std::string& name)
|
const std::string& name)
|
||||||
{
|
{
|
||||||
++_counter;
|
++_counter;
|
||||||
|
|
||||||
if (! strcmp (actual, expected))
|
if (! strcmp (actual, expected))
|
||||||
{
|
{
|
||||||
++_passed;
|
report_success (name);
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< name
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
++_failed;
|
report_failure (create_message<const char*> (actual, expected, name));
|
||||||
std::cout << red ("not ok")
|
}
|
||||||
<< " "
|
}
|
||||||
<< _counter
|
|
||||||
<< " - "
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
<< name
|
template <typename T>
|
||||||
<< "\n# expected: '"
|
void UnitTest::_is (T actual, T expected, const std::string& name)
|
||||||
<< expected
|
{
|
||||||
<< "'"
|
++_counter;
|
||||||
<< "\n# got: '"
|
|
||||||
<< actual
|
if (actual == expected)
|
||||||
<< "'\n";
|
{
|
||||||
|
report_success (name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
report_failure (create_message<T> (actual, expected, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,74 +228,109 @@ void UnitTest::diag (const std::string& text)
|
||||||
{
|
{
|
||||||
auto start = text.find_first_not_of (" \t\n\r\f");
|
auto start = text.find_first_not_of (" \t\n\r\f");
|
||||||
auto end = text.find_last_not_of (" \t\n\r\f");
|
auto end = text.find_last_not_of (" \t\n\r\f");
|
||||||
|
|
||||||
if (start != std::string::npos &&
|
if (start != std::string::npos &&
|
||||||
end != std::string::npos)
|
end != std::string::npos)
|
||||||
|
{
|
||||||
std::cout << "# " << text.substr (start, end - start + 1) << '\n';
|
std::cout << "# " << text.substr (start, end - start + 1) << '\n';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::pass (const std::string& text)
|
void UnitTest::pass (const std::string& text)
|
||||||
{
|
{
|
||||||
++_counter;
|
++_counter;
|
||||||
++_passed;
|
report_success (text);
|
||||||
std::cout << green ("ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< text
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::fail (const std::string& text)
|
void UnitTest::fail (const std::string& text)
|
||||||
{
|
{
|
||||||
++_counter;
|
++_counter;
|
||||||
++_failed;
|
report_failure (text);
|
||||||
std::cout << red ("not ok")
|
|
||||||
<< " "
|
|
||||||
<< _counter
|
|
||||||
<< " - "
|
|
||||||
<< text
|
|
||||||
<< '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::skip (const std::string& text)
|
void UnitTest::skip (const std::string& text)
|
||||||
{
|
{
|
||||||
++_counter;
|
++_counter;
|
||||||
|
report_skip (text);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
std::string UnitTest::create_message (T actual, T expected, const std::string &name)
|
||||||
|
{
|
||||||
|
std::stringstream message;
|
||||||
|
|
||||||
|
message << name
|
||||||
|
<< "\n# expected: '"
|
||||||
|
<< expected
|
||||||
|
<< "'"
|
||||||
|
<< "\n# got: '"
|
||||||
|
<< actual
|
||||||
|
<< "'";
|
||||||
|
|
||||||
|
return message.str ();
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void UnitTest::report_success (const std::string& message)
|
||||||
|
{
|
||||||
|
++_passed;
|
||||||
|
report_status (green ("ok"), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void UnitTest::report_failure (const std::string& message)
|
||||||
|
{
|
||||||
|
++_failed;
|
||||||
|
report_status (red ("not ok"), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void UnitTest::report_skip (const std::string& message)
|
||||||
|
{
|
||||||
++_skipped;
|
++_skipped;
|
||||||
std::cout << yellow ("ok")
|
report_status (yellow ("ok"), message + " # skip");
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void UnitTest::report_status (const std::string& status, const std::string& message) const
|
||||||
|
{
|
||||||
|
std::cout << status
|
||||||
<< " "
|
<< " "
|
||||||
<< _counter
|
<< _counter
|
||||||
<< " - "
|
<< " - "
|
||||||
<< text
|
<< message
|
||||||
<< " # skip"
|
|
||||||
<< '\n';
|
<< '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
std::string UnitTest::red (const std::string& input)
|
std::string UnitTest::red (const std::string& input) const
|
||||||
|
{
|
||||||
|
return colored (COLOR_RED, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string UnitTest::green (const std::string& input) const
|
||||||
|
{
|
||||||
|
return colored (COLOR_GREEN, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string UnitTest::yellow (const std::string& input) const
|
||||||
|
{
|
||||||
|
return colored (COLOR_YELLOW, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string UnitTest::colored (const std::string& color_code, const std::string& input) const
|
||||||
{
|
{
|
||||||
if (isatty (fileno (stdout)))
|
if (isatty (fileno (stdout)))
|
||||||
return std::string ("\033[31m" + input + "\033[0m");
|
{
|
||||||
|
return std::string (color_code + input + COLOR_RESET);
|
||||||
return input;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string UnitTest::green (const std::string& input)
|
|
||||||
{
|
|
||||||
if (isatty (fileno (stdout)))
|
|
||||||
return std::string ("\033[32m" + input + "\033[0m");
|
|
||||||
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string UnitTest::yellow (const std::string& input)
|
|
||||||
{
|
|
||||||
if (isatty (fileno (stdout)))
|
|
||||||
return std::string ("\033[33m" + input + "\033[0m");
|
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
22
test/test.h
22
test/test.h
|
@ -29,6 +29,11 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
static constexpr auto COLOR_RESET = "\033[0m";
|
||||||
|
static constexpr auto COLOR_YELLOW = "\033[33m";
|
||||||
|
static constexpr auto COLOR_GREEN = "\033[32m";
|
||||||
|
static constexpr auto COLOR_RED = "\033[31m";
|
||||||
|
|
||||||
class UnitTest
|
class UnitTest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -40,6 +45,7 @@ public:
|
||||||
void planMore (int);
|
void planMore (int);
|
||||||
void ok (bool, const std::string&);
|
void ok (bool, const std::string&);
|
||||||
void notok (bool, const std::string&);
|
void notok (bool, const std::string&);
|
||||||
|
|
||||||
void is (bool, bool, const std::string&);
|
void is (bool, bool, const std::string&);
|
||||||
void is (size_t, size_t, const std::string&);
|
void is (size_t, size_t, const std::string&);
|
||||||
void is (int, int, const std::string&);
|
void is (int, int, const std::string&);
|
||||||
|
@ -48,15 +54,25 @@ public:
|
||||||
void is (unsigned char, unsigned char, const std::string&);
|
void is (unsigned char, unsigned char, const std::string&);
|
||||||
void is (const std::string&, const std::string&, const std::string&);
|
void is (const std::string&, const std::string&, const std::string&);
|
||||||
void is (const char*, const char*, const std::string&);
|
void is (const char*, const char*, const std::string&);
|
||||||
|
|
||||||
void diag (const std::string&);
|
void diag (const std::string&);
|
||||||
void pass (const std::string&);
|
void pass (const std::string&);
|
||||||
void fail (const std::string&);
|
void fail (const std::string&);
|
||||||
void skip (const std::string&);
|
void skip (const std::string&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string red (const std::string&);
|
std::string red (const std::string&) const;
|
||||||
std::string green (const std::string&);
|
std::string green (const std::string&) const;
|
||||||
std::string yellow (const std::string&);
|
std::string yellow (const std::string&) const;
|
||||||
|
std::string colored (const std::string&, const std::string&) const;
|
||||||
|
|
||||||
|
void report_success (const std::string&);
|
||||||
|
void report_failure (const std::string&);
|
||||||
|
void report_skip (const std::string&);
|
||||||
|
void report_status (const std::string&, const std::string&) const;
|
||||||
|
|
||||||
|
template <typename T> void _is (T, T, const std::string &);
|
||||||
|
template <typename T> static std::string create_message (T, T, const std::string&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _planned;
|
int _planned;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue