libcruft-util/cruft/util/tap.hpp

271 lines
8.8 KiB
C++
Raw Permalink 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>
bool
expect (const bool test, fmt::format_string<Args...> format, Args&&... args)
{
m_output << (test ? "ok " : "not ok ") << ++m_size
2021-04-13 16:05:08 +10:00
<< " - ";
fmt::print (m_output, format, std::forward<Args> (args)...);
2021-04-13 16:05:08 +10:00
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>
decltype(auto)
expect (const std::function<bool(Args...)> &test, Args&&...args, fmt::format_string<Args...> fmt)
{
try {
return expect (test (std::forward<Args> (args)...), std::move (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, typename ...ArgsT>
2018-08-08 12:49:47 +10:00
decltype(auto)
expect_valid (ValueT &&value, fmt::format_string<ArgsT...> fmt, ArgsT &&...args)
2018-08-08 12:49:47 +10:00
{
return expect (
debug::is_valid (
std::forward<ValueT> (value)
),
std::move (fmt),
2018-08-08 12:49:47 +10:00
std::forward<ArgsT> (args)...
);
}
2018-03-02 12:16:05 +11:00
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, typename ...Args>
decltype(auto)
expect_mod (ValueA &&a, ValueB &&b, fmt::format_string<Args...> fmt, Args &&...args)
2018-03-02 12:16:05 +11:00
{
return expect (
a % b == 0,
std::move (fmt),
std::forward<Args> (args)...
);
2018-03-02 12:16:05 +11:00
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args>
decltype(auto)
expect_eq (const T &a, const U &b, fmt::format_string<Args...> fmt, Args&&...args)
{
#if 1
return expect (almost_equal (a, b), std::move (fmt), std::forward<Args> (args)...);
#else
if (almost_equal (a, b))
return expect (true, fmt, std::forward<Args> (args)...);
else
2021-04-14 10:23:33 +10:00
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>
decltype(auto)
expect_neq (const T &a, const U &b, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (!almost_equal (a, b), std::move (fmt), std::forward<Args> (args)...);
}
2015-07-21 02:53:46 +10:00
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, typename ...Args>
decltype(auto)
expect_gt (const ValueA &a, const ValueB &b, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (a > b, std::move (fmt), std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args>
decltype(auto)
expect_ge (const T &a, const U &b, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (a >= b, std::move (fmt), std::forward<Args> (args)...);
}
2015-07-21 02:53:46 +10:00
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args>
decltype(auto)
expect_lt (const T &a, const U &b, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (a < b, std::move (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>
decltype(auto)
expect_le (const T &a, const U &b, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (a <= b, std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args>
decltype(auto)
expect_nan (const T &t, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (std::isnan (t), std::move (fmt), std::forward<Args> (args)...);
}
2015-04-13 21:46:56 +10:00
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args>
decltype(auto)
expect_nothrow (T &&t, fmt::format_string<Args...> fmt, Args&&...args)
{
bool success = true;
try {
t ();
} catch (...) {
success = false;
}
return expect (success, std::move (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>
decltype(auto)
expect_throw (T &&t, fmt::format_string<Args...> fmt, Args&&...args)
{
bool success = false;
try {
t ();
} catch (const E&) {
success = true;
} catch (...) {
success = false;
}
return expect (success, std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename ...Args>
decltype(auto)
fail (fmt::format_string<Args...> fmt, 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) {
2021-04-14 10:23:33 +10:00
tap.fail ("no exceptions: {:s}", err.what ());
2019-01-03 15:48:34 +11:00
} catch (cruft::error const &err) {
tap.fail ("no exceptions: {}", err);
2019-01-03 15:48:34 +11:00
} 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;
};
}