Tab Audio Mirroring: WebContentsAudioInputStream is a new implementation which repres...
[chromium-blink-merge.git] / base / string_split_unittest.cc
bloba4412e44a7be61de081fa9dfc4ef322b1e1d1771
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;
13 namespace base {
15 namespace {
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,
21 wchar_t c,
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]));
28 #endif
30 } // anonymous namespace
32 class SplitStringIntoKeyValuesTest : public testing::Test {
33 protected:
34 std::string key;
35 std::vector<std::string> values;
38 TEST_F(SplitStringIntoKeyValuesTest, EmptyInputMultipleValues) {
39 EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input
40 '\t', // Key separators
41 &key, &values));
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
49 &key, &values));
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
57 &key, &values));
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
65 &key, &values));
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
74 &key, &values));
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
82 &key, &values));
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
90 &key, &values));
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
99 &key, &values));
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 {
106 protected:
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
114 &kv_pairs));
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
122 &kv_pairs));
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
134 &kv_pairs));
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
146 &kv_pairs));
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());
167 r.clear();
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");
174 r.clear();
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");
181 r.clear();
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");
188 r.clear();
190 SplitString(L" ", L'*', &r);
191 EXPECT_EQ(0U, r.size());
192 r.clear();
194 SplitString(L"foo", L'*', &r);
195 ASSERT_EQ(1U, r.size());
196 EXPECT_EQ(r[0], L"foo");
197 r.clear();
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"");
203 r.clear();
205 SplitString(L",", L',', &r);
206 ASSERT_EQ(2U, r.size());
207 EXPECT_EQ(r[0], L"");
208 EXPECT_EQ(r[1], L"");
209 r.clear();
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"");
217 r.clear();
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");
223 r.clear();
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",
237 "DELIMITER",
238 &results);
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",
247 "DELIMITER",
248 &results);
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",
257 "DELIMITER",
258 &results);
259 ASSERT_EQ(7u, results.size());
260 EXPECT_THAT(
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());
273 EXPECT_EQ(r[0], "");
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) {
285 struct TestData {
286 const char* input;
287 const size_t expected_result_count;
288 const char* output1;
289 const char* output2;
290 } data[] = {
291 { "a", 1, "a", "" },
292 { " ", 0, "", "" },
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]);
316 } // namespace base