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
3 // found in the LICENSE file.
5 #include "base/strings/string_split.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using ::testing::ElementsAre
;
17 #if !defined(WCHAR_T_IS_UTF16)
18 // Overload SplitString with a wide-char version to make it easier to
19 // test the string16 version with wide character literals.
20 void SplitString(const std::wstring
& str
,
22 std::vector
<std::wstring
>* result
) {
23 std::vector
<string16
> result16
;
24 SplitString(WideToUTF16(str
), c
, &result16
);
25 for (size_t i
= 0; i
< result16
.size(); ++i
)
26 result
->push_back(UTF16ToWide(result16
[i
]));
30 } // anonymous namespace
32 class SplitStringIntoKeyValuePairsTest
: public testing::Test
{
34 base::StringPairs kv_pairs
;
37 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptyString
) {
38 EXPECT_TRUE(SplitStringIntoKeyValuePairs(std::string(),
39 ':', // Key-value delimiter
40 ',', // Key-value pair delimiter
42 EXPECT_TRUE(kv_pairs
.empty());
45 TEST_F(SplitStringIntoKeyValuePairsTest
, MissingKeyValueDelimiter
) {
46 EXPECT_FALSE(SplitStringIntoKeyValuePairs("key1,key2:value2",
47 ':', // Key-value delimiter
48 ',', // Key-value pair delimiter
50 ASSERT_EQ(2U, kv_pairs
.size());
51 EXPECT_TRUE(kv_pairs
[0].first
.empty());
52 EXPECT_TRUE(kv_pairs
[0].second
.empty());
53 EXPECT_EQ("key2", kv_pairs
[1].first
);
54 EXPECT_EQ("value2", kv_pairs
[1].second
);
57 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptyKeyWithKeyValueDelimiter
) {
58 EXPECT_TRUE(SplitStringIntoKeyValuePairs(":value1,key2:value2",
59 ':', // Key-value delimiter
60 ',', // Key-value pair delimiter
62 ASSERT_EQ(2U, kv_pairs
.size());
63 EXPECT_TRUE(kv_pairs
[0].first
.empty());
64 EXPECT_EQ("value1", kv_pairs
[0].second
);
65 EXPECT_EQ("key2", kv_pairs
[1].first
);
66 EXPECT_EQ("value2", kv_pairs
[1].second
);
69 TEST_F(SplitStringIntoKeyValuePairsTest
, TrailingAndLeadingPairDelimiter
) {
70 EXPECT_TRUE(SplitStringIntoKeyValuePairs(",key1:value1,key2:value2,",
71 ':', // Key-value delimiter
72 ',', // Key-value pair delimiter
74 ASSERT_EQ(2U, kv_pairs
.size());
75 EXPECT_EQ("key1", kv_pairs
[0].first
);
76 EXPECT_EQ("value1", kv_pairs
[0].second
);
77 EXPECT_EQ("key2", kv_pairs
[1].first
);
78 EXPECT_EQ("value2", kv_pairs
[1].second
);
81 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptyPair
) {
82 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:value1,,key3:value3",
83 ':', // Key-value delimiter
84 ',', // Key-value pair delimiter
86 ASSERT_EQ(2U, kv_pairs
.size());
87 EXPECT_EQ("key1", kv_pairs
[0].first
);
88 EXPECT_EQ("value1", kv_pairs
[0].second
);
89 EXPECT_EQ("key3", kv_pairs
[1].first
);
90 EXPECT_EQ("value3", kv_pairs
[1].second
);
93 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptyValue
) {
94 EXPECT_FALSE(SplitStringIntoKeyValuePairs("key1:,key2:value2",
95 ':', // Key-value delimiter
96 ',', // Key-value pair delimiter
98 ASSERT_EQ(2U, kv_pairs
.size());
99 EXPECT_EQ("key1", kv_pairs
[0].first
);
100 EXPECT_EQ("", kv_pairs
[0].second
);
101 EXPECT_EQ("key2", kv_pairs
[1].first
);
102 EXPECT_EQ("value2", kv_pairs
[1].second
);
105 TEST_F(SplitStringIntoKeyValuePairsTest
, UntrimmedWhitespace
) {
106 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1 : value1",
107 ':', // Key-value delimiter
108 ',', // Key-value pair delimiter
110 ASSERT_EQ(1U, kv_pairs
.size());
111 EXPECT_EQ("key1 ", kv_pairs
[0].first
);
112 EXPECT_EQ(" value1", kv_pairs
[0].second
);
115 TEST_F(SplitStringIntoKeyValuePairsTest
, TrimmedWhitespace
) {
116 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:value1 , key2:value2",
117 ':', // Key-value delimiter
118 ',', // Key-value pair delimiter
120 ASSERT_EQ(2U, kv_pairs
.size());
121 EXPECT_EQ("key1", kv_pairs
[0].first
);
122 EXPECT_EQ("value1", kv_pairs
[0].second
);
123 EXPECT_EQ("key2", kv_pairs
[1].first
);
124 EXPECT_EQ("value2", kv_pairs
[1].second
);
127 TEST_F(SplitStringIntoKeyValuePairsTest
, MultipleKeyValueDelimiters
) {
128 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:::value1,key2:value2",
129 ':', // Key-value delimiter
130 ',', // Key-value pair delimiter
132 ASSERT_EQ(2U, kv_pairs
.size());
133 EXPECT_EQ("key1", kv_pairs
[0].first
);
134 EXPECT_EQ("value1", kv_pairs
[0].second
);
135 EXPECT_EQ("key2", kv_pairs
[1].first
);
136 EXPECT_EQ("value2", kv_pairs
[1].second
);
139 TEST_F(SplitStringIntoKeyValuePairsTest
, OnlySplitAtGivenSeparator
) {
140 std::string
a("a ?!@#$%^&*()_+:/{}\\\t\nb");
141 EXPECT_TRUE(SplitStringIntoKeyValuePairs(a
+ "X" + a
+ "Y" + a
+ "X" + a
,
142 'X', // Key-value delimiter
143 'Y', // Key-value pair delimiter
145 ASSERT_EQ(2U, kv_pairs
.size());
146 EXPECT_EQ(a
, kv_pairs
[0].first
);
147 EXPECT_EQ(a
, kv_pairs
[0].second
);
148 EXPECT_EQ(a
, kv_pairs
[1].first
);
149 EXPECT_EQ(a
, kv_pairs
[1].second
);
153 TEST_F(SplitStringIntoKeyValuePairsTest
, DelimiterInValue
) {
154 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:va:ue1,key2:value2",
155 ':', // Key-value delimiter
156 ',', // Key-value pair delimiter
158 ASSERT_EQ(2U, kv_pairs
.size());
159 EXPECT_EQ("key1", kv_pairs
[0].first
);
160 EXPECT_EQ("va:ue1", kv_pairs
[0].second
);
161 EXPECT_EQ("key2", kv_pairs
[1].first
);
162 EXPECT_EQ("value2", kv_pairs
[1].second
);
165 TEST(SplitStringUsingSubstrTest
, EmptyString
) {
166 std::vector
<std::string
> results
;
167 SplitStringUsingSubstr(std::string(), "DELIMITER", &results
);
168 ASSERT_EQ(1u, results
.size());
169 EXPECT_THAT(results
, ElementsAre(""));
172 TEST(StringUtilTest
, SplitString_Basics
) {
173 std::vector
<std::string
> r
;
175 r
= SplitString(std::string(), ",:;", KEEP_WHITESPACE
, SPLIT_WANT_ALL
);
176 EXPECT_TRUE(r
.empty());
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]);
183 // Should split on any of the separators.
184 r
= SplitString("::,,;;", ",:;", KEEP_WHITESPACE
, SPLIT_WANT_ALL
);
185 ASSERT_EQ(7u, r
.size());
187 ASSERT_TRUE(str
.empty());
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]);
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]);
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.
212 TEST(StringUtilTest
, SplitString_WhitespaceAndResultType
) {
213 std::vector
<std::string
> r
;
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());
221 // Input string is space and we're trimming.
222 r
= SplitString(" ", ",", TRIM_WHITESPACE
, SPLIT_WANT_ALL
);
223 ASSERT_EQ(1u, r
.size());
225 r
= SplitString(" ", ",", TRIM_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
226 EXPECT_TRUE(r
.empty());
228 // Test all 4 combinations of flags on ", ,".
229 r
= SplitString(", ,", ",", KEEP_WHITESPACE
, SPLIT_WANT_ALL
);
230 ASSERT_EQ(3u, r
.size());
232 EXPECT_EQ(" ", r
[1]);
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());
242 r
= SplitString(", ,", ",", TRIM_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
243 ASSERT_TRUE(r
.empty());
246 TEST(StringUtilTest
, SplitString_Legacy
) {
247 std::vector
<std::wstring
> r
;
249 SplitString(std::wstring(), L
',', &r
);
250 EXPECT_EQ(0U, r
.size());
253 SplitString(L
"a,b,c", L
',', &r
);
254 ASSERT_EQ(3U, r
.size());
255 EXPECT_EQ(r
[0], L
"a");
256 EXPECT_EQ(r
[1], L
"b");
257 EXPECT_EQ(r
[2], L
"c");
260 SplitString(L
"a, b, c", L
',', &r
);
261 ASSERT_EQ(3U, r
.size());
262 EXPECT_EQ(r
[0], L
"a");
263 EXPECT_EQ(r
[1], L
"b");
264 EXPECT_EQ(r
[2], L
"c");
267 SplitString(L
"a,,c", L
',', &r
);
268 ASSERT_EQ(3U, r
.size());
269 EXPECT_EQ(r
[0], L
"a");
270 EXPECT_EQ(r
[1], L
"");
271 EXPECT_EQ(r
[2], L
"c");
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");
281 SplitString(L
" ", L
'*', &r
);
282 EXPECT_EQ(0U, r
.size());
285 SplitString(L
"foo", L
'*', &r
);
286 ASSERT_EQ(1U, r
.size());
287 EXPECT_EQ(r
[0], L
"foo");
290 SplitString(L
"foo ,", L
',', &r
);
291 ASSERT_EQ(2U, r
.size());
292 EXPECT_EQ(r
[0], L
"foo");
293 EXPECT_EQ(r
[1], L
"");
296 SplitString(L
",", L
',', &r
);
297 ASSERT_EQ(2U, r
.size());
298 EXPECT_EQ(r
[0], L
"");
299 EXPECT_EQ(r
[1], L
"");
302 SplitString(L
"\t\ta\t", L
'\t', &r
);
303 ASSERT_EQ(4U, r
.size());
304 EXPECT_EQ(r
[0], L
"");
305 EXPECT_EQ(r
[1], L
"");
306 EXPECT_EQ(r
[2], L
"a");
307 EXPECT_EQ(r
[3], L
"");
310 SplitString(L
"\ta\t\nb\tcc", L
'\n', &r
);
311 ASSERT_EQ(2U, r
.size());
312 EXPECT_EQ(r
[0], L
"a");
313 EXPECT_EQ(r
[1], L
"b\tcc");
317 TEST(SplitStringUsingSubstrTest
, StringWithNoDelimiter
) {
318 std::vector
<std::string
> results
;
319 SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results
);
320 ASSERT_EQ(1u, results
.size());
321 EXPECT_THAT(results
, ElementsAre("alongwordwithnodelimiter"));
324 TEST(SplitStringUsingSubstrTest
, LeadingDelimitersSkipped
) {
325 std::vector
<std::string
> results
;
326 SplitStringUsingSubstr(
327 "DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree",
330 ASSERT_EQ(6u, results
.size());
331 EXPECT_THAT(results
, ElementsAre("", "", "", "one", "two", "three"));
334 TEST(SplitStringUsingSubstrTest
, ConsecutiveDelimitersSkipped
) {
335 std::vector
<std::string
> results
;
336 SplitStringUsingSubstr(
337 "unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro",
340 ASSERT_EQ(7u, results
.size());
341 EXPECT_THAT(results
, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
344 TEST(SplitStringUsingSubstrTest
, TrailingDelimitersSkipped
) {
345 std::vector
<std::string
> results
;
346 SplitStringUsingSubstr(
347 "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
350 ASSERT_EQ(7u, results
.size());
352 results
, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
355 TEST(StringSplitTest
, StringSplitDontTrim
) {
356 std::vector
<std::string
> r
;
358 SplitStringDontTrim(" ", '*', &r
);
359 ASSERT_EQ(1U, r
.size());
360 EXPECT_EQ(r
[0], " ");
362 SplitStringDontTrim("\t \ta\t ", '\t', &r
);
363 ASSERT_EQ(4U, r
.size());
365 EXPECT_EQ(r
[1], " ");
366 EXPECT_EQ(r
[2], "a");
367 EXPECT_EQ(r
[3], " ");
369 SplitStringDontTrim("\ta\t\nb\tcc", '\n', &r
);
370 ASSERT_EQ(2U, r
.size());
371 EXPECT_EQ(r
[0], "\ta\t");
372 EXPECT_EQ(r
[1], "b\tcc");
375 TEST(StringSplitTest
, SplitStringAlongWhitespace
) {
378 const size_t expected_result_count
;
384 { " a", 1, "a", "" },
385 { " ab ", 1, "ab", "" },
386 { " ab c", 2, "ab", "c" },
387 { " ab c ", 2, "ab", "c" },
388 { " ab cd", 2, "ab", "cd" },
389 { " ab cd ", 2, "ab", "cd" },
390 { " \ta\t", 1, "a", "" },
391 { " b\ta\t", 2, "b", "a" },
392 { " b\tat", 2, "b", "at" },
393 { "b\tat", 2, "b", "at" },
394 { "b\t at", 2, "b", "at" },
396 for (size_t i
= 0; i
< arraysize(data
); ++i
) {
397 std::vector
<std::string
> results
;
398 SplitStringAlongWhitespace(data
[i
].input
, &results
);
399 ASSERT_EQ(data
[i
].expected_result_count
, results
.size());
400 if (data
[i
].expected_result_count
> 0)
401 ASSERT_EQ(data
[i
].output1
, results
[0]);
402 if (data
[i
].expected_result_count
> 1)
403 ASSERT_EQ(data
[i
].output2
, results
[1]);