1 /*
2 * Copyright (c) 2025 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 "gtest/gtest.h"
17
18 #include "util/file.h"
19 #include "util/logger.h"
20 #include "util/options.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::Idl;
25
26 namespace OHOS {
27 namespace idl {
28 class IdlTool2UtilOptionsTest : public testing::Test {
29 public:
IdlTool2UtilOptionsTest()30 IdlTool2UtilOptionsTest() {}
~IdlTool2UtilOptionsTest()31 virtual ~IdlTool2UtilOptionsTest() {}
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase()38 void IdlTool2UtilOptionsTest::SetUpTestCase() {}
TearDownTestCase()39 void IdlTool2UtilOptionsTest::TearDownTestCase() {}
SetUp()40 void IdlTool2UtilOptionsTest::SetUp() {}
TearDown()41 void IdlTool2UtilOptionsTest::TearDown() {}
42
43 /*
44 * @tc.name: Options_test_001
45 * @tc.desc: test DoHitraceState DoLogOn GetGenerateHitraceTag GetDomainId function.
46 * @tc.type: FUNC
47 * @tc.require:
48 */
HWTEST_F(IdlTool2UtilOptionsTest,Options_test_001,Level1)49 HWTEST_F(IdlTool2UtilOptionsTest, Options_test_001, Level1)
50 {
51 std::string strings[] = {"-t", "-log-domainid", "-log-tag"};
52 int32_t count = sizeof(strings) / sizeof(strings[0]);
53 char **argv = new char *[count];
54 for (int32_t i = 0; i < count; i++) {
55 argv[i] = const_cast<char*>(strings[i].c_str());
56 }
57
58 Options &options = Options::GetInstance();
59 options.Parse(count, argv);
60 auto doHitrace = options.DoHitraceState();
61 auto logOn = options.DoLogOn();
62 auto hitRaceTag = options.GetGenerateHitraceTag();
63 auto doMainId = options.GetDomainId();
64 auto logTag = options.GetLogTag();
65 auto attRibute = options.GetAttribute();
66 options.ShowUsage();
67 options.ShowWarning();
68 EXPECT_FALSE(doHitrace);
69 EXPECT_FALSE(logOn);
70 delete[] argv;
71 }
72
73 /*
74 * @tc.name: Options_test_002
75 * @tc.desc: test ShowErrors ShowVersion function.
76 * @tc.type: FUNC
77 * @tc.require:
78 */
HWTEST_F(IdlTool2UtilOptionsTest,Options_test_002,Level1)79 HWTEST_F(IdlTool2UtilOptionsTest, Options_test_002, Level1)
80 {
81 std::string strings[] = {"-t", "-log-domainid", "-log-tag"};
82 int32_t count = sizeof(strings) / sizeof(strings[0]);
83 char **argv = new char *[count];
84 for (int32_t i = 0; i < count; i++) {
85 argv[i] = const_cast<char*>(strings[i].c_str());
86 }
87
88 Options &options = Options::GetInstance();
89 options.Parse(count, argv);
90 auto doHitrace = options.DoHitraceState();
91 auto logOn = options.DoLogOn();
92 options.ShowErrors();
93 options.ShowVersion();
94 EXPECT_FALSE(doHitrace);
95 EXPECT_FALSE(logOn);
96 delete[] argv;
97 }
98
99 /*
100 * @tc.name: Options_test_003
101 * @tc.desc: test Parse function.
102 * @tc.type: FUNC
103 * @tc.require:
104 */
HWTEST_F(IdlTool2UtilOptionsTest,Options_test_003,Level1)105 HWTEST_F(IdlTool2UtilOptionsTest, Options_test_003, Level1)
106 {
107 std::string strings[] = {"./idl", "--help", "--version", "-c", "-dump-ast",
108 "-dump-metadata", "-s", "-gen-rust", "-gen-cpp", "-gen-ts", "-d",
109 "-log-domainid", "-log-tag", "-t", "-others"};
110 int32_t count = sizeof(strings) / sizeof(strings[0]);
111 char **argv = new char *[count];
112 for (int32_t i = 0; i < count; i++) {
113 argv[i] = const_cast<char*>(strings[i].c_str());
114 }
115
116 Options &options = Options::GetInstance();
117 options.Parse(count, argv);
118 EXPECT_TRUE(argv != nullptr);
119 delete[] argv;
120 }
121
122 /*
123 * @tc.name: Options_test_004
124 * @tc.desc: test WriteData ReadData function.
125 * @tc.type: FUNC
126 * @tc.require:
127 */
HWTEST_F(IdlTool2UtilOptionsTest,Options_test_004,Level1)128 HWTEST_F(IdlTool2UtilOptionsTest, Options_test_004, Level1)
129 {
130 Logger::SetLevel(Logger::VERBOSE);
131 Logger::D("IdlTool2UtilOptionsTest", "idl log ut test::DEBUG");
132 Logger::E("IdlTool2UtilOptionsTest", "idl log ut test::ERROR");
133 Logger::V("IdlTool2UtilOptionsTest", "idl log ut test::VERBOSE");
134
135 File file("", File::WRITE);
136 EXPECT_EQ(file.Skip(0), false);
137 EXPECT_EQ(file.Reset(), false);
138 EXPECT_EQ(file.WriteData(nullptr, 0), true);
139 EXPECT_EQ(file.ReadData(nullptr, 0), 0);
140 int32_t count = 0;
141 void* data = &count;
142 EXPECT_EQ(file.ReadData(data, INT16_MAX), false);
143 }
144 } // namespace idl
145 } // namespace OHOS