Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: base/ini_parser_unittest.cc

Issue 16982004: Move Firefox importer's INI parser to c/browser/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move to base/, namespace base, and exported. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 HandlePair(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", "value5" },
49 { "section2", "key6", "value=with=equals" },
50 });
51
52 test_parser.Parse(
53 "[section1]\n"
54 "key1=value1\n"
55 "key2=value2\r\n" // Testing inconsistent/Windows line endings.
56 "key3=value3\n"
Mark Mentovai 2013/06/17 18:52:37 Also test that the right thing happens when there
tommycli 2013/06/17 19:49:24 Done.
57 "[section2\n" // Testing omitted closing bracket.
58 "key4=value4\n"
59 "key5=value5\n"
60 "key6=value=with=equals\n");
Mark Mentovai 2013/06/17 18:52:37 Also test that the right thing happens when the la
tommycli 2013/06/17 19:49:24 Done.
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"
Mark Mentovai 2013/06/17 18:52:37 You supported # as a comment character, test that
tommycli 2013/06/17 19:49:24 Done.
87 "key5=value5\n"
88 "\n"
89 "key6=value6\n");
90 }
91
92 TEST(INIParserTest, DictionaryValueINIParser) {
93 DictionaryValueINIParser test_parser;
94
95 test_parser.Parse(
96 "[section1]\n"
97 "key1=value1\n"
98 "key.2=value2\n"
99 "key3=va.lue3\n"
100 "[se.ction2]\n"
101 "key.4=value4\n"
102 "key5=value5\n");
103
104 const DictionaryValue& root = test_parser.root();
105 std::string value;
106 EXPECT_TRUE(root.GetString("section1.key1", &value));
107 EXPECT_EQ("value1", value);
108 EXPECT_FALSE(root.GetString("section1.key.2", &value));
109 EXPECT_TRUE(root.GetString("section1.key3", &value));
110 EXPECT_EQ("va.lue3", value);
111 EXPECT_FALSE(root.GetString("se.ction2.key.4", &value));
112 EXPECT_FALSE(root.GetString("se.ction2.key5", &value));
113 }
114
115 } // namespace
116
117 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698