• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #define private public
18 #define protected public
19 #include "parser/lexer.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace Idl {
25 
26 class LexerUnitTest : public testing::Test {
27 public:
LexerUnitTest()28     LexerUnitTest() {}
29 
~LexerUnitTest()30     virtual ~LexerUnitTest() {}
31 
32     static void SetUpTestCase();
33 
34     static void TearDownTestCase();
35 
36     void SetUp();
37 
38     void TearDown();
39 };
40 
SetUpTestCase()41 void LexerUnitTest::SetUpTestCase() {}
42 
TearDownTestCase()43 void LexerUnitTest::TearDownTestCase() {}
44 
SetUp()45 void LexerUnitTest::SetUp() {}
46 
TearDown()47 void LexerUnitTest::TearDown() {}
48 
49 /*
50  * @tc.name: OpenSourceFileTest_0100
51  * @tc.desc: test Lexer's OpenSourceFile function return false.
52  * @tc.type: FUNC
53  * @tc.require:
54  */
55 HWTEST_F(LexerUnitTest, OpenSourceFileTest_0100, Function | MediumTest | Level1)
56 {
57     GTEST_LOG_(INFO)
58         << "LexerUnitTest, OpenSourceFileTest_0100, TestSize.Level1";
59     Lexer imageLexer;
60     String filePath = "";
61     bool result = imageLexer.OpenSourceFile(filePath);
62     EXPECT_EQ(result, false);
63 }
64 
65 /*
66  * @tc.name: TokenToCharTest_0100
67  * @tc.desc: test the token in Lexer's TokenToChar function is ANGLE_BRACKETS_LEFT.
68  * @tc.type: FUNC
69  * @tc.require:
70  */
71 HWTEST_F(LexerUnitTest, TokenToCharTest_0100, Function | MediumTest | Level1)
72 {
73     GTEST_LOG_(INFO)
74         << "LexerUnitTest, TokenToCharTest_0100, TestSize.Level1";
75     Lexer imageLexer;
76     Token token = Token::ANGLE_BRACKETS_LEFT;
77     int result = imageLexer.TokenToChar(token);
78     EXPECT_EQ(result, 60);
79 }
80 
81 /*
82  * @tc.name: TokenToCharTest_0200
83  * @tc.desc: test the token in Lexer's TokenToChar function is ANGLE_BRACKETS_RIGHT.
84  * @tc.type: FUNC
85  * @tc.require:
86  */
87 HWTEST_F(LexerUnitTest, TokenToCharTest_0200, Function | MediumTest | Level1)
88 {
89     GTEST_LOG_(INFO)
90         << "LexerUnitTest, TokenToCharTest_0200, TestSize.Level1";
91     Lexer imageLexer;
92     Token token = Token::ANGLE_BRACKETS_RIGHT;
93     int result = imageLexer.TokenToChar(token);
94     EXPECT_EQ(result, 62);
95 }
96 
97 /*
98  * @tc.name: TokenToCharTest_0300
99  * @tc.desc: test the token in Lexer's TokenToChar function is BRACES_LEFT.
100  * @tc.type: FUNC
101  * @tc.require:
102  */
103 HWTEST_F(LexerUnitTest, TokenToCharTest_0300, Function | MediumTest | Level1)
104 {
105     GTEST_LOG_(INFO)
106         << "LexerUnitTest, TokenToCharTest_0300, TestSize.Level1";
107     Lexer imageLexer;
108     Token token = Token::BRACES_LEFT;
109     int result = imageLexer.TokenToChar(token);
110     EXPECT_EQ(result, 123);
111 }
112 
113 /*
114  * @tc.name: TokenToCharTest_0400
115  * @tc.desc: test the token in Lexer's TokenToChar function is BRACES_RIGHT.
116  * @tc.type: FUNC
117  * @tc.require:
118  */
119 HWTEST_F(LexerUnitTest, TokenToCharTest_0400, Function | MediumTest | Level1)
120 {
121     GTEST_LOG_(INFO)
122         << "LexerUnitTest, TokenToCharTest_0400, TestSize.Level1";
123     Lexer imageLexer;
124     Token token = Token::BRACES_RIGHT;
125     int result = imageLexer.TokenToChar(token);
126     EXPECT_EQ(result, 125);
127 }
128 
129 /*
130  * @tc.name: TokenToCharTest_0500
131  * @tc.desc: test the token in Lexer's TokenToChar function is BRACKETS_LEFT.
132  * @tc.type: FUNC
133  * @tc.require:
134  */
135 HWTEST_F(LexerUnitTest, TokenToCharTest_0500, Function | MediumTest | Level1)
136 {
137     GTEST_LOG_(INFO)
138         << "LexerUnitTest, TokenToCharTest_0500, TestSize.Level1";
139     Lexer imageLexer;
140     Token token = Token::BRACKETS_LEFT;
141     int result = imageLexer.TokenToChar(token);
142     EXPECT_EQ(result, 91);
143 }
144 
145 /*
146  * @tc.name: TokenToCharTest_0600
147  * @tc.desc: test the token in Lexer's TokenToChar function is BRACKETS_RIGHT.
148  * @tc.type: FUNC
149  * @tc.require:
150  */
151 HWTEST_F(LexerUnitTest, TokenToCharTest_0600, Function | MediumTest | Level1)
152 {
153     GTEST_LOG_(INFO)
154         << "LexerUnitTest, TokenToCharTest_0600, TestSize.Level1";
155     Lexer imageLexer;
156     Token token = Token::BRACKETS_RIGHT;
157     int result = imageLexer.TokenToChar(token);
158     EXPECT_EQ(result, 93);
159 }
160 
161 /*
162  * @tc.name: TokenToCharTest_0700
163  * @tc.desc: test the token in Lexer's TokenToChar function is COMMA.
164  * @tc.type: FUNC
165  * @tc.require:
166  */
167 HWTEST_F(LexerUnitTest, TokenToCharTest_0700, Function | MediumTest | Level1)
168 {
169     GTEST_LOG_(INFO)
170         << "LexerUnitTest, TokenToCharTest_0700, TestSize.Level1";
171     Lexer imageLexer;
172     Token token = Token::COMMA;
173     int result = imageLexer.TokenToChar(token);
174     EXPECT_EQ(result, 44);
175 }
176 
177 /*
178  * @tc.name: TokenToCharTest_0800
179  * @tc.desc: test the token in Lexer's TokenToChar function is DOT.
180  * @tc.type: FUNC
181  * @tc.require:
182  */
183 HWTEST_F(LexerUnitTest, TokenToCharTest_0800, Function | MediumTest | Level1)
184 {
185     GTEST_LOG_(INFO)
186         << "LexerUnitTest, TokenToCharTest_0800, TestSize.Level1";
187     Lexer imageLexer;
188     Token token = Token::DOT;
189     int result = imageLexer.TokenToChar(token);
190     EXPECT_EQ(result, 46);
191 }
192 
193 /*
194  * @tc.name: TokenToCharTest_0900
195  * @tc.desc: test the token in Lexer's TokenToChar function is PARENTHESES_LEFT.
196  * @tc.type: FUNC
197  * @tc.require:
198  */
199 HWTEST_F(LexerUnitTest, TokenToCharTest_0900, Function | MediumTest | Level1)
200 {
201     GTEST_LOG_(INFO)
202         << "LexerUnitTest, TokenToCharTest_0900, TestSize.Level1";
203     Lexer imageLexer;
204     Token token = Token::PARENTHESES_LEFT;
205     int result = imageLexer.TokenToChar(token);
206     EXPECT_EQ(result, 40);
207 }
208 
209 /*
210  * @tc.name: TokenToCharTest_1000
211  * @tc.desc: test the token in Lexer's TokenToChar function is PARENTHESES_RIGHT.
212  * @tc.type: FUNC
213  * @tc.require:
214  */
215 HWTEST_F(LexerUnitTest, TokenToCharTest_1000, Function | MediumTest | Level1)
216 {
217     GTEST_LOG_(INFO)
218         << "LexerUnitTest, TokenToCharTest_1000, TestSize.Level1";
219     Lexer imageLexer;
220     Token token = Token::PARENTHESES_RIGHT;
221     int result = imageLexer.TokenToChar(token);
222     EXPECT_EQ(result, 41);
223 }
224 
225 /*
226  * @tc.name: TokenToCharTest_1100
227  * @tc.desc: test the token in Lexer's TokenToChar function is other.
228  * @tc.type: FUNC
229  * @tc.require:
230  */
231 HWTEST_F(LexerUnitTest, TokenToCharTest_1100, Function | MediumTest | Level1)
232 {
233     GTEST_LOG_(INFO)
234         << "LexerUnitTest, TokenToCharTest_1100, TestSize.Level1";
235     Lexer imageLexer;
236     Token token = Token::IDENTIFIER;
237     int result = imageLexer.TokenToChar(token);
238     EXPECT_EQ(result, -1);
239     token = Token::SEMICOLON;
240     result = imageLexer.TokenToChar(token);
241     EXPECT_EQ(result, ';');
242 }
243 
244 /*
245  * @tc.name: strToIntTest_0100
246  * @tc.desc: test str to int
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(LexerUnitTest, strToIntTest_0100, Function | MediumTest | Level1)
251 {
252     GTEST_LOG_(INFO) << "LexerUnitTest, strToIntTest_0100, TestSize.Level1";
253     Lexer imageLexer;
254     int number;
255     {
256         const char *str = "100";
257         bool result = imageLexer.strToInt(str, 3, number);
258         EXPECT_EQ(result, true);
259         EXPECT_EQ(number, 100);
260     }
261 
262     {
263         const char *str = "0000100";
264         bool result = imageLexer.strToInt(str, 7, number);
265         EXPECT_EQ(result, true);
266         EXPECT_EQ(number, 100);
267     }
268 
269     {
270         const char *str = "0";
271         bool result = imageLexer.strToInt(str, 1, number);
272         EXPECT_EQ(result, true);
273         EXPECT_EQ(number, 0);
274     }
275 
276     {
277         const char *str = "-1";
278         bool result = imageLexer.strToInt(str, 2, number);
279         EXPECT_EQ(result, false);
280     }
281 
282     // exceed the upper limit of int
283     {
284         const char *str = "2147483647";
285         bool result = imageLexer.strToInt(str, 10, number);
286         EXPECT_EQ(result, true);
287         EXPECT_EQ(number, 2147483647);
288     }
289 
290     // exceed the upper limit of int
291     {
292         const char *str = "2147483648";
293         bool result = imageLexer.strToInt(str, 10, number);
294         EXPECT_EQ(result, false);
295     }
296 
297     {
298         const char *str = "abdc";
299         bool result = imageLexer.strToInt(str, 4, number);
300         EXPECT_EQ(result, false);
301     }
302 
303     {
304         const char *str = "0000";
305         bool result = imageLexer.strToInt(str, 4, number);
306         EXPECT_EQ(result, true);
307         EXPECT_EQ(number, 0);
308     }
309 }
310 
311 /*
312  * @tc.name: DumpTokenSecondTest_1100
313  * @tc.desc: test the token in Lexer's DumpTokenSecond function is other.
314  * @tc.type: FUNC
315  * @tc.require:
316  */
317 HWTEST_F(LexerUnitTest, DumpTokenSecondTest_1100, Function | MediumTest | Level1)
318 {
319     Lexer* lexer = new Lexer();
320     lexer->currentToken_ = Token::DOUBLE;
321     EXPECT_STREQ(lexer->DumpTokenSecond(), String("double"));
322     lexer->currentToken_ = Token::END_OF_FILE;
323     EXPECT_STREQ(lexer->DumpTokenSecond(), String("eof"));
324     lexer->currentToken_ = Token::FLOAT;
325     EXPECT_STREQ(lexer->DumpTokenSecond(), String("float"));
326     lexer->currentToken_ = Token::IDENTIFIER;
327     EXPECT_STREQ(lexer->DumpTokenSecond(), String());
328     lexer->currentToken_ = Token::IN;
329     EXPECT_STREQ(lexer->DumpTokenSecond(), String("in"));
330     lexer->currentToken_ = Token::INOUT;
331     EXPECT_STREQ(lexer->DumpTokenSecond(), String("inout"));
332     lexer->currentToken_ = Token::INTEGER;
333     EXPECT_STREQ(lexer->DumpTokenSecond(), String("int"));
334     lexer->currentToken_ = Token::LIST;
335     EXPECT_STREQ(lexer->DumpTokenSecond(), String("List"));
336     lexer->currentToken_ = Token::LONG;
337     EXPECT_STREQ(lexer->DumpTokenSecond(), String("long"));
338     lexer->currentToken_ = Token::MAP;
339     EXPECT_STREQ(lexer->DumpTokenSecond(), String("Map"));
340     lexer->currentToken_ = Token::ONEWAY;
341     EXPECT_STREQ(lexer->DumpTokenSecond(), String("oneway"));
342     lexer->currentToken_ = Token::OUT;
343     EXPECT_STREQ(lexer->DumpTokenSecond(), String("out"));
344     lexer->currentToken_ = Token::SEQUENCEABLE;
345     EXPECT_STREQ(lexer->DumpTokenSecond(), String("sequenceable"));
346     lexer->currentToken_ = Token::PARENTHESES_LEFT;
347     EXPECT_STREQ(lexer->DumpTokenSecond(), String("("));
348     lexer->currentToken_ = Token::PARENTHESES_RIGHT;
349     EXPECT_STREQ(lexer->DumpTokenSecond(), String(")"));
350     lexer->currentToken_ = Token::SEMICOLON;
351     EXPECT_STREQ(lexer->DumpTokenSecond(), String(";"));
352     lexer->currentToken_ = Token::SHORT;
353     EXPECT_STREQ(lexer->DumpTokenSecond(), String("short"));
354     lexer->currentToken_ = Token::STRING;
355     EXPECT_STREQ(lexer->DumpTokenSecond(), String("String"));
356     lexer->currentToken_ = Token::ANGLE_BRACKETS_RIGHT;
357     EXPECT_STREQ(lexer->DumpTokenSecond(), String("unknown token"));
358 
359     delete lexer;
360 }
361 } // namespace idl
362 } // namespace OHOS