OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/string_split.h" | 5 #include "base/strings/string_split.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 EXPECT_EQ("value2", kv_pairs[1].second); | 162 EXPECT_EQ("value2", kv_pairs[1].second); |
163 } | 163 } |
164 | 164 |
165 TEST(SplitStringUsingSubstrTest, EmptyString) { | 165 TEST(SplitStringUsingSubstrTest, EmptyString) { |
166 std::vector<std::string> results; | 166 std::vector<std::string> results; |
167 SplitStringUsingSubstr(std::string(), "DELIMITER", &results); | 167 SplitStringUsingSubstr(std::string(), "DELIMITER", &results); |
168 ASSERT_EQ(1u, results.size()); | 168 ASSERT_EQ(1u, results.size()); |
169 EXPECT_THAT(results, ElementsAre("")); | 169 EXPECT_THAT(results, ElementsAre("")); |
170 } | 170 } |
171 | 171 |
172 TEST(StringUtilTest, SplitString) { | 172 TEST(StringUtilTest, SplitString_Basics) { |
| 173 std::vector<std::string> r; |
| 174 |
| 175 r = SplitString(std::string(), ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 176 EXPECT_TRUE(r.empty()); |
| 177 |
| 178 // Empty separator list |
| 179 r = SplitString("hello, world", "", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 180 ASSERT_EQ(1u, r.size()); |
| 181 EXPECT_EQ("hello, world", r[0]); |
| 182 |
| 183 // Should split on any of the separators. |
| 184 r = SplitString("::,,;;", ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 185 ASSERT_EQ(7u, r.size()); |
| 186 for (auto str : r) |
| 187 ASSERT_TRUE(str.empty()); |
| 188 |
| 189 r = SplitString("red, green; blue:", ",:;", TRIM_WHITESPACE, |
| 190 SPLIT_WANT_NONEMPTY); |
| 191 ASSERT_EQ(3u, r.size()); |
| 192 EXPECT_EQ("red", r[0]); |
| 193 EXPECT_EQ("green", r[1]); |
| 194 EXPECT_EQ("blue", r[2]); |
| 195 |
| 196 // Want to split a string along whitespace sequences. |
| 197 r = SplitString(" red green \tblue\n", " \t\n", TRIM_WHITESPACE, |
| 198 SPLIT_WANT_NONEMPTY); |
| 199 ASSERT_EQ(3u, r.size()); |
| 200 EXPECT_EQ("red", r[0]); |
| 201 EXPECT_EQ("green", r[1]); |
| 202 EXPECT_EQ("blue", r[2]); |
| 203 |
| 204 // Weird case of splitting on spaces but not trimming. |
| 205 r = SplitString(" red ", " ", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 206 ASSERT_EQ(3u, r.size()); |
| 207 EXPECT_EQ("", r[0]); // Before the first space. |
| 208 EXPECT_EQ("red", r[1]); |
| 209 EXPECT_EQ("", r[2]); // After the last space. |
| 210 } |
| 211 |
| 212 TEST(StringUtilTest, SplitString_WhitespaceAndResultType) { |
| 213 std::vector<std::string> r; |
| 214 |
| 215 // Empty input handling. |
| 216 r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 217 EXPECT_TRUE(r.empty()); |
| 218 r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 219 EXPECT_TRUE(r.empty()); |
| 220 |
| 221 // Input string is space and we're trimming. |
| 222 r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 223 ASSERT_EQ(1u, r.size()); |
| 224 EXPECT_EQ("", r[0]); |
| 225 r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 226 EXPECT_TRUE(r.empty()); |
| 227 |
| 228 // Test all 4 combinations of flags on ", ,". |
| 229 r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 230 ASSERT_EQ(3u, r.size()); |
| 231 EXPECT_EQ("", r[0]); |
| 232 EXPECT_EQ(" ", r[1]); |
| 233 EXPECT_EQ("", r[2]); |
| 234 r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 235 ASSERT_EQ(1u, r.size()); |
| 236 ASSERT_EQ(" ", r[0]); |
| 237 r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 238 ASSERT_EQ(3u, r.size()); |
| 239 EXPECT_EQ("", r[0]); |
| 240 EXPECT_EQ("", r[1]); |
| 241 EXPECT_EQ("", r[2]); |
| 242 r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 243 ASSERT_TRUE(r.empty()); |
| 244 } |
| 245 |
| 246 TEST(StringUtilTest, SplitString_Legacy) { |
173 std::vector<std::wstring> r; | 247 std::vector<std::wstring> r; |
174 | 248 |
175 SplitString(std::wstring(), L',', &r); | 249 SplitString(std::wstring(), L',', &r); |
176 EXPECT_EQ(0U, r.size()); | 250 EXPECT_EQ(0U, r.size()); |
177 r.clear(); | 251 r.clear(); |
178 | 252 |
179 SplitString(L"a,b,c", L',', &r); | 253 SplitString(L"a,b,c", L',', &r); |
180 ASSERT_EQ(3U, r.size()); | 254 ASSERT_EQ(3U, r.size()); |
181 EXPECT_EQ(r[0], L"a"); | 255 EXPECT_EQ(r[0], L"a"); |
182 EXPECT_EQ(r[1], L"b"); | 256 EXPECT_EQ(r[1], L"b"); |
183 EXPECT_EQ(r[2], L"c"); | 257 EXPECT_EQ(r[2], L"c"); |
184 r.clear(); | 258 r.clear(); |
185 | 259 |
186 SplitString(L"a, b, c", L',', &r); | 260 SplitString(L"a, b, c", L',', &r); |
187 ASSERT_EQ(3U, r.size()); | 261 ASSERT_EQ(3U, r.size()); |
188 EXPECT_EQ(r[0], L"a"); | 262 EXPECT_EQ(r[0], L"a"); |
189 EXPECT_EQ(r[1], L"b"); | 263 EXPECT_EQ(r[1], L"b"); |
190 EXPECT_EQ(r[2], L"c"); | 264 EXPECT_EQ(r[2], L"c"); |
191 r.clear(); | 265 r.clear(); |
192 | 266 |
193 SplitString(L"a,,c", L',', &r); | 267 SplitString(L"a,,c", L',', &r); |
194 ASSERT_EQ(3U, r.size()); | 268 ASSERT_EQ(3U, r.size()); |
195 EXPECT_EQ(r[0], L"a"); | 269 EXPECT_EQ(r[0], L"a"); |
196 EXPECT_EQ(r[1], L""); | 270 EXPECT_EQ(r[1], L""); |
197 EXPECT_EQ(r[2], L"c"); | 271 EXPECT_EQ(r[2], L"c"); |
198 r.clear(); | 272 r.clear(); |
199 | 273 |
| 274 SplitString(L"a, ,c", L',', &r); |
| 275 ASSERT_EQ(3U, r.size()); |
| 276 EXPECT_EQ(r[0], L"a"); |
| 277 EXPECT_EQ(r[1], L""); |
| 278 EXPECT_EQ(r[2], L"c"); |
| 279 r.clear(); |
| 280 |
200 SplitString(L" ", L'*', &r); | 281 SplitString(L" ", L'*', &r); |
201 EXPECT_EQ(0U, r.size()); | 282 EXPECT_EQ(0U, r.size()); |
202 r.clear(); | 283 r.clear(); |
203 | 284 |
204 SplitString(L"foo", L'*', &r); | 285 SplitString(L"foo", L'*', &r); |
205 ASSERT_EQ(1U, r.size()); | 286 ASSERT_EQ(1U, r.size()); |
206 EXPECT_EQ(r[0], L"foo"); | 287 EXPECT_EQ(r[0], L"foo"); |
207 r.clear(); | 288 r.clear(); |
208 | 289 |
209 SplitString(L"foo ,", L',', &r); | 290 SplitString(L"foo ,", L',', &r); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 SplitStringAlongWhitespace(data[i].input, &results); | 398 SplitStringAlongWhitespace(data[i].input, &results); |
318 ASSERT_EQ(data[i].expected_result_count, results.size()); | 399 ASSERT_EQ(data[i].expected_result_count, results.size()); |
319 if (data[i].expected_result_count > 0) | 400 if (data[i].expected_result_count > 0) |
320 ASSERT_EQ(data[i].output1, results[0]); | 401 ASSERT_EQ(data[i].output1, results[0]); |
321 if (data[i].expected_result_count > 1) | 402 if (data[i].expected_result_count > 1) |
322 ASSERT_EQ(data[i].output2, results[1]); | 403 ASSERT_EQ(data[i].output2, results[1]); |
323 } | 404 } |
324 } | 405 } |
325 | 406 |
326 } // namespace base | 407 } // namespace base |
OLD | NEW |