Index: base/ini_parser_unittest.cc |
diff --git a/base/ini_parser_unittest.cc b/base/ini_parser_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..06cadd9e98e0ef93c5600117e7949cb92f778282 |
--- /dev/null |
+++ b/base/ini_parser_unittest.cc |
@@ -0,0 +1,117 @@ |
+// 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 TestINIParserPair { |
+ std::string section; |
+ std::string key; |
+ std::string value; |
+}; |
+ |
+class TestINIParser : public INIParser { |
+ public: |
+ explicit TestINIParser(const std::vector<TestINIParserPair>& expected_values) |
+ : expected_pairs_(expected_values), |
+ pair_i_(0) { |
+ } |
+ virtual ~TestINIParser() {} |
+ |
+ private: |
+ virtual void HandlePair(const std::string& section, const std::string& key, |
+ const std::string& value) OVERRIDE { |
+ EXPECT_EQ(expected_pairs_[pair_i_].section, section); |
+ EXPECT_EQ(expected_pairs_[pair_i_].key, key); |
+ EXPECT_EQ(expected_pairs_[pair_i_].value, value); |
+ ++pair_i_; |
+ } |
+ |
+ std::vector<TestINIParserPair> expected_pairs_; |
+ size_t pair_i_; |
+}; |
+ |
+TEST(INIParserTest, BasicValid) { |
+ TestINIParser test_parser({ |
+ { "section1", "key1", "value1" }, |
+ { "section1", "key2", "value2" }, |
+ { "section1", "key3", "value3" }, |
+ { "section2", "key4", "value4" }, |
+ { "section2", "key5", "value5" }, |
+ { "section2", "key6", "value=with=equals" }, |
+ }); |
+ |
+ test_parser.Parse( |
+ "[section1]\n" |
+ "key1=value1\n" |
+ "key2=value2\r\n" // Testing inconsistent/Windows line endings. |
+ "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.
|
+ "[section2\n" // Testing omitted closing bracket. |
+ "key4=value4\n" |
+ "key5=value5\n" |
+ "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.
|
+} |
+ |
+TEST(INIParserTest, IgnoreBlankLinesAndComments) { |
+ TestINIParser test_parser({ |
+ { "section1", "key1", "value1" }, |
+ { "section1", "key2", "value2" }, |
+ { "section1", "key3", "value3" }, |
+ { "section2", "key4", "value4" }, |
+ { "section2", "key5", "value5" }, |
+ { "section2", "key6", "value6" }, |
+ }); |
+ |
+ 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" |
Mark Mentovai
2013/06/17 18:52:37
You supported # as a comment character, test that
tommycli
2013/06/17 19:49:24
Done.
|
+ "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 |