1 /*
2 * Copyright (c) 2023 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 <cstdint>
17 #include <gtest/gtest.h>
18 #define private public
19 #define protected public
20 #include "ast/ast_module.h"
21 #include "parser/lexer.h"
22 #include "parser/parser.h"
23 #include "util/file.h"
24 #include "util/logger.h"
25 #include "util/options.h"
26 #undef protected
27 #undef private
28
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::Idl;
32
33 namespace OHOS {
34 namespace idl {
35 class OptionsUnitTest : public testing::Test {
36 public:
OptionsUnitTest()37 OptionsUnitTest() {}
~OptionsUnitTest()38 virtual ~OptionsUnitTest() {}
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void OptionsUnitTest::SetUpTestCase() {}
TearDownTestCase()46 void OptionsUnitTest::TearDownTestCase() {}
SetUp()47 void OptionsUnitTest::SetUp() {}
TearDown()48 void OptionsUnitTest::TearDown() {}
49
50 /*
51 * @tc.name: OptionsUnitTest_0100
52 * @tc.desc: test GetDoHitraceState and GetLogState in OptionsUnitTest.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0100, Function | MediumTest | Level1)
56 {
57 /**
58 * @tc.steps: step1. Initialization parameters.
59 */
60 std::string strings[] = {"-t", "-log-domainid", "-log-tag"};
61 int32_t count = sizeof(strings) / sizeof(strings[0]);
62 char **argv = new char *[count];
63 for (int32_t i = 0; i < count; i++) {
64 argv[i] = const_cast<char*>(strings[i].c_str());
65 }
66
67 /**
68 * @tc.steps: step2. Set options to call functions.
69 * @tc.expected: Can obtain the correct doHitrace, reasonAble and logState value.
70 */
71 Options options(count, argv);
72 auto doHitrace = options.DoHitraceState();
73 auto logOn = options.DoLogOn();
74 auto hitRaceTag = options.GetGenerateHitraceTag();
75 auto doMainId = options.GetDomainId();
76 auto logTag = options.GetLogTag();
77 auto attRibute = options.GetAttribute();
78 options.ShowUsage();
79 options.ShowWarning();
80 EXPECT_FALSE(doHitrace);
81 EXPECT_FALSE(logOn);
82 delete[] argv;
83 }
84
85 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0101, Function | MediumTest | Level1)
86 {
87 std::string strings[] = {"-t", "-log-domainid", "-log-tag"};
88 int32_t count = sizeof(strings) / sizeof(strings[0]);
89 char **argv = new char *[count];
90 for (int32_t i = 0; i < count; i++) {
91 argv[i] = const_cast<char*>(strings[i].c_str());
92 }
93
94 Options options(count, argv);
95 auto doHitrace = options.DoHitraceState();
96 auto logOn = options.DoLogOn();
97 options.illegalOptions_ = "show warning test";
98 options.ShowErrors();
99 options.ShowVersion();
100 EXPECT_FALSE(doHitrace);
101 EXPECT_FALSE(logOn);
102 delete[] argv;
103 }
104
105 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0102, Function | MediumTest | 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(count, argv);
117 options.ParseSub(String("-gen-cpp"), count, argv);
118 options.Parse(count, argv);
119 EXPECT_TRUE(argv != nullptr);
120 }
121
122 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0103, Function | MediumTest | Level1)
123 {
124 Logger::SetLevel(Logger::VERBOSE);
125 Logger::D("OptionsUnitTest", "idl log ut test::DEBUG");
126 Logger::E("OptionsUnitTest", "idl log ut test::ERROR");
127 Logger::V("OptionsUnitTest", "idl log ut test::VERBOSE");
128 EXPECT_EQ(Logger::level_, Logger::VERBOSE);
129 }
130
131 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0104, Function | MediumTest | Level1)
132 {
133 File file(String(), File::WRITE);
134 EXPECT_EQ(file.Skip(0), false);
135 EXPECT_EQ(file.Reset(), false);
136 EXPECT_EQ(file.WriteData(nullptr, 0), true);
137 EXPECT_EQ(file.ReadData(nullptr, 0), true);
138 int32_t count = 0;
139 void* data = &count;
140 EXPECT_EQ(file.ReadData(data, INT16_MAX), false);
141 }
142
143 HWTEST_F(OptionsUnitTest, OptionsUnitTest_0105, Function | MediumTest | Level1)
144 {
145 std::string strings[] = {"-t", "-log-domainid", "-log-tag"};
146 int32_t count = sizeof(strings) / sizeof(strings[0]);
147 char **argv = new char *[count];
148 for (int32_t i = 0; i < count; i++) {
149 argv[i] = const_cast<char*>(strings[i].c_str());
150 }
151
152 Options options(count, argv);
153 Parser* parser = new Parser(options);
154 parser->module_ = new ASTModule();
155 EXPECT_NE(parser->NameSpaceEmpty(), nullptr);
156 delete [] argv;
157 delete parser;
158 }
159 }
160 }