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