t: extend the tap module

This commit is contained in:
Frédéric Mangano-Tarumi 2018-12-18 20:23:22 -05:00
parent 19c1a8361d
commit 2dbba5a23e

38
t/tap.h
View File

@ -3,6 +3,11 @@
*
* \brief
* Helpers for following the Test Anything Protocol.
*
* Its interface mimics Test::More from Perl:
* https://perldoc.perl.org/Test/More.html
*
* Unlike Test::More, a test failure raises an exception and aborts the whole subtest.
*/
#pragma once
@ -10,6 +15,8 @@
#include <exception>
#include <iostream>
inline namespace tap {
struct failure : std::runtime_error {
failure(const std::string& what) : std::runtime_error(what) {}
};
@ -22,7 +29,36 @@ static void run(F test, const char *name)
test();
ok = true;
} catch (failure& e) {
std::cerr << "# " << e.what() << "\n";
std::cerr << "# fail: " << e.what() << "\n";
}
std::cout << (ok ? "ok" : "not ok") << " - " << name << "\n";
}
void plan(int tests)
{
std::cout << "1.." << tests << "\n";
}
template <typename T, typename U>
void is(const T& got, const U& expected, const char* name)
{
if (got != expected) {
std::cerr << "# got: " << got << "\n"
"# expected: " << expected << "\n";
throw failure(name);
}
}
template <>
void is(const ot::status& got, const ot::st& expected, const char* name)
{
if (got.code != expected) {
if (got.code == ot::st::ok)
std::cerr << "# unexpected success\n";
else
std::cerr << "# unexpected error: " << got.message << "\n";
throw failure(name);
}
}
}