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