| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER |
| 10 |
#include <catch2/catch_test_macros.hpp> |
| 11 |
|
| 12 |
#include <map> |
| 13 |
#include <set> |
| 14 |
|
| 15 |
TEST_CASE( "Character pretty printing" ){ |
| 16 |
SECTION("Specifically escaped"){ |
| 17 |
CHECK(::Catch::Detail::stringify('\t') == "'\\t'"); |
| 18 |
CHECK(::Catch::Detail::stringify('\n') == "'\\n'"); |
| 19 |
CHECK(::Catch::Detail::stringify('\r') == "'\\r'"); |
| 20 |
CHECK(::Catch::Detail::stringify('\f') == "'\\f'"); |
| 21 |
} |
| 22 |
SECTION("General chars"){ |
| 23 |
CHECK(::Catch::Detail::stringify( ' ' ) == "' '" ); |
| 24 |
CHECK(::Catch::Detail::stringify( 'A' ) == "'A'" ); |
| 25 |
CHECK(::Catch::Detail::stringify( 'z' ) == "'z'" ); |
| 26 |
} |
| 27 |
SECTION("Low ASCII"){ |
| 28 |
CHECK(::Catch::Detail::stringify( '\0' ) == "0" ); |
| 29 |
CHECK(::Catch::Detail::stringify( static_cast<char>(2) ) == "2" ); |
| 30 |
CHECK(::Catch::Detail::stringify( static_cast<char>(5) ) == "5" ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
TEST_CASE( "Capture and info messages" ) { |
| 36 |
SECTION("Capture should stringify like assertions") { |
| 37 |
int i = 2; |
| 38 |
CAPTURE(i); |
| 39 |
REQUIRE(true); |
| 40 |
} |
| 41 |
SECTION("Info should NOT stringify the way assertions do") { |
| 42 |
int i = 3; |
| 43 |
INFO(i); |
| 44 |
REQUIRE(true); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
TEST_CASE( "std::map is convertible string", "[toString]" ) { |
| 49 |
|
| 50 |
SECTION( "empty" ) { |
| 51 |
std::map<std::string, int> emptyMap; |
| 52 |
|
| 53 |
REQUIRE( Catch::Detail::stringify( emptyMap ) == "{ }" ); |
| 54 |
} |
| 55 |
|
| 56 |
SECTION( "single item" ) { |
| 57 |
std::map<std::string, int> map = { { "one", 1 } }; |
| 58 |
|
| 59 |
REQUIRE( Catch::Detail::stringify( map ) == "{ { \"one\", 1 } }" ); |
| 60 |
} |
| 61 |
|
| 62 |
SECTION( "several items" ) { |
| 63 |
std::map<std::string, int> map = { |
| 64 |
{ "abc", 1 }, |
| 65 |
{ "def", 2 }, |
| 66 |
{ "ghi", 3 } |
| 67 |
}; |
| 68 |
|
| 69 |
REQUIRE( Catch::Detail::stringify( map ) == "{ { \"abc\", 1 }, { \"def\", 2 }, { \"ghi\", 3 } }" ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
TEST_CASE( "std::set is convertible string", "[toString]" ) { |
| 74 |
|
| 75 |
SECTION( "empty" ) { |
| 76 |
std::set<std::string> emptySet; |
| 77 |
|
| 78 |
REQUIRE( Catch::Detail::stringify( emptySet ) == "{ }" ); |
| 79 |
} |
| 80 |
|
| 81 |
SECTION( "single item" ) { |
| 82 |
std::set<std::string> set = { "one" }; |
| 83 |
|
| 84 |
REQUIRE( Catch::Detail::stringify( set ) == "{ \"one\" }" ); |
| 85 |
} |
| 86 |
|
| 87 |
SECTION( "several items" ) { |
| 88 |
std::set<std::string> set = { "abc", "def", "ghi" }; |
| 89 |
|
| 90 |
REQUIRE( Catch::Detail::stringify( set ) == "{ \"abc\", \"def\", \"ghi\" }" ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
TEST_CASE("Static arrays are convertible to string", "[toString]") { |
| 95 |
SECTION("Single item") { |
| 96 |
int singular[1] = { 1 }; |
| 97 |
REQUIRE(Catch::Detail::stringify(singular) == "{ 1 }"); |
| 98 |
} |
| 99 |
SECTION("Multiple") { |
| 100 |
int arr[3] = { 3, 2, 1 }; |
| 101 |
REQUIRE(Catch::Detail::stringify(arr) == "{ 3, 2, 1 }"); |
| 102 |
} |
| 103 |
SECTION("Non-trivial inner items") { |
| 104 |
std::vector<std::string> arr[2] = { {"1:1", "1:2", "1:3"}, {"2:1", "2:2"} }; |
| 105 |
REQUIRE(Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })"); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#ifdef CATCH_CONFIG_CPP17_STRING_VIEW |
| 110 |
|
| 111 |
TEST_CASE("String views are stringified like other strings", "[toString][approvals]") { |
| 112 |
std::string_view view{"abc"}; |
| 113 |
CHECK(Catch::Detail::stringify(view) == R"("abc")"); |
| 114 |
|
| 115 |
std::string_view arr[] { view }; |
| 116 |
CHECK(Catch::Detail::stringify(arr) == R"({ "abc" })"); |
| 117 |
} |
| 118 |
|
| 119 |
#endif |
| 120 |
|
| 121 |
TEST_CASE("Precision of floating point stringification can be set", "[toString][floatingPoint]") { |
| 122 |
SECTION("Floats") { |
| 123 |
using sm = Catch::StringMaker<float>; |
| 124 |
const auto oldPrecision = sm::precision; |
| 125 |
|
| 126 |
const float testFloat = 1.12345678901234567899f; |
| 127 |
sm::precision = 5; |
| 128 |
auto str1 = sm::convert( testFloat ); |
| 129 |
// "1." prefix = 2 chars, f suffix is another char |
| 130 |
CHECK(str1.size() == 3 + 5); |
| 131 |
|
| 132 |
sm::precision = 10; |
| 133 |
auto str2 = sm::convert(testFloat); |
| 134 |
REQUIRE(str2.size() == 3 + 10); |
| 135 |
sm::precision = oldPrecision; |
| 136 |
} |
| 137 |
SECTION("Double") { |
| 138 |
using sm = Catch::StringMaker<double>; |
| 139 |
const auto oldPrecision = sm::precision; |
| 140 |
|
| 141 |
const double testDouble = 1.123456789012345678901234567899; |
| 142 |
sm::precision = 5; |
| 143 |
auto str1 = sm::convert(testDouble); |
| 144 |
// "1." prefix = 2 chars |
| 145 |
CHECK(str1.size() == 2 + 5); |
| 146 |
|
| 147 |
sm::precision = 15; |
| 148 |
auto str2 = sm::convert(testDouble); |
| 149 |
REQUIRE(str2.size() == 2 + 15); |
| 150 |
|
| 151 |
sm::precision = oldPrecision; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
namespace { |
| 156 |
|
| 157 |
struct WhatException : std::exception { |
| 158 |
char const* what() const noexcept override { |
| 159 |
return "This exception has overridden what() method"; |
| 160 |
} |
| 161 |
~WhatException() override; |
| 162 |
}; |
| 163 |
|
| 164 |
struct OperatorException : std::exception { |
| 165 |
~OperatorException() override; |
| 166 |
}; |
| 167 |
|
| 168 |
std::ostream& operator<<(std::ostream& out, OperatorException const&) { |
| 169 |
out << "OperatorException"; |
| 170 |
return out; |
| 171 |
} |
| 172 |
|
| 173 |
struct StringMakerException : std::exception { |
| 174 |
~StringMakerException() override; |
| 175 |
}; |
| 176 |
|
| 177 |
} // end anonymous namespace |
| 178 |
|
| 179 |
namespace Catch { |
| 180 |
template <> |
| 181 |
struct StringMaker<StringMakerException> { |
| 182 |
static std::string convert(StringMakerException const&) { |
| 183 |
return "StringMakerException"; |
| 184 |
} |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
// Avoid -Wweak-tables |
| 189 |
WhatException::~WhatException() = default; |
| 190 |
OperatorException::~OperatorException() = default; |
| 191 |
StringMakerException::~StringMakerException() = default; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
TEST_CASE("Exception as a value (e.g. in REQUIRE_THROWS_MATCHES) can be stringified", "[toString][exception]") { |
| 197 |
REQUIRE(::Catch::Detail::stringify(WhatException{}) == "This exception has overridden what() method"); |
| 198 |
REQUIRE(::Catch::Detail::stringify(OperatorException{}) == "OperatorException"); |
| 199 |
REQUIRE(::Catch::Detail::stringify(StringMakerException{}) == "StringMakerException"); |
| 200 |
} |