Implement multiple alternative services per origin.
[chromium-blink-merge.git] / chrome / common / ini_parser_unittest.cc
blob8d7ada55d18f8ab77faff71dfb6c10343b569b87
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.
5 #include <string>
6 #include <vector>
8 #include "chrome/common/ini_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 struct TestTriplet {
14 TestTriplet(const std::string& section,
15 const std::string& key,
16 const std::string& value)
17 : section(section),
18 key(key),
19 value(value) {
22 std::string section;
23 std::string key;
24 std::string value;
27 class TestINIParser : public INIParser {
28 public:
29 explicit TestINIParser(
30 const std::vector<TestTriplet>& expected_triplets)
31 : expected_triplets_(expected_triplets),
32 pair_i_(0) {
34 ~TestINIParser() override {}
36 size_t pair_i() {
37 return pair_i_;
40 private:
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);
47 ++pair_i_;
50 std::vector<TestTriplet> expected_triplets_;
51 size_t pair_i_;
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);
65 test_parser.Parse(
66 "[section1]\n"
67 "key1=value1\n"
68 "key2=value2\r\n" // Testing DOS "\r\n" line endings.
69 "key3=value3\n"
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);
86 test_parser.Parse(
87 "\n"
88 "[section1]\n"
89 "key1=value1\n"
90 "\n"
91 "\n"
92 "key2=value2\n"
93 "key3=value3\n"
94 "\n"
95 ";Comment1"
96 "\n"
97 "[section2]\n"
98 "key4=value4\n"
99 "#Comment2\n"
100 "key5=value5\n"
101 "\n"
102 "key6=value6\n");
105 TEST(INIParserTest, DictionaryValueINIParser) {
106 DictionaryValueINIParser test_parser;
108 test_parser.Parse(
109 "[section1]\n"
110 "key1=value1\n"
111 "key.2=value2\n"
112 "key3=va.lue3\n"
113 "[se.ction2]\n"
114 "key.4=value4\n"
115 "key5=value5\n");
117 const base::DictionaryValue& root = test_parser.root();
118 std::string value;
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));
128 } // namespace