ToStringVariant.tests.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
  2. #include "catch.hpp"
  3. #if defined(CATCH_CONFIG_CPP17_VARIANT)
  4. #include <string>
  5. #include <variant>
  6. // We need 2 types with non-trivial copies/moves
  7. struct MyType1 {
  8. MyType1() = default;
  9. [[noreturn]] MyType1(MyType1 const&) { throw 1; }
  10. MyType1& operator=(MyType1 const&) { throw 3; }
  11. };
  12. struct MyType2 {
  13. MyType2() = default;
  14. [[noreturn]] MyType2(MyType2 const&) { throw 2; }
  15. MyType2& operator=(MyType2 const&) { throw 4; }
  16. };
  17. TEST_CASE( "variant<std::monostate>", "[toString][variant][approvals]")
  18. {
  19. using type = std::variant<std::monostate>;
  20. CHECK( "{ }" == ::Catch::Detail::stringify(type{}) );
  21. type value {};
  22. CHECK( "{ }" == ::Catch::Detail::stringify(value) );
  23. CHECK( "{ }" == ::Catch::Detail::stringify(std::get<0>(value)) );
  24. }
  25. TEST_CASE( "variant<int>", "[toString][variant][approvals]")
  26. {
  27. using type = std::variant<int>;
  28. CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
  29. }
  30. TEST_CASE( "variant<float, int>", "[toString][variant][approvals]")
  31. {
  32. using type = std::variant<float, int>;
  33. CHECK( "0.5f" == ::Catch::Detail::stringify(type{0.5f}) );
  34. CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
  35. }
  36. TEST_CASE( "variant -- valueless-by-exception", "[toString][variant][approvals]" ) {
  37. using type = std::variant<MyType1, MyType2>;
  38. type value;
  39. REQUIRE_THROWS_AS(value.emplace<MyType2>(MyType2{}), int);
  40. REQUIRE(value.valueless_by_exception());
  41. CHECK("{valueless variant}" == ::Catch::Detail::stringify(value));
  42. }
  43. TEST_CASE( "variant<string, int>", "[toString][variant][approvals]")
  44. {
  45. using type = std::variant<std::string, int>;
  46. CHECK( "\"foo\"" == ::Catch::Detail::stringify(type{"foo"}) );
  47. CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
  48. }
  49. TEST_CASE( "variant<variant<float, int>, string>", "[toString][variant][approvals]")
  50. {
  51. using inner = std::variant<MyType1, float, int>;
  52. using type = std::variant<inner, std::string>;
  53. CHECK( "0.5f" == ::Catch::Detail::stringify(type{0.5f}) );
  54. CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
  55. CHECK( "\"foo\"" == ::Catch::Detail::stringify(type{"foo"}) );
  56. SECTION("valueless nested variant") {
  57. type value = inner{0.5f};
  58. REQUIRE( std::holds_alternative<inner>(value) );
  59. REQUIRE( std::holds_alternative<float>(std::get<inner>(value)) );
  60. REQUIRE_THROWS_AS( std::get<0>(value).emplace<MyType1>(MyType1{}), int );
  61. // outer variant is still valid and contains inner
  62. REQUIRE( std::holds_alternative<inner>(value) );
  63. // inner variant is valueless
  64. REQUIRE( std::get<inner>(value).valueless_by_exception() );
  65. CHECK( "{valueless variant}" == ::Catch::Detail::stringify(value) );
  66. }
  67. }
  68. TEST_CASE( "variant<nullptr,int,const char *>", "[toString][variant][approvals]" )
  69. {
  70. using type = std::variant<std::nullptr_t,int,const char *>;
  71. CHECK( "nullptr" == ::Catch::Detail::stringify(type{nullptr}) );
  72. CHECK( "42" == ::Catch::Detail::stringify(type{42}) );
  73. CHECK( "\"Catch me\"" == ::Catch::Detail::stringify(type{"Catch me"}) );
  74. }
  75. #endif // CATCH_INTERNAL_CONFIG_CPP17_VARIANT