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_help_test.h"
17
18 #include "debug_logger.h"
19 #include "subcommand.h"
20 #include "subcommand_help.h"
21 #include "utilities.h"
22
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Developtools {
26 namespace HiPerf {
27 class SubCommandHelpTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33
34 SubCommandHelp subCommandHelp;
35 };
36
37 class SubCommandTmp : public SubCommand {
38 public:
SubCommandTmp(std::string name)39 explicit SubCommandTmp(std::string name)
40 : SubCommand(TEST_CMD_HLP, TEST_HLP_BRIEF, TEST_HLP_HELP)
41 {
42 }
43
OnSubCommand(std::vector<std::string> & args)44 HiperfError OnSubCommand(std::vector<std::string>& args) override
45 {
46 return HiperfError::NO_ERR;
47 }
48 };
49
SetUpTestCase()50 void SubCommandHelpTest::SetUpTestCase()
51 {
52 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
53 SubCommand::RegisterSubCommand("help", std::make_unique<SubCommandHelp>());
54 }
55
TearDownTestCase()56 void SubCommandHelpTest::TearDownTestCase()
57 {
58 SubCommand::ClearSubCommands();
59 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
60 }
61
SetUp()62 void SubCommandHelpTest::SetUp() {}
63
TearDown()64 void SubCommandHelpTest::TearDown() {}
65
66 /**
67 * @tc.name: TestOnSubCommand
68 * @tc.desc:
69 * @tc.type: FUNC
70 */
71 HWTEST_F(SubCommandHelpTest, TestOnSubCommand, TestSize.Level1)
72 {
73 std::vector<std::string> args;
74
75 args = {"--help"};
76 EXPECT_EQ(subCommandHelp.OnSubCommand(args), HiperfError::NO_ERR);
77 }
78
79 /**
80 * @tc.name: TestOnHelpAll
81 * @tc.desc:
82 * @tc.type: FUNC
83 */
84 HWTEST_F(SubCommandHelpTest, TestOnHelpAll, TestSize.Level1)
85 {
86 std::vector<std::string> args;
87
88 EXPECT_EQ(subCommandHelp.OnHelp(args), true);
89 }
90
91 /**
92 * @tc.name: TestOnHelpRegCmd
93 * @tc.desc:
94 * @tc.type: FUNC
95 */
96 HWTEST_F(SubCommandHelpTest, TestOnHelpRegCmd, TestSize.Level1)
97 {
98 std::vector<std::string> args;
99 SubCommand::RegisterSubCommand(TEST_CMD_HLP, std::make_unique<SubCommandTmp>(TEST_CMD_HLP));
100 args = {"TEST_CMD_HLP"};
101 EXPECT_EQ(subCommandHelp.OnHelp(args), true);
102 }
103
104 /**
105 * @tc.name: TestOnHelpUnknownCmd
106 * @tc.desc:
107 * @tc.type: FUNC
108 */
109 HWTEST_F(SubCommandHelpTest, TestOnHelpUnknownCmd, TestSize.Level1)
110 {
111 std::vector<std::string> args;
112
113 args = {"unknowcmd"};
114 EXPECT_EQ(subCommandHelp.OnHelp(args), false);
115 }
116
117 /**
118 * @tc.name: GetInstance
119 * @tc.desc:
120 * @tc.type: FUNC
121 */
122 HWTEST_F(SubCommandHelpTest, GetInstance, TestSize.Level1)
123 {
124 StdoutRecord stdoutRecord;
125 stdoutRecord.Start();
126
127 EXPECT_EQ(SubCommandHelp::GetInstance().Name(), "help");
128 }
129
130 /**
131 * @tc.name: GetInstance
132 * @tc.desc:
133 * @tc.type: FUNC
134 */
135 HWTEST_F(SubCommandHelpTest, RegisterSubCommandHelp, TestSize.Level1)
136 {
137 StdoutRecord stdoutRecord;
138 stdoutRecord.Start();
139
140 Option::ClearMainOptions();
141 ASSERT_EQ(Option::GetMainOptions().size(), 0u);
142
143 SubCommandHelp::RegisterSubCommandHelp();
144 const std::map<std::string, std::unique_ptr<Option::MainOption>>& optionMap = Option::GetMainOptions();
145 EXPECT_TRUE(optionMap.find("--help") != optionMap.end());
146 EXPECT_TRUE(optionMap.find("-h") != optionMap.end());
147 std::string help = "help";
148 EXPECT_TRUE(SubCommand::FindSubCommand(help) != nullptr);
149 }
150 } // namespace HiPerf
151 } // namespace Developtools
152 } // namespace OHOS
153