tap: consolidate expect_* functions

This commit is contained in:
Danny Robson 2015-04-13 21:45:49 +10:00
parent 5ee3d14811
commit 7f5533ff35
2 changed files with 24 additions and 22 deletions

15
tap.hpp
View File

@ -32,19 +32,21 @@ namespace util { namespace TAP {
TODO TODO
}; };
//---------------------------------------------------------------------
logger (); logger ();
~logger (); ~logger ();
//---------------------------------------------------------------------
void expect (bool, const std::string &msg); void expect (bool, const std::string &msg);
template <typename T, typename U> template <typename ...Args>
void expect (std::function<bool(const T&, const U&)> test, const T&, const U&, const std::string &msg); void expect (std::function<bool(Args...)>, Args&&..., const std::string& msg);
template <typename T, typename U> //---------------------------------------------------------------------
void expect_eq (const T&, const U&, const std::string &msg = "equality"); template <typename T, typename U> void expect_eq (const T&, const U&, const std::string &msg = "equality");
template <typename T, typename U> template <typename T, typename U> void expect_neq (const T&, const U&, const std::string &msg = "inequality");
void expect_neq (const T&, const U&, const std::string &msg = "inequality");
//---------------------------------------------------------------------
template <typename T, typename U> void expect_gt (const T&, const U&, const std::string &msg = "gt"); template <typename T, typename U> void expect_gt (const T&, const U&, const std::string &msg = "gt");
template <typename T, typename U> void expect_ge (const T&, const U&, const std::string &msg = "ge"); template <typename T, typename U> void expect_ge (const T&, const U&, const std::string &msg = "ge");
template <typename T, typename U> void expect_lt (const T&, const U&, const std::string &msg = "lt"); template <typename T, typename U> 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 todo (const std::string &msg);
void noop (void); void noop (void);
//---------------------------------------------------------------------
int status (void) const; int status (void) const;
private: private:

31
tap.ipp
View File

@ -24,27 +24,26 @@
#include <cstdlib> #include <cstdlib>
#include <algorithm> #include <algorithm>
//-----------------------------------------------------------------------------
template <typename T, typename U>
void
util::TAP::logger::expect (std::function<bool(const T&, const U&)> 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 <typename... Args>
void
util::TAP::logger::expect (std::function<bool(Args...)> test, Args&&... args, const std::string &msg)
{
expect (test (std::forward<Args> (args)...), msg);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U>
void void
util::TAP::logger::expect_eq (const T&a, const U &b, const std::string &msg) util::TAP::logger::expect_eq (const T&a, const U &b, const std::string &msg)
{ {
expect (almost_equal<T,U>, a, b, msg); static const auto TEST = [] (const T &t, const U &u) -> bool {
return almost_equal (t, u);
};
expect<const T&, const U&> (TEST, a, b, msg);
} }
@ -53,11 +52,11 @@ template <typename T, typename U>
void void
util::TAP::logger::expect_neq (const T&a, const U &b, const std::string &msg) util::TAP::logger::expect_neq (const T&a, const U &b, const std::string &msg)
{ {
static const std::function<bool(const T&, const U&)> TEST = [] (const T &t, const U &u) -> bool { static const auto TEST = [] (const T &t, const U &u) -> bool {
return !almost_equal (t, u); return !almost_equal (t, u);
}; };
expect (TEST, a, b, msg); expect<const T&, const U&> (TEST, a, b, msg);
} }
@ -69,7 +68,7 @@ util::TAP::logger::expect_ ## SUFFIX (const T &a, \
const U &b, \ const U &b, \
const std::string &msg) \ const std::string &msg) \
{ \ { \
std::cout << ((a OP b) ? "ok " : "not ok ") << ++m_size << " - " << msg << '\n'; \ expect<const T&, const U&> ([] (const T&t, const U&u) { return t OP u; }, a, b, msg); \
} }
TAP_TEST(gt, > ) TAP_TEST(gt, > )