summaryrefslogtreecommitdiffstats
path: root/base/ini_parser_unittest.cc
blob: ff90f6c8fc7f686a2600a85a0180fe5829063766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include <vector>

#include "base/ini_parser.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

struct TestTriplet {
  TestTriplet(const std::string& section,
          const std::string& key,
          const std::string& value)
      : section(section),
        key(key),
        value(value) {
  }

  std::string section;
  std::string key;
  std::string value;
};

class TestINIParser : public INIParser {
 public:
  explicit TestINIParser(
      const std::vector<TestTriplet>& expected_triplets)
      : expected_triplets_(expected_triplets),
        pair_i_(0) {
  }
  virtual ~TestINIParser() {}

  size_t pair_i() {
    return pair_i_;
  }

 private:
  virtual void HandleTriplet(const std::string& section, const std::string& key,
                             const std::string& value) OVERRIDE {
    EXPECT_EQ(expected_triplets_[pair_i_].section, section);
    EXPECT_EQ(expected_triplets_[pair_i_].key, key);
    EXPECT_EQ(expected_triplets_[pair_i_].value, value);
    ++pair_i_;
  }

  std::vector<TestTriplet> expected_triplets_;
  size_t pair_i_;
};

TEST(INIParserTest, BasicValid) {
  std::vector<TestTriplet> expected_triplets;
  expected_triplets.push_back(TestTriplet("section1", "key1", "value1"));
  expected_triplets.push_back(TestTriplet("section1", "key2", "value2"));
  expected_triplets.push_back(TestTriplet("section1", "key3", "value3"));
  expected_triplets.push_back(TestTriplet("section2", "key4", "value4"));
  expected_triplets.push_back(TestTriplet("section2", "key5",
                                          "value=with=equals"));
  expected_triplets.push_back(TestTriplet("section2", "key6", "value6"));
  TestINIParser test_parser(expected_triplets);

  test_parser.Parse(
      "[section1]\n"
      "key1=value1\n"
      "key2=value2\r\n"  // Testing DOS "\r\n" line endings.
      "key3=value3\n"
      "[section2\n"      // Testing omitted closing bracket.
      "key4=value4\r"    // Testing "\r" line endings.
      "key5=value=with=equals\n"
      "key6=value6");    // Testing omitted final line ending.
}

TEST(INIParserTest, IgnoreBlankLinesAndComments) {
  std::vector<TestTriplet> expected_triplets;
  expected_triplets.push_back(TestTriplet("section1", "key1", "value1"));
  expected_triplets.push_back(TestTriplet("section1", "key2", "value2"));
  expected_triplets.push_back(TestTriplet("section1", "key3", "value3"));
  expected_triplets.push_back(TestTriplet("section2", "key4", "value4"));
  expected_triplets.push_back(TestTriplet("section2", "key5", "value5"));
  expected_triplets.push_back(TestTriplet("section2", "key6", "value6"));
  TestINIParser test_parser(expected_triplets);

  test_parser.Parse(
      "\n"
      "[section1]\n"
      "key1=value1\n"
      "\n"
      "\n"
      "key2=value2\n"
      "key3=value3\n"
      "\n"
      ";Comment1"
      "\n"
      "[section2]\n"
      "key4=value4\n"
      "#Comment2\n"
      "key5=value5\n"
      "\n"
      "key6=value6\n");
}

TEST(INIParserTest, DictionaryValueINIParser) {
  DictionaryValueINIParser test_parser;

  test_parser.Parse(
      "[section1]\n"
      "key1=value1\n"
      "key.2=value2\n"
      "key3=va.lue3\n"
      "[se.ction2]\n"
      "key.4=value4\n"
      "key5=value5\n");

  const DictionaryValue& root = test_parser.root();
  std::string value;
  EXPECT_TRUE(root.GetString("section1.key1", &value));
  EXPECT_EQ("value1", value);
  EXPECT_FALSE(root.GetString("section1.key.2", &value));
  EXPECT_TRUE(root.GetString("section1.key3", &value));
  EXPECT_EQ("va.lue3", value);
  EXPECT_FALSE(root.GetString("se.ction2.key.4", &value));
  EXPECT_FALSE(root.GetString("se.ction2.key5", &value));
}

}  // namespace

}  // namespace base