tap: use printf style output messages

This commit is contained in:
Danny Robson 2015-07-21 02:53:46 +10:00
parent a4a93a1a72
commit 8c3dc295cf
2 changed files with 58 additions and 27 deletions

32
tap.hpp
View File

@ -43,21 +43,35 @@ namespace util { namespace TAP {
void expect (std::function<bool(Args...)>, Args&&..., 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, typename ...Args>
template <typename T, typename U> void expect_neq (const T&, const U&, const std::string &msg = "inequality"); void expect_eq (const T&, const U&, const std::string &fmt = "equality", Args&...);
template <typename T, typename U, typename ...Args>
void expect_neq (const T&, const U&, const std::string &msg = "inequality", Args&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T, typename U> void expect_gt (const T&, const U&, const std::string &msg = "gt"); template <typename T, typename U, typename ...Args>
template <typename T, typename U> void expect_ge (const T&, const U&, const std::string &msg = "ge"); void expect_gt (const T&, const U&, const std::string &msg = "gt", Args&...);
template <typename T, typename U> void expect_lt (const T&, const U&, const std::string &msg = "lt");
template <typename T, typename U> void expect_le (const T&, const U&, const std::string &msg = "le"); template <typename T, typename U, typename ...Args>
void expect_ge (const T&, const U&, const std::string &msg = "ge", Args&...);
template <typename T, typename U, typename ...Args>
void expect_lt (const T&, const U&, const std::string &msg = "lt", Args&...);
template <typename T, typename U, typename ...Args>
void expect_le (const T&, const U&, const std::string &msg = "le", Args&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T> void expect_nan (const T&, const std::string &msg = "nan"); template <typename T, typename ...Args>
void expect_nan (const T&, const std::string &msg = "nan", Args&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T> void expect_nothrow (T&&, const std::string &msg = "nothrow"); template <typename T, typename ...Args>
template <typename E, typename T> void expect_throw (T&&, const std::string &msg = "throw"); void expect_nothrow (T&&, const std::string &msg = "nothrow", Args&...);
template <typename E, typename T, typename ...Args>
void expect_throw (T&&, const std::string &msg = "throw", Args&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
void skip (const std::string &msg); void skip (const std::string &msg);

53
tap.ipp
View File

@ -24,6 +24,8 @@
#include <cstdlib> #include <cstdlib>
#include <algorithm> #include <algorithm>
#include "format.hpp"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename... Args> template <typename... Args>
@ -35,43 +37,52 @@ util::TAP::logger::expect (std::function<bool(Args...)> test, Args&&... args, co
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U, typename ...Args>
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 &fmt, Args&... args)
{ {
static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool { static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool {
return almost_equal (t, u); return almost_equal (t, u);
}; };
expect<const T&, const U&> (TEST, a, b, msg); expect<const T&, const U&> (TEST, a, b, util::format::render (fmt, std::forward<Args> (args)...));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T, typename U> template <typename T, typename U, typename ...Args>
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 &fmt, Args&... args)
{ {
static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool { static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool {
return !almost_equal (t, u); return !almost_equal (t, u);
}; };
expect<const T&, const U&> (TEST, a, b, msg); expect<const T&, const U&> (TEST, a, b, util::format::render (fmt, std::forward<Args> (args)...));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define TAP_TEST(SUFFIX,OP) \ #define TAP_TEST(SUFFIX,OP) \
template <typename T, typename U> \ template <typename T, typename U, typename ...Args> \
void \ void \
util::TAP::logger::expect_ ## SUFFIX (const T &a, \ util::TAP::logger::expect_ ## SUFFIX (const T &a, \
const U &b, \ const U &b, \
const std::string &msg) \ const std::string &fmt, \
Args&... args) \
{ \ { \
static const std::function< \ static const std::function< \
bool(const T&,const U&) \ bool(const T&,const U&) \
> TEST = [] (const T&t, const U&u) { return t OP u; }; \ > TEST = [] (const T&t, const U&u) { return t OP u; }; \
expect<const T&, const U&> (TEST, a, b, msg); \ \
expect<const T&, const U&> ( \
TEST, \
a, b, \
util::format::render ( \
fmt, \
std::forward<Args> (args)... \
) \
); \
} }
TAP_TEST(gt, > ) TAP_TEST(gt, > )
@ -83,19 +94,23 @@ TAP_TEST(le, <=)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T, typename ...Args>
void void
util::TAP::logger::expect_nan (const T &t, const std::string &msg) util::TAP::logger::expect_nan (const T &t, const std::string &fmt, Args&... args)
{ {
bool(*func)(T) = std::isnan; bool(*func)(T) = std::isnan;
expect<const T&> (std::function<bool(const T&)> (func), t, msg); expect<const T&> (
std::function<bool(const T&)> (func),
t,
util::format::render (fmt, std::forward<Args> (args)...)
);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T, typename ...Args>
void void
util::TAP::logger::expect_nothrow (T &&t, const std::string &msg) util::TAP::logger::expect_nothrow (T &&t, const std::string &fmt, Args&... args)
{ {
bool success = true; bool success = true;
@ -105,14 +120,14 @@ util::TAP::logger::expect_nothrow (T &&t, const std::string &msg)
success = false; success = false;
} }
expect (success, msg); expect (success, util::format::render (fmt, std::forward<Args> (args)...));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename E, typename T> template <typename E, typename T, typename ...Args>
void void
util::TAP::logger::expect_throw (T &&t, const std::string &msg) util::TAP::logger::expect_throw (T &&t, const std::string &fmt, Args&... args)
{ {
bool success = false; bool success = false;
@ -120,7 +135,9 @@ util::TAP::logger::expect_throw (T &&t, const std::string &msg)
t (); t ();
} catch (const E&) { } catch (const E&) {
success = true; success = true;
} catch (...) {
success = false;
} }
expect (success, msg); expect (success, util::format::render (fmt, std::forward<Args> (args)...));
} }