Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 HandleTriplet(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", "value=with=equals" }, | |
| 49 { "section2", "key6", "value6" }, | |
| 50 }); | |
| 51 | |
| 52 test_parser.Parse( | |
| 53 "[section1]\n" | |
| 54 "key1=value1\n" | |
| 55 "key2=value2\r\n" // Testing DOS "\r\n" line endings. | |
| 56 "key3=value3\n" | |
| 57 "[section2\n" // Testing omitted closing bracket. | |
| 58 "key4=value4\r" // Testing Mac "\r" line endings. | |
|
Mark Mentovai
2013/06/17 19:59:48
\r is an obsolete (pre-Mac OS X) line ending. It’s
tommycli
2013/06/17 21:23:25
Done.
| |
| 59 "key5=value=with=equals\n" | |
| 60 "key6=value6"); // Testing omitted final line ending. | |
| 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" | |
| 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 | |
| OLD | NEW |