From 2dbba5a23e386f89658a5554bbe8289abbd4f908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Tue, 18 Dec 2018 20:23:22 -0500 Subject: [PATCH] t: extend the tap module --- t/tap.h | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/t/tap.h b/t/tap.h index d77ac19..15e5144 100644 --- a/t/tap.h +++ b/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 #include +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 +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); + } +} + +}