File: ToStringVariant.tests.cpp

package info (click to toggle)
log4cplus 2.0.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,592 kB
  • sloc: cpp: 53,091; sh: 10,537; ansic: 1,845; python: 1,226; perl: 263; makefile: 209; xml: 85; objc: 59
file content (91 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
#include "catch.hpp"

#if defined(CATCH_CONFIG_CPP17_VARIANT)

#include <string>
#include <variant>

// We need 2 types with non-trivial copies/moves
struct MyType1 {
    MyType1() = default;
    [[noreturn]] MyType1(MyType1 const&) { throw 1; }
    MyType1& operator=(MyType1 const&) { throw 3; }
};
struct MyType2 {
    MyType2() = default;
    [[noreturn]] 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>;
    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}) );
}

TEST_CASE( "variant -- valueless-by-exception", "[toString][variant][approvals]" ) {
    using type = std::variant<MyType1, MyType2>;

    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>;
    CHECK( "\"foo\"" == ::Catch::Detail::stringify(type{"foo"}) );
    CHECK( "0" == ::Catch::Detail::stringify(type{0}) );
}

TEST_CASE( "variant<variant<float, int>, string>", "[toString][variant][approvals]")
{
    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}) );
    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)) );

        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) );
        // 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