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