libcruft-util/tap.hpp

268 lines
8.6 KiB
C++
Raw Normal View History

2015-01-29 15:45:30 +11:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015-01-29 15:45:30 +11:00
*
2019-01-03 15:48:34 +11:00
* Copyright 2015-2019 Danny Robson <danny@nerdcruft.net>
2015-01-29 15:45:30 +11:00
*/
2019-01-03 15:48:34 +11:00
#pragma once
2015-01-29 15:45:30 +11:00
2019-01-03 15:48:34 +11:00
#include "except.hpp"
#include "maths.hpp"
2021-04-13 16:05:08 +10:00
#include "debug/validate.hpp"
#include <fmt/ostream.h>
2018-03-22 16:10:06 +11:00
#include <iosfwd>
2015-04-13 16:43:49 +10:00
#include <functional>
2015-01-29 15:45:30 +11:00
#include <cmath>
namespace cruft::TAP {
2015-04-13 16:43:49 +10:00
/// A simple TAP (Test Anything Protocol) test case output
class logger {
public:
enum {
PASS,
FAIL,
SKIP,
TODO
};
2015-01-29 15:45:30 +11:00
2015-04-13 21:45:49 +10:00
//---------------------------------------------------------------------
2015-04-13 16:43:49 +10:00
logger ();
logger (std::ostream&);
2015-04-13 16:43:49 +10:00
~logger ();
2015-01-29 15:45:30 +11:00
// NOTE: explicitly disable copy constructors and all assignment
// operators so that we don't accidentally overwrite the contained
// status code in any instance. This value is required in all
// instances so that we can return the correct status code from a test
// binary.
logger (logger&&) = default;
logger (logger const &) = delete;
logger& operator= (logger const &) = delete;
logger& operator= (logger &&) = delete;
2019-01-03 15:48:34 +11:00
2015-04-13 21:45:49 +10:00
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
bool
expect (const bool test, const char (&fmt)[N], Args&&... args)
{
2021-04-13 16:05:08 +10:00
CHECK (!strstr (fmt, "%"));
m_output << (test ? "ok " : "not ok ") << ++m_size
2021-04-13 16:05:08 +10:00
<< " - ";
fmt::print (m_output, fmt, std::forward<Args> (args)...);
m_output << std::endl;
if (!test)
m_status = EXIT_FAILURE;
return test;
}
2015-01-29 15:45:30 +11:00
2015-04-13 21:45:49 +10:00
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
decltype(auto)
expect (const std::function<bool(Args...)> &test, Args&&...args, const char (&fmt)[N])
{
try {
return expect (test (std::forward<Args> (args)...), fmt);
} catch (...) {
return expect (false, fmt);
}
}
2015-07-21 02:53:46 +10:00
2018-08-08 12:49:47 +10:00
///////////////////////////////////////////////////////////////////////
template <typename ValueT, size_t N, typename ...ArgsT>
decltype(auto)
expect_valid (ValueT &&value, char const (&fmt)[N], ArgsT &&...args)
{
return expect (
debug::is_valid (
std::forward<ValueT> (value)
),
fmt,
std::forward<ArgsT> (args)...
);
}
2018-03-02 12:16:05 +11:00
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, std::size_t N, typename ...Args>
decltype(auto)
2018-03-02 12:16:05 +11:00
expect_mod (ValueA &&a, ValueB &&b, const char (&fmt)[N], Args &&...args)
{
return expect (a % b == 0, fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args, size_t N>
decltype(auto)
expect_eq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
#if 1
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)...);
else
return expect (false, "%! # %! != %!", format::printf (fmt)(std::forward<Args> (args)...), a, b);
#endif
}
2015-01-29 15:45:30 +11:00
2015-04-13 21:45:49 +10:00
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
decltype(auto)
expect_neq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
return expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
}
2015-07-21 02:53:46 +10:00
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, typename ...Args, size_t N>
decltype(auto)
expect_gt (const ValueA &a, const ValueB &b, const char (&fmt)[N], Args&&...args)
{
return expect (a > b, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
decltype(auto)
expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
return expect (a >= b, fmt, std::forward<Args> (args)...);
}
2015-07-21 02:53:46 +10:00
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
decltype(auto)
expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
return expect (a < b, fmt, std::forward<Args> (args)...);
}
2015-01-29 15:45:30 +11:00
2015-04-13 21:46:56 +10:00
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
decltype(auto)
expect_le (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
return expect (a <= b, fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
decltype(auto)
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
{
return expect (std::isnan (t), fmt, std::forward<Args> (args)...);
}
2015-04-13 21:46:56 +10:00
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
decltype(auto)
expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args)
{
bool success = true;
try {
t ();
} catch (...) {
success = false;
}
return expect (success, fmt, std::forward<Args> (args)...);
}
2015-07-21 02:53:46 +10:00
2015-07-02 17:03:36 +10:00
2015-04-13 21:46:56 +10:00
//---------------------------------------------------------------------
template <typename E, typename T, typename ...Args, size_t N>
decltype(auto)
expect_throw (T &&t, const char (&fmt)[N], Args&&...args)
{
bool success = false;
try {
t ();
} catch (const E&) {
success = true;
} catch (...) {
success = false;
}
return expect (success, fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
2017-10-02 14:14:39 +11:00
template <size_t N, typename ...Args>
decltype(auto)
fail (const char (&fmt)[N], Args &&...args)
2017-10-02 14:14:39 +11:00
{
return expect (false, fmt, std::forward<Args> (args)...);
2017-10-02 14:14:39 +11:00
}
//---------------------------------------------------------------------
2015-04-13 16:43:49 +10:00
void skip (const std::string &msg);
void todo (const std::string &msg);
void noop (void);
2015-01-29 15:45:30 +11:00
///////////////////////////////////////////////////////////////////////
int status [[nodiscard]] (void) const;
2015-01-29 15:45:30 +11:00
2019-01-03 15:48:34 +11:00
/// Invoke a FunctionT with a TAP::logger as the first argument, and
/// the remainder forwarded from the provided parameter pack.
///
/// If an exception escapes from the FunctionT the logger will be
/// marked as failed, using a message that attempts to use the raised
/// exception value (else a static generic message if the type isn't
/// easily discoverable).
///
/// Returns the status of the logger provided to the FunctionT.
template <typename FunctionT, typename ...Args>
static auto
run [[nodiscard]] (FunctionT &&function, Args&&...args) noexcept
{
logger tap;
try {
function (tap, args...);
return tap.status ();
} catch (std::exception const &err) {
tap.fail ("no exceptions: %s", err.what ());
} catch (cruft::error const &err) {
tap.fail ("no exceptions: %s", err);
} catch (...) {
tap.fail ("no exceptions");
}
return EXIT_FAILURE;
}
2015-04-13 16:43:49 +10:00
private:
2018-03-20 14:50:32 +11:00
#if !defined(NDEBUG)
mutable int m_reported = -1;
#endif
std::ostream &m_output;
2015-04-13 16:43:49 +10:00
int m_status;
size_t m_size;
};
}