1 // Copyright (c) 2011 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/string_split.h"
7 #include "base/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 SplitStringIntoKeyValuesTest
: public testing::Test
{
35 std::vector
<std::string
> values
;
38 TEST_F(SplitStringIntoKeyValuesTest
, EmptyInputMultipleValues
) {
39 EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input
40 '\t', // Key separators
42 EXPECT_TRUE(key
.empty());
43 EXPECT_TRUE(values
.empty());
46 TEST_F(SplitStringIntoKeyValuesTest
, EmptyValueInputMultipleValues
) {
47 EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t",
48 '\t', // Key separators
50 EXPECT_EQ("key_with_no_value", key
);
51 EXPECT_TRUE(values
.empty());
54 TEST_F(SplitStringIntoKeyValuesTest
, EmptyKeyInputMultipleValues
) {
55 EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key",
56 '\t', // Key separators
58 EXPECT_TRUE(key
.empty());
59 ASSERT_EQ(1U, values
.size());
62 TEST_F(SplitStringIntoKeyValuesTest
, KeyWithMultipleValues
) {
63 EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1, value2 value3",
64 '\t', // Key separators
66 EXPECT_EQ("key1", key
);
67 ASSERT_EQ(1U, values
.size());
68 EXPECT_EQ("value1, value2 value3", values
[0]);
71 TEST_F(SplitStringIntoKeyValuesTest
, EmptyInputSingleValue
) {
72 EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input
73 '\t', // Key separators
75 EXPECT_TRUE(key
.empty());
76 EXPECT_TRUE(values
.empty());
79 TEST_F(SplitStringIntoKeyValuesTest
, EmptyValueInputSingleValue
) {
80 EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t",
81 '\t', // Key separators
83 EXPECT_EQ("key_with_no_value", key
);
84 EXPECT_TRUE(values
.empty());
87 TEST_F(SplitStringIntoKeyValuesTest
, EmptyKeyInputSingleValue
) {
88 EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key",
89 '\t', // Key separators
91 EXPECT_TRUE(key
.empty());
92 ASSERT_EQ(1U, values
.size());
93 EXPECT_EQ("value for empty key", values
[0]);
96 TEST_F(SplitStringIntoKeyValuesTest
, KeyWithSingleValue
) {
97 EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1, value2 value3",
98 '\t', // Key separators
100 EXPECT_EQ("key1", key
);
101 ASSERT_EQ(1U, values
.size());
102 EXPECT_EQ("value1, value2 value3", values
[0]);
105 class SplitStringIntoKeyValuePairsTest
: public testing::Test
{
107 std::vector
<std::pair
<std::string
, std::string
> > kv_pairs
;
110 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptyString
) {
111 EXPECT_TRUE(SplitStringIntoKeyValuePairs("",
112 ':', // Key-value delimiters
113 ',', // Key-value pair delims
115 EXPECT_TRUE(kv_pairs
.empty());
118 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptySecondPair
) {
119 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:value1,,key3:value3",
120 ':', // Key-value delimiters
121 ',', // Key-value pair delims
123 ASSERT_EQ(2U, kv_pairs
.size());
124 EXPECT_EQ("key1", kv_pairs
[0].first
);
125 EXPECT_EQ("value1", kv_pairs
[0].second
);
126 EXPECT_EQ("key3", kv_pairs
[1].first
);
127 EXPECT_EQ("value3", kv_pairs
[1].second
);
130 TEST_F(SplitStringIntoKeyValuePairsTest
, EmptySecondValue
) {
131 EXPECT_FALSE(SplitStringIntoKeyValuePairs("key1:value1 , key2:",
132 ':', // Key-value delimiters
133 ',', // Key-value pair delims
135 ASSERT_EQ(2U, kv_pairs
.size());
136 EXPECT_EQ("key1", kv_pairs
[0].first
);
137 EXPECT_EQ("value1", kv_pairs
[0].second
);
138 EXPECT_EQ("key2", kv_pairs
[1].first
);
139 EXPECT_EQ("", kv_pairs
[1].second
);
142 TEST_F(SplitStringIntoKeyValuePairsTest
, DelimiterInValue
) {
143 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:va:ue1 , key2:value2",
144 ':', // Key-value delimiters
145 ',', // Key-value pair delims
147 ASSERT_EQ(2U, kv_pairs
.size());
148 EXPECT_EQ("key1", kv_pairs
[0].first
);
149 EXPECT_EQ("va:ue1", kv_pairs
[0].second
);
150 EXPECT_EQ("key2", kv_pairs
[1].first
);
151 EXPECT_EQ("value2", kv_pairs
[1].second
);
154 TEST(SplitStringUsingSubstrTest
, EmptyString
) {
155 std::vector
<std::string
> results
;
156 SplitStringUsingSubstr("", "DELIMITER", &results
);
157 ASSERT_EQ(1u, results
.size());
158 EXPECT_THAT(results
, ElementsAre(""));
161 // Test for SplitString
162 TEST(StringUtilTest
, SplitString
) {
163 std::vector
<std::wstring
> r
;
165 SplitString(L
"", L
',', &r
);
166 ASSERT_EQ(1U, r
.size());
167 EXPECT_EQ(r
[0], L
"");
170 SplitString(L
"a,b,c", L
',', &r
);
171 ASSERT_EQ(3U, r
.size());
172 EXPECT_EQ(r
[0], L
"a");
173 EXPECT_EQ(r
[1], L
"b");
174 EXPECT_EQ(r
[2], L
"c");
177 SplitString(L
"a, b, c", L
',', &r
);
178 ASSERT_EQ(3U, r
.size());
179 EXPECT_EQ(r
[0], L
"a");
180 EXPECT_EQ(r
[1], L
"b");
181 EXPECT_EQ(r
[2], L
"c");
184 SplitString(L
"a,,c", L
',', &r
);
185 ASSERT_EQ(3U, r
.size());
186 EXPECT_EQ(r
[0], L
"a");
187 EXPECT_EQ(r
[1], L
"");
188 EXPECT_EQ(r
[2], L
"c");
191 SplitString(L
"", L
'*', &r
);
192 ASSERT_EQ(1U, r
.size());
193 EXPECT_EQ(r
[0], L
"");
196 SplitString(L
"foo", L
'*', &r
);
197 ASSERT_EQ(1U, r
.size());
198 EXPECT_EQ(r
[0], L
"foo");
201 SplitString(L
"foo ,", L
',', &r
);
202 ASSERT_EQ(2U, r
.size());
203 EXPECT_EQ(r
[0], L
"foo");
204 EXPECT_EQ(r
[1], L
"");
207 SplitString(L
",", L
',', &r
);
208 ASSERT_EQ(2U, r
.size());
209 EXPECT_EQ(r
[0], L
"");
210 EXPECT_EQ(r
[1], L
"");
213 SplitString(L
"\t\ta\t", L
'\t', &r
);
214 ASSERT_EQ(4U, r
.size());
215 EXPECT_EQ(r
[0], L
"");
216 EXPECT_EQ(r
[1], L
"");
217 EXPECT_EQ(r
[2], L
"a");
218 EXPECT_EQ(r
[3], L
"");
221 SplitString(L
"\ta\t\nb\tcc", L
'\n', &r
);
222 ASSERT_EQ(2U, r
.size());
223 EXPECT_EQ(r
[0], L
"a");
224 EXPECT_EQ(r
[1], L
"b\tcc");
228 TEST(SplitStringUsingSubstrTest
, StringWithNoDelimiter
) {
229 std::vector
<std::string
> results
;
230 SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results
);
231 ASSERT_EQ(1u, results
.size());
232 EXPECT_THAT(results
, ElementsAre("alongwordwithnodelimiter"));
235 TEST(SplitStringUsingSubstrTest
, LeadingDelimitersSkipped
) {
236 std::vector
<std::string
> results
;
237 SplitStringUsingSubstr(
238 "DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree",
241 ASSERT_EQ(6u, results
.size());
242 EXPECT_THAT(results
, ElementsAre("", "", "", "one", "two", "three"));
245 TEST(SplitStringUsingSubstrTest
, ConsecutiveDelimitersSkipped
) {
246 std::vector
<std::string
> results
;
247 SplitStringUsingSubstr(
248 "unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro",
251 ASSERT_EQ(7u, results
.size());
252 EXPECT_THAT(results
, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
255 TEST(SplitStringUsingSubstrTest
, TrailingDelimitersSkipped
) {
256 std::vector
<std::string
> results
;
257 SplitStringUsingSubstr(
258 "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
261 ASSERT_EQ(7u, results
.size());
263 results
, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
266 TEST(StringSplitTest
, StringSplitDontTrim
) {
267 std::vector
<std::string
> r
;
269 SplitStringDontTrim("\t\ta\t", '\t', &r
);
270 ASSERT_EQ(4U, r
.size());
273 EXPECT_EQ(r
[2], "a");
277 SplitStringDontTrim("\ta\t\nb\tcc", '\n', &r
);
278 ASSERT_EQ(2U, r
.size());
279 EXPECT_EQ(r
[0], "\ta\t");
280 EXPECT_EQ(r
[1], "b\tcc");
284 TEST(StringSplitTest
, SplitStringAlongWhitespace
) {
286 const std::wstring input
;
287 const size_t expected_result_count
;
288 const std::wstring output1
;
289 const std::wstring output2
;
291 { L
"a", 1, L
"a", L
"" },
292 { L
" ", 0, L
"", L
"" },
293 { L
" a", 1, L
"a", L
"" },
294 { L
" ab ", 1, L
"ab", L
"" },
295 { L
" ab c", 2, L
"ab", L
"c" },
296 { L
" ab c ", 2, L
"ab", L
"c" },
297 { L
" ab cd", 2, L
"ab", L
"cd" },
298 { L
" ab cd ", 2, L
"ab", L
"cd" },
299 { L
" \ta\t", 1, L
"a", L
"" },
300 { L
" b\ta\t", 2, L
"b", L
"a" },
301 { L
" b\tat", 2, L
"b", L
"at" },
302 { L
"b\tat", 2, L
"b", L
"at" },
303 { L
"b\t at", 2, L
"b", L
"at" },
305 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(data
); ++i
) {
306 std::vector
<std::wstring
> results
;
307 SplitStringAlongWhitespace(data
[i
].input
, &results
);
308 ASSERT_EQ(data
[i
].expected_result_count
, results
.size());
309 if (data
[i
].expected_result_count
> 0)
310 ASSERT_EQ(data
[i
].output1
, results
[0]);
311 if (data
[i
].expected_result_count
> 1)
312 ASSERT_EQ(data
[i
].output2
, results
[1]);