2015-01-29 15:45:30 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-01-29 15:45:30 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-29 15:45:30 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-01-29 15:45:30 +11:00
|
|
|
*
|
2017-11-02 18:05:33 +11:00
|
|
|
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
|
2015-01-29 15:45:30 +11:00
|
|
|
*/
|
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
#ifndef CRUFT_UTIL_TAP_HPP
|
|
|
|
#define CRUFT_UTIL_TAP_HPP
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
#include "format.hpp"
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <ostream>
|
2015-04-13 16:43:49 +10:00
|
|
|
#include <functional>
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
namespace util::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 ();
|
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
|
|
|
|
2015-04-13 21:45:49 +10:00
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect (const bool test, const char (&fmt)[N], Args&&... args)
|
|
|
|
{
|
|
|
|
m_output << (test ? "ok " : "not ok ") << ++m_size
|
|
|
|
<< " - "
|
2018-01-09 16:28:25 +11:00
|
|
|
<< format::printf (fmt) (std::forward<Args> (args)...) << '\n';
|
2017-11-02 18:05:33 +11:00
|
|
|
|
|
|
|
if (!test)
|
|
|
|
m_status = EXIT_FAILURE;
|
|
|
|
}
|
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>
|
|
|
|
void
|
|
|
|
expect (const std::function<bool(Args...)> &test, Args&&...args, const char (&fmt)[N])
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
expect (test (std::forward<Args> (args)...), fmt);
|
|
|
|
} catch (...) {
|
|
|
|
expect (false, fmt);
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 02:53:46 +10:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
|
2018-03-02 12:16:05 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ValueA, typename ValueB, std::size_t N, typename ...Args>
|
|
|
|
void
|
|
|
|
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>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_eq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
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>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_neq (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
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>
|
|
|
|
void
|
|
|
|
expect_gt (const ValueA &a, const ValueB &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (a > b, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename U, typename ...Args, size_t N>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (a >= b, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
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>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (a < b, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
void
|
|
|
|
expect_le (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (a <= b, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename T, typename ...Args, size_t N>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
expect (std::isnan (t), fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
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>
|
2017-11-02 18:05:33 +11:00
|
|
|
void
|
|
|
|
expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
t ();
|
|
|
|
} catch (...) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
//---------------------------------------------------------------------
|
2017-11-02 18:05:33 +11:00
|
|
|
template <typename E, typename T, typename ...Args, size_t N>
|
|
|
|
void
|
|
|
|
expect_throw (T &&t, const char (&fmt)[N], Args&&...args)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
t ();
|
|
|
|
} catch (const E&) {
|
|
|
|
success = true;
|
|
|
|
} catch (...) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect (success, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-10-02 14:14:39 +11:00
|
|
|
template <size_t N, typename ...Args>
|
|
|
|
void fail (const char (&fmt)[N], Args &&...args)
|
|
|
|
{
|
|
|
|
expect (false, fmt, std::forward<Args> (args)...);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2015-04-13 16:43:49 +10:00
|
|
|
int status (void) const;
|
2015-01-29 15:45:30 +11:00
|
|
|
|
2017-11-02 18:05:33 +11:00
|
|
|
|
2015-04-13 16:43:49 +10:00
|
|
|
private:
|
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
|
|
|
}
|
2015-01-29 15:45:30 +11:00
|
|
|
|
|
|
|
#endif
|