diff options
| author | Robin Moussu | 2019-01-26 18:32:17 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2019-01-26 18:32:17 +0100 |
| commit | a2baabbf712130f96c087a13fb80ff3c0c4fd9f0 (patch) | |
| tree | 3eab0fe2cd7ba64f326f3e83e72fc7ebea993710 /projects/SelfTest/UsageTests/ToStringVariant.tests.cpp | |
| parent | 6f9cdd6583f3f2ea92eac334213c4fa3c79bc71f (diff) | |
| download | catch3-a2baabbf712130f96c087a13fb80ff3c0c4fd9f0.tar.gz | |
Fix variant test for libstdc++ version 9 (#1515)
By using non-trivially copyable types, we force libstdc++-9's variant to
properly enter the valueless-by-exception state for our stringification
test.
Related to #1511
Diffstat (limited to 'projects/SelfTest/UsageTests/ToStringVariant.tests.cpp')
| -rw-r--r-- | projects/SelfTest/UsageTests/ToStringVariant.tests.cpp | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/projects/SelfTest/UsageTests/ToStringVariant.tests.cpp b/projects/SelfTest/UsageTests/ToStringVariant.tests.cpp index 62f923f0..86475bd8 100644 --- a/projects/SelfTest/UsageTests/ToStringVariant.tests.cpp +++ b/projects/SelfTest/UsageTests/ToStringVariant.tests.cpp @@ -6,6 +6,18 @@ #include <string> #include <variant> +// We need 2 types with non-trivial copies/moves +struct MyType1 { + MyType1() = default; + MyType1(MyType1 const&) { throw 1; } + MyType1& operator=(MyType1 const&) { throw 3; } +}; +struct MyType2 { + MyType2() = default; + MyType2(MyType2 const&) { throw 2; } + MyType2& operator=(MyType2 const&) { throw 4; } +}; + TEST_CASE( "variant<std::monostate>", "[toString][variant][approvals]") { using type = std::variant<std::monostate>; @@ -26,19 +38,18 @@ TEST_CASE( "variant<float, int>", "[toString][variant][approvals]") using type = std::variant<float, int>; CHECK( "0.5f" == ::Catch::Detail::stringify(type{0.5f}) ); CHECK( "0" == ::Catch::Detail::stringify(type{0}) ); +} - SECTION("valueless by exception") { - struct sample { - operator int() const { throw 42; } - }; +TEST_CASE( "variant -- valueless-by-exception", "[toString][variant][approvals]" ) { + using type = std::variant<MyType1, MyType2>; - type value{1.5f}; - REQUIRE_THROWS_AS( value.emplace<int>(sample{}), int ); - REQUIRE( value.valueless_by_exception() ); - CHECK( "{valueless variant}" == ::Catch::Detail::stringify(value) ); - } + type value; + REQUIRE_THROWS_AS(value.emplace<MyType2>(MyType2{}), int); + REQUIRE(value.valueless_by_exception()); + CHECK("{valueless variant}" == ::Catch::Detail::stringify(value)); } + TEST_CASE( "variant<string, int>", "[toString][variant][approvals]") { using type = std::variant<std::string, int>; @@ -48,7 +59,7 @@ TEST_CASE( "variant<string, int>", "[toString][variant][approvals]") TEST_CASE( "variant<variant<float, int>, string>", "[toString][variant][approvals]") { - using inner = std::variant<float, int>; + using inner = std::variant<MyType1, float, int>; using type = std::variant<inner, std::string>; CHECK( "0.5f" == ::Catch::Detail::stringify(type{0.5f}) ); CHECK( "0" == ::Catch::Detail::stringify(type{0}) ); @@ -63,7 +74,7 @@ TEST_CASE( "variant<variant<float, int>, string>", "[toString][variant][approval REQUIRE( std::holds_alternative<inner>(value) ); REQUIRE( std::holds_alternative<float>(std::get<inner>(value)) ); - REQUIRE_THROWS_AS( std::get<0>(value).emplace<int>(sample{}), int ); + REQUIRE_THROWS_AS( std::get<0>(value).emplace<MyType1>(MyType1{}), int ); // outer variant is still valid and contains inner REQUIRE( std::holds_alternative<inner>(value) ); |