2012-05-08 09:10:49 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 8/5/2012.
|
|
|
|
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
2014-04-23 08:03:15 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-11-07 11:35:59 +01:00
|
|
|
#include <vector>
|
2014-04-23 19:19:19 +02:00
|
|
|
#include <cstddef>
|
2017-07-25 22:13:14 +02:00
|
|
|
#include <type_traits>
|
|
|
|
#include <string>
|
2018-03-01 18:41:17 +01:00
|
|
|
#include "catch_compiler_capabilities.h"
|
2017-11-07 19:01:10 +01:00
|
|
|
#include "catch_stream.h"
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2018-09-04 17:01:26 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP17_STRING_VIEW
|
|
|
|
#include <string_view>
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 09:17:07 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
#include "catch_objc_arc.hpp"
|
|
|
|
#endif
|
|
|
|
|
2017-09-07 12:25:10 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
2017-09-07 16:51:33 +02:00
|
|
|
#pragma warning(disable:4180) // We attempt to stream a function (address) by const&, which MSVC complains about but is harmless
|
2017-09-07 12:25:10 +02:00
|
|
|
#endif
|
2017-05-02 23:51:03 +02:00
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
// We need a dummy global operator<< so we can bring it into Catch namespace later
|
2017-11-19 14:47:18 +01:00
|
|
|
struct Catch_global_namespace_dummy {};
|
2017-05-02 23:51:03 +02:00
|
|
|
std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy);
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
namespace Catch {
|
2017-05-02 23:51:03 +02:00
|
|
|
// Bring in operator<< from global namespace into Catch namespace
|
|
|
|
using ::operator<<;
|
2014-09-01 18:35:01 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
namespace Detail {
|
2017-07-19 09:16:55 +02:00
|
|
|
|
|
|
|
extern const std::string unprintableString;
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string rawMemoryToString( const void *object, std::size_t size );
|
2014-09-04 01:12:25 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<typename T>
|
|
|
|
std::string rawMemoryToString( const T& object ) {
|
|
|
|
return rawMemoryToString( &object, sizeof(object) );
|
|
|
|
}
|
2014-09-04 01:12:25 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<typename T>
|
|
|
|
class IsStreamInsertable {
|
|
|
|
template<typename SS, typename TT>
|
|
|
|
static auto test(int)
|
|
|
|
-> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
|
|
|
|
|
|
|
|
template<typename, typename>
|
|
|
|
static auto test(...)->std::false_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value = decltype(test<std::ostream, const T&>(0))::value;
|
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
|
2018-01-04 11:21:52 +01:00
|
|
|
template<typename E>
|
|
|
|
std::string convertUnknownEnumToString( E e );
|
|
|
|
|
|
|
|
template<typename T>
|
2018-05-12 17:46:25 +02:00
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_enum<T>::value && !std::is_base_of<std::exception, T>::value,
|
2018-05-14 19:46:30 +02:00
|
|
|
std::string>::type convertUnstreamable( T const& ) {
|
2018-01-04 11:21:52 +01:00
|
|
|
return Detail::unprintableString;
|
2018-02-03 18:16:33 +01:00
|
|
|
}
|
2018-01-04 11:21:52 +01:00
|
|
|
template<typename T>
|
2018-05-12 17:46:25 +02:00
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_enum<T>::value && std::is_base_of<std::exception, T>::value,
|
|
|
|
std::string>::type convertUnstreamable(T const& ex) {
|
|
|
|
return ex.what();
|
2018-02-03 18:16:33 +01:00
|
|
|
}
|
2018-01-04 11:21:52 +01:00
|
|
|
|
2018-03-08 22:40:18 +01:00
|
|
|
|
2018-05-12 17:46:25 +02:00
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_enum<T>::value
|
|
|
|
, std::string>::type convertUnstreamable( T const& value ) {
|
|
|
|
return convertUnknownEnumToString( value );
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:40:18 +01:00
|
|
|
#if defined(_MANAGED)
|
|
|
|
//! Convert a CLR string to a utf8 std::string
|
|
|
|
template<typename T>
|
|
|
|
std::string clrReferenceToString( T^ ref ) {
|
|
|
|
if (ref == nullptr)
|
|
|
|
return std::string("null");
|
|
|
|
auto bytes = System::Text::Encoding::UTF8->GetBytes(ref->ToString());
|
|
|
|
cli::pin_ptr<System::Byte> p = &bytes[0];
|
|
|
|
return std::string(reinterpret_cast<char const *>(p), bytes->Length);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
} // namespace Detail
|
|
|
|
|
2018-01-04 11:21:52 +01:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
// If we decide for C++14, change these to enable_if_ts
|
2018-01-04 10:30:06 +01:00
|
|
|
template <typename T, typename = void>
|
2017-05-02 23:51:03 +02:00
|
|
|
struct StringMaker {
|
|
|
|
template <typename Fake = T>
|
2017-05-21 23:40:05 +02:00
|
|
|
static
|
2017-05-02 23:51:03 +02:00
|
|
|
typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
|
2018-01-04 11:21:52 +01:00
|
|
|
convert(const Fake& value) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
2018-05-31 04:32:59 +02:00
|
|
|
// NB: call using the function-like syntax to avoid ambiguity with
|
|
|
|
// user-defined templated operator<< under clang.
|
|
|
|
rss.operator<<(value);
|
2017-11-07 19:01:10 +01:00
|
|
|
return rss.str();
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template <typename Fake = T>
|
2017-05-21 23:40:05 +02:00
|
|
|
static
|
2017-05-02 23:51:03 +02:00
|
|
|
typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
|
2018-01-04 11:21:52 +01:00
|
|
|
convert( const Fake& value ) {
|
2018-05-14 19:46:30 +02:00
|
|
|
#if !defined(CATCH_CONFIG_FALLBACK_STRINGIFIER)
|
|
|
|
return Detail::convertUnstreamable(value);
|
|
|
|
#else
|
|
|
|
return CATCH_CONFIG_FALLBACK_STRINGIFIER(value);
|
|
|
|
#endif
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
};
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
namespace Detail {
|
2014-09-08 09:14:59 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
// This function dispatches all stringification requests inside of Catch.
|
|
|
|
// Should be preferably called fully qualified, like ::Catch::Detail::stringify
|
|
|
|
template <typename T>
|
|
|
|
std::string stringify(const T& e) {
|
2017-05-21 23:40:05 +02:00
|
|
|
return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e);
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 11:21:52 +01:00
|
|
|
template<typename E>
|
|
|
|
std::string convertUnknownEnumToString( E e ) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<E>::type>(e));
|
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
|
2018-03-08 22:40:18 +01:00
|
|
|
#if defined(_MANAGED)
|
|
|
|
template <typename T>
|
|
|
|
std::string stringify( T^ e ) {
|
|
|
|
return ::Catch::StringMaker<T^>::convert(e);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-04 11:21:52 +01:00
|
|
|
} // namespace Detail
|
2017-05-02 23:51:03 +02:00
|
|
|
|
|
|
|
// Some predefined specializations
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct StringMaker<std::string> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(const std::string& str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2018-09-04 17:01:26 +02:00
|
|
|
|
|
|
|
#ifdef CATCH_CONFIG_CPP17_STRING_VIEW
|
2017-05-02 23:51:03 +02:00
|
|
|
template<>
|
2018-09-04 17:01:26 +02:00
|
|
|
struct StringMaker<std::string_view> {
|
|
|
|
static std::string convert(std::string_view str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2018-03-01 18:41:17 +01:00
|
|
|
#endif
|
2017-05-02 23:51:03 +02:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct StringMaker<char const *> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(char const * str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<char *> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(char * str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2018-04-06 11:39:40 +02:00
|
|
|
|
2018-03-01 18:41:17 +01:00
|
|
|
#ifdef CATCH_CONFIG_WCHAR
|
2018-09-04 17:01:26 +02:00
|
|
|
template<>
|
|
|
|
struct StringMaker<std::wstring> {
|
|
|
|
static std::string convert(const std::wstring& wstr);
|
|
|
|
};
|
|
|
|
|
|
|
|
# ifdef CATCH_CONFIG_CPP17_STRING_VIEW
|
|
|
|
template<>
|
|
|
|
struct StringMaker<std::wstring_view> {
|
|
|
|
static std::string convert(std::wstring_view str);
|
|
|
|
};
|
|
|
|
# endif
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<>
|
|
|
|
struct StringMaker<wchar_t const *> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(wchar_t const * str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<wchar_t *> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(wchar_t * str);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2018-03-01 18:41:17 +01:00
|
|
|
#endif
|
2017-04-05 09:53:10 +02:00
|
|
|
|
2018-04-06 11:39:40 +02:00
|
|
|
// TBD: Should we use `strnlen` to ensure that we don't go out of the buffer,
|
|
|
|
// while keeping string semantics?
|
2017-05-02 23:51:03 +02:00
|
|
|
template<int SZ>
|
|
|
|
struct StringMaker<char[SZ]> {
|
2018-04-06 11:39:40 +02:00
|
|
|
static std::string convert(char const* str) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<int SZ>
|
|
|
|
struct StringMaker<signed char[SZ]> {
|
2018-04-06 11:39:40 +02:00
|
|
|
static std::string convert(signed char const* str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) });
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<int SZ>
|
|
|
|
struct StringMaker<unsigned char[SZ]> {
|
2018-04-06 11:39:40 +02:00
|
|
|
static std::string convert(unsigned char const* str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ reinterpret_cast<char const *>(str) });
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2017-04-05 09:53:10 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<>
|
|
|
|
struct StringMaker<int> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(int value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<long> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(long value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<long long> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(long long value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<unsigned int> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(unsigned int value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<unsigned long> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(unsigned long value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<unsigned long long> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(unsigned long long value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2017-04-25 11:21:22 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<>
|
|
|
|
struct StringMaker<bool> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(bool b);
|
2014-09-01 18:35:01 +02:00
|
|
|
};
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<>
|
|
|
|
struct StringMaker<char> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(char c);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<signed char> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(signed char c);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<unsigned char> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(unsigned char c);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2017-05-03 00:29:36 +02:00
|
|
|
struct StringMaker<std::nullptr_t> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(std::nullptr_t);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct StringMaker<float> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(float value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<double> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(double value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StringMaker<T*> {
|
|
|
|
template <typename U>
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(U* p) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (p) {
|
|
|
|
return ::Catch::Detail::rawMemoryToString(p);
|
|
|
|
} else {
|
|
|
|
return "nullptr";
|
|
|
|
}
|
2014-09-01 18:35:01 +02:00
|
|
|
}
|
|
|
|
};
|
2017-04-25 11:21:22 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template <typename R, typename C>
|
|
|
|
struct StringMaker<R C::*> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(R C::* p) {
|
2017-05-02 23:51:03 +02:00
|
|
|
if (p) {
|
|
|
|
return ::Catch::Detail::rawMemoryToString(p);
|
|
|
|
} else {
|
|
|
|
return "nullptr";
|
|
|
|
}
|
2014-09-01 18:35:01 +02:00
|
|
|
}
|
2013-04-21 00:12:17 +02:00
|
|
|
};
|
2012-05-15 08:42:26 +02:00
|
|
|
|
2018-03-08 22:40:18 +01:00
|
|
|
#if defined(_MANAGED)
|
|
|
|
template <typename T>
|
|
|
|
struct StringMaker<T^> {
|
|
|
|
static std::string convert( T^ ref ) {
|
|
|
|
return ::Catch::Detail::clrReferenceToString(ref);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<typename InputIterator>
|
|
|
|
std::string rangeToString(InputIterator first, InputIterator last) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << "{ ";
|
2017-05-02 23:51:03 +02:00
|
|
|
if (first != last) {
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << ::Catch::Detail::stringify(*first);
|
2017-05-02 23:51:03 +02:00
|
|
|
for (++first; first != last; ++first)
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << ", " << ::Catch::Detail::stringify(*first);
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2017-11-07 19:01:10 +01:00
|
|
|
rss << " }";
|
|
|
|
return rss.str();
|
2013-04-21 00:12:17 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 12:31:22 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
template<>
|
|
|
|
struct StringMaker<NSString*> {
|
|
|
|
static std::string convert(NSString * nsstring) {
|
|
|
|
if (!nsstring)
|
|
|
|
return "nil";
|
|
|
|
return std::string("@") + [nsstring UTF8String];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct StringMaker<NSObject*> {
|
|
|
|
static std::string convert(NSObject* nsObject) {
|
|
|
|
return ::Catch::Detail::stringify([nsObject description]);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
namespace Detail {
|
|
|
|
inline std::string stringify( NSString* nsstring ) {
|
|
|
|
return StringMaker<NSString*>::convert( nsstring );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Detail
|
|
|
|
#endif // __OBJC__
|
|
|
|
|
|
|
|
} // namespace Catch
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// Separate std-lib types stringification, so it can be selectively enabled
|
|
|
|
// This means that we do not bring in
|
|
|
|
|
|
|
|
#if defined(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS)
|
|
|
|
# define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER
|
|
|
|
# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
|
2018-09-20 14:13:35 +02:00
|
|
|
# define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
|
2017-10-09 12:31:22 +02:00
|
|
|
# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Separate std::pair specialization
|
|
|
|
#if defined(CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER)
|
|
|
|
#include <utility>
|
|
|
|
namespace Catch {
|
2017-05-02 23:51:03 +02:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct StringMaker<std::pair<T1, T2> > {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(const std::pair<T1, T2>& pair) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << "{ "
|
2017-05-02 23:51:03 +02:00
|
|
|
<< ::Catch::Detail::stringify(pair.first)
|
|
|
|
<< ", "
|
|
|
|
<< ::Catch::Detail::stringify(pair.second)
|
|
|
|
<< " }";
|
2017-11-07 19:01:10 +01:00
|
|
|
return rss.str();
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
}
|
|
|
|
#endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER
|
2014-04-22 09:19:11 +02:00
|
|
|
|
2017-10-09 12:31:22 +02:00
|
|
|
// Separate std::tuple specialization
|
|
|
|
#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER)
|
|
|
|
#include <tuple>
|
|
|
|
namespace Catch {
|
2017-05-02 23:51:03 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<
|
|
|
|
typename Tuple,
|
|
|
|
std::size_t N = 0,
|
|
|
|
bool = (N < std::tuple_size<Tuple>::value)
|
|
|
|
>
|
|
|
|
struct TupleElementPrinter {
|
|
|
|
static void print(const Tuple& tuple, std::ostream& os) {
|
|
|
|
os << (N ? ", " : " ")
|
|
|
|
<< ::Catch::Detail::stringify(std::get<N>(tuple));
|
|
|
|
TupleElementPrinter<Tuple, N + 1>::print(tuple, os);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename Tuple,
|
|
|
|
std::size_t N
|
|
|
|
>
|
|
|
|
struct TupleElementPrinter<Tuple, N, false> {
|
|
|
|
static void print(const Tuple&, std::ostream&) {}
|
|
|
|
};
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2014-01-07 18:25:27 +01:00
|
|
|
}
|
2017-04-25 11:21:22 +02:00
|
|
|
|
2014-09-04 01:31:11 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
template<typename ...Types>
|
|
|
|
struct StringMaker<std::tuple<Types...>> {
|
2017-05-21 23:40:05 +02:00
|
|
|
static std::string convert(const std::tuple<Types...>& tuple) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << '{';
|
|
|
|
Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get());
|
|
|
|
rss << " }";
|
|
|
|
return rss.str();
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
}
|
|
|
|
#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
|
|
|
|
|
2018-09-20 14:13:35 +02:00
|
|
|
#if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT)
|
|
|
|
#include <variant>
|
|
|
|
namespace Catch {
|
|
|
|
template<>
|
|
|
|
struct StringMaker<std::monostate> {
|
|
|
|
static std::string convert(const std::monostate&) {
|
|
|
|
return "{ }";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Elements>
|
|
|
|
struct StringMaker<std::variant<Elements...>> {
|
|
|
|
static std::string convert(const std::variant<Elements...>& variant) {
|
|
|
|
if (variant.valueless_by_exception()) {
|
|
|
|
return "{valueless variant}";
|
|
|
|
} else {
|
|
|
|
return std::visit(
|
|
|
|
[](const auto& value) {
|
|
|
|
return ::Catch::Detail::stringify(value);
|
|
|
|
},
|
|
|
|
variant
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif // CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
|
|
|
|
|
2018-01-04 10:30:06 +01:00
|
|
|
namespace Catch {
|
|
|
|
struct not_this_one {}; // Tag type for detecting which begin/ end are being selected
|
|
|
|
|
|
|
|
// Import begin/ end from std here so they are considered alongside the fallback (...) overloads in this namespace
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
|
|
|
|
not_this_one begin( ... );
|
|
|
|
not_this_one end( ... );
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_range {
|
|
|
|
static const bool value =
|
|
|
|
!std::is_same<decltype(begin(std::declval<T>())), not_this_one>::value &&
|
|
|
|
!std::is_same<decltype(end(std::declval<T>())), not_this_one>::value;
|
|
|
|
};
|
|
|
|
|
2018-03-08 22:40:18 +01:00
|
|
|
#if defined(_MANAGED) // Managed types are never ranges
|
|
|
|
template <typename T>
|
|
|
|
struct is_range<T^> {
|
|
|
|
static const bool value = false;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-01-04 11:05:02 +01:00
|
|
|
template<typename Range>
|
|
|
|
std::string rangeToString( Range const& range ) {
|
|
|
|
return ::Catch::Detail::rangeToString( begin( range ), end( range ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle vector<bool> specially
|
|
|
|
template<typename Allocator>
|
|
|
|
std::string rangeToString( std::vector<bool, Allocator> const& v ) {
|
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << "{ ";
|
|
|
|
bool first = true;
|
|
|
|
for( bool b : v ) {
|
|
|
|
if( first )
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
rss << ", ";
|
|
|
|
rss << ::Catch::Detail::stringify( b );
|
|
|
|
}
|
|
|
|
rss << " }";
|
|
|
|
return rss.str();
|
|
|
|
}
|
|
|
|
|
2018-01-04 10:30:06 +01:00
|
|
|
template<typename R>
|
2018-02-01 14:07:23 +01:00
|
|
|
struct StringMaker<R, typename std::enable_if<is_range<R>::value && !::Catch::Detail::IsStreamInsertable<R>::value>::type> {
|
2018-01-04 10:30:06 +01:00
|
|
|
static std::string convert( R const& range ) {
|
2018-01-04 11:05:02 +01:00
|
|
|
return rangeToString( range );
|
2018-01-04 10:30:06 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-14 18:06:21 +01:00
|
|
|
template <typename T, int SZ>
|
|
|
|
struct StringMaker<T[SZ]> {
|
|
|
|
static std::string convert(T const(&arr)[SZ]) {
|
|
|
|
return rangeToString(arr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-04 10:30:06 +01:00
|
|
|
} // namespace Catch
|
2017-10-09 12:31:22 +02:00
|
|
|
|
|
|
|
// Separate std::chrono::duration specialization
|
|
|
|
#if defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER)
|
2017-11-08 08:31:48 +01:00
|
|
|
#include <ctime>
|
2017-10-09 12:31:22 +02:00
|
|
|
#include <ratio>
|
2017-10-09 13:13:30 +02:00
|
|
|
#include <chrono>
|
2017-10-09 12:31:22 +02:00
|
|
|
|
2017-11-19 14:54:52 +01:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
2017-10-09 12:31:22 +02:00
|
|
|
template <class Ratio>
|
|
|
|
struct ratio_string {
|
|
|
|
static std::string symbol();
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ratio>
|
|
|
|
std::string ratio_string<Ratio>::symbol() {
|
2017-11-07 19:01:10 +01:00
|
|
|
Catch::ReusableStringStream rss;
|
|
|
|
rss << '[' << Ratio::num << '/'
|
2017-10-09 12:31:22 +02:00
|
|
|
<< Ratio::den << ']';
|
2017-11-07 19:01:10 +01:00
|
|
|
return rss.str();
|
2017-10-09 12:31:22 +02:00
|
|
|
}
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::atto> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::femto> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::pico> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::nano> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::micro> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct ratio_string<std::milli> {
|
2017-11-19 14:54:52 +01:00
|
|
|
static std::string symbol();
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-10-09 12:31:22 +02:00
|
|
|
////////////
|
|
|
|
// std::chrono::duration specializations
|
|
|
|
template<typename Value, typename Ratio>
|
|
|
|
struct StringMaker<std::chrono::duration<Value, Ratio>> {
|
|
|
|
static std::string convert(std::chrono::duration<Value, Ratio> const& duration) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << duration.count() << ' ' << ratio_string<Ratio>::symbol() << 's';
|
|
|
|
return rss.str();
|
2013-09-21 19:45:42 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
template<typename Value>
|
|
|
|
struct StringMaker<std::chrono::duration<Value, std::ratio<1>>> {
|
|
|
|
static std::string convert(std::chrono::duration<Value, std::ratio<1>> const& duration) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << duration.count() << " s";
|
|
|
|
return rss.str();
|
2017-09-06 16:43:26 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
template<typename Value>
|
|
|
|
struct StringMaker<std::chrono::duration<Value, std::ratio<60>>> {
|
|
|
|
static std::string convert(std::chrono::duration<Value, std::ratio<60>> const& duration) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << duration.count() << " m";
|
|
|
|
return rss.str();
|
2017-09-06 16:43:26 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2017-10-09 12:31:22 +02:00
|
|
|
template<typename Value>
|
|
|
|
struct StringMaker<std::chrono::duration<Value, std::ratio<3600>>> {
|
|
|
|
static std::string convert(std::chrono::duration<Value, std::ratio<3600>> const& duration) {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << duration.count() << " h";
|
|
|
|
return rss.str();
|
2017-09-06 16:43:26 +02:00
|
|
|
}
|
2017-10-09 12:31:22 +02:00
|
|
|
};
|
2017-09-06 16:43:26 +02:00
|
|
|
|
2017-10-09 12:31:22 +02:00
|
|
|
////////////
|
|
|
|
// std::chrono::time_point specialization
|
|
|
|
// Generic time_point cannot be specialized, only std::chrono::time_point<system_clock>
|
|
|
|
template<typename Clock, typename Duration>
|
|
|
|
struct StringMaker<std::chrono::time_point<Clock, Duration>> {
|
|
|
|
static std::string convert(std::chrono::time_point<Clock, Duration> const& time_point) {
|
|
|
|
return ::Catch::Detail::stringify(time_point.time_since_epoch()) + " since epoch";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// std::chrono::time_point<system_clock> specialization
|
|
|
|
template<typename Duration>
|
|
|
|
struct StringMaker<std::chrono::time_point<std::chrono::system_clock, Duration>> {
|
|
|
|
static std::string convert(std::chrono::time_point<std::chrono::system_clock, Duration> const& time_point) {
|
|
|
|
auto converted = std::chrono::system_clock::to_time_t(time_point);
|
2017-11-08 08:31:48 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::tm timeInfo = {};
|
|
|
|
gmtime_s(&timeInfo, &converted);
|
|
|
|
#else
|
|
|
|
std::tm* timeInfo = std::gmtime(&converted);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");
|
|
|
|
char timeStamp[timeStampSize];
|
|
|
|
const char * const fmt = "%Y-%m-%dT%H:%M:%SZ";
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
|
|
|
|
#else
|
|
|
|
std::strftime(timeStamp, timeStampSize, fmt, timeInfo);
|
|
|
|
#endif
|
|
|
|
return std::string(timeStamp);
|
2017-10-09 12:31:22 +02:00
|
|
|
}
|
|
|
|
};
|
2017-11-08 08:31:48 +01:00
|
|
|
}
|
2017-10-09 12:31:22 +02:00
|
|
|
#endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
|
2017-05-02 23:51:03 +02:00
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2017-09-07 12:25:10 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
|