• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "command_test.h"
17 
18 using namespace testing::ext;
19 namespace OHOS {
20 namespace Developtools {
21 namespace HiPerf {
22 const std::string TEST_CMD_TRUE = "TEST_CMD_TRUE";
23 const std::string TEST_CMD_FALSE = "TEST_CMD_FALSE";
24 const std::string TEST_OPTION_TRUE = "-TEST_OPTION_TRUE";
25 const std::string TEST_OPTION_FALSE = "-TEST_OPTION_FALSE";
26 
27 class CommandTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 
34 private:
35     std::unique_ptr<MockSubCommand> subCommandAlwaysTure =
36         std::make_unique<MockSubCommand>(TEST_CMD_TRUE);
37     std::unique_ptr<MockSubCommand> subCommandAlwaysFalse =
38         std::make_unique<MockSubCommand>(TEST_CMD_FALSE);
39 };
40 
SetUpTestCase()41 void CommandTest::SetUpTestCase() {}
42 
TearDownTestCase()43 void CommandTest::TearDownTestCase() {}
44 
SetUp()45 void CommandTest::SetUp()
46 {
47     static constexpr HiperfError noError = HiperfError::NO_ERR;
48     static constexpr HiperfError optionNotSupport = HiperfError::OPTION_NOT_SUPPORT;
49     ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_TRUE, TEST_OPTION_HELP, OptionAlwaysTrue),
50               true);
51     ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_FALSE, TEST_OPTION_HELP, OptionAlwaysFalse),
52               true);
53 
54     EXPECT_CALL(*subCommandAlwaysTure, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(noError));
55     EXPECT_CALL(*subCommandAlwaysFalse, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(optionNotSupport));
56 
57     ASSERT_TRUE(SubCommand::RegisterSubCommand(subCommandAlwaysTure.get()->Name(),
58                                                std::move(subCommandAlwaysTure)));
59     ASSERT_TRUE(SubCommand::RegisterSubCommand(subCommandAlwaysFalse.get()->Name(),
60                                                std::move(subCommandAlwaysFalse)));
61 }
62 
TearDown()63 void CommandTest::TearDown()
64 {
65     SubCommand::ClearSubCommands();
66     Option::ClearMainOptions();
67 }
68 
69 /**
70  * @tc.name: TestCommandDistribution
71  * @tc.desc:
72  * @tc.type: FUNC
73  */
74 HWTEST_F(CommandTest, TestCommandDistribution, TestSize.Level0)
75 {
76     std::string args;
77 
78     args = TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE;
79     EXPECT_EQ(Command::DispatchCommand(args), false);
80 
81     args = TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_CMD_TRUE;
82     EXPECT_EQ(Command::DispatchCommand(args), true);
83 
84     args = TEST_OPTION_TRUE + " " + TEST_CMD_TRUE + " " + TEST_OPTION_TRUE;
85     EXPECT_EQ(Command::DispatchCommand(args), true);
86 
87     args = TEST_CMD_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE;
88     EXPECT_EQ(Command::DispatchCommand(args), true);
89 
90     args = TEST_CMD_TRUE + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
91     EXPECT_EQ(Command::DispatchCommand(args), true);
92 
93     args = TEST_NOREG_CMD + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
94     EXPECT_EQ(Command::DispatchCommand(args), false);
95 
96     args = TEST_NO_OPTION_CMD + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
97     EXPECT_EQ(Command::DispatchCommand(args), false);
98 
99     args = TEST_CMD_TRUE + " " + TEST_NOREG_CMD + " " + TEST_CMD_TRUE;
100     EXPECT_EQ(Command::DispatchCommand(args), true);
101 
102     args = TEST_OPTION_FALSE + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
103     EXPECT_EQ(Command::DispatchCommand(args), false);
104 
105     args = TEST_OPTION_TRUE + " " + TEST_CMD_FALSE + " " + TEST_CMD_TRUE;
106     EXPECT_EQ(Command::DispatchCommand(args), false);
107 }
108 } // namespace HiPerf
109 } // namespace Developtools
110 } // namespace OHOS
111