Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/SelfTest/IntrospectiveTests
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-05-18 00:02:46 +0200
committerMartin Hořeňovský2021-05-18 23:15:22 +0200
commit9137e591fa7d7fee2e8c1d3bfeb55068deb2c665 (patch)
tree983f925118ed17cffc9cc35d553bf9dae2814a11 /tests/SelfTest/IntrospectiveTests
parentf50a06affa77d4dbe0c94f684b3a6dfae04c2ebe (diff)
downloadcatch3-9137e591fa7d7fee2e8c1d3bfeb55068deb2c665.tar.gz
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.
Diffstat (limited to 'tests/SelfTest/IntrospectiveTests')
-rw-r--r--tests/SelfTest/IntrospectiveTests/ToString.tests.cpp38
1 files changed, 37 insertions, 1 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp b/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp
index 47dfdd37..c0415dc3 100644
--- a/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp
+++ b/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp
@@ -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{}) == "{ }" );
-} \ No newline at end of file
+}
+
+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 );
+}