OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/string_split.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace base { |
| 9 |
| 10 class SplitStringIntoKeyValuesTest : public testing::Test { |
| 11 protected: |
| 12 std::string key; |
| 13 std::vector<std::string> values; |
| 14 }; |
| 15 |
| 16 TEST_F(SplitStringIntoKeyValuesTest, EmptyInputMultipleValues) { |
| 17 EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input |
| 18 '\t', // Key separators |
| 19 &key, &values)); |
| 20 EXPECT_TRUE(key.empty()); |
| 21 EXPECT_TRUE(values.empty()); |
| 22 } |
| 23 |
| 24 TEST_F(SplitStringIntoKeyValuesTest, EmptyValueInputMultipleValues) { |
| 25 EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t", |
| 26 '\t', // Key separators |
| 27 &key, &values)); |
| 28 EXPECT_EQ("key_with_no_value", key); |
| 29 EXPECT_TRUE(values.empty()); |
| 30 } |
| 31 |
| 32 TEST_F(SplitStringIntoKeyValuesTest, EmptyKeyInputMultipleValues) { |
| 33 EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key", |
| 34 '\t', // Key separators |
| 35 &key, &values)); |
| 36 EXPECT_TRUE(key.empty()); |
| 37 ASSERT_EQ(1, values.size()); |
| 38 } |
| 39 |
| 40 TEST_F(SplitStringIntoKeyValuesTest, KeyWithMultipleValues) { |
| 41 EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1, value2 value3", |
| 42 '\t', // Key separators |
| 43 &key, &values)); |
| 44 EXPECT_EQ("key1", key); |
| 45 ASSERT_EQ(1, values.size()); |
| 46 EXPECT_EQ("value1, value2 value3", values[0]); |
| 47 } |
| 48 |
| 49 TEST_F(SplitStringIntoKeyValuesTest, EmptyInputSingleValue) { |
| 50 EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input |
| 51 '\t', // Key separators |
| 52 &key, &values)); |
| 53 EXPECT_TRUE(key.empty()); |
| 54 EXPECT_TRUE(values.empty()); |
| 55 } |
| 56 |
| 57 TEST_F(SplitStringIntoKeyValuesTest, EmptyValueInputSingleValue) { |
| 58 EXPECT_FALSE(SplitStringIntoKeyValues("key_with_no_value\t", |
| 59 '\t', // Key separators |
| 60 &key, &values)); |
| 61 EXPECT_EQ("key_with_no_value", key); |
| 62 EXPECT_TRUE(values.empty()); |
| 63 } |
| 64 |
| 65 TEST_F(SplitStringIntoKeyValuesTest, EmptyKeyInputSingleValue) { |
| 66 EXPECT_TRUE(SplitStringIntoKeyValues("\tvalue for empty key", |
| 67 '\t', // Key separators |
| 68 &key, &values)); |
| 69 EXPECT_TRUE(key.empty()); |
| 70 ASSERT_EQ(1, values.size()); |
| 71 EXPECT_EQ("value for empty key", values[0]); |
| 72 } |
| 73 |
| 74 TEST_F(SplitStringIntoKeyValuesTest, KeyWithSingleValue) { |
| 75 EXPECT_TRUE(SplitStringIntoKeyValues("key1\tvalue1, value2 value3", |
| 76 '\t', // Key separators |
| 77 &key, &values)); |
| 78 EXPECT_EQ("key1", key); |
| 79 ASSERT_EQ(1, values.size()); |
| 80 EXPECT_EQ("value1, value2 value3", values[0]); |
| 81 } |
| 82 |
| 83 class SplitStringIntoKeyValuePairsTest : public testing::Test { |
| 84 protected: |
| 85 std::vector<std::pair<std::string, std::string> > kv_pairs; |
| 86 }; |
| 87 |
| 88 TEST_F(SplitStringIntoKeyValuePairsTest, DISABLED_EmptyString) { |
| 89 EXPECT_TRUE(SplitStringIntoKeyValuePairs("", |
| 90 ':', // Key-value delimiters |
| 91 ',', // Key-value pair delims |
| 92 &kv_pairs)); |
| 93 EXPECT_TRUE(kv_pairs.empty()); |
| 94 } |
| 95 |
| 96 TEST_F(SplitStringIntoKeyValuePairsTest, EmptySecondValue) { |
| 97 EXPECT_FALSE(SplitStringIntoKeyValuePairs("key1:value1 , key2:", |
| 98 ':', // Key-value delimiters |
| 99 ',', // Key-value pair delims |
| 100 &kv_pairs)); |
| 101 ASSERT_EQ(2, kv_pairs.size()); |
| 102 EXPECT_EQ("key1", kv_pairs[0].first); |
| 103 EXPECT_EQ("value1", kv_pairs[0].second); |
| 104 EXPECT_EQ("key2", kv_pairs[1].first); |
| 105 EXPECT_EQ("", kv_pairs[1].second); |
| 106 } |
| 107 |
| 108 TEST_F(SplitStringIntoKeyValuePairsTest, DelimiterInValue) { |
| 109 EXPECT_TRUE(SplitStringIntoKeyValuePairs("key1:va:ue1 , key2:value2", |
| 110 ':', // Key-value delimiters |
| 111 ',', // Key-value pair delims |
| 112 &kv_pairs)); |
| 113 ASSERT_EQ(2, kv_pairs.size()); |
| 114 EXPECT_EQ("key1", kv_pairs[0].first); |
| 115 EXPECT_EQ("va:ue1", kv_pairs[0].second); |
| 116 EXPECT_EQ("key2", kv_pairs[1].first); |
| 117 EXPECT_EQ("value2", kv_pairs[1].second); |
| 118 } |
| 119 |
| 120 } // namespace base |
OLD | NEW |