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