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:
Martin Hořeňovský
2021-05-18 00:02:46 +02:00
parent f50a06affa
commit 9137e591fa
12 changed files with 251 additions and 39 deletions

View File

@@ -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 );
}