| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "chromeos/system/name_value_pairs_parser.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace chromeos { | |
| 11 namespace system { | |
| 12 | |
| 13 TEST(NameValuePairsParser, TestGetSingleValueFromTool) { | |
| 14 NameValuePairsParser::NameValueMap map; | |
| 15 NameValuePairsParser parser(&map); | |
| 16 const char* command[] = { "/bin/echo", "Foo" }; | |
| 17 EXPECT_TRUE(parser.GetSingleValueFromTool(arraysize(command), command, | |
| 18 "foo")); | |
| 19 ASSERT_EQ(1U, map.size()); | |
| 20 EXPECT_EQ("Foo", map["foo"]); | |
| 21 } | |
| 22 | |
| 23 TEST(NameValuePairsParser, TestParseNameValuePairs) { | |
| 24 NameValuePairsParser::NameValueMap map; | |
| 25 NameValuePairsParser parser(&map); | |
| 26 const std::string contents1 = "foo=Foo bar=Bar\nfoobar=FooBar\n"; | |
| 27 EXPECT_TRUE(parser.ParseNameValuePairs(contents1, "=", " \n")); | |
| 28 EXPECT_EQ(3U, map.size()); | |
| 29 EXPECT_EQ("Foo", map["foo"]); | |
| 30 EXPECT_EQ("Bar", map["bar"]); | |
| 31 EXPECT_EQ("FooBar", map["foobar"]); | |
| 32 | |
| 33 map.clear(); | |
| 34 const std::string contents2 = "foo=Foo,bar=Bar"; | |
| 35 EXPECT_TRUE(parser.ParseNameValuePairs(contents2, "=", ",\n")); | |
| 36 EXPECT_EQ(2U, map.size()); | |
| 37 EXPECT_EQ("Foo", map["foo"]); | |
| 38 EXPECT_EQ("Bar", map["bar"]); | |
| 39 | |
| 40 map.clear(); | |
| 41 const std::string contents3 = "foo=Foo=foo,bar=Bar"; | |
| 42 EXPECT_TRUE(parser.ParseNameValuePairs(contents3, "=", ",\n")); | |
| 43 EXPECT_EQ(2U, map.size()); | |
| 44 EXPECT_EQ("Foo=foo", map["foo"]); | |
| 45 EXPECT_EQ("Bar", map["bar"]); | |
| 46 | |
| 47 map.clear(); | |
| 48 const std::string contents4 = "foo=Foo,=Bar"; | |
| 49 EXPECT_FALSE(parser.ParseNameValuePairs(contents4, "=", ",\n")); | |
| 50 EXPECT_EQ(1U, map.size()); | |
| 51 EXPECT_EQ("Foo", map["foo"]); | |
| 52 | |
| 53 map.clear(); | |
| 54 const std::string contents5 = | |
| 55 "\"initial_locale\"=\"ja\"\n" | |
| 56 "\"initial_timezone\"=\"Asia/Tokyo\"\n" | |
| 57 "\"keyboard_layout\"=\"mozc-jp\"\n"; | |
| 58 EXPECT_TRUE(parser.ParseNameValuePairs(contents5, "=", "\n")); | |
| 59 EXPECT_EQ(3U, map.size()); | |
| 60 EXPECT_EQ("ja", map["initial_locale"]); | |
| 61 EXPECT_EQ("Asia/Tokyo", map["initial_timezone"]); | |
| 62 EXPECT_EQ("mozc-jp", map["keyboard_layout"]); | |
| 63 } | |
| 64 | |
| 65 TEST(NameValuePairsParser, TestParseNameValuePairsWithComments) { | |
| 66 NameValuePairsParser::NameValueMap map; | |
| 67 NameValuePairsParser parser(&map); | |
| 68 | |
| 69 const std::string contents1 = "foo=Foo,bar=#Bar,baz= 0 #Baz"; | |
| 70 EXPECT_TRUE(parser.ParseNameValuePairsWithComments( | |
| 71 contents1, "=", ",\n", "#")); | |
| 72 EXPECT_EQ(3U, map.size()); | |
| 73 EXPECT_EQ("Foo", map["foo"]); | |
| 74 EXPECT_EQ("", map["bar"]); | |
| 75 EXPECT_EQ("0", map["baz"]); | |
| 76 | |
| 77 map.clear(); | |
| 78 const std::string contents2 = "foo="; | |
| 79 EXPECT_TRUE(parser.ParseNameValuePairsWithComments( | |
| 80 contents2, "=", ",\n", "#")); | |
| 81 EXPECT_EQ(1U, map.size()); | |
| 82 EXPECT_EQ("", map["foo"]); | |
| 83 | |
| 84 map.clear(); | |
| 85 const std::string contents3 = " \t ,,#all empty,"; | |
| 86 EXPECT_FALSE(parser.ParseNameValuePairsWithComments( | |
| 87 contents3, "=", ",\n", "#")); | |
| 88 EXPECT_EQ(0U, map.size()); | |
| 89 } | |
| 90 | |
| 91 TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) { | |
| 92 // Sample output is taken from the /usr/bin/crosssytem tool. | |
| 93 const char* command[] = { "/bin/echo", | |
| 94 "arch = x86 # Platform architecture\n" \ | |
| 95 "cros_debug = 1 # OS should allow debug\n" \ | |
| 96 "dbg_reset = (error) # Debug reset mode request\n" \ | |
| 97 "key#with_comment = some value # Multiple # comment # delims\n" \ | |
| 98 "key = # No value.\n" \ | |
| 99 "vdat_timers = " \ | |
| 100 "LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170 " \ | |
| 101 "# Timer values from VbSharedData\n" | |
| 102 }; | |
| 103 | |
| 104 NameValuePairsParser::NameValueMap map; | |
| 105 NameValuePairsParser parser(&map); | |
| 106 parser.ParseNameValuePairsFromTool( | |
| 107 arraysize(command), command, "=", "\n", "#"); | |
| 108 EXPECT_EQ(6u, map.size()); | |
| 109 EXPECT_EQ("x86", map["arch"]); | |
| 110 EXPECT_EQ("1", map["cros_debug"]); | |
| 111 EXPECT_EQ("(error)", map["dbg_reset"]); | |
| 112 EXPECT_EQ("some value", map["key#with_comment"]); | |
| 113 EXPECT_EQ("", map["key"]); | |
| 114 EXPECT_EQ("LFS=0,0 LF=1784220250,2971030570 LK=9064076660,9342689170", | |
| 115 map["vdat_timers"]); | |
| 116 } | |
| 117 | |
| 118 } // namespace system | |
| 119 } // namespace chromeos | |
| OLD | NEW |