2022-01-29 00:03:43 +01:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
|
2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/internal/catch_enum_values_registry.hpp>
|
2014-09-01 18:24:10 +02:00
|
|
|
|
|
|
|
|
2018-07-02 11:13:07 +02:00
|
|
|
namespace {
|
2014-09-01 18:24:10 +02:00
|
|
|
// Enum without user-provided stream operator
|
|
|
|
enum Enum1 { Enum1Value0, Enum1Value1 };
|
|
|
|
|
|
|
|
// Enum with user-provided stream operator
|
|
|
|
enum Enum2 { Enum2Value0, Enum2Value1 };
|
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::ostream& operator<<( std::ostream& os, Enum2 v ) {
|
2014-09-01 18:24:10 +02:00
|
|
|
return os << "E2{" << static_cast<int>(v) << "}";
|
|
|
|
}
|
2018-07-02 11:13:07 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
TEST_CASE( "toString(enum)", "[toString][enum]" ) {
|
|
|
|
Enum1 e0 = Enum1Value0;
|
|
|
|
CHECK( ::Catch::Detail::stringify(e0) == "0" );
|
|
|
|
Enum1 e1 = Enum1Value1;
|
|
|
|
CHECK( ::Catch::Detail::stringify(e1) == "1" );
|
|
|
|
}
|
2014-09-01 18:24:10 +02:00
|
|
|
|
2014-09-04 08:27:09 +02:00
|
|
|
TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) {
|
2014-09-01 18:24:10 +02:00
|
|
|
Enum2 e0 = Enum2Value0;
|
2017-05-02 23:51:03 +02:00
|
|
|
CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" );
|
2014-09-01 18:24:10 +02:00
|
|
|
Enum2 e1 = Enum2Value1;
|
2017-05-02 23:51:03 +02:00
|
|
|
CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" );
|
2014-09-01 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enum class without user-provided stream operator
|
2018-07-02 11:13:07 +02:00
|
|
|
namespace {
|
2014-09-01 18:24:10 +02:00
|
|
|
enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 };
|
|
|
|
|
|
|
|
// Enum class with user-provided stream operator
|
2018-07-02 11:13:07 +02:00
|
|
|
enum class EnumClass2 { EnumClass2Value0, EnumClass2Value1 };
|
2014-09-01 18:24:10 +02:00
|
|
|
|
2017-05-02 23:51:03 +02:00
|
|
|
std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) {
|
2014-10-03 09:17:40 +02:00
|
|
|
switch( static_cast<int>( e2 ) ) {
|
|
|
|
case static_cast<int>( EnumClass2::EnumClass2Value0 ):
|
2014-09-04 08:27:09 +02:00
|
|
|
return os << "E2/V0";
|
2014-10-03 09:17:40 +02:00
|
|
|
case static_cast<int>( EnumClass2::EnumClass2Value1 ):
|
2014-09-04 08:27:09 +02:00
|
|
|
return os << "E2/V1";
|
|
|
|
default:
|
2014-10-03 09:17:40 +02:00
|
|
|
return os << "Unknown enum value " << static_cast<int>( e2 );
|
2014-09-01 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 11:13:07 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
TEST_CASE( "toString(enum class)", "[toString][enum][enumClass]" ) {
|
|
|
|
EnumClass1 e0 = EnumClass1::EnumClass1Value0;
|
|
|
|
CHECK( ::Catch::Detail::stringify(e0) == "0" );
|
|
|
|
EnumClass1 e1 = EnumClass1::EnumClass1Value1;
|
|
|
|
CHECK( ::Catch::Detail::stringify(e1) == "1" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-13 09:52:51 +02:00
|
|
|
TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass]" ) {
|
2014-09-01 18:24:10 +02:00
|
|
|
EnumClass2 e0 = EnumClass2::EnumClass2Value0;
|
2017-05-02 23:51:03 +02:00
|
|
|
CHECK( ::Catch::Detail::stringify(e0) == "E2/V0" );
|
2014-09-01 18:24:10 +02:00
|
|
|
EnumClass2 e1 = EnumClass2::EnumClass2Value1;
|
2017-05-02 23:51:03 +02:00
|
|
|
CHECK( ::Catch::Detail::stringify(e1) == "E2/V1" );
|
2014-09-04 08:27:09 +02:00
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
auto e3 = static_cast<EnumClass2>(10);
|
2017-05-02 23:51:03 +02:00
|
|
|
CHECK( ::Catch::Detail::stringify(e3) == "Unknown enum value 10" );
|
2014-09-01 18:24:10 +02:00
|
|
|
}
|
2019-04-04 16:55:46 +02:00
|
|
|
|
|
|
|
enum class EnumClass3 { Value1, Value2, Value3, Value4 };
|
|
|
|
|
2019-04-21 19:26:46 +02:00
|
|
|
CATCH_REGISTER_ENUM( EnumClass3, EnumClass3::Value1, EnumClass3::Value2, EnumClass3::Value3 )
|
2019-04-04 16:55:46 +02:00
|
|
|
|
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
TEST_CASE( "Enums can quickly have stringification enabled using REGISTER_ENUM" ) {
|
2019-04-04 16:55:46 +02:00
|
|
|
using Catch::Detail::stringify;
|
|
|
|
REQUIRE( stringify( EnumClass3::Value1 ) == "Value1" );
|
|
|
|
REQUIRE( stringify( EnumClass3::Value2 ) == "Value2" );
|
|
|
|
REQUIRE( stringify( EnumClass3::Value3 ) == "Value3" );
|
2019-04-21 18:56:19 +02:00
|
|
|
REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected enum value **}" );
|
2019-04-04 16:55:46 +02:00
|
|
|
|
2022-02-21 15:13:00 +01:00
|
|
|
EnumClass3 ec3 = EnumClass3::Value2;
|
2019-04-04 16:55:46 +02:00
|
|
|
REQUIRE( stringify( ec3 ) == "Value2" );
|
|
|
|
}
|
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
namespace Bikeshed {
|
|
|
|
enum class Colours { Red, Green, Blue };
|
2019-04-04 16:55:46 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
// Important!: This macro must appear at top level scope - not inside a namespace
|
|
|
|
// You can fully qualify the names, or use a using if you prefer
|
|
|
|
CATCH_REGISTER_ENUM( Bikeshed::Colours,
|
|
|
|
Bikeshed::Colours::Red,
|
|
|
|
Bikeshed::Colours::Green,
|
2019-04-27 19:50:05 +02:00
|
|
|
Bikeshed::Colours::Blue )
|
2019-04-04 16:55:46 +02:00
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
TEST_CASE( "Enums in namespaces can quickly have stringification enabled using REGISTER_ENUM" ) {
|
|
|
|
using Catch::Detail::stringify;
|
|
|
|
REQUIRE( stringify( Bikeshed::Colours::Red ) == "Red" );
|
|
|
|
REQUIRE( stringify( Bikeshed::Colours::Blue ) == "Blue" );
|
2019-04-04 16:55:46 +02:00
|
|
|
}
|