Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 19 | #include "command.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 20 | |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 21 | using namespace simpleperf; |
| 22 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 23 | class MockCommand : public Command { |
| 24 | public: |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 25 | MockCommand() : Command("mock", "mock_short_help", "mock_long_help") {} |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 26 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 27 | bool Run(const std::vector<std::string>&) override { return true; } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 30 | TEST(command, CreateCommandInstance) { |
| 31 | ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr); |
| 32 | RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); }); |
| 33 | ASSERT_TRUE(CreateCommandInstance("mock1") != nullptr); |
| 34 | UnRegisterCommand("mock1"); |
| 35 | ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | TEST(command, GetAllCommands) { |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 39 | size_t command_count = GetAllCommandNames().size(); |
| 40 | RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); }); |
| 41 | ASSERT_EQ(command_count + 1, GetAllCommandNames().size()); |
| 42 | UnRegisterCommand("mock1"); |
| 43 | ASSERT_EQ(command_count, GetAllCommandNames().size()); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 44 | } |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 45 | |
| 46 | TEST(command, GetValueForOption) { |
| 47 | MockCommand command; |
| 48 | uint64_t value; |
| 49 | size_t i; |
| 50 | for (bool allow_suffixes : {true, false}) { |
| 51 | i = 0; |
| 52 | ASSERT_TRUE(command.GetUintOption({"-s", "156"}, &i, &value, 0, |
| 53 | std::numeric_limits<uint64_t>::max(), allow_suffixes)); |
| 54 | ASSERT_EQ(i, 1u); |
| 55 | ASSERT_EQ(value, 156u); |
| 56 | } |
| 57 | i = 0; |
| 58 | ASSERT_TRUE(command.GetUintOption({"-s", "156k"}, &i, &value, 0, |
| 59 | std::numeric_limits<uint64_t>::max(), true)); |
| 60 | ASSERT_EQ(value, 156 * (1ULL << 10)); |
| 61 | i = 0; |
| 62 | ASSERT_FALSE(command.GetUintOption({"-s"}, &i, &value)); |
| 63 | i = 0; |
| 64 | ASSERT_FALSE(command.GetUintOption({"-s", "0"}, &i, &value, 1)); |
| 65 | i = 0; |
| 66 | ASSERT_FALSE(command.GetUintOption({"-s", "156"}, &i, &value, 0, 155)); |
| 67 | i = 0; |
| 68 | double double_value; |
| 69 | ASSERT_TRUE(command.GetDoubleOption({"-s", "3.2"}, &i, &double_value, 0, 4)); |
| 70 | ASSERT_DOUBLE_EQ(double_value, 3.2); |
| 71 | } |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 72 | |
| 73 | TEST(command, PreprocessOptions) { |
| 74 | MockCommand cmd; |
| 75 | OptionValueMap options; |
| 76 | std::vector<std::pair<OptionName, OptionValue>> ordered_options; |
| 77 | std::vector<std::string> non_option_args; |
| 78 | |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 79 | OptionFormatMap option_formats = { |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 80 | {"--bool-option", {OptionValueType::NONE, OptionType::SINGLE}}, |
| 81 | {"--str-option", {OptionValueType::STRING, OptionType::MULTIPLE}}, |
| 82 | {"--str2-option", {OptionValueType::STRING, OptionType::SINGLE}}, |
| 83 | {"--opt-str-option", {OptionValueType::OPT_STRING, OptionType::MULTIPLE}}, |
| 84 | {"--uint-option", {OptionValueType::UINT, OptionType::SINGLE}}, |
| 85 | {"--double-option", {OptionValueType::DOUBLE, OptionType::SINGLE}}, |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 86 | |
| 87 | // ordered options |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 88 | {"--ord-str-option", {OptionValueType::STRING, OptionType::ORDERED}}, |
| 89 | {"--ord-uint-option", {OptionValueType::UINT, OptionType::ORDERED}}, |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // Check options. |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 93 | std::vector<std::string> args = { |
| 94 | "--bool-option", "--str-option", "str1", "--str-option", |
| 95 | "str1_2", "--str2-option", "str2_value", "--opt-str-option", |
| 96 | "--opt-str-option", "opt_str", "--uint-option", "34", |
| 97 | "--double-option", "-32.75"}; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 98 | ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr)); |
| 99 | ASSERT_TRUE(options.PullBoolValue("--bool-option")); |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 100 | auto values = options.PullValues("--str-option"); |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 101 | ASSERT_EQ(values.size(), 2); |
| 102 | ASSERT_EQ(*values[0].str_value, "str1"); |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 103 | ASSERT_EQ(*values[1].str_value, "str1_2"); |
| 104 | std::string str2_value; |
| 105 | options.PullStringValue("--str2-option", &str2_value); |
| 106 | ASSERT_EQ(str2_value, "str2_value"); |
| 107 | values = options.PullValues("--opt-str-option"); |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 108 | ASSERT_EQ(values.size(), 2); |
| 109 | ASSERT_TRUE(values[0].str_value == nullptr); |
| 110 | ASSERT_EQ(*values[1].str_value, "opt_str"); |
| 111 | size_t uint_value; |
| 112 | ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value)); |
| 113 | ASSERT_EQ(uint_value, 34); |
| 114 | double double_value; |
| 115 | ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value)); |
| 116 | ASSERT_DOUBLE_EQ(double_value, -32.75); |
| 117 | ASSERT_TRUE(options.values.empty()); |
| 118 | |
| 119 | // Check ordered options. |
| 120 | args = {"--ord-str-option", "str1", "--ord-uint-option", "32", "--ord-str-option", "str2"}; |
| 121 | ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr)); |
| 122 | ASSERT_EQ(ordered_options.size(), 3); |
| 123 | ASSERT_EQ(ordered_options[0].first, "--ord-str-option"); |
| 124 | ASSERT_EQ(*(ordered_options[0].second.str_value), "str1"); |
| 125 | ASSERT_EQ(ordered_options[1].first, "--ord-uint-option"); |
| 126 | ASSERT_EQ(ordered_options[1].second.uint_value, 32); |
| 127 | ASSERT_EQ(ordered_options[2].first, "--ord-str-option"); |
| 128 | ASSERT_EQ(*(ordered_options[2].second.str_value), "str2"); |
| 129 | |
| 130 | // Check non_option_args. |
| 131 | ASSERT_TRUE(cmd.PreprocessOptions({"arg1", "--arg2"}, option_formats, &options, &ordered_options, |
| 132 | &non_option_args)); |
| 133 | ASSERT_EQ(non_option_args, std::vector<std::string>({"arg1", "--arg2"})); |
| 134 | // "--" can force following args to be non_option_args. |
| 135 | ASSERT_TRUE(cmd.PreprocessOptions({"--", "--bool-option"}, option_formats, &options, |
| 136 | &ordered_options, &non_option_args)); |
| 137 | ASSERT_EQ(non_option_args, std::vector<std::string>({"--bool-option"})); |
Yabin Cui | e09cb9f | 2020-11-03 09:24:48 -0800 | [diff] [blame] | 138 | // Pass nullptr to not accept non option args. |
| 139 | ASSERT_FALSE(cmd.PreprocessOptions({"non_option_arg"}, option_formats, &options, &ordered_options, |
| 140 | nullptr)); |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 141 | |
| 142 | // Check different errors. |
| 143 | // unknown option |
| 144 | ASSERT_FALSE(cmd.PreprocessOptions({"--unknown-option"}, option_formats, &options, |
| 145 | &ordered_options, nullptr)); |
| 146 | // no option value |
| 147 | ASSERT_FALSE( |
| 148 | cmd.PreprocessOptions({"--str-option"}, option_formats, &options, &ordered_options, nullptr)); |
| 149 | // wrong option value format |
| 150 | ASSERT_FALSE(cmd.PreprocessOptions({"--uint-option", "-2"}, option_formats, &options, |
| 151 | &ordered_options, nullptr)); |
| 152 | ASSERT_FALSE(cmd.PreprocessOptions({"--double-option", "str"}, option_formats, &options, |
| 153 | &ordered_options, nullptr)); |
| 154 | // unexpected non_option_args |
| 155 | ASSERT_FALSE(cmd.PreprocessOptions({"non_option_args"}, option_formats, &options, |
| 156 | &ordered_options, nullptr)); |
| 157 | } |
| 158 | |
| 159 | TEST(command, OptionValueMap) { |
| 160 | OptionValue value; |
| 161 | value.uint_value = 10; |
| 162 | |
| 163 | OptionValueMap options; |
| 164 | uint64_t uint_value; |
| 165 | options.values.emplace("--uint-option", value); |
| 166 | ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 11)); |
| 167 | options.values.emplace("--uint-option", value); |
| 168 | ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 0, 9)); |
| 169 | options.values.emplace("--uint-option", value); |
| 170 | ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value, 10, 10)); |
| 171 | |
| 172 | double double_value; |
| 173 | value.double_value = 0.0; |
| 174 | options.values.emplace("--double-option", value); |
| 175 | ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, 1.0)); |
| 176 | options.values.emplace("--double-option", value); |
| 177 | ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, -2.0, -1.0)); |
| 178 | options.values.emplace("--double-option", value); |
| 179 | ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value, 0.0, 0.0)); |
| 180 | } |