tap: move ipp implementation into hpp/cpp

This makes it easier for CLion to perform syntax highlighting
accurately.
This commit is contained in:
Danny Robson 2017-11-02 18:05:33 +11:00
parent 28ec0bd48e
commit badba6de3b
4 changed files with 131 additions and 168 deletions

View File

@ -402,7 +402,6 @@ list (
strongdef.hpp strongdef.hpp
tap.cpp tap.cpp
tap.hpp tap.hpp
tap.ipp
term.cpp term.cpp
term.hpp term.hpp
time.cpp time.cpp

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/ */
#include "tap.hpp" #include "tap.hpp"
@ -23,6 +23,11 @@ using util::TAP::logger;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
logger::logger (): logger::logger ():
logger (std::cout)
{ ; }
logger::logger (std::ostream &_output):
m_output (_output),
m_status (EXIT_SUCCESS), m_status (EXIT_SUCCESS),
m_size (0) m_size (0)
{ ; } { ; }

153
tap.hpp
View File

@ -11,15 +11,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_TAP_HPP #ifndef CRUFT_UTIL_TAP_HPP
#define __UTIL_TAP_HPP #define CRUFT_UTIL_TAP_HPP
#include "format.hpp"
#include "maths.hpp"
#include <cmath>
#include <ostream>
#include <functional> #include <functional>
namespace util { namespace TAP { namespace util::TAP {
/// A simple TAP (Test Anything Protocol) test case output /// A simple TAP (Test Anything Protocol) test case output
class logger { class logger {
public: public:
@ -32,66 +37,158 @@ namespace util { namespace TAP {
//--------------------------------------------------------------------- //---------------------------------------------------------------------
logger (); logger ();
logger (std::ostream&);
~logger (); ~logger ();
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename ...Args, size_t N> template <typename ...Args, size_t N>
void expect (bool, const char (&fmt)[N], Args&&...); void
expect (const bool test, const char (&fmt)[N], Args&&... args)
{
m_output << (test ? "ok " : "not ok ") << ++m_size
<< " - "
<< util::format::render (fmt, std::forward<Args> (args)...) << '\n';
if (!test)
m_status = EXIT_FAILURE;
}
//---------------------------------------------------------------------
template <typename ...Args, size_t N> template <typename ...Args, size_t N>
void expect (const std::function<bool(Args...)>&, Args&&..., const char (&msg)[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);
}
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args, size_t N>
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)...);
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N> template <typename T, typename U, typename ...Args, size_t N>
void expect_eq (const T&, const U&, const char (&fmt)[N], Args&&...); 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)...);
}
///////////////////////////////////////////////////////////////////////
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)...);
}
template <typename T, typename U, typename ...Args, size_t N>
void expect_neq (const T&, const U&, const char (&fmt)[N], Args&&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N> template <typename T, typename U, typename ...Args, size_t N>
void expect_gt (const T&, const U&, const char (&fmt)[N], Args&&...); void
expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (a >= b, fmt, std::forward<Args> (args)...);
}
template <typename T, typename U, typename ...Args, size_t N>
void expect_ge (const T&, const U&, const char (&fmt)[N], Args&&...);
template <typename T, typename U, typename ...Args, size_t N>
void expect_lt (const T&, const U&, const char (&fmt)[N], Args&&...);
template <typename T, typename U, typename ...Args, size_t N>
void expect_le (const T&, const U&, const char (&fmt)[N], Args&&...);
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args)
{
expect (a < b, fmt, std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
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)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N> template <typename T, typename ...Args, size_t N>
void expect_nan (const T&, const char (&fmt)[N], Args&&...); void
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
{
expect (std::isnan (t), fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
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)...);
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
template <typename T, typename ...Args, size_t N>
void expect_nothrow (T&&, const char (&fmt)[N], Args&&...);
template <typename E, typename T, typename ...Args, size_t N> template <typename E, typename T, typename ...Args, size_t N>
void expect_throw (T&&, const char (&fmt)[N], Args&&...); 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)...);
}
///////////////////////////////////////////////////////////////////////
template <size_t N, typename ...Args> template <size_t N, typename ...Args>
void fail (const char (&fmt)[N], Args &&...args) void fail (const char (&fmt)[N], Args &&...args)
{ {
expect (false, fmt, std::forward<Args> (args)...); expect (false, fmt, std::forward<Args> (args)...);
} }
//---------------------------------------------------------------------
void skip (const std::string &msg); void skip (const std::string &msg);
void todo (const std::string &msg); void todo (const std::string &msg);
void noop (void); void noop (void);
//---------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////
int status (void) const; int status (void) const;
private: private:
std::ostream &m_output;
int m_status; int m_status;
size_t m_size; size_t m_size;
}; };
} } }
#include "tap.ipp"
#endif #endif

138
tap.ipp
View File

@ -1,138 +0,0 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_TAP_IPP
#error
#endif
#define __UTIL_TAP_IPP
#include "maths.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include "format.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename ...Args, size_t N>
void
util::TAP::logger::expect (bool test, const char (&fmt)[N], Args&&... args)
{
std::cout << (test ? "ok " : "not ok ") << ++m_size
<< " - "
<< util::format::render (fmt, std::forward<Args> (args)...) << '\n';
if (!test)
m_status = EXIT_FAILURE;
}
//-----------------------------------------------------------------------------
template <typename ...Args, size_t N>
void
util::TAP::logger::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);
}
}
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args, size_t N>
void
util::TAP::logger::expect_eq (const T&a, const U &b, const char (&fmt)[N], Args&&... args)
{
expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
}
//-----------------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
void
util::TAP::logger::expect_neq (const T&a, const U &b, const char (&fmt)[N], Args&&... args)
{
expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////////////
#define TAP_TEST(SUFFIX,OP) \
template <typename T, typename U, typename ...Args, size_t N> \
void \
util::TAP::logger::expect_ ## SUFFIX (const T &a, \
const U &b, \
const char (&fmt)[N], \
Args&&... args) \
{ \
expect ((a) OP (b), fmt, std::forward<Args> (args)...); \
}
TAP_TEST(gt, > )
TAP_TEST(ge, >=)
TAP_TEST(lt, < )
TAP_TEST(le, <=)
#undef TAP_TEST
//-----------------------------------------------------------------------------
template <typename T, typename ...Args, size_t N>
void
util::TAP::logger::expect_nan (const T &t, const char (&fmt)[N], Args&&... args)
{
expect (std::isnan (t), fmt, std::forward<Args> (args)...);
}
//-----------------------------------------------------------------------------
template <typename T, typename ...Args, size_t N>
void
util::TAP::logger::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)...);
}
//-----------------------------------------------------------------------------
template <typename E, typename T, typename ...Args, size_t N>
void
util::TAP::logger::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)...);
}