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. 4 5 #include <string> 6 #include <vector> 7 8 #include "chrome/common/ini_parser.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace { 12 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) { 20 } 21 22 std::string section; 23 std::string key; 24 std::string value; 25 }; 26 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) { 33 } 34 virtual ~TestINIParser() {} 35 36 size_t pair_i() { 37 return pair_i_; 38 } 39 40 private: 41 virtual void HandleTriplet(const std::string& section, const std::string& key, 42 const std::string& value) OVERRIDE { 43 EXPECT_EQ(expected_triplets_[pair_i_].section, section); 44 EXPECT_EQ(expected_triplets_[pair_i_].key, key); 45 EXPECT_EQ(expected_triplets_[pair_i_].value, value); 46 ++pair_i_; 47 } 48 49 std::vector<TestTriplet> expected_triplets_; 50 size_t pair_i_; 51 }; 52 53 TEST(INIParserTest, BasicValid) { 54 std::vector<TestTriplet> expected_triplets; 55 expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); 56 expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); 57 expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); 58 expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); 59 expected_triplets.push_back(TestTriplet("section2", "key5", 60 "value=with=equals")); 61 expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); 62 TestINIParser test_parser(expected_triplets); 63 64 test_parser.Parse( 65 "[section1]\n" 66 "key1=value1\n" 67 "key2=value2\r\n" // Testing DOS "\r\n" line endings. 68 "key3=value3\n" 69 "[section2\n" // Testing omitted closing bracket. 70 "key4=value4\r" // Testing "\r" line endings. 71 "key5=value=with=equals\n" 72 "key6=value6"); // Testing omitted final line ending. 73 } 74 75 TEST(INIParserTest, IgnoreBlankLinesAndComments) { 76 std::vector<TestTriplet> expected_triplets; 77 expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); 78 expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); 79 expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); 80 expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); 81 expected_triplets.push_back(TestTriplet("section2", "key5", "value5")); 82 expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); 83 TestINIParser test_parser(expected_triplets); 84 85 test_parser.Parse( 86 "\n" 87 "[section1]\n" 88 "key1=value1\n" 89 "\n" 90 "\n" 91 "key2=value2\n" 92 "key3=value3\n" 93 "\n" 94 ";Comment1" 95 "\n" 96 "[section2]\n" 97 "key4=value4\n" 98 "#Comment2\n" 99 "key5=value5\n" 100 "\n" 101 "key6=value6\n"); 102 } 103 104 TEST(INIParserTest, DictionaryValueINIParser) { 105 DictionaryValueINIParser test_parser; 106 107 test_parser.Parse( 108 "[section1]\n" 109 "key1=value1\n" 110 "key.2=value2\n" 111 "key3=va.lue3\n" 112 "[se.ction2]\n" 113 "key.4=value4\n" 114 "key5=value5\n"); 115 116 const base::DictionaryValue& root = test_parser.root(); 117 std::string value; 118 EXPECT_TRUE(root.GetString("section1.key1", &value)); 119 EXPECT_EQ("value1", value); 120 EXPECT_FALSE(root.GetString("section1.key.2", &value)); 121 EXPECT_TRUE(root.GetString("section1.key3", &value)); 122 EXPECT_EQ("va.lue3", value); 123 EXPECT_FALSE(root.GetString("se.ction2.key.4", &value)); 124 EXPECT_FALSE(root.GetString("se.ction2.key5", &value)); 125 } 126 127 } // namespace 128