ToStringGeneral.tests.cpp 5.8 KB

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