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 using namespace std;
25 using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace Developtools {
28 namespace HiPerf {
29 class SubCommandHelpTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35
36 SubCommandHelp subCommandHelp;
37 };
38
39 class SubCommandTmp : public SubCommand {
40 public:
SubCommandTmp(std::string name)41 explicit SubCommandTmp(std::string name)
42 : SubCommand(TEST_CMD_HLP, TEST_HLP_BRIEF, TEST_HLP_HELP)
43 {
44 }
45
OnSubCommand(std::vector<std::string> & args)46 bool OnSubCommand(std::vector<std::string> &args) override
47 {
48 return true;
49 }
50 };
51
SetUpTestCase()52 void SubCommandHelpTest::SetUpTestCase()
53 {
54 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
55 SubCommandHelp::RegisterSubCommandHelp();
56 }
57
TearDownTestCase()58 void SubCommandHelpTest::TearDownTestCase()
59 {
60 SubCommand::ClearSubCommands();
61 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
62 }
63
SetUp()64 void SubCommandHelpTest::SetUp() {}
65
TearDown()66 void SubCommandHelpTest::TearDown() {}
67
68 /**
69 * @tc.name: TestOnSubCommand
70 * @tc.desc:
71 * @tc.type: FUNC
72 */
73 HWTEST_F(SubCommandHelpTest, TestOnSubCommand, TestSize.Level1)
74 {
75 std::vector<std::string> args;
76
77 args = {"--help"};
78 EXPECT_EQ(subCommandHelp.OnSubCommand(args), true);
79 }
80
81 /**
82 * @tc.name: TestOnHelpAll
83 * @tc.desc:
84 * @tc.type: FUNC
85 */
86 HWTEST_F(SubCommandHelpTest, TestOnHelpAll, TestSize.Level1)
87 {
88 std::vector<std::string> args;
89
90 EXPECT_EQ(subCommandHelp.OnHelp(args), true);
91 }
92
93 /**
94 * @tc.name: TestOnHelpRegCmd
95 * @tc.desc:
96 * @tc.type: FUNC
97 */
98 HWTEST_F(SubCommandHelpTest, TestOnHelpRegCmd, TestSize.Level1)
99 {
100 std::vector<std::string> args;
101 SubCommand::RegisterSubCommand(TEST_CMD_HLP, std::make_unique<SubCommandTmp>(TEST_CMD_HLP));
102 args = {"TEST_CMD_HLP"};
103 EXPECT_EQ(subCommandHelp.OnHelp(args), true);
104 }
105
106 /**
107 * @tc.name: TestOnHelpUnknownCmd
108 * @tc.desc:
109 * @tc.type: FUNC
110 */
111 HWTEST_F(SubCommandHelpTest, TestOnHelpUnknownCmd, TestSize.Level1)
112 {
113 std::vector<std::string> args;
114
115 args = {"unknowcmd"};
116 EXPECT_EQ(subCommandHelp.OnHelp(args), false);
117 }
118 } // namespace HiPerf
119 } // namespace Developtools
120 } // namespace OHOS
121