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 "subcommand_test.h"
1748f512ceSopenharmony_ci
1848f512ceSopenharmony_ci#include <gmock/gmock.h>
1948f512ceSopenharmony_ci#include <gtest/gtest.h>
2048f512ceSopenharmony_ci#include <hilog/log.h>
2148f512ceSopenharmony_ci
2248f512ceSopenharmony_ciusing namespace testing::ext;
2348f512ceSopenharmony_ciusing namespace std;
2448f512ceSopenharmony_ciusing namespace OHOS::HiviewDFX;
2548f512ceSopenharmony_cinamespace OHOS {
2648f512ceSopenharmony_cinamespace Developtools {
2748f512ceSopenharmony_cinamespace HiPerf {
2848f512ceSopenharmony_ciclass HiPerfSubcommandTest : public testing::Test {
2948f512ceSopenharmony_cipublic:
3048f512ceSopenharmony_ci    static void SetUpTestCase(void);
3148f512ceSopenharmony_ci    static void TearDownTestCase(void);
3248f512ceSopenharmony_ci    void SetUp();
3348f512ceSopenharmony_ci    void TearDown();
3448f512ceSopenharmony_ci};
3548f512ceSopenharmony_ci
3648f512ceSopenharmony_ciclass SubcommandObj : public SubCommand {
3748f512ceSopenharmony_cipublic:
3848f512ceSopenharmony_ci    SubcommandObj() : SubCommand("subcomm", "test subcomm", "ut test subcomm") {}
3948f512ceSopenharmony_ci    bool OnSubCommand(std::vector<std::string> &args) override
4048f512ceSopenharmony_ci    {
4148f512ceSopenharmony_ci        return true;
4248f512ceSopenharmony_ci    }
4348f512ceSopenharmony_ci};
4448f512ceSopenharmony_ci
4548f512ceSopenharmony_civoid HiPerfSubcommandTest::SetUpTestCase() {}
4648f512ceSopenharmony_ci
4748f512ceSopenharmony_civoid HiPerfSubcommandTest::TearDownTestCase() {}
4848f512ceSopenharmony_ci
4948f512ceSopenharmony_civoid HiPerfSubcommandTest::SetUp()
5048f512ceSopenharmony_ci{
5148f512ceSopenharmony_ci    ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
5248f512ceSopenharmony_ci    SubCommand::RegisterSubCommand(TEST_CMD_1, std::make_unique<SubCommandTest>(TEST_CMD_1));
5348f512ceSopenharmony_ci    SubCommand::RegisterSubCommand(TEST_CMD_2, std::make_unique<SubCommandTest>(TEST_CMD_2));
5448f512ceSopenharmony_ci    SubCommand::RegisterSubCommand(TEST_CMD_3, std::make_unique<SubCommandTest>(TEST_CMD_3));
5548f512ceSopenharmony_ci}
5648f512ceSopenharmony_ci
5748f512ceSopenharmony_civoid HiPerfSubcommandTest::TearDown()
5848f512ceSopenharmony_ci{
5948f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
6048f512ceSopenharmony_ci    ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
6148f512ceSopenharmony_ci}
6248f512ceSopenharmony_ci
6348f512ceSopenharmony_ci/**
6448f512ceSopenharmony_ci * @tc.name: TestRegisterSubCommand
6548f512ceSopenharmony_ci * @tc.desc:
6648f512ceSopenharmony_ci * @tc.type: FUNC
6748f512ceSopenharmony_ci */
6848f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestRegisterSubCommand, TestSize.Level1)
6948f512ceSopenharmony_ci{
7048f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("", std::make_unique<SubCommandTest>(TEST_CMD_1)),
7148f512ceSopenharmony_ci              false);
7248f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
7348f512ceSopenharmony_ci              true);
7448f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("-t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
7548f512ceSopenharmony_ci              false);
7648f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("--t", std::make_unique<SubCommandTest>(TEST_CMD_1)),
7748f512ceSopenharmony_ci              false);
7848f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("test", std::make_unique<SubCommandTest>(TEST_CMD_1)),
7948f512ceSopenharmony_ci              true);
8048f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::RegisterSubCommand("test", std::make_unique<SubCommandTest>(TEST_CMD_1)),
8148f512ceSopenharmony_ci              false);
8248f512ceSopenharmony_ci}
8348f512ceSopenharmony_ci
8448f512ceSopenharmony_ci/**
8548f512ceSopenharmony_ci * @tc.name: TestGetSubCommands
8648f512ceSopenharmony_ci * @tc.desc: also test SubCommand::ClearSubCommands()
8748f512ceSopenharmony_ci * @tc.type: FUNC
8848f512ceSopenharmony_ci */
8948f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestGetSubCommands, TestSize.Level1)
9048f512ceSopenharmony_ci{
9148f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::GetSubCommands().size(), 3u);
9248f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
9348f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::GetSubCommands().size(), 0u);
9448f512ceSopenharmony_ci}
9548f512ceSopenharmony_ci
9648f512ceSopenharmony_ci/**
9748f512ceSopenharmony_ci * @tc.name: TestFindSubCommand
9848f512ceSopenharmony_ci * @tc.desc:
9948f512ceSopenharmony_ci * @tc.type: FUNC
10048f512ceSopenharmony_ci */
10148f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestFindSubCommand, TestSize.Level1)
10248f512ceSopenharmony_ci{
10348f512ceSopenharmony_ci    ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_1), nullptr);
10448f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Name(), TEST_CMD_1);
10548f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Brief(), TEST_BRIEF);
10648f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_1)->Help(), TEST_HELP);
10748f512ceSopenharmony_ci
10848f512ceSopenharmony_ci    ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_2), nullptr);
10948f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Name(), TEST_CMD_2);
11048f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Brief(), TEST_BRIEF);
11148f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_2)->Help(), TEST_HELP);
11248f512ceSopenharmony_ci
11348f512ceSopenharmony_ci    ASSERT_NE(SubCommand::FindSubCommand(TEST_CMD_3), nullptr);
11448f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Name(), TEST_CMD_3);
11548f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Brief(), TEST_BRIEF);
11648f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_CMD_3)->Help(), TEST_HELP);
11748f512ceSopenharmony_ci
11848f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::FindSubCommand(TEST_NOREG_CMD), nullptr);
11948f512ceSopenharmony_ci}
12048f512ceSopenharmony_ci
12148f512ceSopenharmony_ci/**
12248f512ceSopenharmony_ci * @tc.name: TestFindSubCommand
12348f512ceSopenharmony_ci * @tc.desc:
12448f512ceSopenharmony_ci * @tc.type: FUNC
12548f512ceSopenharmony_ci */
12648f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestOnSubCommandOptionsDump, TestSize.Level1)
12748f512ceSopenharmony_ci{
12848f512ceSopenharmony_ci    std::vector<std::string> args;
12948f512ceSopenharmony_ci    SubcommandObj subcomm;
13048f512ceSopenharmony_ci    args = {"test"};
13148f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
13248f512ceSopenharmony_ci    args = {"--dumpoption"};
13348f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
13448f512ceSopenharmony_ci    args = {"--dumpoption", "opt"};
13548f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
13648f512ceSopenharmony_ci    args = {"--dumpoption", " "};
13748f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
13848f512ceSopenharmony_ci    args = {"-dumpoption", "opt"};
13948f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
14048f512ceSopenharmony_ci    args = {"--test"};
14148f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), true);
14248f512ceSopenharmony_ci    args = {"--help"};
14348f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
14448f512ceSopenharmony_ci    args = {"--help", "opt"};
14548f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
14648f512ceSopenharmony_ci    args = {"--help", " "};
14748f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
14848f512ceSopenharmony_ci    args = {"-help"};
14948f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
15048f512ceSopenharmony_ci    args = {"-help"};
15148f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
15248f512ceSopenharmony_ci    args = {"-help", "opt"};
15348f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnSubCommandOptions(args), false);
15448f512ceSopenharmony_ci}
15548f512ceSopenharmony_ci
15648f512ceSopenharmony_ci/**
15748f512ceSopenharmony_ci * @tc.name: TestFindSubCommand
15848f512ceSopenharmony_ci * @tc.desc:
15948f512ceSopenharmony_ci * @tc.type: FUNC
16048f512ceSopenharmony_ci */
16148f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestOnPreSubCommand, TestSize.Level1)
16248f512ceSopenharmony_ci{
16348f512ceSopenharmony_ci    std::vector<std::string> args;
16448f512ceSopenharmony_ci    SubcommandObj subcomm;
16548f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnPreSubCommand(), false);
16648f512ceSopenharmony_ci    args = {"--help"};
16748f512ceSopenharmony_ci    subcomm.OnSubCommandOptions(args);
16848f512ceSopenharmony_ci    EXPECT_EQ(subcomm.OnPreSubCommand(), true);
16948f512ceSopenharmony_ci}
17048f512ceSopenharmony_ci
17148f512ceSopenharmony_ci/**
17248f512ceSopenharmony_ci * @tc.name: TestFindSubCommand
17348f512ceSopenharmony_ci * @tc.desc:
17448f512ceSopenharmony_ci * @tc.type: FUNC
17548f512ceSopenharmony_ci */
17648f512ceSopenharmony_ciHWTEST_F(HiPerfSubcommandTest, TestClearSubCommands, TestSize.Level1)
17748f512ceSopenharmony_ci{
17848f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
17948f512ceSopenharmony_ci    EXPECT_EQ(SubCommand::GetSubCommands().size(), 0u);
18048f512ceSopenharmony_ci}
18148f512ceSopenharmony_ci} // namespace HiPerf
18248f512ceSopenharmony_ci} // namespace Developtools
18348f512ceSopenharmony_ci} // namespace OHOS
184