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"
|
2017-11-02 18:05:33 +11:00
|
|
|
#include "format.hpp"
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
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
|
|
|
|
2018-08-08 12:49:33 +10:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
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 ();
|
2018-10-16 18:06:18 +11:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
logger (std::ostream&);
|
2015-04-13 16:43:49 +10:00
|
|
|
~logger ();
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2018-10-16 18:06:18 +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
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
bool
|
2017-11-02 18:05:33 +11:00
|
|
|
expect (const bool test, const char (&fmt)[N], Args&&... args)
|
|
|
|
{
|
|
|
|
m_output << (test ? "ok " : "not ok ") << ++m_size
|
|
|
|
<< " - "
|
2018-08-13 14:51:33 +10:00
|
|
|
<< format::printf (fmt) (std::forward<Args> (args)...)
|
|
|
|
<< std::endl;
|
2017-11-02 18:05:33 +11:00
|
|
|
|
|
|
|
if (!test)
|
|
|
|
m_status = EXIT_FAILURE;
|
2018-08-08 12:49:33 +10:00
|
|
|
return test;
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
2015-01-29 15:45:30 +11:00
|
|
|
|
|
|
|
|
2015-04-13 21:45:49 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-11-02 18:05:33 +11:00
|
|
|
template <typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect (const std::function<bool(Args...)> &test, Args&&...args, const char (&fmt)[N])
|
|
|
|
{
|
|
|
|
try {
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (test (std::forward<Args> (args)...), fmt);
|
2017-11-02 18:05:33 +11:00
|
|
|
} catch (...) {
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (false, fmt);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 02:53:46 +10:00
|
|
|
|
2017-11-02 18:05:33 +11: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>
|
2018-08-08 12:49:33 +10:00
|
|
|
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)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_eq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-04-16 15:55:47 +10:00
|
|
|
#if 1
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
|
2018-04-16 15:55:47 +10:00
|
|
|
#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
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2015-04-13 21:45:49 +10:00
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_neq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
2015-07-21 02:53:46 +10:00
|
|
|
|
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ValueA, typename ValueB, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_gt (const ValueA &a, const ValueB &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (a > b, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (a >= b, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
2015-07-21 02:53:46 +10:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (a < b, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2015-04-13 21:46:56 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-11-02 18:05:33 +11:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_le (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (a <= b, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (std::isnan (t), fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
2015-04-13 21:46:56 +10:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
t ();
|
|
|
|
} catch (...) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (success, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
2015-07-21 02:53:46 +10:00
|
|
|
|
2015-07-02 17:03:36 +10:00
|
|
|
|
2015-04-13 21:46:56 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-11-02 18:05:33 +11:00
|
|
|
template <typename E, typename T, typename ...Args, size_t N>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
2017-11-02 18:05:33 +11:00
|
|
|
expect_throw (T &&t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
t ();
|
|
|
|
} catch (const E&) {
|
|
|
|
success = true;
|
|
|
|
} catch (...) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (success, fmt, std::forward<Args> (args)...);
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-10-02 14:14:39 +11:00
|
|
|
template <size_t N, typename ...Args>
|
2018-08-08 12:49:33 +10:00
|
|
|
decltype(auto)
|
|
|
|
fail (const char (&fmt)[N], Args &&...args)
|
2017-10-02 14:14:39 +11:00
|
|
|
{
|
2018-08-08 12:49:33 +10:00
|
|
|
return expect (false, fmt, std::forward<Args> (args)...);
|
2017-10-02 14:14:39 +11:00
|
|
|
}
|
|
|
|
|
2017-11-02 18:05:33 +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
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-04-05 13:55:07 +10:00
|
|
|
int status [[nodiscard]] (void) const;
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2017-11-02 18:05:33 +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
|
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
std::ostream &m_output;
|
2015-04-13 16:43:49 +10:00
|
|
|
int m_status;
|
|
|
|
size_t m_size;
|
|
|
|
};
|
2017-11-02 18:05:33 +11:00
|
|
|
}
|