Home | History | Annotate | Download | only in strings
      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.
      4 
      5 #include "base/strings/string_split.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "testing/gmock/include/gmock/gmock.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 using ::testing::ElementsAre;
     12 
     13 namespace base {
     14 
     15 namespace {
     16 
     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]));
     27 }
     28 #endif
     29 
     30 }  // anonymous namespace
     31 
     32 class SplitStringIntoKeyValuesTest : public testing::Test {
     33  protected:
     34   std::string key;
     35   std::vector<std::string> values;
     36 };
     37 
     38 TEST_F(SplitStringIntoKeyValuesTest, EmptyInputMultipleValues) {
     39   EXPECT_FALSE(SplitStringIntoKeyValues(std::string(),  // Empty input
     40                                         '\t',           // Key separators
     41                                         &key,
     42                                         &values));
     43   EXPECT_TRUE(key.empty());
     44   EXPECT_TRUE(values.empty());
     45 }
     46 
     47 TEST_F(SplitStringIntoKeyValuesTest, EmptyValueInputMultipleValues) {
     48   EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t",
     49                                         '\t',  // Key separators
     50                                         &key, &values));
     51   EXPECT_EQ("key_with_no_value", key);
     52   EXPECT_TRUE(values.empty());
     53 }
     54 
     55 TEST_F(SplitStringIntoKeyValuesTest, EmptyKeyInputMultipleValues) {
     56   EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key",
     57                                        '\t',  // Key separators
     58                                        &key, &values));
     59   EXPECT_TRUE(key.empty());
     60   ASSERT_EQ(1U, values.size());
     61 }
     62 
     63 TEST_F(SplitStringIntoKeyValuesTest, KeyWithMultipleValues) {
     64   EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1,   value2   value3",
     65                                        '\t',  // Key separators
     66                                        &key, &values));
     67   EXPECT_EQ("key1", key);
     68   ASSERT_EQ(1U, values.size());
     69   EXPECT_EQ("value1,   value2   value3", values[0]);
     70 }
     71 
     72 TEST_F(SplitStringIntoKeyValuesTest, EmptyInputSingleValue) {
     73   EXPECT_FALSE(SplitStringIntoKeyValues(std::string(),  // Empty input
     74                                         '\t',           // Key separators
     75                                         &key,
     76                                         &values));
     77   EXPECT_TRUE(key.empty());
     78   EXPECT_TRUE(values.empty());
     79 }
     80 
     81 TEST_F(SplitStringIntoKeyValuesTest, EmptyValueInputSingleValue) {
     82   EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t",
     83                                         '\t',  // Key separators
     84                                         &key, &values));
     85   EXPECT_EQ("key_with_no_value", key);
     86   EXPECT_TRUE(values.empty());
     87 }
     88 
     89 TEST_F(SplitStringIntoKeyValuesTest, EmptyKeyInputSingleValue) {
     90   EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key",
     91                                        '\t',  // Key separators
     92                                        &key, &values));
     93   EXPECT_TRUE(key.empty());
     94   ASSERT_EQ(1U, values.size());
     95   EXPECT_EQ("value for empty key", values[0]);
     96 }
     97 
     98 TEST_F(SplitStringIntoKeyValuesTest, KeyWithSingleValue) {
     99   EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1,   value2   value3",
    100                                        '\t',  // Key separators
    101                                        &key, &values));
    102   EXPECT_EQ("key1", key);
    103   ASSERT_EQ(1U, values.size());
    104   EXPECT_EQ("value1,   value2   value3", values[0]);
    105 }
    106 
    107 class SplitStringIntoKeyValuePairsTest : public testing::Test {
    108  protected:
    109   std::vector<std::pair<std::string, std::string> > kv_pairs;
    110 };
    111 
    112 TEST_F(SplitStringIntoKeyValuePairsTest, EmptyString) {
    113   EXPECT_TRUE(SplitStringIntoKeyValuePairs(std::string(),
    114                                            ':',  // Key-value delimiters
    115                                            ',',  // Key-value pair delims
    116                                            &kv_pairs));
    117   EXPECT_TRUE(kv_pairs.empty());
    118 }
    119 
    120 TEST_F(SplitStringIntoKeyValuePairsTest, EmptySecondPair) {
    121   EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:value1,,key3:value3",
    122                                            ':',   // Key-value delimiters
    123                                            ',',   // Key-value pair delims
    124                                            &kv_pairs));
    125   ASSERT_EQ(2U, kv_pairs.size());
    126   EXPECT_EQ("key1", kv_pairs[0].first);
    127   EXPECT_EQ("value1", kv_pairs[0].second);
    128   EXPECT_EQ("key3", kv_pairs[1].first);
    129   EXPECT_EQ("value3", kv_pairs[1].second);
    130 }
    131 
    132 TEST_F(SplitStringIntoKeyValuePairsTest, EmptySecondValue) {
    133   EXPECT_FALSE(SplitStringIntoKeyValuePairs("key1:value1 , key2:",
    134                                             ':',   // Key-value delimiters
    135                                             ',',   // Key-value pair delims
    136                                             &kv_pairs));
    137   ASSERT_EQ(2U, kv_pairs.size());
    138   EXPECT_EQ("key1", kv_pairs[0].first);
    139   EXPECT_EQ("value1", kv_pairs[0].second);
    140   EXPECT_EQ("key2", kv_pairs[1].first);
    141   EXPECT_EQ("", kv_pairs[1].second);
    142 }
    143 
    144 TEST_F(SplitStringIntoKeyValuePairsTest, DelimiterInValue) {
    145   EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:va:ue1 , key2:value2",
    146                                            ':',   // Key-value delimiters
    147                                            ',',   // Key-value pair delims
    148                                            &kv_pairs));
    149   ASSERT_EQ(2U, kv_pairs.size());
    150   EXPECT_EQ("key1", kv_pairs[0].first);
    151   EXPECT_EQ("va:ue1", kv_pairs[0].second);
    152   EXPECT_EQ("key2", kv_pairs[1].first);
    153   EXPECT_EQ("value2", kv_pairs[1].second);
    154 }
    155 
    156 TEST(SplitStringUsingSubstrTest, EmptyString) {
    157   std::vector<std::string> results;
    158   SplitStringUsingSubstr(std::string(), "DELIMITER", &results);
    159   ASSERT_EQ(1u, results.size());
    160   EXPECT_THAT(results, ElementsAre(""));
    161 }
    162 
    163 // Test for SplitString
    164 TEST(StringUtilTest, SplitString) {
    165   std::vector<std::wstring> r;
    166 
    167   SplitString(std::wstring(), L',', &r);
    168   EXPECT_EQ(0U, r.size());
    169   r.clear();
    170 
    171   SplitString(L"a,b,c", L',', &r);
    172   ASSERT_EQ(3U, r.size());
    173   EXPECT_EQ(r[0], L"a");
    174   EXPECT_EQ(r[1], L"b");
    175   EXPECT_EQ(r[2], L"c");
    176   r.clear();
    177 
    178   SplitString(L"a, b, c", L',', &r);
    179   ASSERT_EQ(3U, r.size());
    180   EXPECT_EQ(r[0], L"a");
    181   EXPECT_EQ(r[1], L"b");
    182   EXPECT_EQ(r[2], L"c");
    183   r.clear();
    184 
    185   SplitString(L"a,,c", L',', &r);
    186   ASSERT_EQ(3U, r.size());
    187   EXPECT_EQ(r[0], L"a");
    188   EXPECT_EQ(r[1], L"");
    189   EXPECT_EQ(r[2], L"c");
    190   r.clear();
    191 
    192   SplitString(L"   ", L'*', &r);
    193   EXPECT_EQ(0U, r.size());
    194   r.clear();
    195 
    196   SplitString(L"foo", L'*', &r);
    197   ASSERT_EQ(1U, r.size());
    198   EXPECT_EQ(r[0], L"foo");
    199   r.clear();
    200 
    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"");
    205   r.clear();
    206 
    207   SplitString(L",", L',', &r);
    208   ASSERT_EQ(2U, r.size());
    209   EXPECT_EQ(r[0], L"");
    210   EXPECT_EQ(r[1], L"");
    211   r.clear();
    212 
    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"");
    219   r.clear();
    220 
    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");
    225   r.clear();
    226 }
    227 
    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"));
    233 }
    234 
    235 TEST(SplitStringUsingSubstrTest, LeadingDelimitersSkipped) {
    236   std::vector<std::string> results;
    237   SplitStringUsingSubstr(
    238       "DELIMITERDELIMITERDELIMITERoneDELIMITERtwoDELIMITERthree",
    239       "DELIMITER",
    240       &results);
    241   ASSERT_EQ(6u, results.size());
    242   EXPECT_THAT(results, ElementsAre("", "", "", "one", "two", "three"));
    243 }
    244 
    245 TEST(SplitStringUsingSubstrTest, ConsecutiveDelimitersSkipped) {
    246   std::vector<std::string> results;
    247   SplitStringUsingSubstr(
    248       "unoDELIMITERDELIMITERDELIMITERdosDELIMITERtresDELIMITERDELIMITERcuatro",
    249       "DELIMITER",
    250       &results);
    251   ASSERT_EQ(7u, results.size());
    252   EXPECT_THAT(results, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
    253 }
    254 
    255 TEST(SplitStringUsingSubstrTest, TrailingDelimitersSkipped) {
    256   std::vector<std::string> results;
    257   SplitStringUsingSubstr(
    258       "unDELIMITERdeuxDELIMITERtroisDELIMITERquatreDELIMITERDELIMITERDELIMITER",
    259       "DELIMITER",
    260       &results);
    261   ASSERT_EQ(7u, results.size());
    262   EXPECT_THAT(
    263       results, ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
    264 }
    265 
    266 TEST(StringSplitTest, StringSplitDontTrim) {
    267   std::vector<std::string> r;
    268 
    269   SplitStringDontTrim("   ", '*', &r);
    270   ASSERT_EQ(1U, r.size());
    271   EXPECT_EQ(r[0], "   ");
    272 
    273   SplitStringDontTrim("\t  \ta\t ", '\t', &r);
    274   ASSERT_EQ(4U, r.size());
    275   EXPECT_EQ(r[0], "");
    276   EXPECT_EQ(r[1], "  ");
    277   EXPECT_EQ(r[2], "a");
    278   EXPECT_EQ(r[3], " ");
    279 
    280   SplitStringDontTrim("\ta\t\nb\tcc", '\n', &r);
    281   ASSERT_EQ(2U, r.size());
    282   EXPECT_EQ(r[0], "\ta\t");
    283   EXPECT_EQ(r[1], "b\tcc");
    284 }
    285 
    286 TEST(StringSplitTest, SplitStringAlongWhitespace) {
    287   struct TestData {
    288     const char* input;
    289     const size_t expected_result_count;
    290     const char* output1;
    291     const char* output2;
    292   } data[] = {
    293     { "a",       1, "a",  ""   },
    294     { " ",       0, "",   ""   },
    295     { " a",      1, "a",  ""   },
    296     { " ab ",    1, "ab", ""   },
    297     { " ab c",   2, "ab", "c"  },
    298     { " ab c ",  2, "ab", "c"  },
    299     { " ab cd",  2, "ab", "cd" },
    300     { " ab cd ", 2, "ab", "cd" },
    301     { " \ta\t",  1, "a",  ""   },
    302     { " b\ta\t", 2, "b",  "a"  },
    303     { " b\tat",  2, "b",  "at" },
    304     { "b\tat",   2, "b",  "at" },
    305     { "b\t at",  2, "b",  "at" },
    306   };
    307   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
    308     std::vector<std::string> results;
    309     SplitStringAlongWhitespace(data[i].input, &results);
    310     ASSERT_EQ(data[i].expected_result_count, results.size());
    311     if (data[i].expected_result_count > 0)
    312       ASSERT_EQ(data[i].output1, results[0]);
    313     if (data[i].expected_result_count > 1)
    314       ASSERT_EQ(data[i].output2, results[1]);
    315   }
    316 }
    317 
    318 }  // namespace base
    319