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

Side by Side Diff: chrome/browser/common/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: Add unit test missing file. 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 "chrome/browser/common/ini_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 struct TestINIParserPair {
14 std::string section;
15 std::string key;
16 std::string value;
17 };
18
19 class TestINIParser : public INIParser {
20 public:
21 explicit TestINIParser(const std::vector<TestINIParserPair>& expected_values)
22 : expected_pairs_(expected_values),
23 pair_i_(0) {
24 }
25 virtual ~TestINIParser() {}
26
27 private:
28 virtual void HandlePair(const std::string& section, const std::string& key,
29 const std::string& value) OVERRIDE {
30 EXPECT_EQ(expected_pairs_[pair_i_].section, section);
31 EXPECT_EQ(expected_pairs_[pair_i_].key, key);
32 EXPECT_EQ(expected_pairs_[pair_i_].value, value);
33 ++pair_i_;
34 }
35
36 std::vector<TestINIParserPair> expected_pairs_;
37 size_t pair_i_;
38 };
39
40 TEST(INIParserTest, BasicValid) {
41 TestINIParser test_parser({
42 { "section1", "key1", "value1" },
43 { "section1", "key2", "value2" },
44 { "section1", "key3", "value3" },
45 { "section2", "key4", "value4" },
46 { "section2", "key5", "value5" },
47 { "section2", "key6", "value=with=equals" },
48 });
49
50 test_parser.Parse(
51 "[section1]\n"
52 "key1=value1\n"
53 "key2=value2\r\n" // Testing inconsistent/Windows line endings.
54 "key3=value3\n"
55 "[section2\n" // Testing omitted closing bracket.
56 "key4=value4\n"
57 "key5=value5\n"
58 "key6=value=with=equals\n");
59 }
60
61 TEST(INIParserTest, IgnoreBlankLinesAndComments) {
62 TestINIParser test_parser({
63 { "section1", "key1", "value1" },
64 { "section1", "key2", "value2" },
65 { "section1", "key3", "value3" },
66 { "section2", "key4", "value4" },
67 { "section2", "key5", "value5" },
68 { "section2", "key6", "value6" },
69 });
70
71 test_parser.Parse(
72 "\n"
73 "[section1]\n"
74 "key1=value1\n"
75 "\n"
76 "\n"
77 "key2=value2\n"
78 "key3=value3\n"
79 "\n"
80 ";Comment1"
81 "\n"
82 "[section2]\n"
83 "key4=value4\n"
84 ";Comment2\n"
85 "key5=value5\n"
86 "\n"
87 "key6=value6\n");
88 }
89
90 TEST(INIParserTest, DictionaryValueINIParser) {
91 DictionaryValueINIParser test_parser;
92
93 test_parser.Parse(
94 "[section1]\n"
95 "key1=value1\n"
96 "key.2=value2\n"
97 "key3=va.lue3\n"
98 "[se.ction2]\n"
99 "key.4=value4\n"
100 "key5=value5\n");
101
102 const DictionaryValue& root = test_parser.root();
103 std::string value;
104 EXPECT_TRUE(root.GetString("section1.key1", &value));
105 EXPECT_EQ("value1", value);
106 EXPECT_FALSE(root.GetString("section1.key.2", &value));
107 EXPECT_TRUE(root.GetString("section1.key3", &value));
108 EXPECT_EQ("va.lue3", value);
109 EXPECT_FALSE(root.GetString("se.ction2.key.4", &value));
110 EXPECT_FALSE(root.GetString("se.ction2.key5", &value));
111 }
112
113 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698