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.
8 #include "base/ini_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
16 TestTriplet(const std::string& section,
17 const std::string& key,
18 const std::string& value)
29 class TestINIParser : public INIParser {
31 explicit TestINIParser(
32 const std::vector<TestTriplet>& expected_triplets)
33 : expected_triplets_(expected_triplets),
36 virtual ~TestINIParser() {}
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);
51 std::vector<TestTriplet> expected_triplets_;
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);
69 "key2=value2\r\n" // Testing DOS "\r\n" line endings.
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.
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);
106 TEST(INIParserTest, DictionaryValueINIParser) {
107 DictionaryValueINIParser test_parser;
118 const DictionaryValue& root = test_parser.root();
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));