diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-27 14:37:40 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-28 00:13:46 +0000 |
commit | c59268d15b4c04449a649909f75a24202eb9d8d2 (patch) | |
tree | d29bd515f5710f39833c6aaf4f158e9443fa6246 /Tests | |
parent | cccaa94767c7cdd896364116acaca91701c743fe (diff) | |
download | serenity-c59268d15b4c04449a649909f75a24202eb9d8d2.zip |
AK: Add String::trim
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/AK/TestString.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp index 4f5d87dee0..38799ddb61 100644 --- a/Tests/AK/TestString.cpp +++ b/Tests/AK/TestString.cpp @@ -495,3 +495,115 @@ TEST_CASE(join) auto string7 = MUST(String::join(" - "sv, Array { 1, 16, 256, 4096 }, "[{:#04x}]"sv)); EXPECT_EQ(string7, "[0x0001] - [0x0010] - [0x0100] - [0x1000]"sv); } + +TEST_CASE(trim) +{ + { + String string {}; + + auto result = MUST(string.trim(" "sv, TrimMode::Both)); + EXPECT(result.is_empty()); + + result = MUST(string.trim(" "sv, TrimMode::Left)); + EXPECT(result.is_empty()); + + result = MUST(string.trim(" "sv, TrimMode::Right)); + EXPECT(result.is_empty()); + } + { + auto string = MUST(String::from_utf8("word"sv)); + + auto result = MUST(string.trim(" "sv, TrimMode::Both)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Left)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Right)); + EXPECT_EQ(result, "word"sv); + } + { + auto string = MUST(String::from_utf8(" word"sv)); + + auto result = MUST(string.trim(" "sv, TrimMode::Both)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Left)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Right)); + EXPECT_EQ(result, " word"sv); + } + { + auto string = MUST(String::from_utf8("word "sv)); + + auto result = MUST(string.trim(" "sv, TrimMode::Both)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Left)); + EXPECT_EQ(result, "word "sv); + + result = MUST(string.trim(" "sv, TrimMode::Right)); + EXPECT_EQ(result, "word"sv); + } + { + auto string = MUST(String::from_utf8(" word "sv)); + + auto result = MUST(string.trim(" "sv, TrimMode::Both)); + EXPECT_EQ(result, "word"sv); + + result = MUST(string.trim(" "sv, TrimMode::Left)); + EXPECT_EQ(result, "word "sv); + + result = MUST(string.trim(" "sv, TrimMode::Right)); + EXPECT_EQ(result, " word"sv); + } + { + auto string = MUST(String::from_utf8(" word "sv)); + + auto result = MUST(string.trim("\t"sv, TrimMode::Both)); + EXPECT_EQ(result, " word "sv); + + result = MUST(string.trim("\t"sv, TrimMode::Left)); + EXPECT_EQ(result, " word "sv); + + result = MUST(string.trim("\t"sv, TrimMode::Right)); + EXPECT_EQ(result, " word "sv); + } + { + auto string = MUST(String::from_utf8("ωΣωΣω"sv)); + + auto result = MUST(string.trim("ω"sv, TrimMode::Both)); + EXPECT_EQ(result, "ΣωΣ"sv); + + result = MUST(string.trim("ω"sv, TrimMode::Left)); + EXPECT_EQ(result, "ΣωΣω"sv); + + result = MUST(string.trim("ω"sv, TrimMode::Right)); + EXPECT_EQ(result, "ωΣωΣ"sv); + } + { + auto string = MUST(String::from_utf8("ωΣωΣω"sv)); + + auto result = MUST(string.trim("ωΣ"sv, TrimMode::Both)); + EXPECT(result.is_empty()); + + result = MUST(string.trim("ωΣ"sv, TrimMode::Left)); + EXPECT(result.is_empty()); + + result = MUST(string.trim("ωΣ"sv, TrimMode::Right)); + EXPECT(result.is_empty()); + } + { + auto string = MUST(String::from_utf8("ωΣωΣω"sv)); + + auto result = MUST(string.trim("Σω"sv, TrimMode::Both)); + EXPECT(result.is_empty()); + + result = MUST(string.trim("Σω"sv, TrimMode::Left)); + EXPECT(result.is_empty()); + + result = MUST(string.trim("Σω"sv, TrimMode::Right)); + EXPECT(result.is_empty()); + } +} |