From 7f5533ff35a79fa48b6d9f002f0a629c24de8a5a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 13 Apr 2015 21:45:49 +1000 Subject: [PATCH] tap: consolidate expect_* functions --- tap.hpp | 15 +++++++++------ tap.ipp | 31 +++++++++++++++---------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/tap.hpp b/tap.hpp index 6ae4c451..57eea4ba 100644 --- a/tap.hpp +++ b/tap.hpp @@ -32,19 +32,21 @@ namespace util { namespace TAP { TODO }; + //--------------------------------------------------------------------- logger (); ~logger (); + //--------------------------------------------------------------------- void expect (bool, const std::string &msg); - template - void expect (std::function test, const T&, const U&, const std::string &msg); + template + void expect (std::function, Args&&..., const std::string& msg); - template - void expect_eq (const T&, const U&, const std::string &msg = "equality"); - template - void expect_neq (const T&, const U&, const std::string &msg = "inequality"); + //--------------------------------------------------------------------- + template void expect_eq (const T&, const U&, const std::string &msg = "equality"); + template void expect_neq (const T&, const U&, const std::string &msg = "inequality"); + //--------------------------------------------------------------------- template void expect_gt (const T&, const U&, const std::string &msg = "gt"); template void expect_ge (const T&, const U&, const std::string &msg = "ge"); template void expect_lt (const T&, const U&, const std::string &msg = "lt"); @@ -54,6 +56,7 @@ namespace util { namespace TAP { void todo (const std::string &msg); void noop (void); + //--------------------------------------------------------------------- int status (void) const; private: diff --git a/tap.ipp b/tap.ipp index a3c48e18..fa16d456 100644 --- a/tap.ipp +++ b/tap.ipp @@ -24,27 +24,26 @@ #include #include -//----------------------------------------------------------------------------- -template -void -util::TAP::logger::expect (std::function test, - const T &a, - const U &b, - const std::string &msg) -{ - bool success = test (a, b); - std::cout << (success ? "ok " : "not ok ") << ++m_size << " - " << msg << '\n'; - if (!success) - m_status = EXIT_FAILURE; +//----------------------------------------------------------------------------- +template +void +util::TAP::logger::expect (std::function test, Args&&... args, const std::string &msg) +{ + expect (test (std::forward (args)...), msg); } + //----------------------------------------------------------------------------- template void util::TAP::logger::expect_eq (const T&a, const U &b, const std::string &msg) { - expect (almost_equal, a, b, msg); + static const auto TEST = [] (const T &t, const U &u) -> bool { + return almost_equal (t, u); + }; + + expect (TEST, a, b, msg); } @@ -53,11 +52,11 @@ template void util::TAP::logger::expect_neq (const T&a, const U &b, const std::string &msg) { - static const std::function TEST = [] (const T &t, const U &u) -> bool { + static const auto TEST = [] (const T &t, const U &u) -> bool { return !almost_equal (t, u); }; - expect (TEST, a, b, msg); + expect (TEST, a, b, msg); } @@ -69,7 +68,7 @@ util::TAP::logger::expect_ ## SUFFIX (const T &a, \ const U &b, \ const std::string &msg) \ { \ - std::cout << ((a OP b) ? "ok " : "not ok ") << ++m_size << " - " << msg << '\n'; \ + expect ([] (const T&t, const U&u) { return t OP u; }, a, b, msg); \ } TAP_TEST(gt, > )