|
1 | 1 | #include "ul/string/string_misc.h" |
2 | 2 | #include "gtest/gtest.h" |
3 | 3 | #include <string> |
| 4 | +#include <vector> |
4 | 5 |
|
5 | 6 | namespace ul = mb::ul; |
6 | 7 |
|
@@ -51,3 +52,86 @@ TEST(str_apply_ellipse, test_wider_chars) { |
51 | 52 | const auto ret4 = ul::str::apply_ellipse(s, 4); |
52 | 53 | EXPECT_STREQ("\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88", ret4.c_str()); |
53 | 54 | } |
| 55 | + |
| 56 | +TEST(str_make_upper, basic) { |
| 57 | + std::string s = "hello"; |
| 58 | + EXPECT_EQ(ul::str::make_upper(s), "HELLO"); |
| 59 | +} |
| 60 | + |
| 61 | +TEST(str_make_upper, special_chars) { |
| 62 | + std::string s = "hello!@#$%^&*()"; // Only ASCII special characters |
| 63 | + EXPECT_EQ(ul::str::make_upper(s), "HELLO!@#$%^&*()"); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(str_make_lower, basic) { |
| 67 | + std::string s = "HELLO"; |
| 68 | + EXPECT_EQ(ul::str::make_lower(s), "hello"); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(str_make_lower, special_chars) { |
| 72 | + std::string s = "HELLO!@#$%^&*()"; // Only ASCII special characters |
| 73 | + EXPECT_EQ(ul::str::make_lower(s), "hello!@#$%^&*()"); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(str_replace_all, basic) { |
| 77 | + std::string s = "hello world"; |
| 78 | + ul::str::replace_all(s, "world", "there"); |
| 79 | + EXPECT_EQ(s, "hello there"); |
| 80 | + |
| 81 | + s = "hello hello hello"; |
| 82 | + ul::str::replace_all(s, "hello", "hi"); |
| 83 | + EXPECT_EQ(s, "hi hi hi"); |
| 84 | +} |
| 85 | + |
| 86 | +TEST(str_replace_all, empty_strings) { |
| 87 | + std::string s = ""; |
| 88 | + ul::str::replace_all(s, "hello", "hi"); // Empty source string is OK |
| 89 | + EXPECT_EQ(s, ""); |
| 90 | + |
| 91 | + // Note: Empty from_sub is not supported by design |
| 92 | +} |
| 93 | + |
| 94 | +TEST(str_replace_all, with_vector) { |
| 95 | + std::string s = "hello {1} world {2}"; |
| 96 | + std::vector<std::string> replacements = {"beautiful", "today"}; |
| 97 | + ul::str::replace_all(s, "{1}", replacements); |
| 98 | + EXPECT_EQ(s, "hello beautiful world {2}"); |
| 99 | + |
| 100 | + s = "hello {} world {}"; |
| 101 | + ul::str::replace_all(s, "{}", replacements); |
| 102 | + EXPECT_EQ(s, "hello beautiful world today"); |
| 103 | +} |
| 104 | + |
| 105 | +TEST(str_bool_conversion, bool2str) { |
| 106 | + EXPECT_EQ(ul::str::bool2str(true), "1"); |
| 107 | + EXPECT_EQ(ul::str::bool2str(false), "0"); |
| 108 | + |
| 109 | + EXPECT_EQ(ul::str::bool2str<ul::str::BoolStrBoolalpha>(true), "true"); |
| 110 | + EXPECT_EQ(ul::str::bool2str<ul::str::BoolStrBoolalpha>(false), "false"); |
| 111 | +} |
| 112 | + |
| 113 | +TEST(str_bool_conversion, str2bool) { |
| 114 | + EXPECT_TRUE(ul::str::str2bool("1")); |
| 115 | + EXPECT_FALSE(ul::str::str2bool("0")); |
| 116 | + |
| 117 | + EXPECT_TRUE(ul::str::str2bool<ul::str::BoolStrBoolalpha>("true")); |
| 118 | + EXPECT_FALSE(ul::str::str2bool<ul::str::BoolStrBoolalpha>("false")); |
| 119 | +} |
| 120 | + |
| 121 | +TEST(str_ends_with, basic) { |
| 122 | + EXPECT_TRUE(ul::str::ends_with("hello world", "world")); |
| 123 | + EXPECT_FALSE(ul::str::ends_with("hello world", "hello")); |
| 124 | + EXPECT_TRUE(ul::str::ends_with("hello", "hello")); |
| 125 | +} |
| 126 | + |
| 127 | +TEST(str_ends_with, empty_strings) { |
| 128 | + EXPECT_TRUE(ul::str::ends_with("", "")); |
| 129 | + EXPECT_FALSE(ul::str::ends_with("", "hello")); |
| 130 | + EXPECT_TRUE(ul::str::ends_with("hello", "")); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(str_ends_with, special_chars) { |
| 134 | + EXPECT_TRUE(ul::str::ends_with("hello\xc3\xa4", "\xc3\xa4")); // UTF-8 character |
| 135 | + EXPECT_TRUE(ul::str::ends_with("hello.txt", ".txt")); |
| 136 | + EXPECT_TRUE(ul::str::ends_with("hello\n", "\n")); |
| 137 | +} |
0 commit comments