mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 21:05:39 +02:00
Cleanup and optimize stringifying of string-like types
More specifically, made the actual implementation of string-like type handling take argument as `Catch::StringRef`, instead of taking `std::string const&`. This means that string-like types that are not `std::string` no longer need to pay for an extra construction of `std::string` (including the potential allocation), before they can be stringified. The actual string stringification routine is now also better about reserving sufficient space.
This commit is contained in:
@@ -205,6 +205,9 @@ Message from section two
|
||||
:test-result: PASS String matchers
|
||||
:test-result: PASS StringRef
|
||||
:test-result: PASS StringRef at compilation time
|
||||
:test-result: PASS Stringifying char arrays with statically known sizes - char
|
||||
:test-result: PASS Stringifying char arrays with statically known sizes - signed char
|
||||
:test-result: PASS Stringifying char arrays with statically known sizes - unsigned char
|
||||
:test-result: PASS Stringifying std::chrono::duration helpers
|
||||
:test-result: PASS Stringifying std::chrono::duration with weird ratios
|
||||
:test-result: PASS Stringifying std::chrono::time_point<system_clock>
|
||||
|
@@ -1532,6 +1532,12 @@ String.tests.cpp:<line number>: passed: with 1 message: '!(sr1.empty())'
|
||||
String.tests.cpp:<line number>: passed: with 1 message: 'sr1.size() == 3'
|
||||
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.empty()'
|
||||
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.size() == 0'
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
ToStringChrono.tests.cpp:<line number>: passed: minute == seconds for: 1 m == 60 s
|
||||
ToStringChrono.tests.cpp:<line number>: passed: hour != seconds for: 1 h != 60 s
|
||||
ToStringChrono.tests.cpp:<line number>: passed: micro != milli for: 1 us != 1 ms
|
||||
|
@@ -1386,6 +1386,6 @@ due to unexpected exception with message:
|
||||
Why would you throw a std::string?
|
||||
|
||||
===============================================================================
|
||||
test cases: 360 | 284 passed | 70 failed | 6 failed as expected
|
||||
assertions: 2084 | 1932 passed | 129 failed | 23 failed as expected
|
||||
test cases: 363 | 287 passed | 70 failed | 6 failed as expected
|
||||
assertions: 2090 | 1938 passed | 129 failed | 23 failed as expected
|
||||
|
||||
|
@@ -10865,6 +10865,54 @@ String.tests.cpp:<line number>: PASSED:
|
||||
with message:
|
||||
sr2.size() == 0
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Stringifying char arrays with statically known sizes - char
|
||||
-------------------------------------------------------------------------------
|
||||
ToString.tests.cpp:<line number>
|
||||
...............................................................................
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Stringifying char arrays with statically known sizes - signed char
|
||||
-------------------------------------------------------------------------------
|
||||
ToString.tests.cpp:<line number>
|
||||
...............................................................................
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Stringifying char arrays with statically known sizes - unsigned char
|
||||
-------------------------------------------------------------------------------
|
||||
ToString.tests.cpp:<line number>
|
||||
...............................................................................
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
ToString.tests.cpp:<line number>: PASSED:
|
||||
CHECK( ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s )
|
||||
with expansion:
|
||||
""abc"" == ""abc""
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Stringifying std::chrono::duration helpers
|
||||
-------------------------------------------------------------------------------
|
||||
@@ -16782,6 +16830,6 @@ Misc.tests.cpp:<line number>
|
||||
Misc.tests.cpp:<line number>: PASSED:
|
||||
|
||||
===============================================================================
|
||||
test cases: 360 | 268 passed | 86 failed | 6 failed as expected
|
||||
assertions: 2101 | 1932 passed | 146 failed | 23 failed as expected
|
||||
test cases: 363 | 271 passed | 86 failed | 6 failed as expected
|
||||
assertions: 2107 | 1938 passed | 146 failed | 23 failed as expected
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuitesloose text artifact
|
||||
>
|
||||
<testsuite name="<exe-name>" errors="17" failures="130" tests="2102" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
|
||||
<testsuite name="<exe-name>" errors="17" failures="130" tests="2108" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
|
||||
<properties>
|
||||
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
|
||||
<property name="random-seed" value="1"/>
|
||||
@@ -1194,6 +1194,9 @@ Matchers.tests.cpp:<line number>
|
||||
<testcase classname="<exe-name>.global" name="StringRef/StringRef + StringRef" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - char" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - signed char" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - unsigned char" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration with weird ratios" time="{duration}" status="run"/>
|
||||
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::time_point<system_clock>" time="{duration}" status="run"/>
|
||||
|
@@ -222,6 +222,9 @@
|
||||
<file path="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp">
|
||||
<testCase name="Directly creating an EnumInfo" duration="{duration}"/>
|
||||
<testCase name="Range type with sentinel" duration="{duration}"/>
|
||||
<testCase name="Stringifying char arrays with statically known sizes - char" duration="{duration}"/>
|
||||
<testCase name="Stringifying char arrays with statically known sizes - signed char" duration="{duration}"/>
|
||||
<testCase name="Stringifying char arrays with statically known sizes - unsigned char" duration="{duration}"/>
|
||||
<testCase name="parseEnums/No enums" duration="{duration}"/>
|
||||
<testCase name="parseEnums/One enum value" duration="{duration}"/>
|
||||
<testCase name="parseEnums/Multiple enum values" duration="{duration}"/>
|
||||
|
@@ -2743,6 +2743,18 @@ ok {test-number} - with 1 message: 'sr1.size() == 3'
|
||||
ok {test-number} - with 1 message: 'sr2.empty()'
|
||||
# StringRef at compilation time
|
||||
ok {test-number} - with 1 message: 'sr2.size() == 0'
|
||||
# Stringifying char arrays with statically known sizes - char
|
||||
ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying char arrays with statically known sizes - char
|
||||
ok {test-number} - ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying char arrays with statically known sizes - signed char
|
||||
ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying char arrays with statically known sizes - signed char
|
||||
ok {test-number} - ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying char arrays with statically known sizes - unsigned char
|
||||
ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying char arrays with statically known sizes - unsigned char
|
||||
ok {test-number} - ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
|
||||
# Stringifying std::chrono::duration helpers
|
||||
ok {test-number} - minute == seconds for: 1 m == 60 s
|
||||
# Stringifying std::chrono::duration helpers
|
||||
@@ -4204,5 +4216,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
|
||||
ok {test-number} -
|
||||
# xmlentitycheck
|
||||
ok {test-number} -
|
||||
1..2101
|
||||
1..2107
|
||||
|
||||
|
@@ -510,6 +510,12 @@ Matchers.tests.cpp:<line number>|nexpression failed|n CHECK_THAT( testStringFor
|
||||
##teamcity[testFinished name='StringRef' duration="{duration}"]
|
||||
##teamcity[testStarted name='StringRef at compilation time']
|
||||
##teamcity[testFinished name='StringRef at compilation time' duration="{duration}"]
|
||||
##teamcity[testStarted name='Stringifying char arrays with statically known sizes - char']
|
||||
##teamcity[testFinished name='Stringifying char arrays with statically known sizes - char' duration="{duration}"]
|
||||
##teamcity[testStarted name='Stringifying char arrays with statically known sizes - signed char']
|
||||
##teamcity[testFinished name='Stringifying char arrays with statically known sizes - signed char' duration="{duration}"]
|
||||
##teamcity[testStarted name='Stringifying char arrays with statically known sizes - unsigned char']
|
||||
##teamcity[testFinished name='Stringifying char arrays with statically known sizes - unsigned char' duration="{duration}"]
|
||||
##teamcity[testStarted name='Stringifying std::chrono::duration helpers']
|
||||
##teamcity[testFinished name='Stringifying std::chrono::duration helpers' duration="{duration}"]
|
||||
##teamcity[testStarted name='Stringifying std::chrono::duration with weird ratios']
|
||||
|
@@ -12900,6 +12900,63 @@ Message from section two
|
||||
</Section>
|
||||
<OverallResult success="true"/>
|
||||
</TestCase>
|
||||
<TestCase name="Stringifying char arrays with statically known sizes - char" tags="[toString]" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<OverallResult success="true"/>
|
||||
</TestCase>
|
||||
<TestCase name="Stringifying char arrays with statically known sizes - signed char" tags="[toString]" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<OverallResult success="true"/>
|
||||
</TestCase>
|
||||
<TestCase name="Stringifying char arrays with statically known sizes - unsigned char" tags="[toString]" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
|
||||
<Original>
|
||||
::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s
|
||||
</Original>
|
||||
<Expanded>
|
||||
""abc"" == ""abc""
|
||||
</Expanded>
|
||||
</Expression>
|
||||
<OverallResult success="true"/>
|
||||
</TestCase>
|
||||
<TestCase name="Stringifying std::chrono::duration helpers" tags="[chrono][toString]" filename="tests/<exe-name>/UsageTests/ToStringChrono.tests.cpp" >
|
||||
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/UsageTests/ToStringChrono.tests.cpp" >
|
||||
<Original>
|
||||
@@ -19745,9 +19802,9 @@ loose text artifact
|
||||
</Section>
|
||||
<OverallResult success="true"/>
|
||||
</TestCase>
|
||||
<OverallResults successes="1932" failures="147" expectedFailures="23"/>
|
||||
<OverallResultsCases successes="268" failures="86" expectedFailures="6"/>
|
||||
<OverallResults successes="1938" failures="147" expectedFailures="23"/>
|
||||
<OverallResultsCases successes="271" failures="86" expectedFailures="6"/>
|
||||
</Group>
|
||||
<OverallResults successes="1932" failures="146" expectedFailures="23"/>
|
||||
<OverallResultsCases successes="268" failures="86" expectedFailures="6"/>
|
||||
<OverallResults successes="1938" failures="146" expectedFailures="23"/>
|
||||
<OverallResultsCases successes="271" failures="86" expectedFailures="6"/>
|
||||
</Catch>
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#include <catch2/internal/catch_enum_values_registry.hpp>
|
||||
#include <catch2/matchers/catch_matchers_vector.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_template_test_macros.hpp>
|
||||
|
||||
enum class EnumClass3 { Value1, Value2, Value3, Value4 };
|
||||
|
||||
@@ -50,4 +51,39 @@ TEST_CASE( "Directly creating an EnumInfo" ) {
|
||||
|
||||
TEST_CASE("Range type with sentinel") {
|
||||
CHECK( Catch::Detail::stringify(UsesSentinel{}) == "{ }" );
|
||||
}
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
|
Reference in New Issue
Block a user