tap: return the success value from test functions

This commit is contained in:
Danny Robson 2018-08-08 12:49:33 +10:00
parent 1c814c02d2
commit 3a4a7bf1d8

56
tap.hpp
View File

@ -9,13 +9,15 @@
#ifndef CRUFT_UTIL_TAP_HPP
#define CRUFT_UTIL_TAP_HPP
#include "debug.hpp"
#include "format.hpp"
#include "maths.hpp"
#include <cmath>
#include <iosfwd>
#include <functional>
#include <cmath>
namespace cruft::TAP {
/// A simple TAP (Test Anything Protocol) test case output
class logger {
@ -34,7 +36,7 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
void
bool
expect (const bool test, const char (&fmt)[N], Args&&... args)
{
m_output << (test ? "ok " : "not ok ") << ++m_size
@ -43,25 +45,26 @@ namespace cruft::TAP {
if (!test)
m_status = EXIT_FAILURE;
return test;
}
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
void
decltype(auto)
expect (const std::function<bool(Args...)> &test, Args&&...args, const char (&fmt)[N])
{
try {
expect (test (std::forward<Args> (args)...), fmt);
return expect (test (std::forward<Args> (args)...), fmt);
} catch (...) {
expect (false, fmt);
return expect (false, fmt);
}
}
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, std::size_t N, typename ...Args>
void
decltype(auto)
expect_mod (ValueA &&a, ValueB &&b, const char (&fmt)[N], Args &&...args)
{
return expect (a % b == 0, fmt, std::forward<Args> (args)...);
@ -70,11 +73,11 @@ namespace cruft::TAP {
///////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args, size_t N>
void
decltype(auto)
expect_eq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
#if 1
expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
return expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
#else
if (almost_equal (a, b))
return expect (true, fmt, std::forward<Args> (args)...);
@ -86,61 +89,61 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
decltype(auto)
expect_neq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
return expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, typename ...Args, size_t N>
void
decltype(auto)
expect_gt (const ValueA &a, const ValueB &b, const char (&fmt)[N], Args&&...args)
{
expect (a > b, fmt, std::forward<Args> (args)...);
return expect (a > b, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
decltype(auto)
expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (a >= b, fmt, std::forward<Args> (args)...);
return expect (a >= b, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
decltype(auto)
expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (a < b, fmt, std::forward<Args> (args)...);
return expect (a < b, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
decltype(auto)
expect_le (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (a <= b, fmt, std::forward<Args> (args)...);
return expect (a <= b, fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
void
decltype(auto)
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
{
expect (std::isnan (t), fmt, std::forward<Args> (args)...);
return expect (std::isnan (t), fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
void
decltype(auto)
expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args)
{
bool success = true;
@ -151,13 +154,13 @@ namespace cruft::TAP {
success = false;
}
expect (success, fmt, std::forward<Args> (args)...);
return expect (success, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename E, typename T, typename ...Args, size_t N>
void
decltype(auto)
expect_throw (T &&t, const char (&fmt)[N], Args&&...args)
{
bool success = false;
@ -170,15 +173,16 @@ namespace cruft::TAP {
success = false;
}
expect (success, fmt, std::forward<Args> (args)...);
return expect (success, fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <size_t N, typename ...Args>
void fail (const char (&fmt)[N], Args &&...args)
decltype(auto)
fail (const char (&fmt)[N], Args &&...args)
{
expect (false, fmt, std::forward<Args> (args)...);
return expect (false, fmt, std::forward<Args> (args)...);
}