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
|
|
|
|
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/internal/catch_enum_values_registry.hpp>
|
2020-03-01 19:59:18 +01:00
|
|
|
#include <catch2/matchers/catch_matchers_vector.hpp>
|
2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2021-05-18 00:02:46 +02:00
|
|
|
#include <catch2/catch_template_test_macros.hpp>
|
2019-04-25 11:13:11 +02:00
|
|
|
|
|
|
|
enum class EnumClass3 { Value1, Value2, Value3, Value4 };
|
|
|
|
|
2020-08-18 10:34:47 +02:00
|
|
|
struct UsesSentinel {
|
|
|
|
using const_iterator = int const*;
|
|
|
|
using const_sentinel = std::nullptr_t;
|
|
|
|
|
|
|
|
const_iterator begin() const { return nullptr; }
|
|
|
|
const_iterator end() const { return nullptr; }
|
|
|
|
};
|
2019-04-25 11:13:11 +02:00
|
|
|
|
|
|
|
TEST_CASE( "parseEnums", "[Strings][enums]" ) {
|
|
|
|
using namespace Catch::Matchers;
|
|
|
|
using Catch::Detail::parseEnums;
|
|
|
|
|
|
|
|
SECTION( "No enums" )
|
2019-09-08 14:07:18 +02:00
|
|
|
CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
|
|
|
|
SECTION( "One enum value" ) {
|
|
|
|
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals(std::vector<Catch::StringRef>{"Value1"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
CHECK_THAT( parseEnums( "Value1" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals( std::vector<Catch::StringRef>{"Value1"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
CHECK_THAT( parseEnums( "EnumName::Value1" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals(std::vector<Catch::StringRef>{"Value1"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION( "Multiple enum values" ) {
|
|
|
|
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ),
|
2019-09-08 14:07:18 +02:00
|
|
|
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
|
2019-04-25 11:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Directly creating an EnumInfo" ) {
|
|
|
|
|
|
|
|
using namespace Catch::Detail;
|
2020-05-25 09:45:24 +02:00
|
|
|
auto enumInfo = makeEnumInfo( "EnumName", "EnumName::Value1, EnumName::Value2", {0, 1} );
|
2019-04-25 11:13:11 +02:00
|
|
|
|
|
|
|
CHECK( enumInfo->lookup(0) == "Value1" );
|
|
|
|
CHECK( enumInfo->lookup(1) == "Value2" );
|
|
|
|
CHECK( enumInfo->lookup(3) == "{** unexpected enum value **}" );
|
|
|
|
}
|
2020-08-18 10:34:47 +02:00
|
|
|
|
|
|
|
TEST_CASE("Range type with sentinel") {
|
|
|
|
CHECK( Catch::Detail::stringify(UsesSentinel{}) == "{ }" );
|
2021-05-18 00:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("convertIntoString stringification helper", "[toString][approvals]") {
|
|
|
|
using namespace std::string_literals;
|
|
|
|
using Catch::Detail::convertIntoString;
|
|
|
|
using namespace Catch;
|
|
|
|
|
|
|
|
SECTION("No escaping") {
|
|
|
|
CHECK(convertIntoString(""_sr, false) == R"("")"s);
|
|
|
|
CHECK(convertIntoString("abcd"_sr, false) == R"("abcd")"s);
|
|
|
|
CHECK(convertIntoString("ab\ncd"_sr, false) == "\"ab\ncd\""s);
|
|
|
|
CHECK(convertIntoString("ab\r\ncd"_sr, false) == "\"ab\r\ncd\""s);
|
|
|
|
CHECK(convertIntoString("ab\"cd"_sr, false) == R"("ab"cd")"s);
|
|
|
|
}
|
|
|
|
SECTION("Escaping invisibles") {
|
|
|
|
CHECK(convertIntoString(""_sr, true) == R"("")"s);
|
|
|
|
CHECK(convertIntoString("ab\ncd"_sr, true) == R"("ab\ncd")"s);
|
|
|
|
CHECK(convertIntoString("ab\r\ncd"_sr, true) == R"("ab\r\ncd")"s);
|
|
|
|
CHECK(convertIntoString("ab\tcd"_sr, true) == R"("ab\tcd")"s);
|
|
|
|
CHECK(convertIntoString("ab\fcd"_sr, true) == R"("ab\fcd")"s);
|
|
|
|
CHECK(convertIntoString("ab\"cd"_sr, true) == R"("ab"cd")"s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEMPLATE_TEST_CASE( "Stringifying char arrays with statically known sizes",
|
|
|
|
"[toString]",
|
|
|
|
char,
|
|
|
|
signed char,
|
|
|
|
unsigned char ) {
|
|
|
|
using namespace std::string_literals;
|
|
|
|
TestType with_null_terminator[10] = "abc";
|
|
|
|
CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s );
|
|
|
|
|
|
|
|
TestType no_null_terminator[3] = { 'a', 'b', 'c' };
|
|
|
|
CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s );
|
|
|
|
}
|