1 /*
2 * Copyright (c) 2024 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 <cstdio>
17
18 #include "ecmascript/extractortool/src/source_map.h"
19 #include "ecmascript/tests/test_helper.h"
20
21 using namespace panda::ecmascript;
22 const std::string sourceMapData =
23 "{\n"
24 " \"entry/src/main/ets/pages/Index.ts\": {\n"
25 " \"version\": 3,\n"
26 " \"file\": \"Index.ets\",\n"
27 " \"sources\": [\n"
28 " \"entry/src/main/ets/pages/Index.ets\"\n"
29 " ],\n"
30 " \"names\": [],\n"
31 " \"mappings\": \";MAEO,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
32 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
33 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AAFZ,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
34 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
35 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
36 "CAAA,CAAA,CAAA,CAAA;;sDAG2B,CAAa,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
37 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;AAHxC,CAAA,CAAA,CAAA,"
38 "CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;QAGS,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;;QAAP,CAAO,"
39 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;;AAEd,CAAA,"
40 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;;"
41 "YACE,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;YAAH,CAAG,CAAA,CAAA,CAQF,CAAM,"
42 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAC,CAAM,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;gBARd,CAAG,CAAA,CAAA,CAAA,CAAA,"
43 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;;;;;YACD,CAAM,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
44 "CAAA,CAAA,CAAA,CAAA;YAAN,CAAM,CAAA,CAAA,CAAA,CAAA,CAAA,CAKL,CAAK,CAAA,CAAA,CAAA,CAAA,CAAC,CAAM,CAAA,CAAA,"
45 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;gBALb,CAAM,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;"
46 ";;;;;YACJ,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAC,CAAI,CAAA,CAAA,CAAA,CAAC,CAAO,CAAA,"
47 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;YAAjB,CAAI,CAAA,CAAA,CAAA,CACD,CAAQ,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
48 "CAAA,CAAC,CAAE,CAAA,CAAA,CAAA,CAAA;AADd,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
49 "CAAA,CAAA,CAAI,CAED,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAU,CAAC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
50 "CAAA,CAAA,CAAA,CAAU,CAAC,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;gBAF7B,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
51 "CAAA,CAAA,CAAA,CAAA;;;;QAAJ,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;QADN,CAAM,CAAA,CAAA,"
52 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;QADR,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
53 "CAAA;AASJ,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;;;;;;;;\"\n"
54 " \"entry-package-info\": \"entry|1.0.0\n"
55 " }\n"
56 "}";
57 const std::string sourceMapDataWithoutSources =
58 "{\n"
59 " \"entry/src/main/ets/pages/Index.ts\": {\n"
60 " \"version\": 3,\n"
61 " \"file\": \"Index.ets\",\n"
62 " \"sources\": [],\n"
63 " \"names\": [],\n"
64 " \"mappings\": \";MAEO,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
65 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;\"\n"
66 " \"entry-package-info\": \"entry|1.0.0\n"
67 " }\n"
68 "}";
69 const std::string sourceMapDataWithShortMappings =
70 "{\n"
71 " \"entry/src/main/ets/pages/Index.ts\": {\n"
72 " \"version\": 3,\n"
73 " \"file\": \"Index.ets\",\n"
74 " \"sources\": [\n"
75 " \"entry/src/main/ets/pages/Index.ets\"\n"
76 " ],\n"
77 " \"names\": [],\n"
78 " \"mappings\": \"\n"
79 " \"entry-package-info\": \"entry|1.0.0\n"
80 " }\n"
81 "}";
82 const std::string sourceMapDataWithInvalidMappings1 =
83 "{\n"
84 " \"entry/src/main/ets/pages/Index.ts\": {\n"
85 " \"version\": 3,\n"
86 " \"file\": \"Index.ets\",\n"
87 " \"sources\": [\n"
88 " \"entry/src/main/ets/pages/Index.ets\"\n"
89 " ],\n"
90 " \"names\": [],\n"
91 " \"mappings\": pA;\n"
92 " \"entry-package-info\": \"entry|1.0.0\n"
93 " }\n"
94 "}";
95 const std::string sourceMapDataWithInvalidMappings2 =
96 "{\n"
97 " \"entry/src/main/ets/pages/Index.ts\": {\n"
98 " \"version\": 3,\n"
99 " \"file\": \"Index.ets\",\n"
100 " \"sources\": [\n"
101 " \"entry/src/main/ets/pages/Index.ets\"\n"
102 " ],\n"
103 " \"names\": [],\n"
104 " \"mappings\": pAq;ABCD;abcd;0123;0+/-;\n"
105 " \"entry-package-info\": \"entry|1.0.0\n"
106 " }\n"
107 "}";
108 const std::string sourceMapDataWithInvalidMappings3 =
109 "{\n"
110 " \"entry/src/main/ets/pages/Index.ts\": {\n"
111 " \"version\": 3,\n"
112 " \"file\": \"Index.ets\",\n"
113 " \"sources\": [\n"
114 " \"entry/src/main/ets/pages/Index.ets\"\n"
115 " ],\n"
116 " \"names\": [],\n"
117 " \"mappings\": X;\n"
118 " \"entry-package-info\": \"entry|1.0.0\n"
119 " }\n"
120 "}";
121 const std::string sourceMapDataWithInvalidMappings4 =
122 "{\n"
123 " \"entry/src/main/ets/pages/Index.ts\": {\n"
124 " \"version\": 3,\n"
125 " \"file\": \"Index.ets\",\n"
126 " \"sources\": [\n"
127 " \"entry/src/main/ets/pages/Index.ets\"\n"
128 " ],\n"
129 " \"names\": [],\n"
130 " \"mappings\": A;\n"
131 " \"entry-package-info\": \"entry|1.0.0\n"
132 " }\n"
133 "}";
134 const std::string sourceMapDataWithInvalidMappings5 =
135 "{\n"
136 " \"entry/src/main/ets/pages/Index.ts\": {\n"
137 " \"version\": 3,\n"
138 " \"file\": \"Index.ets\",\n"
139 " \"sources\": [\n"
140 " \"entry/src/main/ets/pages/Index.ets\"\n"
141 " ],\n"
142 " \"names\": [],\n"
143 " \"mappings\": ;\n"
144 " \"entry-package-info\": \"entry|1.0.0\n"
145 " }\n"
146 "}";
147 const std::string sourceMapDataWithJsSources =
148 "{\n"
149 " \"entry/src/main/ets/pages/Index.js\": {\n"
150 " \"version\": 3,\n"
151 " \"file\": \"Index.ets\",\n"
152 " \"sources\": [\n"
153 " \"entry/src/main/ets/pages/Index.js\"\n"
154 " ],\n"
155 " \"names\": [],\n"
156 " \"mappings\": ;\n"
157 " \"entry-package-info\": \"entry|1.0.0\n"
158 " }\n"
159 "}";
160 const std::string sourceMapData1 =
161 "{\n"
162 " \"entry/src/main/ets/pages/Index.ts\": {\n"
163 " \"version\": 3,\n"
164 " \"file\": \"Index.ets\",\n"
165 " \"sources\": [\n"
166 " \"entry/src/main/ets/pages/Index.ets\"\n"
167 " ],\n"
168 " \"names\": [],\n"
169 " \"mappings\": ;\n"
170 " \"entry-package-info\": \"entry|1.0.0\n"
171 " \"package-info\": \"library|1.0.0\n"
172 " }\n"
173 "}";
174 const std::string sourceMapData2 =
175 "{\n"
176 " \"entry/src/main/ets/pages/Index.ts\": {\n"
177 " \"version\": 3,\n"
178 " \"file\": \"Index.ets\",\n"
179 " \"sources\": [\n"
180 " \"entry/src/main/ets/pages/Index.ets\"\n"
181 " ],\n"
182 " \"names\": [],\n"
183 " \"mappings\": ;\n"
184 " \"entry-package-info\": \"entry|1.0.0\n"
185 " }\n"
186 "}";
187 const std::string sourceMapData3 =
188 "{\n"
189 " \"entry/src/main/ets/pages/Index.ts\": {\n"
190 " \"version\": 3,\n"
191 " \"file\": \"Index.ets\",\n"
192 " \"sources\": [\n"
193 " \"entry/src/main/ets/pages/Index.ets\"\n"
194 " ],\n"
195 " \"names\": [],\n"
196 " \"mappings\": ;\n"
197 " \"package-info\": \"library|1.0.0\n"
198 " }\n"
199 "}";
200 const std::string sourceMapData4 =
201 "{\n"
202 " \"entry/src/main/ets/pages/Index.ts\": {\n"
203 " \"version\": 3,\n"
204 " \"file\": \"Index.ets\",\n"
205 " \"sources\": [\n"
206 " \"entry/src/main/ets/pages/Index.ets\"\n"
207 " ],\n"
208 " \"names\": [],\n"
209 " \"mappings\": \";MAEO,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,"
210 "CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;\"\n"
211 " }\n"
212 "}";
213
214 namespace panda::ecmascript {
215 class SourceMapFriend {
216 public:
Base64CharToInt(char charCode)217 uint32_t Base64CharToInt(char charCode)
218 {
219 return sourceMap.Base64CharToInt(charCode);
220 }
221
SplitSourceMap(const std::string & sourceMapData)222 void SplitSourceMap(const std::string &sourceMapData)
223 {
224 sourceMap.SplitSourceMap(sourceMapData);
225 }
226
ExtractSourceMapData(const std::string & allmappings,std::shared_ptr<SourceMapData> & curMapData)227 void ExtractSourceMapData(const std::string &allmappings, std::shared_ptr<SourceMapData> &curMapData)
228 {
229 sourceMap.ExtractSourceMapData(allmappings, curMapData);
230 }
231
Find(int32_t row,int32_t col,const SourceMapData & targetMap,bool & isReplaces)232 MappingInfo Find(int32_t row, int32_t col, const SourceMapData &targetMap, bool& isReplaces)
233 {
234 return sourceMap.Find(row, col, targetMap, isReplaces);
235 }
236
ExtractKeyInfo(const std::string & aSourceMap,std::vector<std::string> & sourceKeyInfo)237 void ExtractKeyInfo(const std::string &aSourceMap, std::vector<std::string> &sourceKeyInfo)
238 {
239 sourceMap.ExtractKeyInfo(aSourceMap, sourceKeyInfo);
240 }
241
GetPosInfo(const std::string & temp,int32_t start,std::string & line,std::string & column)242 void GetPosInfo(const std::string &temp, int32_t start, std::string &line, std::string &column)
243 {
244 sourceMap.GetPosInfo(temp, start, line, column);
245 }
246
HandleMappings(const std::string & mapping)247 std::vector<std::string> HandleMappings(const std::string &mapping)
248 {
249 return sourceMap.HandleMappings(mapping);
250 }
251
VlqRevCode(const std::string & vStr,std::vector<int32_t> & ans)252 bool VlqRevCode(const std::string &vStr, std::vector<int32_t> &ans)
253 {
254 return sourceMap.VlqRevCode(vStr, ans);
255 }
256
TranslateUrlPositionBySourceMap(std::string & url,int & line,int & column,std::string & packageName)257 bool TranslateUrlPositionBySourceMap(std::string &url, int &line, int &column, std::string& packageName)
258 {
259 return sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName);
260 }
261
GetLineAndColumnNumbers(int & line,int & column,SourceMapData & targetMap,bool & isReplaces)262 bool GetLineAndColumnNumbers(int &line, int &column, SourceMapData &targetMap, bool& isReplaces)
263 {
264 return sourceMap.GetLineAndColumnNumbers(line, column, targetMap, isReplaces);
265 }
266
267 private:
268 SourceMap sourceMap;
269 };
270 }
271
272 namespace panda::test {
273 class SourceMapTest : public testing::Test {
274 public:
SetUpTestCase()275 static void SetUpTestCase()
276 {
277 GTEST_LOG_(INFO) << "SetUpTestCase";
278 }
279
TearDownTestCase()280 static void TearDownTestCase()
281 {
282 GTEST_LOG_(INFO) << "TearDownCase";
283 }
284
SetUp()285 void SetUp() override
286 {
287 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
288 instance->SetEnableForceGC(false);
289 }
290
TearDown()291 void TearDown() override
292 {
293 TestHelper::DestroyEcmaVMWithScope(instance, scope);
294 }
295
296 EcmaVM *instance{nullptr};
297 EcmaHandleScope *scope{nullptr};
298 JSThread *thread{nullptr};
299 };
300
StringToUint8(const std::string & str)301 uint8_t *StringToUint8(const std::string &str)
302 {
303 const auto *data = reinterpret_cast<const uint8_t *>(str.c_str());
304 return const_cast<uint8_t *>(data);
305 }
306
TestInit(const std::string & sourceMapData)307 SourceMap TestInit(const std::string &sourceMapData)
308 {
309 uint8_t *data = StringToUint8(sourceMapData);
310 size_t dataSize = sourceMapData.size();
311 SourceMap sourceMap;
312 sourceMap.Init(const_cast<uint8_t *>(data), dataSize);
313 return sourceMap;
314 }
315
HWTEST_F_L0(SourceMapTest,TranslateUrlPositionBySourceMapTest)316 HWTEST_F_L0(SourceMapTest, TranslateUrlPositionBySourceMapTest)
317 {
318 SourceMap sourceMap;
319 int line = 10, column = 5;
320 std::string url = "test.js";
321 std::string packageName;
322 // if url contains ".js" returns true
323 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
324
325 // if it can't find sources which match url after init, returns false and throw Translate failed
326 // e.g. 1. sourceMapData is valid, but url is not valid;
327 sourceMap = TestInit(sourceMapData);
328 url = "entry/src/main/ets/pages/Index1.ts";
329 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
330
331 // e.g. 2. sourceMapData is not valid
332 sourceMap = TestInit("testInvalidSourceMapData");
333 url = "entry/src/main/ets/pages/Index.ts";
334 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
335
336 sourceMap = TestInit(sourceMapDataWithoutSources);
337 url = "entry/src/main/ets/pages/Index.ts";
338 EXPECT_TRUE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
339
340 // if mappings is too short(<18), it will throw translate failed
341 sourceMap = TestInit(sourceMapDataWithShortMappings);
342 url = "entry/src/main/ets/pages/Index.ts";
343 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
344
345 sourceMap = TestInit(sourceMapData);
346 url = "entry/src/main/ets/pages/Index.ts";
347 EXPECT_TRUE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
348
349 // if sourceMapData was used twice, sourceMap should cache url/modularMap
350 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
351
352 sourceMap = TestInit(sourceMapDataWithInvalidMappings1);
353 url = "entry/src/main/ets/pages/Index.ts";
354 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
355
356 sourceMap = TestInit(sourceMapDataWithInvalidMappings2);
357 url = "entry/src/main/ets/pages/Index.ts";
358 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
359
360 sourceMap = TestInit(sourceMapDataWithInvalidMappings3);
361 url = "entry/src/main/ets/pages/Index.ts";
362 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
363
364 sourceMap = TestInit(sourceMapDataWithInvalidMappings4);
365 url = "entry/src/main/ets/pages/Index.ts";
366 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
367
368 sourceMap = TestInit(sourceMapDataWithInvalidMappings5);
369 url = "entry/src/main/ets/pages/Index.ts";
370 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
371
372 sourceMap = TestInit(sourceMapData);
373 url = "entry/src/main/ets/pages/Index.ts";
374 line = 0, column = 5;
375 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
376
377 sourceMap = TestInit(sourceMapData);
378 url = "entry/src/main/ets/pages/Index.ts";
379 line = 5, column = 0;
380 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
381
382 // if sourceMapData is valid and url is end by ".js", it should return true
383 url = "entry/src/main/ets/pages/Index.js";
384 sourceMap = TestInit(sourceMapDataWithJsSources);
385 EXPECT_TRUE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
386 }
387
HWTEST_F_L0(SourceMapTest,Base64CharToIntTest)388 HWTEST_F_L0(SourceMapTest, Base64CharToIntTest)
389 {
390 SourceMapFriend sourceMapFriend;
391 EXPECT_EQ(sourceMapFriend.Base64CharToInt('A'), 0);
392 EXPECT_EQ(sourceMapFriend.Base64CharToInt('a'), 26);
393 EXPECT_EQ(sourceMapFriend.Base64CharToInt('0'), 52);
394 EXPECT_EQ(sourceMapFriend.Base64CharToInt('+'), 62);
395 EXPECT_EQ(sourceMapFriend.Base64CharToInt('/'), 63);
396 EXPECT_EQ(sourceMapFriend.Base64CharToInt('-'), 64);
397 }
398
PrintSourceMapData(const SourceMapData & data)399 void PrintSourceMapData(const SourceMapData &data)
400 {
401 std::cout << "URL: " << data.url_ << std::endl;
402 std::cout << "Now Position: (" << data.nowPos_.afterRow << ", " << data.nowPos_.afterColumn << ")" << std::endl;
403
404 std::cout << "Sources:" << std::endl;
405 for (const auto &source: data.sources_) {
406 std::cout << "- " << source << std::endl;
407 }
408
409 std::cout << "Mappings:" << std::endl;
410 for (const auto &mapping: data.mappings_) {
411 std::cout << "- " << mapping << std::endl;
412 }
413
414 std::cout << "After Positions:" << std::endl;
415 for (const auto &pos: data.afterPos_) {
416 std::cout << "- (" << pos.beforeRow << ", " << pos.beforeColumn << ", " << pos.afterRow << ", "
417 << pos.afterColumn << ")" << std::endl;
418 }
419 }
420
HWTEST_F_L0(SourceMapTest,ExtractSourceMapDataTest)421 HWTEST_F_L0(SourceMapTest, ExtractSourceMapDataTest)
422 {
423 SourceMapFriend sourceMapFriend;
424 std::string mappings = ";MAEO,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA";
425 auto sourceMapDataInstance = std::make_shared<SourceMapData>();
426 std::shared_ptr<SourceMapData> &curMapData = sourceMapDataInstance;
427
428 sourceMapFriend.ExtractSourceMapData(mappings, curMapData);
429 SourceMapData &data = *curMapData;
430 EXPECT_EQ(data.afterPos_.size(), 13);
431 }
432
HWTEST_F_L0(SourceMapTest,FindTest)433 HWTEST_F_L0(SourceMapTest, FindTest)
434 {
435 // Find Full Covered
436 SourceMapFriend sourceMapFriend;
437 SourceMapData targetMap;
438 MappingInfo mappingInfo;
439 bool isReplaces = true;
440
441 mappingInfo = sourceMapFriend.Find(0, 1, targetMap, isReplaces);
442 EXPECT_EQ(mappingInfo.row, 0);
443 EXPECT_EQ(mappingInfo.col, 0);
444 EXPECT_TRUE(isReplaces);
445 mappingInfo = sourceMapFriend.Find(1, 1, targetMap, isReplaces);
446 EXPECT_EQ(mappingInfo.row, 0);
447 EXPECT_EQ(mappingInfo.col, 0);
448 EXPECT_TRUE(isReplaces);
449
450 std::vector<SourceMapInfo> afterPos;
451 SourceMapInfo info;
452 info.beforeRow = 1;
453 info.beforeColumn = 1;
454 info.afterRow = 2;
455 info.afterColumn = 2;
456 info.sourcesVal = 1;
457 info.namesVal = 1;
458 afterPos.push_back(info);
459 targetMap.afterPos_ = afterPos;
460 mappingInfo = sourceMapFriend.Find(3, 3, targetMap, isReplaces);
461 EXPECT_EQ(mappingInfo.row, 2);
462 EXPECT_EQ(mappingInfo.col, 2);
463 EXPECT_TRUE(isReplaces);
464
465 mappingInfo = sourceMapFriend.Find(3, 2, targetMap, isReplaces);
466 EXPECT_EQ(mappingInfo.row, 2);
467 EXPECT_EQ(mappingInfo.col, 2);
468 EXPECT_TRUE(isReplaces);
469
470 mappingInfo = sourceMapFriend.Find(2, 2, targetMap, isReplaces);
471 EXPECT_EQ(mappingInfo.row, 2);
472 EXPECT_EQ(mappingInfo.col, 2);
473 EXPECT_TRUE(isReplaces);
474 }
475
HWTEST_F_L0(SourceMapTest,ExtractKeyInfoTest)476 HWTEST_F_L0(SourceMapTest, ExtractKeyInfoTest)
477 {
478 // ExtractKeyInfo Full Covered
479 SourceMapFriend sourceMapFriend;
480 std::string sourceMap = R"({"key1": "value1", "key2": "value\"2"})";
481 std::vector<std::string> sourceKey;
482 std::vector<std::string> &sourceKeyInfo = sourceKey;
483 sourceMapFriend.ExtractKeyInfo(sourceMap, sourceKeyInfo);
484 EXPECT_EQ(sourceKeyInfo[0], "key1");
485 EXPECT_EQ(sourceKeyInfo[1], "value1");
486 EXPECT_EQ(sourceKeyInfo[2], "key2");
487 EXPECT_EQ(sourceKeyInfo[3], "value\"2");
488 }
489
HWTEST_F_L0(SourceMapTest,GetPosInfoTest)490 HWTEST_F_L0(SourceMapTest, GetPosInfoTest)
491 {
492 SourceMapFriend sourceMapFriend;
493 std::string temp = "005:012:0";
494 int32_t start = 6;
495 std::string line, column;
496
497 sourceMapFriend.GetPosInfo(temp, start, line, column);
498 EXPECT_EQ(line, "05");
499 EXPECT_EQ(column, "01");
500 }
501
HWTEST_F_L0(SourceMapTest,HandleMappingsTest)502 HWTEST_F_L0(SourceMapTest, HandleMappingsTest)
503 {
504 SourceMapFriend sourceMapFriend;
505 std::string mapping = "X;Y";
506 std::vector<std::string> result = sourceMapFriend.HandleMappings(mapping);
507 ASSERT_EQ(result.size(), 3u);
508 EXPECT_EQ(result[0], "X");
509 EXPECT_EQ(result[1], ";");
510 EXPECT_EQ(result[2], "Y");
511 }
512
HWTEST_F_L0(SourceMapTest,VlqRevCodeTest)513 HWTEST_F_L0(SourceMapTest, VlqRevCodeTest)
514 {
515 SourceMapFriend sourceMapFriend;
516 std::string vStr;
517 std::vector<int32_t> ans;
518 EXPECT_TRUE(sourceMapFriend.VlqRevCode(vStr, ans));
519
520 vStr = "A";
521 EXPECT_TRUE(sourceMapFriend.VlqRevCode(vStr, ans));
522 ASSERT_EQ(ans.size(), 1u);
523
524 vStr = "A=A";
525 EXPECT_FALSE(sourceMapFriend.VlqRevCode(vStr, ans));
526
527 vStr = "X";
528 EXPECT_TRUE(sourceMapFriend.VlqRevCode(vStr, ans));
529
530 vStr = "A";
531 EXPECT_TRUE(sourceMapFriend.VlqRevCode(vStr, ans));
532 }
533
HWTEST_F_L0(SourceMapTest,GetLineAndColumnNumbersTest)534 HWTEST_F_L0(SourceMapTest, GetLineAndColumnNumbersTest)
535 {
536 // GetLineAndColumnNumbers is full covered
537 SourceMapFriend sourceMapFriend;
538 SourceMapData targetMap;
539 std::vector<SourceMapInfo> afterPos;
540 SourceMapInfo info;
541 info.beforeRow = 1;
542 info.beforeColumn = 1;
543 info.afterRow = 2;
544 info.afterColumn = 2;
545 info.sourcesVal = 1;
546 info.namesVal = 1;
547 afterPos.push_back(info);
548 targetMap.afterPos_ = afterPos;
549 bool isReplaces = true;
550
551 int line = 1;
552 int column = 1;
553 bool result = sourceMapFriend.GetLineAndColumnNumbers(line, column, targetMap, isReplaces);
554 EXPECT_TRUE(result);
555 EXPECT_EQ(line, 2);
556 EXPECT_EQ(column, 2);
557 EXPECT_TRUE(isReplaces);
558
559 line = 5;
560 column = 5;
561 result = sourceMapFriend.GetLineAndColumnNumbers(line, column, targetMap, isReplaces);
562 EXPECT_TRUE(result);
563 EXPECT_EQ(line, 5);
564 EXPECT_EQ(column, 5);
565 EXPECT_FALSE(isReplaces);
566
567 line = 99;
568 column = 99;
569 result = sourceMapFriend.GetLineAndColumnNumbers(line, column, targetMap, isReplaces);
570 EXPECT_TRUE(result);
571 EXPECT_EQ(line, 99);
572 EXPECT_EQ(column, 99);
573 EXPECT_FALSE(isReplaces);
574 }
575
HWTEST_F_L0(SourceMapTest,TranslateUrlPositionBySourceMapTest1)576 HWTEST_F_L0(SourceMapTest, TranslateUrlPositionBySourceMapTest1)
577 {
578 SourceMap sourceMap;
579 int line = 10, column = 5;
580 std::string url = "entry/src/main/ets/pages/Index.ts";
581 sourceMap = TestInit(sourceMapData1);
582 std::string packageName;
583 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
584 EXPECT_TRUE(packageName == "library");
585 }
586
HWTEST_F_L0(SourceMapTest,TranslateUrlPositionBySourceMapTest2)587 HWTEST_F_L0(SourceMapTest, TranslateUrlPositionBySourceMapTest2)
588 {
589 SourceMap sourceMap;
590 int line = 10, column = 5;
591 std::string url = "entry/src/main/ets/pages/Index.ts";
592 std::string packageName;
593 sourceMap = TestInit(sourceMapData2);
594 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
595 EXPECT_TRUE(packageName == "entry");
596 }
597
HWTEST_F_L0(SourceMapTest,TranslateUrlPositionBySourceMapTest3)598 HWTEST_F_L0(SourceMapTest, TranslateUrlPositionBySourceMapTest3)
599 {
600 SourceMap sourceMap;
601 int line = 10, column = 5;
602 std::string url = "entry/src/main/ets/pages/Index.ts";
603 std::string packageName;
604 sourceMap = TestInit(sourceMapData3);
605 EXPECT_FALSE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
606 EXPECT_TRUE(packageName == "library");
607 }
608
HWTEST_F_L0(SourceMapTest,TranslateUrlPositionBySourceMapTest4)609 HWTEST_F_L0(SourceMapTest, TranslateUrlPositionBySourceMapTest4)
610 {
611 SourceMap sourceMap;
612 int line = 10, column = 5;
613 std::string url = "entry/src/main/ets/pages/Index.ts";
614 std::string packageName;
615 sourceMap = TestInit(sourceMapData4);
616 EXPECT_TRUE(sourceMap.TranslateUrlPositionBySourceMap(url, line, column, packageName));
617 EXPECT_TRUE(packageName.empty());
618 }
619 }