ToString.tests.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <catch2/internal/catch_enum_values_registry.hpp>
  7. #include <catch2/matchers/catch_matchers_vector.hpp>
  8. #include <catch2/catch_test_macros.hpp>
  9. #include <catch2/catch_template_test_macros.hpp>
  10. enum class EnumClass3 { Value1, Value2, Value3, Value4 };
  11. struct UsesSentinel {
  12. using const_iterator = int const*;
  13. using const_sentinel = std::nullptr_t;
  14. const_iterator begin() const { return nullptr; }
  15. const_iterator end() const { return nullptr; }
  16. };
  17. TEST_CASE( "parseEnums", "[Strings][enums]" ) {
  18. using namespace Catch::Matchers;
  19. using Catch::Detail::parseEnums;
  20. SECTION( "No enums" )
  21. CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) );
  22. SECTION( "One enum value" ) {
  23. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ),
  24. Equals(std::vector<Catch::StringRef>{"Value1"} ) );
  25. CHECK_THAT( parseEnums( "Value1" ),
  26. Equals( std::vector<Catch::StringRef>{"Value1"} ) );
  27. CHECK_THAT( parseEnums( "EnumName::Value1" ),
  28. Equals(std::vector<Catch::StringRef>{"Value1"} ) );
  29. }
  30. SECTION( "Multiple enum values" ) {
  31. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ),
  32. Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) );
  33. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ),
  34. Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
  35. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ),
  36. Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
  37. }
  38. }
  39. TEST_CASE( "Directly creating an EnumInfo" ) {
  40. using namespace Catch::Detail;
  41. auto enumInfo = makeEnumInfo( "EnumName", "EnumName::Value1, EnumName::Value2", {0, 1} );
  42. CHECK( enumInfo->lookup(0) == "Value1" );
  43. CHECK( enumInfo->lookup(1) == "Value2" );
  44. CHECK( enumInfo->lookup(3) == "{** unexpected enum value **}" );
  45. }
  46. TEST_CASE("Range type with sentinel") {
  47. CHECK( Catch::Detail::stringify(UsesSentinel{}) == "{ }" );
  48. }
  49. TEST_CASE("convertIntoString stringification helper", "[toString][approvals]") {
  50. using namespace std::string_literals;
  51. using Catch::Detail::convertIntoString;
  52. using namespace Catch;
  53. SECTION("No escaping") {
  54. CHECK(convertIntoString(""_sr, false) == R"("")"s);
  55. CHECK(convertIntoString("abcd"_sr, false) == R"("abcd")"s);
  56. CHECK(convertIntoString("ab\ncd"_sr, false) == "\"ab\ncd\""s);
  57. CHECK(convertIntoString("ab\r\ncd"_sr, false) == "\"ab\r\ncd\""s);
  58. CHECK(convertIntoString("ab\"cd"_sr, false) == R"("ab"cd")"s);
  59. }
  60. SECTION("Escaping invisibles") {
  61. CHECK(convertIntoString(""_sr, true) == R"("")"s);
  62. CHECK(convertIntoString("ab\ncd"_sr, true) == R"("ab\ncd")"s);
  63. CHECK(convertIntoString("ab\r\ncd"_sr, true) == R"("ab\r\ncd")"s);
  64. CHECK(convertIntoString("ab\tcd"_sr, true) == R"("ab\tcd")"s);
  65. CHECK(convertIntoString("ab\fcd"_sr, true) == R"("ab\fcd")"s);
  66. CHECK(convertIntoString("ab\"cd"_sr, true) == R"("ab"cd")"s);
  67. }
  68. }
  69. TEMPLATE_TEST_CASE( "Stringifying char arrays with statically known sizes",
  70. "[toString]",
  71. char,
  72. signed char,
  73. unsigned char ) {
  74. using namespace std::string_literals;
  75. TestType with_null_terminator[10] = "abc";
  76. CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s );
  77. TestType no_null_terminator[3] = { 'a', 'b', 'c' };
  78. CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s );
  79. }