mirror of
https://github.com/fmang/opustags.git
synced 2025-01-15 20:53:16 +01:00
t: extend the tap module
This commit is contained in:
parent
19c1a8361d
commit
2dbba5a23e
38
t/tap.h
38
t/tap.h
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user