| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#include <catch2/benchmark/catch_benchmark.hpp> |
| 10 |
#include <catch2/catch_template_test_macros.hpp> |
| 11 |
#include <catch2/catch_test_macros.hpp> |
| 12 |
#include <catch2/generators/catch_generators.hpp> |
| 13 |
#include <catch2/internal/catch_enum_values_registry.hpp> |
| 14 |
#include <catch2/matchers/catch_matchers_string.hpp> |
| 15 |
#include <catch2/matchers/catch_matchers_vector.hpp> |
| 16 |
|
| 17 |
#include <chrono> |
| 18 |
|
| 19 |
enum class EnumClass3 { Value1, Value2, Value3, Value4 }; |
| 20 |
|
| 21 |
struct UsesSentinel { |
| 22 |
using const_iterator = int const*; |
| 23 |
using const_sentinel = std::nullptr_t; |
| 24 |
|
| 25 |
const_iterator begin() const { return nullptr; } |
| 26 |
const_iterator end() const { return nullptr; } |
| 27 |
}; |
| 28 |
|
| 29 |
TEST_CASE( "parseEnums", "[Strings][enums]" ) { |
| 30 |
using namespace Catch::Matchers; |
| 31 |
using Catch::Detail::parseEnums; |
| 32 |
|
| 33 |
SECTION( "No enums" ) |
| 34 |
CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) ); |
| 35 |
|
| 36 |
SECTION( "One enum value" ) { |
| 37 |
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ), |
| 38 |
Equals(std::vector<Catch::StringRef>{"Value1"} ) ); |
| 39 |
CHECK_THAT( parseEnums( "Value1" ), |
| 40 |
Equals( std::vector<Catch::StringRef>{"Value1"} ) ); |
| 41 |
CHECK_THAT( parseEnums( "EnumName::Value1" ), |
| 42 |
Equals(std::vector<Catch::StringRef>{"Value1"} ) ); |
| 43 |
} |
| 44 |
|
| 45 |
SECTION( "Multiple enum values" ) { |
| 46 |
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), |
| 47 |
Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) ); |
| 48 |
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), |
| 49 |
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) ); |
| 50 |
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), |
| 51 |
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
TEST_CASE( "Directly creating an EnumInfo" ) { |
| 56 |
|
| 57 |
using namespace Catch::Detail; |
| 58 |
auto enumInfo = makeEnumInfo( "EnumName", "EnumName::Value1, EnumName::Value2", {0, 1} ); |
| 59 |
|
| 60 |
CHECK( enumInfo->lookup(0) == "Value1" ); |
| 61 |
CHECK( enumInfo->lookup(1) == "Value2" ); |
| 62 |
CHECK( enumInfo->lookup(3) == "{** unexpected enum value **}" ); |
| 63 |
} |
| 64 |
|
| 65 |
TEST_CASE("Range type with sentinel") { |
| 66 |
CHECK( Catch::Detail::stringify(UsesSentinel{}) == "{ }" ); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("convertIntoString stringification helper", "[toString][approvals]") { |
| 70 |
using namespace std::string_literals; |
| 71 |
using Catch::Detail::convertIntoString; |
| 72 |
using namespace Catch; |
| 73 |
|
| 74 |
SECTION("No escaping") { |
| 75 |
CHECK(convertIntoString(""_sr, false) == R"("")"s); |
| 76 |
CHECK(convertIntoString("abcd"_sr, false) == R"("abcd")"s); |
| 77 |
CHECK(convertIntoString("ab\ncd"_sr, false) == "\"ab\ncd\""s); |
| 78 |
CHECK(convertIntoString("ab\r\ncd"_sr, false) == "\"ab\r\ncd\""s); |
| 79 |
CHECK(convertIntoString("ab\"cd"_sr, false) == R"("ab"cd")"s); |
| 80 |
} |
| 81 |
SECTION("Escaping invisibles") { |
| 82 |
CHECK(convertIntoString(""_sr, true) == R"("")"s); |
| 83 |
CHECK(convertIntoString("ab\ncd"_sr, true) == R"("ab\ncd")"s); |
| 84 |
CHECK(convertIntoString("ab\r\ncd"_sr, true) == R"("ab\r\ncd")"s); |
| 85 |
CHECK(convertIntoString("ab\tcd"_sr, true) == R"("ab\tcd")"s); |
| 86 |
CHECK(convertIntoString("ab\fcd"_sr, true) == R"("ab\fcd")"s); |
| 87 |
CHECK(convertIntoString("ab\"cd"_sr, true) == R"("ab"cd")"s); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
TEMPLATE_TEST_CASE( "Stringifying char arrays with statically known sizes", |
| 92 |
"[toString]", |
| 93 |
char, |
| 94 |
signed char, |
| 95 |
unsigned char ) { |
| 96 |
using namespace std::string_literals; |
| 97 |
TestType with_null_terminator[10] = "abc"; |
| 98 |
CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s ); |
| 99 |
|
| 100 |
TestType no_null_terminator[3] = { 'a', 'b', 'c' }; |
| 101 |
CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s ); |
| 102 |
} |
| 103 |
|
| 104 |
TEST_CASE( "#2944 - Stringifying dates before 1970 should not crash", "[.approvals]" ) { |
| 105 |
using Catch::Matchers::Equals; |
| 106 |
using Days = std::chrono::duration<int32_t, std::ratio<86400>>; |
| 107 |
using SysDays = std::chrono::time_point<std::chrono::system_clock, Days>; |
| 108 |
using SM = Catch::StringMaker<std::chrono::system_clock::time_point>; |
| 109 |
|
| 110 |
// Check simple date first |
| 111 |
const SysDays post1970{ Days{ 1 } }; |
| 112 |
auto converted_post = SM::convert( post1970 ); |
| 113 |
REQUIRE( converted_post == "1970-01-02T00:00:00Z" ); |
| 114 |
|
| 115 |
const SysDays pre1970{ Days{ -1 } }; |
| 116 |
auto converted_pre = SM::convert( pre1970 ); |
| 117 |
REQUIRE_THAT( |
| 118 |
converted_pre, |
| 119 |
Equals( "1969-12-31T00:00:00Z" ) || |
| 120 |
Equals( "gmtime from provided timepoint has failed. This " |
| 121 |
"happens e.g. with pre-1970 dates using Microsoft libc" ) ); |
| 122 |
} |
| 123 |
|
| 124 |
namespace { |
| 125 |
struct ThrowsOnStringification { |
| 126 |
friend bool operator==( ThrowsOnStringification, |
| 127 |
ThrowsOnStringification ) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
}; |
| 131 |
} |
| 132 |
|
| 133 |
template <> |
| 134 |
struct Catch::StringMaker<ThrowsOnStringification> { |
| 135 |
static std::string convert(ThrowsOnStringification) { |
| 136 |
throw std::runtime_error( "Invalid" ); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
TEST_CASE( "Exception thrown inside stringify does not fail the test", "[toString]" ) { |
| 141 |
ThrowsOnStringification tos; |
| 142 |
CHECK( tos == tos ); |
| 143 |
} |
| 144 |
|
| 145 |
TEST_CASE( "string escaping benchmark", "[toString][!benchmark]" ) { |
| 146 |
const auto input_length = GENERATE( as<size_t>{}, 10, 100, 10'000, 100'000 ); |
| 147 |
std::string test_input( input_length, 'a' ); |
| 148 |
BENCHMARK( "no-escape string, no-escaping, len=" + |
| 149 |
std::to_string( input_length ) ) { |
| 150 |
return Catch::Detail::convertIntoString( test_input, false ); |
| 151 |
}; |
| 152 |
BENCHMARK( "no-escape string, escaping, len=" + |
| 153 |
std::to_string( input_length ) ) { |
| 154 |
return Catch::Detail::convertIntoString( test_input, true ); |
| 155 |
}; |
| 156 |
|
| 157 |
std::string escape_input( input_length, '\r' ); |
| 158 |
BENCHMARK( "full escape string, no-escaping, len=" + |
| 159 |
std::to_string( input_length ) ) { |
| 160 |
return Catch::Detail::convertIntoString( escape_input, false ); |
| 161 |
}; |
| 162 |
BENCHMARK( "full escape string, escaping, len=" + |
| 163 |
std::to_string( input_length ) ) { |
| 164 |
return Catch::Detail::convertIntoString( escape_input, true ); |
| 165 |
}; |
| 166 |
} |