aboutsummaryrefslogtreecommitdiff
path: root/projects/SelfTest/EnumToString.cpp
blob: 6917d8ae8a07c0f8e72e910e01446f2d07086f0d (plain)
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
#include "catch.hpp"

/*
  TODO: maybe ought to check that user-provided specialisations of
 Catch::toString also do the right thing
*/

// Enum without user-provided stream operator
enum Enum1 { Enum1Value0, Enum1Value1 };

TEST_CASE( "toString(enum)", "[toString][enum]" ) {
    Enum1 e0 = Enum1Value0;
    CHECK( Catch::toString(e0) == "0" );
    Enum1 e1 = Enum1Value1;
    CHECK( Catch::toString(e1) == "1" );
}

// Enum with user-provided stream operator
enum Enum2 { Enum2Value0, Enum2Value1 };

inline std::ostream& operator<<( std::ostream& os, Enum2 v ) {
    return os << "E2{" << static_cast<int>(v) << "}";
}

TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) {
    Enum2 e0 = Enum2Value0;
    CHECK( Catch::toString(e0) == "E2{0}" );
    Enum2 e1 = Enum2Value1;
    CHECK( Catch::toString(e1) == "E2{1}" );
}

#if defined(CATCH_CPP11_OR_GREATER)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif

// Enum class without user-provided stream operator
enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 };

TEST_CASE( "toString(enum class)", "[toString][enum][enumClass]" ) {
    EnumClass1 e0 = EnumClass1::EnumClass1Value0;
    CHECK( Catch::toString(e0) == "0" );
    EnumClass1 e1 = EnumClass1::EnumClass1Value1;
    CHECK( Catch::toString(e1) == "1" );
}

// Enum class with user-provided stream operator
enum class EnumClass2 : short { EnumClass2Value0, EnumClass2Value1 };

inline std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) {
    switch( static_cast<int>( e2 ) ) {
        case static_cast<int>( EnumClass2::EnumClass2Value0 ):
            return os << "E2/V0";
        case static_cast<int>( EnumClass2::EnumClass2Value1 ):
            return os << "E2/V1";
        default:
            return os << "Unknown enum value " << static_cast<int>( e2 );
    }
}

TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass]" ) {
    EnumClass2 e0 = EnumClass2::EnumClass2Value0;
    CHECK( Catch::toString(e0) == "E2/V0" );
    EnumClass2 e1 = EnumClass2::EnumClass2Value1;
    CHECK( Catch::toString(e1) == "E2/V1" );

    EnumClass2 e3 = static_cast<EnumClass2>(10);
    CHECK( Catch::toString(e3) == "Unknown enum value 10" );
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // CATCH_CPP11_OR_GREATER