148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ci#include "command_test.h"
1748f512ceSopenharmony_ci
1848f512ceSopenharmony_ciusing namespace testing::ext;
1948f512ceSopenharmony_ciusing namespace std;
2048f512ceSopenharmony_ciusing namespace OHOS::HiviewDFX;
2148f512ceSopenharmony_cinamespace OHOS {
2248f512ceSopenharmony_cinamespace Developtools {
2348f512ceSopenharmony_cinamespace HiPerf {
2448f512ceSopenharmony_ciconst std::string TEST_CMD_TRUE = "TEST_CMD_TRUE";
2548f512ceSopenharmony_ciconst std::string TEST_CMD_FALSE = "TEST_CMD_FALSE";
2648f512ceSopenharmony_ciconst std::string TEST_OPTION_TRUE = "-TEST_OPTION_TRUE";
2748f512ceSopenharmony_ciconst std::string TEST_OPTION_FALSE = "-TEST_OPTION_FALSE";
2848f512ceSopenharmony_ci
2948f512ceSopenharmony_ciclass CommandTest : public testing::Test {
3048f512ceSopenharmony_cipublic:
3148f512ceSopenharmony_ci    static void SetUpTestCase(void);
3248f512ceSopenharmony_ci    static void TearDownTestCase(void);
3348f512ceSopenharmony_ci    void SetUp();
3448f512ceSopenharmony_ci    void TearDown();
3548f512ceSopenharmony_ci
3648f512ceSopenharmony_ciprivate:
3748f512ceSopenharmony_ci    std::unique_ptr<MockSubCommand> subCommandAlwaysTure =
3848f512ceSopenharmony_ci        std::make_unique<MockSubCommand>(TEST_CMD_TRUE);
3948f512ceSopenharmony_ci    std::unique_ptr<MockSubCommand> subCommandAlwaysFalse =
4048f512ceSopenharmony_ci        std::make_unique<MockSubCommand>(TEST_CMD_FALSE);
4148f512ceSopenharmony_ci};
4248f512ceSopenharmony_ci
4348f512ceSopenharmony_civoid CommandTest::SetUpTestCase() {}
4448f512ceSopenharmony_ci
4548f512ceSopenharmony_civoid CommandTest::TearDownTestCase() {}
4648f512ceSopenharmony_ci
4748f512ceSopenharmony_civoid CommandTest::SetUp()
4848f512ceSopenharmony_ci{
4948f512ceSopenharmony_ci    ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_TRUE, TEST_OPTION_HELP, OptionAlwaysTrue),
5048f512ceSopenharmony_ci              true);
5148f512ceSopenharmony_ci    ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_FALSE, TEST_OPTION_HELP, OptionAlwaysFalse),
5248f512ceSopenharmony_ci              true);
5348f512ceSopenharmony_ci
5448f512ceSopenharmony_ci    EXPECT_CALL(*subCommandAlwaysTure, OnSubCommand(_)).WillRepeatedly(Return(true));
5548f512ceSopenharmony_ci    EXPECT_CALL(*subCommandAlwaysFalse, OnSubCommand(_)).WillRepeatedly(Return(false));
5648f512ceSopenharmony_ci
5748f512ceSopenharmony_ci    ASSERT_TRUE(SubCommand::RegisterSubCommand(subCommandAlwaysTure.get()->Name(),
5848f512ceSopenharmony_ci                                               std::move(subCommandAlwaysTure)));
5948f512ceSopenharmony_ci    ASSERT_TRUE(SubCommand::RegisterSubCommand(subCommandAlwaysFalse.get()->Name(),
6048f512ceSopenharmony_ci                                               std::move(subCommandAlwaysFalse)));
6148f512ceSopenharmony_ci}
6248f512ceSopenharmony_ci
6348f512ceSopenharmony_civoid CommandTest::TearDown()
6448f512ceSopenharmony_ci{
6548f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
6648f512ceSopenharmony_ci    Option::ClearMainOptions();
6748f512ceSopenharmony_ci}
6848f512ceSopenharmony_ci
6948f512ceSopenharmony_ci/**
7048f512ceSopenharmony_ci * @tc.name: TestCommandDistribution
7148f512ceSopenharmony_ci * @tc.desc:
7248f512ceSopenharmony_ci * @tc.type: FUNC
7348f512ceSopenharmony_ci */
7448f512ceSopenharmony_ciHWTEST_F(CommandTest, TestCommandDistribution, TestSize.Level1)
7548f512ceSopenharmony_ci{
7648f512ceSopenharmony_ci    std::string args;
7748f512ceSopenharmony_ci
7848f512ceSopenharmony_ci    args = TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE;
7948f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), false);
8048f512ceSopenharmony_ci
8148f512ceSopenharmony_ci    args = TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_CMD_TRUE;
8248f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), true);
8348f512ceSopenharmony_ci
8448f512ceSopenharmony_ci    args = TEST_OPTION_TRUE + " " + TEST_CMD_TRUE + " " + TEST_OPTION_TRUE;
8548f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), true);
8648f512ceSopenharmony_ci
8748f512ceSopenharmony_ci    args = TEST_CMD_TRUE + " " + TEST_OPTION_TRUE + " " + TEST_OPTION_TRUE;
8848f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), true);
8948f512ceSopenharmony_ci
9048f512ceSopenharmony_ci    args = TEST_CMD_TRUE + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
9148f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), true);
9248f512ceSopenharmony_ci
9348f512ceSopenharmony_ci    args = TEST_NOREG_CMD + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
9448f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), false);
9548f512ceSopenharmony_ci
9648f512ceSopenharmony_ci    args = TEST_NO_OPTION_CMD + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
9748f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), false);
9848f512ceSopenharmony_ci
9948f512ceSopenharmony_ci    args = TEST_CMD_TRUE + " " + TEST_NOREG_CMD + " " + TEST_CMD_TRUE;
10048f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), true);
10148f512ceSopenharmony_ci
10248f512ceSopenharmony_ci    args = TEST_OPTION_FALSE + " " + TEST_CMD_TRUE + " " + TEST_CMD_TRUE;
10348f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), false);
10448f512ceSopenharmony_ci
10548f512ceSopenharmony_ci    args = TEST_OPTION_TRUE + " " + TEST_CMD_FALSE + " " + TEST_CMD_TRUE;
10648f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommand(args), false);
10748f512ceSopenharmony_ci}
10848f512ceSopenharmony_ci} // namespace HiPerf
10948f512ceSopenharmony_ci} // namespace Developtools
11048f512ceSopenharmony_ci} // namespace OHOS
111