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/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 EXPECT_EQ(0U, r
.size());
169 SplitString(L
"a,b,c", L
',', &r
);
170 ASSERT_EQ(3U, r
.size());
171 EXPECT_EQ(r
[0], L
"a");
172 EXPECT_EQ(r
[1], L
"b");
173 EXPECT_EQ(r
[2], L
"c");
176 SplitString(L
"a, b, c", L
',', &r
);
177 ASSERT_EQ(3U, r
.size());
178 EXPECT_EQ(r
[0], L
"a");
179 EXPECT_EQ(r
[1], L
"b");
180 EXPECT_EQ(r
[2], L
"c");
183 SplitString(L
"a,,c", L
',', &r
);
184 ASSERT_EQ(3U, r
.size());
185 EXPECT_EQ(r
[0], L
"a");
186 EXPECT_EQ(r
[1], L
"");
187 EXPECT_EQ(r
[2], L
"c");
190 SplitString(L
" ", L
'*', &r
);
191 EXPECT_EQ(0U, r
.size());
194 SplitString(L
"foo", L
'*', &r
);
195 ASSERT_EQ(1U, r
.size());
196 EXPECT_EQ(r
[0], L
"foo");
199 SplitString(L
"foo ,", L
',', &r
);
200 ASSERT_EQ(2U, r
.size());
201 EXPECT_EQ(r
[0], L
"foo");
202 EXPECT_EQ(r
[1], L
"");
205 SplitString(L
",", L
',', &r
);
206 ASSERT_EQ(2U, r
.size());
207 EXPECT_EQ(r
[0], L
"");
208 EXPECT_EQ(r
[1], L
"");
211 SplitString(L
"\t\ta\t", L
'\t', &r
);
212 ASSERT_EQ(4U, r
.size());
213 EXPECT_EQ(r
[0], L
"");
214 EXPECT_EQ(r
[1], L
"");
215 EXPECT_EQ(r
[2], L
"a");
216 EXPECT_EQ(r
[3], L
"");
219 SplitString(L
"\ta\t\nb\tcc", L
'\n', &r
);
220 ASSERT_EQ(2U, r
.size());
221 EXPECT_EQ(r
[0], L
"a");
222 EXPECT_EQ(r
[1], L
"b\tcc");
226 TEST(SplitStringUsingSubstrTest
, StringWithNoDelimiter
) {
227 std::vector
<std::string
> results
;
228 SplitStringUsingSubstr("alongwordwithnodelimiter", "DELIMITER", &results
);
229 ASSERT_EQ(1u, results
.size());
230 EXPECT_THAT(results
, ElementsAre("alongwordwithnodelimiter"));
233 TEST(SplitStringUsingSubstrTest
, LeadingDelimitersSkipped
) {
234 std::vector
<std::string
> results
;
235 SplitStringUsingSubstr(
236 "DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree",
239 ASSERT_EQ(6u, results
.size());
240 EXPECT_THAT(results
, ElementsAre("", "", "", "one", "two", "three"));
243 TEST(SplitStringUsingSubstrTest
, ConsecutiveDelimitersSkipped
) {
244 std::vector
<std::string
> results
;
245 SplitStringUsingSubstr(
246 "unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro",
249 ASSERT_EQ(7u, results
.size());
250 EXPECT_THAT(results
, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
253 TEST(SplitStringUsingSubstrTest
, TrailingDelimitersSkipped
) {
254 std::vector
<std::string
> results
;
255 SplitStringUsingSubstr(
256 "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
259 ASSERT_EQ(7u, results
.size());
261 results
, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
264 TEST(StringSplitTest
, StringSplitDontTrim
) {
265 std::vector
<std::string
> r
;
267 SplitStringDontTrim(" ", '*', &r
);
268 ASSERT_EQ(1U, r
.size());
269 EXPECT_EQ(r
[0], " ");
271 SplitStringDontTrim("\t \ta\t ", '\t', &r
);
272 ASSERT_EQ(4U, r
.size());
274 EXPECT_EQ(r
[1], " ");
275 EXPECT_EQ(r
[2], "a");
276 EXPECT_EQ(r
[3], " ");
278 SplitStringDontTrim("\ta\t\nb\tcc", '\n', &r
);
279 ASSERT_EQ(2U, r
.size());
280 EXPECT_EQ(r
[0], "\ta\t");
281 EXPECT_EQ(r
[1], "b\tcc");
284 TEST(StringSplitTest
, SplitStringAlongWhitespace
) {
287 const size_t expected_result_count
;
293 { " a", 1, "a", "" },
294 { " ab ", 1, "ab", "" },
295 { " ab c", 2, "ab", "c" },
296 { " ab c ", 2, "ab", "c" },
297 { " ab cd", 2, "ab", "cd" },
298 { " ab cd ", 2, "ab", "cd" },
299 { " \ta\t", 1, "a", "" },
300 { " b\ta\t", 2, "b", "a" },
301 { " b\tat", 2, "b", "at" },
302 { "b\tat", 2, "b", "at" },
303 { "b\t at", 2, "b", "at" },
305 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(data
); ++i
) {
306 std::vector
<std::string
> 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]);