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 using namespace std;
20 using namespace OHOS::HiviewDFX;
21 namespace OHOS {
22 namespace Developtools {
23 namespace HiPerf {
24 const std::string TEST_CMD_TRUE = "TEST_CMD_TRUE";
25 const std::string TEST_CMD_FALSE = "TEST_CMD_FALSE";
26 const std::string TEST_OPTION_TRUE = "-TEST_OPTION_TRUE";
27 const std::string TEST_OPTION_FALSE = "-TEST_OPTION_FALSE";
28
29 class CommandTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35
36 private:
37 std::unique_ptr<MockSubCommand> subCommandAlwaysTure =
38 std::make_unique<MockSubCommand>(TEST_CMD_TRUE);
39 std::unique_ptr<MockSubCommand> subCommandAlwaysFalse =
40 std::make_unique<MockSubCommand>(TEST_CMD_FALSE);
41 };
42
SetUpTestCase()43 void CommandTest::SetUpTestCase() {}
44
TearDownTestCase()45 void CommandTest::TearDownTestCase() {}
46
SetUp()47 void CommandTest::SetUp()
48 {
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(_)).WillRepeatedly(Return(true));
55 EXPECT_CALL(*subCommandAlwaysFalse, OnSubCommand(_)).WillRepeatedly(Return(false));
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.Level1)
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