From 7117768feb2b2acb8d2498229cbaa4afbad377f1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Mar 2024 00:20:38 +0100 Subject: Text operations that return new Text are now const --- FoundationEx/include/Text.h | 6 +++--- FoundationEx/src/Text.cpp | 6 +++--- FoundationEx/test/Text.cpp | 22 ++++++++++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/FoundationEx/include/Text.h b/FoundationEx/include/Text.h index 851cadf..c40c636 100644 --- a/FoundationEx/include/Text.h +++ b/FoundationEx/include/Text.h @@ -124,9 +124,9 @@ public: void toUpper(); // substring - Text substring(int start, int length); - Text trim(); - Text replace(const char* pattern, const char* substitution); + Text substring(int start, int length) const; + Text trim() const; + Text replace(const char* pattern, const char* substitution) const; Text concat(const char* tail) const; static Text format(const char* fmt, ...); diff --git a/FoundationEx/src/Text.cpp b/FoundationEx/src/Text.cpp index be03ce3..cba7720 100644 --- a/FoundationEx/src/Text.cpp +++ b/FoundationEx/src/Text.cpp @@ -598,7 +598,7 @@ Text::toUpper() } Text -Text::substring(int start, int length) +Text::substring(int start, int length) const { Text result; @@ -620,7 +620,7 @@ Text::substring(int start, int length) } Text -Text::trim() +Text::trim() const { Text result; @@ -638,7 +638,7 @@ Text::trim() } Text -Text::replace(const char* pattern, const char* substitution) +Text::replace(const char* pattern, const char* substitution) const { Text result; diff --git a/FoundationEx/test/Text.cpp b/FoundationEx/test/Text.cpp index 7400eb1..deaf742 100644 --- a/FoundationEx/test/Text.cpp +++ b/FoundationEx/test/Text.cpp @@ -31,7 +31,7 @@ TEST(FoundationEx, TextCanBeCopied) TEST(FoundationEx, ConcatenateTextWithLiteralWithoutSideEffects) { - const Text a {"Hello"}; + Text a {"Hello"}; const auto b = a + ", there"; ASSERT_EQ("Hello", a); ASSERT_EQ("Hello, there", b); @@ -41,7 +41,25 @@ TEST(FoundationEx, ConcatenateTextWithLiteralWithoutSideEffects) TEST(FoundationEx, ReplaceInTextWithoutSideEffects) { Text a {"Hello, all"}; - const auto b = a.replace("Hello", "Goodbye"); // Test::replace should be const + const auto b = a.replace("Hello", "Goodbye"); ASSERT_EQ("Hello, all", a); ASSERT_EQ("Goodbye, all", b); } + + +TEST(FoundationEx, SubstringFromTextWithoutSideEffects) +{ + Text a {"Hello, all"}; + const auto b = a.substring(7, 3); + ASSERT_EQ("Hello, all", a); + ASSERT_EQ("all", b); +} + + +TEST(FoundationEx, TrimTextWithoutSideEffects) +{ + Text a {" Hi! "}; + const auto b = a.trim(); + ASSERT_EQ(" Hi! ", a); + ASSERT_EQ("Hi!", b); +} -- cgit v1.1