OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "chromeos/dbus/dbus_command_line_helper.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "chromeos/chromeos_switches.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class DbusCommandLineHelperTests : public testing::Test { |
| 17 public: |
| 18 DbusCommandLineHelperTests() {} |
| 19 |
| 20 protected: |
| 21 bool ParseOption(const std::string& arg0, const std::string& arg1) { |
| 22 results_.push_back(std::make_pair(arg0, arg1)); |
| 23 return true; |
| 24 } |
| 25 |
| 26 bool CallParseOption(const std::string& option) { |
| 27 results_.clear(); |
| 28 return dbus_command_line_helper::ParseOptions( |
| 29 option, |
| 30 base::Bind(&DbusCommandLineHelperTests::ParseOption, |
| 31 base::Unretained(this))); |
| 32 } |
| 33 |
| 34 std::vector<std::pair<std::string, std::string> > results_; |
| 35 }; |
| 36 |
| 37 const char kTestSwitch[] = "test-switch"; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 TEST_F(DbusCommandLineHelperTests, TestParseOptionBasic) { |
| 42 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 43 command_line->AppendSwitchASCII(kTestSwitch, "foo"); |
| 44 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 45 ASSERT_EQ(1u, results_.size()); |
| 46 EXPECT_EQ("foo", results_[0].first); |
| 47 EXPECT_EQ("", results_[0].second); |
| 48 |
| 49 command_line->AppendSwitchASCII(kTestSwitch, "foo="); |
| 50 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 51 ASSERT_EQ(1u, results_.size()); |
| 52 EXPECT_EQ("foo", results_[0].first); |
| 53 EXPECT_EQ("", results_[0].second); |
| 54 |
| 55 command_line->AppendSwitchASCII(kTestSwitch, "foo=1"); |
| 56 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 57 ASSERT_EQ(1u, results_.size()); |
| 58 EXPECT_EQ("foo", results_[0].first); |
| 59 EXPECT_EQ("1", results_[0].second); |
| 60 } |
| 61 |
| 62 TEST_F(DbusCommandLineHelperTests, TestParseOptionMultiple) { |
| 63 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 64 command_line->AppendSwitchASCII(kTestSwitch, "foo,bar"); |
| 65 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 66 ASSERT_EQ(2u, results_.size()); |
| 67 EXPECT_EQ("foo", results_[0].first); |
| 68 EXPECT_EQ("", results_[0].second); |
| 69 EXPECT_EQ("bar", results_[1].first); |
| 70 EXPECT_EQ("", results_[1].second); |
| 71 |
| 72 command_line->AppendSwitchASCII(kTestSwitch, "foo=bar,bar=foo"); |
| 73 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 74 ASSERT_EQ(2u, results_.size()); |
| 75 EXPECT_EQ("foo", results_[0].first); |
| 76 EXPECT_EQ("bar", results_[0].second); |
| 77 EXPECT_EQ("bar", results_[1].first); |
| 78 EXPECT_EQ("foo", results_[1].second); |
| 79 |
| 80 command_line->AppendSwitchASCII(kTestSwitch, "foo=bar,bar=,car"); |
| 81 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 82 ASSERT_EQ(3u, results_.size()); |
| 83 EXPECT_EQ("foo", results_[0].first); |
| 84 EXPECT_EQ("bar", results_[0].second); |
| 85 EXPECT_EQ("bar", results_[1].first); |
| 86 EXPECT_EQ("", results_[1].second); |
| 87 EXPECT_EQ("car", results_[2].first); |
| 88 EXPECT_EQ("", results_[2].second); |
| 89 |
| 90 command_line->AppendSwitchASCII(kTestSwitch, "foo,"); |
| 91 EXPECT_TRUE(CallParseOption(kTestSwitch)); |
| 92 ASSERT_EQ(1u, results_.size()); |
| 93 EXPECT_EQ("foo", results_[0].first); |
| 94 EXPECT_EQ("", results_[0].second); |
| 95 } |
| 96 |
| 97 TEST_F(DbusCommandLineHelperTests, TestParseOptionFail) { |
| 98 EXPECT_FALSE(CallParseOption(kTestSwitch)); |
| 99 } |
OLD | NEW |