1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "subcommand_test.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <hilog/log.h>
21
22 using namespace testing::ext;
23 using namespace std;
24 using namespace OHOS::HiviewDFX;
25 namespace OHOS {
26 namespace Developtools {
27 namespace HiPerf {
28 class HiPerfSubcommandTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
36 class SubcommandObj : public SubCommand {
37 public:
SubcommandObj()38 SubcommandObj() : SubCommand("subcomm", "test subcomm", "ut test subcomm") {}
OnSubCommand(std::vector<std::string> & args)39 bool OnSubCommand(std::vector<std::string> &args) override
40 {
41 return true;
42 }
43 };
44
SetUpTestCase()45 void HiPerfSubcommandTest::SetUpTestCase() {}
46
TearDownTestCase()47 void HiPerfSubcommandTest::TearDownTestCase() {}
48
SetUp()49 void HiPerfSubcommandTest::SetUp()
50 {
51 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
52 SubCommand::RegisterSubCommand(TEST_CMD_1, std::make_unique<SubCommandTest>(TEST_CMD_1));
53 SubCommand::RegisterSubCommand(TEST_CMD_2, std::make_unique<SubCommandTest>(TEST_CMD_2));
54 SubCommand::RegisterSubCommand(TEST_CMD_3, std::make_unique<SubCommandTest>(TEST_CMD_3));
55 }
56
TearDown()57 void HiPerfSubcommandTest::TearDown()
58 {
59 SubCommand::ClearSubCommands();
60 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
61 }
62
63 /**
64 * @tc.name: TestRegisterSubCommand
65 * @tc.desc:
66 * @tc.type: FUNC
67 */
68 HWTEST_F(HiPerfSubcommandTest, TestRegisterSubCommand, TestSize.Level1)
69 {
70 EXPECT_EQ(SubCommand::RegisterSubCommand("", std::make_unique<SubCommandTest>(TEST_CMD_1)),
71 false);
72 EXPECT_EQ(SubCommand::RegisterSubCommand("t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
73 true);
74 EXPECT_EQ(SubCommand::RegisterSubCommand("-t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
75 false);
76 EXPECT_EQ(SubCommand::RegisterSubCommand("--t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
77 false);
78 EXPECT_EQ(SubCommand::RegisterSubCommand("test", std::make_unique<SubCommandTest>(TEST_CMD_1)),
79 true);
80 EXPECT_EQ(SubCommand::RegisterSubCommand("test", std::make_unique<SubCommandTest>(TEST_CMD_1)),
81 false);
82 }
83
84 /**
85 * @tc.name: TestGetSubCommands
86 * @tc.desc: also test SubCommand::ClearSubCommands()
87 * @tc.type: FUNC
88 */
89 HWTEST_F(HiPerfSubcommandTest, TestGetSubCommands, TestSize.Level1)
90 {
91 EXPECT_EQ(SubCommand::GetSubCommands().size(), 3u);
92 SubCommand::ClearSubCommands();
93 EXPECT_EQ(SubCommand::GetSubCommands().size(), 0u);
94 }
95
96 /**
97 * @tc.name: TestFindSubCommand
98 * @tc.desc:
99 * @tc.type: FUNC
100 */
101 HWTEST_F(HiPerfSubcommandTest, TestFindSubCommand, TestSize.Level1)
102 {
103 ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_1), nullptr);
104 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Name(), TEST_CMD_1);
105 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Brief(), TEST_BRIEF);
106 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Help(), TEST_HELP);
107
108 ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_2), nullptr);
109 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Name(), TEST_CMD_2);
110 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Brief(), TEST_BRIEF);
111 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Help(), TEST_HELP);
112
113 ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_3), nullptr);
114 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Name(), TEST_CMD_3);
115 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Brief(), TEST_BRIEF);
116 EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Help(), TEST_HELP);
117
118 EXPECT_EQ(SubCommand::FindSubCommand(TEST_NOREG_CMD), nullptr);
119 }
120
121 /**
122 * @tc.name: TestFindSubCommand
123 * @tc.desc:
124 * @tc.type: FUNC
125 */
126 HWTEST_F(HiPerfSubcommandTest, TestOnSubCommandOptionsDump, TestSize.Level1)
127 {
128 std::vector<std::string> args;
129 SubcommandObj subcomm;
130 args = {"test"};
131 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
132 args = {"--dumpoption"};
133 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
134 args = {"--dumpoption", "opt"};
135 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
136 args = {"--dumpoption", " "};
137 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
138 args = {"-dumpoption", "opt"};
139 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
140 args = {"--test"};
141 EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
142 args = {"--help"};
143 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
144 args = {"--help", "opt"};
145 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
146 args = {"--help", " "};
147 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
148 args = {"-help"};
149 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
150 args = {"-help"};
151 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
152 args = {"-help", "opt"};
153 EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
154 }
155
156 /**
157 * @tc.name: TestFindSubCommand
158 * @tc.desc:
159 * @tc.type: FUNC
160 */
161 HWTEST_F(HiPerfSubcommandTest, TestOnPreSubCommand, TestSize.Level1)
162 {
163 std::vector<std::string> args;
164 SubcommandObj subcomm;
165 EXPECT_EQ(subcomm.OnPreSubCommand(), false);
166 args = {"--help"};
167 subcomm.OnSubCommandOptions(args);
168 EXPECT_EQ(subcomm.OnPreSubCommand(), true);
169 }
170
171 /**
172 * @tc.name: TestFindSubCommand
173 * @tc.desc:
174 * @tc.type: FUNC
175 */
176 HWTEST_F(HiPerfSubcommandTest, TestClearSubCommands, TestSize.Level1)
177 {
178 SubCommand::ClearSubCommands();
179 EXPECT_EQ(SubCommand::GetSubCommands().size(), 0u);
180 }
181 } // namespace HiPerf
182 } // namespace Developtools
183 } // namespace OHOS
184