2022-01-29 00:03:43 +01:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
2022-10-28 11:22:53 +02:00
|
|
|
// (See accompanying file LICENSE.txt or copy at
|
2022-01-29 00:03:43 +01:00
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
|
2018-09-20 14:13:35 +02:00
|
|
|
#define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
|
2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2018-09-20 14:13:35 +02:00
|
|
|
|
|
|
|
#if defined(CATCH_CONFIG_CPP17_VARIANT)
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <variant>
|
|
|
|
|
2019-01-26 18:32:17 +01:00
|
|
|
// We need 2 types with non-trivial copies/moves
|
|
|
|
struct MyType1 {
|
|
|
|
MyType1() = default;
|
2019-10-29 23:38:21 +01:00
|
|
|
[[noreturn]] MyType1(MyType1 const&) { throw 1; }
|
2019-01-26 18:32:17 +01:00
|
|
|
MyType1& operator=(MyType1 const&) { throw 3; }
|
|
|
|
};
|
|
|
|
struct MyType2 {
|
|
|
|
MyType2() = default;
|
2019-10-29 23:38:21 +01:00
|
|
|
[[noreturn]] MyType2(MyType2 const&) { throw 2; }
|
2019-01-26 18:32:17 +01:00
|
|
|
MyType2& operator=(MyType2 const&) { throw 4; }
|
|
|
|
};
|
|
|
|
|
2018-09-20 14:13:35 +02:00
|
|
|
TEST_CASE( "variant<std::monostate>", "[toString][variant][approvals]")
|
|
|
|
{
|
|
|
|
using type = std::variant<std::monostate>;
|
|
|
|
CHECK( "{ }" == ::Catch::Detail::stringify(type{}) );
|
|
|
|
type value {};
|
|
|
|
CHECK( "{ }" == ::Catch::Detail::stringify(value) );
|
|
|
|
CHECK( "{ }" == ::Catch::Detail::stringify(std::get<0>(value)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "variant<int>", "[toString][variant][approvals]")
|
|
|
|
{
|
|
|
|
using type = std::variant<int>;
|
|
|
|
CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
|
|
|
|
}
|
|
|
|
|
|
|
|
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}) );
|
2019-01-26 18:32:17 +01:00
|
|
|
}
|
2018-09-20 14:13:35 +02:00
|
|
|
|
2019-01-26 18:32:17 +01:00
|
|
|
TEST_CASE( "variant -- valueless-by-exception", "[toString][variant][approvals]" ) {
|
|
|
|
using type = std::variant<MyType1, MyType2>;
|
2018-09-20 14:13:35 +02:00
|
|
|
|
2019-01-26 18:32:17 +01:00
|
|
|
type value;
|
|
|
|
REQUIRE_THROWS_AS(value.emplace<MyType2>(MyType2{}), int);
|
|
|
|
REQUIRE(value.valueless_by_exception());
|
|
|
|
CHECK("{valueless variant}" == ::Catch::Detail::stringify(value));
|
2018-09-20 14:13:35 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 18:32:17 +01:00
|
|
|
|
2018-09-20 14:13:35 +02:00
|
|
|
TEST_CASE( "variant<string, int>", "[toString][variant][approvals]")
|
|
|
|
{
|
|
|
|
using type = std::variant<std::string, int>;
|
|
|
|
CHECK( "\"foo\"" == ::Catch::Detail::stringify(type{"foo"}) );
|
|
|
|
CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "variant<variant<float, int>, string>", "[toString][variant][approvals]")
|
|
|
|
{
|
2019-01-26 18:32:17 +01:00
|
|
|
using inner = std::variant<MyType1, float, int>;
|
2018-09-20 14:13:35 +02:00
|
|
|
using type = std::variant<inner, std::string>;
|
|
|
|
CHECK( "0.5f" == ::Catch::Detail::stringify(type{0.5f}) );
|
|
|
|
CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
|
|
|
|
CHECK( "\"foo\"" == ::Catch::Detail::stringify(type{"foo"}) );
|
|
|
|
|
|
|
|
SECTION("valueless nested variant") {
|
|
|
|
type value = inner{0.5f};
|
|
|
|
REQUIRE( std::holds_alternative<inner>(value) );
|
|
|
|
REQUIRE( std::holds_alternative<float>(std::get<inner>(value)) );
|
|
|
|
|
2019-01-26 18:32:17 +01:00
|
|
|
REQUIRE_THROWS_AS( std::get<0>(value).emplace<MyType1>(MyType1{}), int );
|
2018-09-20 14:13:35 +02:00
|
|
|
|
|
|
|
// outer variant is still valid and contains inner
|
|
|
|
REQUIRE( std::holds_alternative<inner>(value) );
|
|
|
|
// inner variant is valueless
|
|
|
|
REQUIRE( std::get<inner>(value).valueless_by_exception() );
|
|
|
|
CHECK( "{valueless variant}" == ::Catch::Detail::stringify(value) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "variant<nullptr,int,const char *>", "[toString][variant][approvals]" )
|
|
|
|
{
|
|
|
|
using type = std::variant<std::nullptr_t,int,const char *>;
|
|
|
|
CHECK( "nullptr" == ::Catch::Detail::stringify(type{nullptr}) );
|
|
|
|
CHECK( "42" == ::Catch::Detail::stringify(type{42}) );
|
|
|
|
CHECK( "\"Catch me\"" == ::Catch::Detail::stringify(type{"Catch me"}) );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CATCH_INTERNAL_CONFIG_CPP17_VARIANT
|