|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +#include "StringHelper.h" |
| 7 | + |
| 8 | +namespace Utility |
| 9 | +{ |
| 10 | + class StringHelperTest : public ::testing::Test |
| 11 | + { |
| 12 | + protected: |
| 13 | + void SetUp() override {} |
| 14 | + void TearDown() override {} |
| 15 | + }; |
| 16 | + |
| 17 | + // Test ReplaceAll with simple strings |
| 18 | + TEST_F(StringHelperTest, ReplaceAll_ShouldReplaceSubstring) |
| 19 | + { |
| 20 | + std::string result = StringHelper::ReplaceAll("hello world", "world", "C++"); |
| 21 | + EXPECT_EQ(result, "hello C++"); |
| 22 | + } |
| 23 | + |
| 24 | + TEST_F(StringHelperTest, ReplaceAll_ShouldReplaceMultipleOccurrences) |
| 25 | + { |
| 26 | + std::string result = StringHelper::ReplaceAll("a quick brown fox jumps over the lazy dog", "o", "0"); |
| 27 | + EXPECT_EQ(result, "a quick br0wn f0x jumps 0ver the lazy d0g"); |
| 28 | + } |
| 29 | + |
| 30 | + TEST_F(StringHelperTest, ReplaceAll_NoOccurrences) |
| 31 | + { |
| 32 | + std::string result = StringHelper::ReplaceAll("hello world", "foo", "bar"); |
| 33 | + EXPECT_EQ(result, "hello world"); |
| 34 | + } |
| 35 | + |
| 36 | + // Test ReplaceAll for wide strings |
| 37 | + TEST_F(StringHelperTest, ReplaceAll_WideString_ShouldReplaceSubstring) |
| 38 | + { |
| 39 | + std::wstring result = StringHelper::ReplaceAll(L"hello world", L"world", L"C++"); |
| 40 | + EXPECT_EQ(result, L"hello C++"); |
| 41 | + } |
| 42 | + |
| 43 | + TEST_F(StringHelperTest, ReplaceAll_WideString_NoOccurrences) |
| 44 | + { |
| 45 | + std::wstring result = StringHelper::ReplaceAll(L"hello world", L"foo", L"bar"); |
| 46 | + EXPECT_EQ(result, L"hello world"); |
| 47 | + } |
| 48 | + |
| 49 | + // Test ToWstring with various encodings |
| 50 | + TEST_F(StringHelperTest, ToWstring_ShouldConvertString) |
| 51 | + { |
| 52 | + std::wstring result = StringHelper::ToWstring("hello", CP_UTF8); |
| 53 | + EXPECT_EQ(result, L"hello"); |
| 54 | + } |
| 55 | + |
| 56 | + TEST_F(StringHelperTest, ToWstring_ShouldHandleEmptyString) |
| 57 | + { |
| 58 | + std::wstring result = StringHelper::ToWstring("", CP_UTF8); |
| 59 | + EXPECT_EQ(result, L""); |
| 60 | + } |
| 61 | + |
| 62 | + TEST_F(StringHelperTest, ToWstring_InvalidEncoding_ShouldReturnEmpty) |
| 63 | + { |
| 64 | + std::wstring result = StringHelper::ToWstring("invalid", 9999); // Invalid codepage |
| 65 | + EXPECT_EQ(result, L""); |
| 66 | + } |
| 67 | + |
| 68 | + // Test ToString with various encodings |
| 69 | + TEST_F(StringHelperTest, ToString_ShouldConvertWstring) |
| 70 | + { |
| 71 | + std::string result = StringHelper::ToString(L"hello", CP_UTF8); |
| 72 | + EXPECT_EQ(result, "hello"); |
| 73 | + } |
| 74 | + |
| 75 | + TEST_F(StringHelperTest, ToString_ShouldHandleEmptyWstring) |
| 76 | + { |
| 77 | + std::string result = StringHelper::ToString(L"", CP_UTF8); |
| 78 | + EXPECT_EQ(result, ""); |
| 79 | + } |
| 80 | + |
| 81 | + TEST_F(StringHelperTest, ToString_InvalidEncoding_ShouldReturnEmpty) |
| 82 | + { |
| 83 | + std::string result = StringHelper::ToString(L"invalid", 9999); // Invalid codepage |
| 84 | + EXPECT_EQ(result, ""); |
| 85 | + } |
| 86 | + |
| 87 | + // Test Split for standard strings |
| 88 | + TEST_F(StringHelperTest, Split_ShouldSplitStringByDelimiter) |
| 89 | + { |
| 90 | + std::vector<std::string> result = StringHelper::Split("a,b,c", ","); |
| 91 | + std::vector<std::string> expected = {"a", "b", "c"}; |
| 92 | + EXPECT_EQ(result.size(), expected.size()); |
| 93 | + EXPECT_EQ(result, expected); |
| 94 | + } |
| 95 | + |
| 96 | + TEST_F(StringHelperTest, Split_ShouldHandleEmptyString) |
| 97 | + { |
| 98 | + std::vector<std::string> result = StringHelper::Split("", ","); |
| 99 | + EXPECT_TRUE(result.empty()); |
| 100 | + } |
| 101 | + |
| 102 | + TEST_F(StringHelperTest, Split_ShouldHandleNoDelimiters) |
| 103 | + { |
| 104 | + std::vector<std::string> result = StringHelper::Split("abc", ","); |
| 105 | + std::vector<std::string> expected = {"abc"}; |
| 106 | + EXPECT_EQ(result.size(), expected.size()); |
| 107 | + EXPECT_EQ(result, expected); |
| 108 | + } |
| 109 | + |
| 110 | + // Test Split for wide strings |
| 111 | + TEST_F(StringHelperTest, Split_WideString_ShouldSplitStringByDelimiter) |
| 112 | + { |
| 113 | + std::vector<std::wstring> result = StringHelper::Split(L"a,b,c", L","); |
| 114 | + std::vector<std::wstring> expected = {L"a", L"b", L"c"}; |
| 115 | + EXPECT_EQ(result.size(), expected.size()); |
| 116 | + EXPECT_EQ(result, expected); |
| 117 | + } |
| 118 | + |
| 119 | + TEST_F(StringHelperTest, Split_WideString_ShouldHandleEmptyString) |
| 120 | + { |
| 121 | + std::vector<std::wstring> result = StringHelper::Split(L"", L","); |
| 122 | + EXPECT_TRUE(result.empty()); |
| 123 | + } |
| 124 | + |
| 125 | + TEST_F(StringHelperTest, Split_WideString_ShouldHandleNoDelimiters) |
| 126 | + { |
| 127 | + std::vector<std::wstring> result = StringHelper::Split(L"abc", L","); |
| 128 | + std::vector<std::wstring> expected = {L"abc"}; |
| 129 | + EXPECT_EQ(result.size(), expected.size()); |
| 130 | + EXPECT_EQ(result, expected); |
| 131 | + } |
| 132 | + |
| 133 | + // Test Contains method with case sensitivity |
| 134 | + TEST_F(StringHelperTest, Contains_ShouldFindSubstring) |
| 135 | + { |
| 136 | + bool result = StringHelper::Contains("hello world", "world", false); |
| 137 | + EXPECT_TRUE(result); |
| 138 | + } |
| 139 | + |
| 140 | + TEST_F(StringHelperTest, Contains_ShouldReturnFalseForNonExistingSubstring) |
| 141 | + { |
| 142 | + bool result = StringHelper::Contains("hello world", "foo", false); |
| 143 | + EXPECT_FALSE(result); |
| 144 | + } |
| 145 | + |
| 146 | + TEST_F(StringHelperTest, Contains_IgnoreCase_ShouldFindSubstring) |
| 147 | + { |
| 148 | + bool result = StringHelper::Contains("Hello World", "world", true); |
| 149 | + EXPECT_TRUE(result); |
| 150 | + } |
| 151 | + |
| 152 | + // Test ToLower |
| 153 | + TEST_F(StringHelperTest, ToLower_ShouldConvertStringToLowercase) |
| 154 | + { |
| 155 | + std::string input = "HeLLo WoRLD"; |
| 156 | + StringHelper::ToLower(input); |
| 157 | + EXPECT_EQ(input, "hello world"); |
| 158 | + } |
| 159 | + |
| 160 | + TEST_F(StringHelperTest, ToLower_WideString_ShouldConvertToLowercase) |
| 161 | + { |
| 162 | + std::wstring input = L"HeLLo WoRLD"; |
| 163 | + StringHelper::ToLower(input); |
| 164 | + EXPECT_EQ(input, L"hello world"); |
| 165 | + } |
| 166 | +} // namespace Utility |
0 commit comments