| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/ini_parser.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace base { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 struct TestTriplet { | |
| 16 TestTriplet(const std::string& section, | |
| 17 const std::string& key, | |
| 18 const std::string& value) | |
| 19 : section(section), | |
| 20 key(key), | |
| 21 value(value) { | |
| 22 } | |
| 23 | |
| 24 std::string section; | |
| 25 std::string key; | |
| 26 std::string value; | |
| 27 }; | |
| 28 | |
| 29 class TestINIParser : public INIParser { | |
| 30 public: | |
| 31 explicit TestINIParser( | |
| 32 const std::vector<TestTriplet>& expected_triplets) | |
| 33 : expected_triplets_(expected_triplets), | |
| 34 pair_i_(0) { | |
| 35 } | |
| 36 virtual ~TestINIParser() {} | |
| 37 | |
| 38 size_t pair_i() { | |
| 39 return pair_i_; | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 virtual void HandleTriplet(const std::string& section, const std::string& key, | |
| 44 const std::string& value) OVERRIDE { | |
| 45 EXPECT_EQ(expected_triplets_[pair_i_].section, section); | |
| 46 EXPECT_EQ(expected_triplets_[pair_i_].key, key); | |
| 47 EXPECT_EQ(expected_triplets_[pair_i_].value, value); | |
| 48 ++pair_i_; | |
| 49 } | |
| 50 | |
| 51 std::vector<TestTriplet> expected_triplets_; | |
| 52 size_t pair_i_; | |
| 53 }; | |
| 54 | |
| 55 TEST(INIParserTest, BasicValid) { | |
| 56 std::vector<TestTriplet> expected_triplets; | |
| 57 expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); | |
| 58 expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); | |
| 59 expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); | |
| 60 expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); | |
| 61 expected_triplets.push_back(TestTriplet("section2", "key5", | |
| 62 "value=with=equals")); | |
| 63 expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); | |
| 64 TestINIParser test_parser(expected_triplets); | |
| 65 | |
| 66 test_parser.Parse( | |
| 67 "[section1]\n" | |
| 68 "key1=value1\n" | |
| 69 "key2=value2\r\n" // Testing DOS "\r\n" line endings. | |
| 70 "key3=value3\n" | |
| 71 "[section2\n" // Testing omitted closing bracket. | |
| 72 "key4=value4\r" // Testing "\r" line endings. | |
| 73 "key5=value=with=equals\n" | |
| 74 "key6=value6"); // Testing omitted final line ending. | |
| 75 } | |
| 76 | |
| 77 TEST(INIParserTest, IgnoreBlankLinesAndComments) { | |
| 78 std::vector<TestTriplet> expected_triplets; | |
| 79 expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); | |
| 80 expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); | |
| 81 expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); | |
| 82 expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); | |
| 83 expected_triplets.push_back(TestTriplet("section2", "key5", "value5")); | |
| 84 expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); | |
| 85 TestINIParser test_parser(expected_triplets); | |
| 86 | |
| 87 test_parser.Parse( | |
| 88 "\n" | |
| 89 "[section1]\n" | |
| 90 "key1=value1\n" | |
| 91 "\n" | |
| 92 "\n" | |
| 93 "key2=value2\n" | |
| 94 "key3=value3\n" | |
| 95 "\n" | |
| 96 ";Comment1" | |
| 97 "\n" | |
| 98 "[section2]\n" | |
| 99 "key4=value4\n" | |
| 100 "#Comment2\n" | |
| 101 "key5=value5\n" | |
| 102 "\n" | |
| 103 "key6=value6\n"); | |
| 104 } | |
| 105 | |
| 106 TEST(INIParserTest, DictionaryValueINIParser) { | |
| 107 DictionaryValueINIParser test_parser; | |
| 108 | |
| 109 test_parser.Parse( | |
| 110 "[section1]\n" | |
| 111 "key1=value1\n" | |
| 112 "key.2=value2\n" | |
| 113 "key3=va.lue3\n" | |
| 114 "[se.ction2]\n" | |
| 115 "key.4=value4\n" | |
| 116 "key5=value5\n"); | |
| 117 | |
| 118 const DictionaryValue& root = test_parser.root(); | |
| 119 std::string value; | |
| 120 EXPECT_TRUE(root.GetString("section1.key1", &value)); | |
| 121 EXPECT_EQ("value1", value); | |
| 122 EXPECT_FALSE(root.GetString("section1.key.2", &value)); | |
| 123 EXPECT_TRUE(root.GetString("section1.key3", &value)); | |
| 124 EXPECT_EQ("va.lue3", value); | |
| 125 EXPECT_FALSE(root.GetString("se.ction2.key.4", &value)); | |
| 126 EXPECT_FALSE(root.GetString("se.ction2.key5", &value)); | |
| 127 } | |
| 128 | |
| 129 } // namespace | |
| 130 | |
| 131 } // namespace base | |
| OLD | NEW |