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 21using namespace testing; 22using namespace testing::ext; 23namespace OHOS { 24namespace Idl { 25 26class LexerUnitTest : public testing::Test { 27public: 28 LexerUnitTest() {} 29 30 virtual ~LexerUnitTest() {} 31 32 static void SetUpTestCase(); 33 34 static void TearDownTestCase(); 35 36 void SetUp(); 37 38 void TearDown(); 39}; 40 41void LexerUnitTest::SetUpTestCase() {} 42 43void LexerUnitTest::TearDownTestCase() {} 44 45void LexerUnitTest::SetUp() {} 46 47void 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 */ 55HWTEST_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 */ 71HWTEST_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 */ 87HWTEST_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 */ 103HWTEST_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 */ 119HWTEST_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 */ 135HWTEST_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 */ 151HWTEST_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 */ 167HWTEST_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 */ 183HWTEST_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 */ 199HWTEST_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 */ 215HWTEST_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 */ 231HWTEST_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} 240 241/* 242 * @tc.name: strToIntTest_0100 243 * @tc.desc: test str to int 244 * @tc.type: FUNC 245 * @tc.require: 246 */ 247HWTEST_F(LexerUnitTest, strToIntTest_0100, Function | MediumTest | Level1) 248{ 249 GTEST_LOG_(INFO) << "LexerUnitTest, strToIntTest_0100, TestSize.Level1"; 250 Lexer imageLexer; 251 int number; 252 { 253 const char *str = "100"; 254 bool result = imageLexer.strToInt(str, 3, number); 255 EXPECT_EQ(result, true); 256 EXPECT_EQ(number, 100); 257 } 258 259 { 260 const char *str = "0000100"; 261 bool result = imageLexer.strToInt(str, 7, number); 262 EXPECT_EQ(result, true); 263 EXPECT_EQ(number, 100); 264 } 265 266 { 267 const char *str = "0"; 268 bool result = imageLexer.strToInt(str, 1, number); 269 EXPECT_EQ(result, true); 270 EXPECT_EQ(number, 0); 271 } 272 273 { 274 const char *str = "-1"; 275 bool result = imageLexer.strToInt(str, 2, number); 276 EXPECT_EQ(result, false); 277 } 278 279 // exceed the upper limit of int 280 { 281 const char *str = "2147483647"; 282 bool result = imageLexer.strToInt(str, 10, number); 283 EXPECT_EQ(result, true); 284 EXPECT_EQ(number, 2147483647); 285 } 286 287 // exceed the upper limit of int 288 { 289 const char *str = "2147483648"; 290 bool result = imageLexer.strToInt(str, 10, number); 291 EXPECT_EQ(result, false); 292 } 293 294 { 295 const char *str = "abdc"; 296 bool result = imageLexer.strToInt(str, 4, number); 297 EXPECT_EQ(result, false); 298 } 299 300 { 301 const char *str = "0000"; 302 bool result = imageLexer.strToInt(str, 4, number); 303 EXPECT_EQ(result, true); 304 EXPECT_EQ(number, 0); 305 } 306} 307} // namespace idl 308} // namespace OHOS