2014-04-23 08:03:15 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 23/4/2014.
|
|
|
|
* Copyright 2014 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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_tostring.h"
|
|
|
|
#include "catch_interfaces_config.h"
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
namespace Detail {
|
|
|
|
|
2015-07-23 20:03:33 +02:00
|
|
|
const std::string unprintableString = "{?}";
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
namespace {
|
2015-07-23 20:03:33 +02:00
|
|
|
const int hexThreshold = 255;
|
|
|
|
|
2014-05-23 19:41:02 +02:00
|
|
|
struct Endianness {
|
|
|
|
enum Arch { Big, Little };
|
|
|
|
|
|
|
|
static Arch which() {
|
|
|
|
union _{
|
|
|
|
int asInt;
|
|
|
|
char asChar[sizeof (int)];
|
|
|
|
} u;
|
|
|
|
|
|
|
|
u.asInt = 1;
|
|
|
|
return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string rawMemoryToString( const void *object, std::size_t size ) {
|
2014-05-23 19:41:02 +02:00
|
|
|
// Reverse order for little endian architectures
|
|
|
|
int i = 0, end = static_cast<int>( size ), inc = 1;
|
|
|
|
if( Endianness::which() == Endianness::Little ) {
|
|
|
|
i = end-1;
|
|
|
|
end = inc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const *bytes = static_cast<unsigned char const *>(object);
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "0x" << std::setfill('0') << std::hex;
|
|
|
|
for( ; i != end; i += inc )
|
|
|
|
os << std::setw(2) << static_cast<unsigned>(bytes[i]);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string fpToString( T value, int precision ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << std::setprecision( precision )
|
|
|
|
<< std::fixed
|
|
|
|
<< value;
|
|
|
|
std::string d = oss.str();
|
|
|
|
std::size_t i = d.find_last_not_of( '0' );
|
|
|
|
if( i != std::string::npos && i != d.size()-1 ) {
|
|
|
|
if( d[i] == '.' )
|
|
|
|
i++;
|
|
|
|
d = d.substr( 0, i+1 );
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//// ======================================================= ////
|
|
|
|
//
|
|
|
|
// Out-of-line defs for full specialization of StringMaker
|
|
|
|
//
|
|
|
|
//// ======================================================= ////
|
|
|
|
|
|
|
|
std::string StringMaker<std::string>::operator()(const std::string& str) {
|
|
|
|
if (!getCurrentContext().getConfig()->showInvisibles()) {
|
|
|
|
return '"' + str + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string s("\"");
|
|
|
|
for (char c : str) {
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
s.append("\\n");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
s.append("\\t");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.push_back(c);
|
|
|
|
break;
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
s.append("\"");
|
|
|
|
return s;
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<std::wstring>::operator()(const std::wstring& wstr) {
|
2014-04-23 08:03:15 +02:00
|
|
|
std::string s;
|
2017-05-02 23:51:03 +02:00
|
|
|
s.reserve(wstr.size());
|
|
|
|
for (auto c : wstr) {
|
|
|
|
s += (c <= 0xff) ? static_cast<char>(c) : '?';
|
|
|
|
}
|
|
|
|
return ::Catch::Detail::stringify(s);
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<char const*>::operator()(char const* str) {
|
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<char*>::operator()(char* str) {
|
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::string{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<wchar_t const*>::operator()(wchar_t const * str) {
|
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::wstring{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-08-14 13:28:23 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<wchar_t *>::operator()(wchar_t * str) {
|
|
|
|
if (str) {
|
|
|
|
return ::Catch::Detail::stringify(std::wstring{ str });
|
|
|
|
} else {
|
|
|
|
return{ "{null string}" };
|
|
|
|
}
|
2014-08-14 13:28:23 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<int>::operator()(int value) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<long long>(value));
|
|
|
|
}
|
|
|
|
std::string StringMaker<long>::operator()(long value) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<long long>(value));
|
|
|
|
}
|
|
|
|
std::string StringMaker<long long>::operator()(long long value) {
|
2014-04-23 08:03:15 +02:00
|
|
|
std::ostringstream oss;
|
2015-05-20 19:28:22 +02:00
|
|
|
oss << value;
|
2017-05-02 23:51:03 +02:00
|
|
|
if (value > Detail::hexThreshold) {
|
2017-01-15 09:41:33 +01:00
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2017-05-02 23:51:03 +02:00
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<unsigned int>::operator()(unsigned int value) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<unsigned long>::operator()(unsigned long value) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
|
|
|
|
}
|
|
|
|
std::string StringMaker<unsigned long long>::operator()(unsigned long long value) {
|
2014-04-23 08:03:15 +02:00
|
|
|
std::ostringstream oss;
|
2017-05-02 23:51:03 +02:00
|
|
|
oss << value;
|
|
|
|
if (value > Detail::hexThreshold) {
|
|
|
|
oss << " (0x" << std::hex << value << ')';
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
return oss.str();
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:16:40 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<bool>::operator()(bool b) {
|
|
|
|
return b ? "true" : "false";
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<char>::operator()(char value) {
|
|
|
|
if (value == '\r') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\r'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\f') {
|
2017-02-01 21:31:14 +01:00
|
|
|
return "'\\f'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\n') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\n'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if (value == '\t') {
|
2016-04-21 05:52:20 +02:00
|
|
|
return "'\\t'";
|
2017-05-02 23:51:03 +02:00
|
|
|
} else if ('\0' <= value && value < ' ') {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<unsigned int>(value));
|
|
|
|
} else {
|
|
|
|
char chstr[] = "' '";
|
|
|
|
chstr[1] = value;
|
|
|
|
return chstr;
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<signed char>::operator()(signed char c) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<char>(c));
|
|
|
|
}
|
|
|
|
std::string StringMaker<unsigned char>::operator()(unsigned char c) {
|
|
|
|
return ::Catch::Detail::stringify(static_cast<char>(c));
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 00:29:36 +02:00
|
|
|
std::string StringMaker<std::nullptr_t>::operator()(std::nullptr_t) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return "nullptr";
|
2014-04-23 08:03:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<float>::operator()(float value) {
|
|
|
|
return fpToString(value, 5) + 'f';
|
2015-07-23 20:03:33 +02:00
|
|
|
}
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<double>::operator()(double value) {
|
|
|
|
return fpToString(value, 10);
|
2015-07-23 20:03:33 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-04-23 08:03:15 +02:00
|
|
|
|
|
|
|
#ifdef __OBJC__
|
2017-05-02 23:51:03 +02:00
|
|
|
std::string StringMaker<NSString const *>::operator()(NSString const * const& nsstring) {
|
|
|
|
if (!nsstring)
|
|
|
|
return "nil";
|
|
|
|
return "@" + toString([nsstring UTF8String]);
|
|
|
|
}
|
|
|
|
std::string StringMaker<NSString * CATCH_ARC_STRONG>::operator()(NSString * CATCH_ARC_STRONG const& nsstring) {
|
|
|
|
if (!nsstring)
|
|
|
|
return "nil";
|
|
|
|
return "@" + toString([nsstring UTF8String]);
|
|
|
|
}
|
|
|
|
std::string StringMaker<NSObject *>::operator()(NSObject * const& nsObject) {
|
|
|
|
return ::Catch::Detail::stringify([nsObject description]);
|
|
|
|
}
|
2014-04-23 08:03:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|