1 // Copyright (c) 2011 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 "chrome/browser/chromeos/name_value_pairs_parser.h"
6
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace chromeos {
11
TEST(NameValuePairsParser,TestGetSingleValueFromTool)12 TEST(NameValuePairsParser, TestGetSingleValueFromTool) {
13 NameValuePairsParser::NameValueMap map;
14 NameValuePairsParser parser(&map);
15 const char* command[] = { "echo", "Foo" };
16 EXPECT_TRUE(parser.GetSingleValueFromTool(arraysize(command), command,
17 "foo"));
18 ASSERT_EQ(1U, map.size());
19 EXPECT_EQ("Foo", map["foo"]);
20 }
21
TEST(NameValuePairsParser,TestParseNameValuePairsFromTool)22 TEST(NameValuePairsParser, TestParseNameValuePairsFromTool) {
23 NameValuePairsParser::NameValueMap map;
24 NameValuePairsParser parser(&map);
25 const char* command1[] = { "echo", "foo=Foo bar=Bar\nfoobar=FooBar\n" };
26 EXPECT_TRUE(parser.ParseNameValuePairsFromTool(
27 arraysize(command1), command1, "=", " \n"));
28 ASSERT_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 char* command2[] = { "echo", "foo=Foo,bar=Bar" };
35 EXPECT_TRUE(parser.ParseNameValuePairsFromTool(
36 arraysize(command2), command2, "=", ",\n"));
37 ASSERT_EQ(2U, map.size());
38 EXPECT_EQ("Foo", map["foo"]);
39 EXPECT_EQ("Bar", map["bar"]);
40
41 map.clear();
42 const char* command3[] = { "echo", "foo=Foo=foo,bar=Bar" };
43 EXPECT_FALSE(parser.ParseNameValuePairsFromTool(
44 arraysize(command3), command3, "=", ",\n"));
45
46 map.clear();
47 const char* command4[] = { "echo", "foo=Foo,=Bar" };
48 EXPECT_FALSE(parser.ParseNameValuePairsFromTool(
49 arraysize(command4), command4, "=", ",\n"));
50
51 map.clear();
52 const char* command5[] = { "echo",
53 "\"initial_locale\"=\"ja\"\n"
54 "\"initial_timezone\"=\"Asia/Tokyo\"\n"
55 "\"keyboard_layout\"=\"mozc-jp\"\n" };
56 EXPECT_TRUE(parser.ParseNameValuePairsFromTool(
57 arraysize(command5), command5, "=", "\n"));
58 ASSERT_EQ(3U, map.size());
59 EXPECT_EQ("ja", map["initial_locale"]);
60 EXPECT_EQ("Asia/Tokyo", map["initial_timezone"]);
61 EXPECT_EQ("mozc-jp", map["keyboard_layout"]);
62 }
63
64 } // namespace chromeos
65