1 // Copyright 2014 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 "chrome/common/ini_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 TestTriplet(const std::string& section,
15 const std::string& key,
16 const std::string& value)
27 class TestINIParser : public INIParser {
29 explicit TestINIParser(
30 const std::vector<TestTriplet>& expected_triplets)
31 : expected_triplets_(expected_triplets),
34 ~TestINIParser() override {}
41 void HandleTriplet(const std::string& section,
42 const std::string& key,
43 const std::string& value) override {
44 EXPECT_EQ(expected_triplets_[pair_i_].section, section);
45 EXPECT_EQ(expected_triplets_[pair_i_].key, key);
46 EXPECT_EQ(expected_triplets_[pair_i_].value, value);
50 std::vector<TestTriplet> expected_triplets_;
54 TEST(INIParserTest, BasicValid) {
55 std::vector<TestTriplet> expected_triplets;
56 expected_triplets.push_back(TestTriplet("section1", "key1", "value1"));
57 expected_triplets.push_back(TestTriplet("section1", "key2", "value2"));
58 expected_triplets.push_back(TestTriplet("section1", "key3", "value3"));
59 expected_triplets.push_back(TestTriplet("section2", "key4", "value4"));
60 expected_triplets.push_back(TestTriplet("section2", "key5",
61 "value=with=equals"));
62 expected_triplets.push_back(TestTriplet("section2", "key6", "value6"));
63 TestINIParser test_parser(expected_triplets);
68 "key2=value2\r\n" // Testing DOS "\r\n" line endings.
70 "[section2\n" // Testing omitted closing bracket.
71 "key4=value4\r" // Testing "\r" line endings.
72 "key5=value=with=equals\n"
73 "key6=value6"); // Testing omitted final line ending.
76 TEST(INIParserTest, IgnoreBlankLinesAndComments) {
77 std::vector<TestTriplet> expected_triplets;
78 expected_triplets.push_back(TestTriplet("section1", "key1", "value1"));
79 expected_triplets.push_back(TestTriplet("section1", "key2", "value2"));
80 expected_triplets.push_back(TestTriplet("section1", "key3", "value3"));
81 expected_triplets.push_back(TestTriplet("section2", "key4", "value4"));
82 expected_triplets.push_back(TestTriplet("section2", "key5", "value5"));
83 expected_triplets.push_back(TestTriplet("section2", "key6", "value6"));
84 TestINIParser test_parser(expected_triplets);
105 TEST(INIParserTest, DictionaryValueINIParser) {
106 DictionaryValueINIParser test_parser;
117 const base::DictionaryValue& root = test_parser.root();
119 EXPECT_TRUE(root.GetString("section1.key1", &value));
120 EXPECT_EQ("value1", value);
121 EXPECT_FALSE(root.GetString("section1.key.2", &value));
122 EXPECT_TRUE(root.GetString("section1.key3", &value));
123 EXPECT_EQ("va.lue3", value);
124 EXPECT_FALSE(root.GetString("se.ction2.key.4", &value));
125 EXPECT_FALSE(root.GetString("se.ction2.key5", &value));