1 /**
2 * Copyright (c) 2024-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 #include <cstring>
18
19 #include "libabckit/include/c/abckit.h"
20 #include "helpers/helpers.h"
21 #include "libabckit/include/c/metadata_core.h"
22 #include "libabckit/include/c/extensions/arkts/metadata_arkts.h"
23 #include "libabckit/src/metadata_inspect_impl.h"
24
25 namespace libabckit::test {
26
27 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 static auto g_implArkI = AbckitGetArktsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implArkM = AbckitGetArktsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32
33 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, cppcoreguidelines-pro-type-cstyle-cast)
34 #define DEFAULT_ANNOTATION (reinterpret_cast<AbckitCoreAnnotation *>(0xcafebabe))
35 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, cppcoreguidelines-pro-type-cstyle-cast)
36 #define DEFAULT_ANNOTATION_INTERFACE (reinterpret_cast<AbckitCoreAnnotationInterface *>(0xcafebabe))
37
38 class LibAbcKitModifyApiAnnotationsTests : public ::testing::Test {};
39
ModifyModule(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)40 static void ModifyModule(AbckitFile * /*unused*/, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
41 {
42 auto *module = g_implI->functionGetModule(method);
43 const char *str = "NewAnnotation";
44 struct AbckitArktsAnnotationInterfaceCreateParams annoIfParams {};
45 annoIfParams.name = str;
46 g_implArkM->moduleAddAnnotationInterface(g_implArkI->coreModuleToArktsModule(module), &annoIfParams);
47
48 g_implI->moduleEnumerateAnnotationInterfaces(module, module, [](AbckitCoreAnnotationInterface *annoI, void *) {
49 auto str = g_implI->annotationInterfaceGetName(annoI);
50
51 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
52 auto name = helpers::AbckitStringToString(str);
53
54 EXPECT_TRUE(name == "_ESSlotNumberAnnotation" || name == "_ESConcurrentModuleRequestsAnnotation" ||
55 name == "Anno1" || name == "Anno2" || name == "NewAnnotation");
56
57 return true;
58 });
59 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
60 }
61
62 struct AnnoIFind {
63 std::string name;
64 AbckitCoreAnnotationInterface *ai = nullptr;
65 };
66
67 struct AnnoFind {
68 std::string name;
69 AbckitCoreAnnotation *anno = nullptr;
70 };
71
72 struct AnnoElemFind {
73 std::string name;
74 AbckitCoreAnnotationElement *annoElem = nullptr;
75 };
76
77 struct AnnoFieldFind {
78 std::string name;
79 AbckitCoreAnnotationInterfaceField *fld = nullptr;
80 };
81
ModifyClassAddAnnotation(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)82 static void ModifyClassAddAnnotation(AbckitFile * /*unused*/, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
83 {
84 auto module = g_implI->functionGetModule(method);
85 const char *str = "Anno2";
86 AnnoIFind af;
87 af.name = str;
88 af.ai = nullptr;
89
90 g_implI->moduleEnumerateAnnotationInterfaces(module, &af, [](AbckitCoreAnnotationInterface *annoI, void *data) {
91 auto af1 = reinterpret_cast<AnnoIFind *>(data);
92
93 auto str = g_implI->annotationInterfaceGetName(annoI);
94
95 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
96 auto name = helpers::AbckitStringToString(str);
97
98 EXPECT_TRUE(name == "Anno1" || name == "Anno2");
99
100 if (name == af1->name) {
101 af1->ai = annoI;
102 }
103
104 return true;
105 });
106 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
107 EXPECT_TRUE(af.ai != nullptr);
108 EXPECT_TRUE(af.name == str);
109
110 auto klass = g_implI->functionGetParentClass(method);
111 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
112
113 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
114 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai);
115 auto anno = g_implArkM->classAddAnnotation(g_implArkI->coreClassToArktsClass(klass), &annoCreateParams);
116 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
117
118 auto annoI = g_implI->annotationGetInterface(g_implArkI->arktsAnnotationToCoreAnnotation(anno));
119 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
120
121 auto str1 = g_implI->annotationInterfaceGetName(annoI);
122 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
123
124 auto name1 = helpers::AbckitStringToString(str1);
125 EXPECT_TRUE(name1 == str);
126 }
127
ModifyClassRemoveAnnotation(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)128 static void ModifyClassRemoveAnnotation(AbckitFile * /*unused*/, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
129 {
130 const char *str = "Anno1";
131 AnnoFind af;
132 af.name = str;
133 af.anno = nullptr;
134
135 auto klass = g_implI->functionGetParentClass(method);
136 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
137
138 g_implI->classEnumerateAnnotations(klass, &af, [](AbckitCoreAnnotation *anno, void *data) {
139 auto af1 = reinterpret_cast<AnnoFind *>(data);
140
141 auto ai = g_implI->annotationGetInterface(anno);
142
143 auto str = g_implI->annotationInterfaceGetName(ai);
144
145 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
146 auto name = helpers::AbckitStringToString(str);
147 if (name == af1->name) {
148 af1->anno = anno;
149 }
150
151 return true;
152 });
153 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
154 EXPECT_TRUE(af.anno != nullptr);
155 EXPECT_TRUE(af.name == str);
156
157 g_implArkM->classRemoveAnnotation(g_implArkI->coreClassToArktsClass(klass),
158 g_implArkI->coreAnnotationToArktsAnnotation(af.anno));
159 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
160
161 g_implI->classEnumerateAnnotations(klass, &af, [](AbckitCoreAnnotation *anno, void *data) {
162 auto af1 = reinterpret_cast<AnnoFind *>(data);
163
164 auto ai = g_implI->annotationGetInterface(anno);
165
166 auto str = g_implI->annotationInterfaceGetName(ai);
167
168 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
169 auto name = helpers::AbckitStringToString(str);
170
171 EXPECT_TRUE(name != af1->name);
172
173 return true;
174 });
175 }
176
ModifyFunctionAddAnnotation(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)177 static void ModifyFunctionAddAnnotation(AbckitFile * /*unused*/, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
178 {
179 auto module = g_implI->functionGetModule(method);
180 const char *str = "Anno2";
181 AnnoIFind af;
182 af.name = str;
183 af.ai = nullptr;
184
185 g_implI->moduleEnumerateAnnotationInterfaces(module, &af, [](AbckitCoreAnnotationInterface *annoI, void *data) {
186 auto af1 = reinterpret_cast<AnnoIFind *>(data);
187
188 auto str = g_implI->annotationInterfaceGetName(annoI);
189
190 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
191 auto name = helpers::AbckitStringToString(str);
192
193 EXPECT_TRUE(name == "Anno1" || name == "Anno2");
194
195 if (name == af1->name) {
196 af1->ai = annoI;
197 }
198
199 return true;
200 });
201 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
202 EXPECT_TRUE(af.ai != nullptr);
203 EXPECT_TRUE(af.name == str);
204
205 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
206 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai);
207 auto anno = g_implArkM->functionAddAnnotation(g_implArkI->coreFunctionToArktsFunction(method), &annoCreateParams);
208 EXPECT_TRUE(anno != nullptr);
209 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
210
211 auto annoI = g_implI->annotationGetInterface(g_implArkI->arktsAnnotationToCoreAnnotation(anno));
212 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
213
214 auto str1 = g_implI->annotationInterfaceGetName(annoI);
215 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
216
217 auto name1 = helpers::AbckitStringToString(str1);
218 EXPECT_TRUE(name1 == str);
219 }
220
ModifyFunctionRemoveAnnotation(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)221 static void ModifyFunctionRemoveAnnotation(AbckitFile * /*unused*/, AbckitCoreFunction *method,
222 AbckitGraph * /*unused*/)
223 {
224 const char *str = "Anno1";
225 AnnoFind af;
226 af.name = str;
227 af.anno = nullptr;
228
229 g_implI->functionEnumerateAnnotations(method, &af, [](AbckitCoreAnnotation *anno, void *data) {
230 auto af1 = reinterpret_cast<AnnoFind *>(data);
231
232 auto ai = g_implI->annotationGetInterface(anno);
233
234 auto str = g_implI->annotationInterfaceGetName(ai);
235
236 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
237 auto name = helpers::AbckitStringToString(str);
238 if (name == af1->name) {
239 af1->anno = anno;
240 }
241
242 return true;
243 });
244 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
245 EXPECT_TRUE(af.anno != nullptr);
246 EXPECT_TRUE(af.name == str);
247
248 g_implArkM->functionRemoveAnnotation(g_implArkI->coreFunctionToArktsFunction(method),
249 g_implArkI->coreAnnotationToArktsAnnotation(af.anno));
250 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
251
252 g_implI->functionEnumerateAnnotations(method, &af, [](AbckitCoreAnnotation *anno, void *data) {
253 auto af1 = reinterpret_cast<AnnoFind *>(data);
254
255 auto ai = g_implI->annotationGetInterface(anno);
256
257 auto str = g_implI->annotationInterfaceGetName(ai);
258
259 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
260 auto name = helpers::AbckitStringToString(str);
261
262 EXPECT_TRUE(name != af1->name);
263
264 return true;
265 });
266 }
267
ModifyAnnotationAddAnnotationElement(AbckitFile * file,AbckitCoreFunction * method,AbckitGraph *)268 static void ModifyAnnotationAddAnnotationElement(AbckitFile *file, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
269 {
270 const char *str = "Anno1";
271 AnnoFind af;
272 af.name = str;
273 af.anno = nullptr;
274
275 g_implI->functionEnumerateAnnotations(method, &af, [](AbckitCoreAnnotation *anno, void *data) {
276 auto af1 = reinterpret_cast<AnnoFind *>(data);
277
278 auto annoI = g_implI->annotationGetInterface(anno);
279 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
280
281 auto str = g_implI->annotationInterfaceGetName(annoI);
282 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
283
284 auto name = helpers::AbckitStringToString(str);
285 EXPECT_TRUE(name == "Anno1");
286
287 if (name == af1->name) {
288 af1->anno = anno;
289 }
290
291 return true;
292 });
293
294 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
295 EXPECT_TRUE(af.anno != nullptr);
296 EXPECT_TRUE(af.name == str);
297
298 struct AbckitArktsAnnotationElementCreateParams annoElementCreateParams {};
299 const double initVal = 0.12;
300 auto value = g_implM->createValueDouble(file, initVal);
301 annoElementCreateParams.value = value;
302 annoElementCreateParams.name = "newValue";
303
304 auto annoElem = g_implArkM->annotationAddAnnotationElement(g_implArkI->coreAnnotationToArktsAnnotation(af.anno),
305 &annoElementCreateParams);
306 EXPECT_TRUE(annoElem != nullptr);
307 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
308
309 auto str1 = g_implI->annotationElementGetName(g_implArkI->arktsAnnotationElementToCoreAnnotationElement(annoElem));
310 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
311
312 auto name1 = helpers::AbckitStringToString(str1);
313
314 EXPECT_TRUE(name1 == annoElementCreateParams.name);
315
316 double val = g_implI->valueGetDouble(value);
317 EXPECT_TRUE(val == 12e-2);
318 }
319
ModifyAnnotationRemoveAnnotationElement(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)320 static void ModifyAnnotationRemoveAnnotationElement(AbckitFile * /*unused*/, AbckitCoreFunction *method,
321 AbckitGraph * /*unused*/)
322 {
323 const char *str = "Anno1";
324 AnnoFind af;
325 af.name = str;
326 af.anno = nullptr;
327
328 g_implI->functionEnumerateAnnotations(method, &af, [](AbckitCoreAnnotation *anno, void *data) {
329 auto af1 = reinterpret_cast<AnnoFind *>(data);
330
331 auto annoI = g_implI->annotationGetInterface(anno);
332 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
333
334 auto str = g_implI->annotationInterfaceGetName(annoI);
335 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
336
337 auto name = helpers::AbckitStringToString(str);
338 EXPECT_TRUE(name == "Anno1");
339
340 if (name == af1->name) {
341 af1->anno = anno;
342 }
343
344 return true;
345 });
346
347 AnnoElemFind aef;
348 aef.name = "a";
349 g_implI->annotationEnumerateElements(af.anno, &aef, [](AbckitCoreAnnotationElement *annoElem, void *data) {
350 auto aef1 = reinterpret_cast<AnnoElemFind *>(data);
351
352 auto str = g_implI->annotationElementGetName(annoElem);
353 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
354
355 auto name = helpers::AbckitStringToString(str);
356 EXPECT_TRUE(name == "a" || name == "b");
357
358 if (name == aef1->name) {
359 aef1->annoElem = annoElem;
360 }
361
362 return true;
363 });
364 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
365
366 EXPECT_TRUE(af.anno != nullptr);
367 EXPECT_TRUE(af.name == str);
368
369 g_implArkM->annotationRemoveAnnotationElement(
370 g_implArkI->coreAnnotationToArktsAnnotation(af.anno),
371 g_implArkI->coreAnnotationElementToArktsAnnotationElement(aef.annoElem));
372 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
373
374 g_implI->annotationEnumerateElements(af.anno, &aef, [](AbckitCoreAnnotationElement *annoElem, void *data) {
375 auto aef1 = reinterpret_cast<AnnoElemFind *>(data);
376
377 auto str = g_implI->annotationElementGetName(annoElem);
378 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
379
380 auto name = helpers::AbckitStringToString(str);
381 EXPECT_TRUE(name != aef1->name);
382
383 return true;
384 });
385 }
386
TestAnnotationRemoveAnnotationElement(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)387 static void TestAnnotationRemoveAnnotationElement(AbckitFile * /*unused*/, AbckitCoreFunction *method,
388 AbckitGraph * /*unused*/)
389 {
390 const char *str = "Anno1";
391 AnnoFind af;
392 af.name = str;
393 af.anno = nullptr;
394
395 g_implI->functionEnumerateAnnotations(method, &af, [](AbckitCoreAnnotation *anno, void *data) {
396 auto af1 = reinterpret_cast<AnnoFind *>(data);
397
398 auto annoI = g_implI->annotationGetInterface(anno);
399 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
400
401 auto str = g_implI->annotationInterfaceGetName(annoI);
402 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
403
404 auto name = helpers::AbckitStringToString(str);
405 EXPECT_TRUE(name == "Anno1");
406
407 if (name == af1->name) {
408 af1->anno = anno;
409 }
410
411 return true;
412 });
413
414 AnnoElemFind aef;
415 aef.name = "a";
416 g_implI->annotationEnumerateElements(af.anno, &aef, [](AbckitCoreAnnotationElement *annoElem, void *data) {
417 auto aef1 = reinterpret_cast<AnnoElemFind *>(data);
418
419 auto str = g_implI->annotationElementGetName(annoElem);
420 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
421
422 auto name = helpers::AbckitStringToString(str);
423 EXPECT_TRUE(name == "a" || name == "b");
424
425 if (name == aef1->name) {
426 aef1->annoElem = annoElem;
427 }
428
429 return true;
430 });
431 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
432
433 EXPECT_TRUE(af.anno != nullptr);
434 EXPECT_TRUE(af.name == str);
435
436 auto annoElem = g_implArkI->coreAnnotationElementToArktsAnnotationElement(aef.annoElem);
437 annoElem->core->ann = DEFAULT_ANNOTATION;
438
439 g_implArkM->annotationRemoveAnnotationElement(g_implArkI->coreAnnotationToArktsAnnotation(af.anno), annoElem);
440 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_BAD_ARGUMENT);
441 }
442
ModifyAnnotationInterfaceAddField(AbckitFile * file,AbckitCoreFunction * method,AbckitGraph *)443 static void ModifyAnnotationInterfaceAddField(AbckitFile *file, AbckitCoreFunction *method, AbckitGraph * /*unused*/)
444 {
445 auto module = g_implI->functionGetModule(method);
446 const char *str = "Anno2";
447 AnnoIFind af;
448 af.name = str;
449 af.ai = nullptr;
450
451 g_implI->moduleEnumerateAnnotationInterfaces(module, &af, [](AbckitCoreAnnotationInterface *annoI, void *data) {
452 auto af1 = reinterpret_cast<AnnoIFind *>(data);
453
454 auto str = g_implI->annotationInterfaceGetName(annoI);
455
456 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
457 auto name = helpers::AbckitStringToString(str);
458
459 EXPECT_TRUE(name == "Anno1" || name == "Anno2");
460
461 if (name == af1->name) {
462 af1->ai = annoI;
463 }
464
465 return true;
466 });
467 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
468 EXPECT_TRUE(af.ai != nullptr);
469 EXPECT_TRUE(af.name == str);
470
471 struct AbckitArktsAnnotationInterfaceFieldCreateParams annoFieldCreateParams {};
472 AbckitType *type = g_implM->createType(file, ABCKIT_TYPE_ID_U1);
473 AbckitValue *value = g_implM->createValueU1(file, false);
474 annoFieldCreateParams.name = "newField";
475 annoFieldCreateParams.type = type;
476 annoFieldCreateParams.defaultValue = value;
477 auto field = g_implArkM->annotationInterfaceAddField(
478 g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai), &annoFieldCreateParams);
479 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
480
481 auto annoI1 = g_implI->annotationInterfaceFieldGetInterface(
482 g_implArkI->arktsAnnotationInterfaceFieldToCoreAnnotationInterfaceField(field));
483 EXPECT_TRUE(annoI1 == af.ai);
484
485 auto str1 = g_implI->annotationInterfaceFieldGetName(
486 g_implArkI->arktsAnnotationInterfaceFieldToCoreAnnotationInterfaceField(field));
487 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
488
489 auto name1 = helpers::AbckitStringToString(str1);
490 EXPECT_TRUE(name1 == "newField");
491
492 auto val1 = g_implI->annotationInterfaceFieldGetDefaultValue(
493 g_implArkI->arktsAnnotationInterfaceFieldToCoreAnnotationInterfaceField(field));
494 bool value1 = g_implI->valueGetU1(val1);
495 EXPECT_TRUE(value1 == false);
496
497 g_implI->annotationInterfaceEnumerateFields(af.ai, nullptr, [](AbckitCoreAnnotationInterfaceField *fld, void *) {
498 auto str = g_implI->annotationInterfaceFieldGetName(fld);
499 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
500
501 auto name = helpers::AbckitStringToString(str);
502
503 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "newField");
504 return true;
505 });
506 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
507 }
508
ModifyAnnotationInterfaceRemoveField(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)509 static void ModifyAnnotationInterfaceRemoveField(AbckitFile * /*unused*/, AbckitCoreFunction *method,
510 AbckitGraph * /*unused*/)
511 {
512 auto module = g_implI->functionGetModule(method);
513 const char *str = "Anno2";
514 AnnoIFind af;
515 af.name = str;
516 af.ai = nullptr;
517
518 g_implI->moduleEnumerateAnnotationInterfaces(module, &af, [](AbckitCoreAnnotationInterface *annoI, void *data) {
519 auto af1 = reinterpret_cast<AnnoIFind *>(data);
520
521 auto str = g_implI->annotationInterfaceGetName(annoI);
522
523 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
524 auto name = helpers::AbckitStringToString(str);
525
526 EXPECT_TRUE(name == "Anno1" || name == "Anno2");
527
528 if (name == af1->name) {
529 af1->ai = annoI;
530 }
531
532 return true;
533 });
534 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
535 EXPECT_TRUE(af.ai != nullptr);
536 EXPECT_TRUE(af.name == str);
537
538 AnnoFieldFind aff;
539 aff.name = "d";
540 aff.fld = nullptr;
541
542 g_implI->annotationInterfaceEnumerateFields(af.ai, &aff, [](AbckitCoreAnnotationInterfaceField *fld, void *data) {
543 auto aff1 = reinterpret_cast<AnnoFieldFind *>(data);
544
545 auto str = g_implI->annotationInterfaceFieldGetName(fld);
546 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
547
548 auto name = helpers::AbckitStringToString(str);
549 if (name == aff1->name) {
550 aff1->fld = fld;
551 }
552 return true;
553 });
554
555 EXPECT_TRUE(aff.name == "d");
556 EXPECT_TRUE(aff.fld != nullptr);
557
558 g_implArkM->annotationInterfaceRemoveField(
559 g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai),
560 g_implArkI->coreAnnotationInterfaceFieldToArktsAnnotationInterfaceField(aff.fld));
561
562 g_implI->annotationInterfaceEnumerateFields(af.ai, &aff, [](AbckitCoreAnnotationInterfaceField *fld, void *) {
563 auto str = g_implI->annotationInterfaceFieldGetName(fld);
564 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
565
566 auto name = helpers::AbckitStringToString(str);
567
568 EXPECT_TRUE(name == "a" || name == "b");
569 return true;
570 });
571 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
572 }
573
TestAnnotationInterfaceRemoveField(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)574 static void TestAnnotationInterfaceRemoveField(AbckitFile * /*unused*/, AbckitCoreFunction *method,
575 AbckitGraph * /*unused*/)
576 {
577 auto module = g_implI->functionGetModule(method);
578 const char *str = "Anno2";
579 AnnoIFind af;
580 af.name = str;
581 af.ai = nullptr;
582
583 g_implI->moduleEnumerateAnnotationInterfaces(module, &af, [](AbckitCoreAnnotationInterface *annoI, void *data) {
584 auto af1 = reinterpret_cast<AnnoIFind *>(data);
585
586 auto str = g_implI->annotationInterfaceGetName(annoI);
587
588 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
589 auto name = helpers::AbckitStringToString(str);
590
591 EXPECT_TRUE(name == "Anno1" || name == "Anno2");
592
593 if (name == af1->name) {
594 af1->ai = annoI;
595 }
596
597 return true;
598 });
599 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
600 EXPECT_TRUE(af.ai != nullptr);
601 EXPECT_TRUE(af.name == str);
602
603 AnnoFieldFind aff;
604 aff.name = "d";
605 aff.fld = nullptr;
606
607 g_implI->annotationInterfaceEnumerateFields(af.ai, &aff, [](AbckitCoreAnnotationInterfaceField *fld, void *data) {
608 auto aff1 = reinterpret_cast<AnnoFieldFind *>(data);
609
610 auto str = g_implI->annotationInterfaceFieldGetName(fld);
611 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
612
613 auto name = helpers::AbckitStringToString(str);
614 if (name == aff1->name) {
615 aff1->fld = fld;
616 }
617 return true;
618 });
619
620 EXPECT_TRUE(aff.fld != nullptr);
621
622 aff.fld->name = nullptr;
623 g_implArkM->annotationInterfaceRemoveField(
624 g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai),
625 g_implArkI->coreAnnotationInterfaceFieldToArktsAnnotationInterfaceField(aff.fld));
626 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_INTERNAL_ERROR);
627
628 auto annoField = g_implArkI->coreAnnotationInterfaceFieldToArktsAnnotationInterfaceField(aff.fld);
629 annoField->core->ai = DEFAULT_ANNOTATION_INTERFACE;
630 g_implArkM->annotationInterfaceRemoveField(g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(af.ai),
631 annoField);
632 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_BAD_ARGUMENT);
633 }
634
635 // Test: test-kind=api, api=ArktsModifyApiImpl::moduleAddAnnotationInterface, abc-kind=ArkTS1, category=positive,
636 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ModuleAddAnnotationInterface)637 TEST_F(LibAbcKitModifyApiAnnotationsTests, ModuleAddAnnotationInterface)
638 {
639 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
640 ABCKIT_ABC_DIR
641 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
642 "foo", ModifyModule);
643 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
644 }
645
646 // Test: test-kind=api, api=ArktsModifyApiImpl::classAddAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassAddAnnotation)647 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassAddAnnotation)
648 {
649 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
650 ABCKIT_ABC_DIR
651 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
652 "foo", ModifyClassAddAnnotation);
653 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
654 }
655
656 // Test: test-kind=api, api=ArktsModifyApiImpl::classAddAnnotation, abc-kind=ArkTS1, category=negative, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassAddAnnotation_WrongTargets)657 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassAddAnnotation_WrongTargets)
658 {
659 // Test is disabled
660 // Annotation imports are not supported yet
661
662 AbckitFile *file = nullptr;
663 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
664
665 helpers::ModuleByNameContext mdlFinder = {nullptr, "annotations_dynamic"};
666 g_implI->fileEnumerateModules(file, &mdlFinder, helpers::ModuleByNameFinder);
667 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
668 ASSERT_NE(mdlFinder.module, nullptr);
669 auto *module = mdlFinder.module;
670
671 helpers::ClassByNameContext clsFinder = {nullptr, "A"};
672 g_implI->moduleEnumerateClasses(module, &clsFinder, helpers::ClassByNameFinder);
673 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
674 ASSERT_NE(clsFinder.klass, nullptr);
675 auto klass = clsFinder.klass;
676
677 helpers::ModuleByNameContext mdlFromFinder = {nullptr, "annotations_imports"};
678 g_implI->fileEnumerateModules(file, &mdlFromFinder, helpers::ModuleByNameFinder);
679 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
680 ASSERT_NE(mdlFromFinder.module, nullptr);
681 auto *moduleFrom = mdlFromFinder.module;
682
683 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno3"};
684 g_implI->moduleEnumerateAnnotationInterfaces(moduleFrom, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
685 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
686 ASSERT_NE(aiFinder.ai, nullptr);
687 auto ai = aiFinder.ai;
688
689 EXPECT_TRUE(ai->owningModule != klass->owningModule);
690
691 ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
692
693 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
694 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai);
695 g_implArkM->classAddAnnotation(g_implArkI->coreClassToArktsClass(klass), &annoCreateParams);
696 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
697
698 g_impl->closeFile(file);
699 }
700
701 // Test: test-kind=api, api=ArktsModifyApiImpl::classRemoveAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassRemoveAnnotation)702 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassRemoveAnnotation)
703 {
704 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
705 ABCKIT_ABC_DIR
706 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
707 "foo", ModifyClassRemoveAnnotation);
708 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
709 }
710
711 // Test: test-kind=api, api=ArktsModifyApiImpl::classRemoveAnnotation, abc-kind=ArkTS1, category=negative, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassRemoveAnnotation_WrongTargets)712 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassRemoveAnnotation_WrongTargets)
713 {
714 // Test is disabled
715 // Annotation imports are not supported yet
716
717 AbckitFile *file = nullptr;
718 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
719
720 helpers::ModuleByNameContext mdlFinder = {nullptr, "annotations_dynamic"};
721 g_implI->fileEnumerateModules(file, &mdlFinder, helpers::ModuleByNameFinder);
722 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
723 ASSERT_NE(mdlFinder.module, nullptr);
724 auto *module = mdlFinder.module;
725
726 helpers::ClassByNameContext clsFinder = {nullptr, "A"};
727 g_implI->moduleEnumerateClasses(module, &clsFinder, helpers::ClassByNameFinder);
728 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
729 ASSERT_NE(clsFinder.klass, nullptr);
730 auto klass = clsFinder.klass;
731
732 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno1"};
733 g_implI->classEnumerateAnnotations(klass, &annoFinder, helpers::AnnotationByNameFinder);
734 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
735 ASSERT_NE(annoFinder.anno, nullptr);
736 auto anno = annoFinder.anno;
737
738 EXPECT_TRUE(anno->ai->owningModule == klass->owningModule);
739
740 anno->ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
741
742 g_implArkM->classRemoveAnnotation(g_implArkI->coreClassToArktsClass(klass),
743 g_implArkI->coreAnnotationToArktsAnnotation(anno));
744 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_UNSUPPORTED);
745
746 g_impl->closeFile(file);
747 }
748
749 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassRemoveAnnotation_WrongName)750 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassRemoveAnnotation_WrongName)
751 {
752 AbckitFile *file = nullptr;
753 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
754
755 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
756 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
757 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
758 ASSERT_NE(mdFinder.module, nullptr);
759 auto *module = mdFinder.module;
760
761 helpers::ClassByNameContext clsFinder = {nullptr, "A"};
762 g_implI->moduleEnumerateClasses(module, &clsFinder, helpers::ClassByNameFinder);
763 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
764 ASSERT_NE(clsFinder.klass, nullptr);
765 auto *klass = clsFinder.klass;
766
767 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno1"};
768 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
769 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
770 ASSERT_NE(aiFinder.ai, nullptr);
771 auto *ai = aiFinder.ai;
772
773 auto *newAnno = new AbckitCoreAnnotation();
774 newAnno->ai = ai;
775 newAnno->owner = klass;
776 newAnno->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
777 auto *newArktsAnno = new AbckitArktsAnnotation {newAnno};
778
779 g_implArkM->classRemoveAnnotation(g_implArkI->coreClassToArktsClass(klass), newArktsAnno);
780 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
781
782 delete newArktsAnno;
783 delete newAnno;
784
785 g_impl->closeFile(file);
786 }
787
788 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionAddAnnotation)789 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionAddAnnotation)
790 {
791 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
792 ABCKIT_ABC_DIR
793 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
794 "foo", ModifyFunctionAddAnnotation);
795 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
796 }
797
798 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionAddAnnotation_WrongTargets)799 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionAddAnnotation_WrongTargets)
800 {
801 // Test is disabled
802 // Annotation imports are not supported yet
803
804 AbckitFile *file = nullptr;
805 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
806
807 helpers::ModuleByNameContext mdlFinder = {nullptr, "annotations_imports"};
808 g_implI->fileEnumerateModules(file, &mdlFinder, helpers::ModuleByNameFinder);
809 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
810 ASSERT_NE(mdlFinder.module, nullptr);
811 auto *module = mdlFinder.module;
812
813 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno3"};
814 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
815 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
816 ASSERT_NE(aiFinder.ai, nullptr);
817 auto ai = aiFinder.ai;
818
819 auto method = helpers::FindMethodByName(file, "foo");
820 ASSERT_NE(method, nullptr);
821
822 EXPECT_TRUE(ai->owningModule != method->owningModule);
823 ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
824
825 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
826 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai);
827 g_implArkM->functionAddAnnotation(g_implArkI->coreFunctionToArktsFunction(method), &annoCreateParams);
828 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
829
830 g_impl->closeFile(file);
831 }
832
833 // Test: test-kind=api, api=ArktsModifyApiImpl::functionRemoveAnnotation, abc-kind=ArkTS1, category=positive,
834 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionRemoveAnnotation)835 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionRemoveAnnotation)
836 {
837 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
838 ABCKIT_ABC_DIR
839 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
840 "foo", ModifyFunctionRemoveAnnotation);
841 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
842 }
843
844 // Test: test-kind=api, api=ArktsModifyApiImpl::functionRemoveAnnotation, abc-kind=ArkTS1, category=positive,
845 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionRemoveAnnotation_WrongTargets)846 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionRemoveAnnotation_WrongTargets)
847 {
848 // Test is disabled
849 // Annotation imports are not supported yet
850
851 AbckitFile *file = nullptr;
852 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
853
854 auto method = helpers::FindMethodByName(file, "bar");
855 ASSERT_NE(method, nullptr);
856
857 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno1"};
858 g_implI->functionEnumerateAnnotations(method, &annoFinder, helpers::AnnotationByNameFinder);
859 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
860 ASSERT_NE(annoFinder.anno, nullptr);
861 auto anno = annoFinder.anno;
862
863 EXPECT_TRUE(anno->ai->owningModule == method->owningModule);
864
865 anno->ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
866
867 g_implArkM->functionRemoveAnnotation(g_implArkI->coreFunctionToArktsFunction(method),
868 g_implArkI->coreAnnotationToArktsAnnotation(anno));
869 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_UNSUPPORTED);
870
871 g_impl->closeFile(file);
872 }
873
874 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationAddAnnotationElement, abc-kind=ArkTS1, category=positive,
875 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionRemoveAnnotation_WrongName)876 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionRemoveAnnotation_WrongName)
877 {
878 AbckitFile *file = nullptr;
879 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
880
881 auto *method = helpers::FindMethodByName(file, "bar");
882 ASSERT_NE(method, nullptr);
883
884 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
885 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
886 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
887 ASSERT_NE(mdFinder.module, nullptr);
888 auto *module = mdFinder.module;
889
890 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno2"};
891 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
892 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
893 ASSERT_NE(aiFinder.ai, nullptr);
894 auto *ai = aiFinder.ai;
895
896 auto *newAnno = new AbckitCoreAnnotation();
897 newAnno->ai = ai;
898 newAnno->owner = method;
899 newAnno->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
900 auto *newArktsAnno = new AbckitArktsAnnotation {newAnno};
901
902 g_implArkM->functionRemoveAnnotation(g_implArkI->coreFunctionToArktsFunction(method), newArktsAnno);
903 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
904
905 delete newArktsAnno;
906 delete newAnno;
907
908 g_impl->closeFile(file);
909 }
910
911 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationAddAnnotationElement, abc-kind=ArkTS1, category=positive,
912 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationAddAnnotationElement)913 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationAddAnnotationElement)
914 {
915 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
916 ABCKIT_ABC_DIR
917 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
918 "bar", ModifyAnnotationAddAnnotationElement);
919 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
920 }
921
922 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationRemoveAnnotationElement, abc-kind=ArkTS1, category=positive,
923 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement)924 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement)
925 {
926 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
927 ABCKIT_ABC_DIR
928 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
929 "bar", ModifyAnnotationRemoveAnnotationElement);
930 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
931 }
932
933 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationRemoveAnnotationElement, abc-kind=ArkTS1, category=positive,
934 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement_2)935 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement_2)
936 {
937 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
938 ABCKIT_ABC_DIR
939 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
940 "bar", TestAnnotationRemoveAnnotationElement);
941 }
942
943 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceAddField, abc-kind=ArkTS1, category=positive,
944 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement_WrongName)945 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement_WrongName)
946 {
947 AbckitFile *file = nullptr;
948 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
949
950 auto *method = helpers::FindMethodByName(file, "bar");
951 ASSERT_NE(method, nullptr);
952
953 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno1"};
954 g_implI->functionEnumerateAnnotations(method, &annoFinder, helpers::AnnotationByNameFinder);
955 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
956 ASSERT_NE(annoFinder.anno, nullptr);
957 auto *anno = annoFinder.anno;
958
959 auto *newAnnoElem = new AbckitCoreAnnotationElement();
960 newAnnoElem->ann = anno;
961 newAnnoElem->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
962 auto *newArktsAnnoElem = new AbckitArktsAnnotationElement {newAnnoElem};
963
964 g_implArkM->annotationRemoveAnnotationElement(g_implArkI->coreAnnotationToArktsAnnotation(anno), newArktsAnnoElem);
965 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
966
967 delete newArktsAnnoElem;
968 delete newAnnoElem;
969
970 g_impl->closeFile(file);
971 }
972
973 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceAddField, abc-kind=ArkTS1, category=positive,
974 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceAddField)975 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceAddField)
976 {
977 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
978 ABCKIT_ABC_DIR
979 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
980 "bar", ModifyAnnotationInterfaceAddField);
981 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
982 }
983
984 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=positive,
985 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField)986 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField)
987 {
988 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
989 ABCKIT_ABC_DIR
990 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
991 "bar", ModifyAnnotationInterfaceRemoveField);
992 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
993 }
994
995 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=positive,
996 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField_2)997 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField_2)
998 {
999 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
1000 ABCKIT_ABC_DIR
1001 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
1002 "bar", TestAnnotationInterfaceRemoveField);
1003 }
1004
1005 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=negative,
1006 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField_WrongName)1007 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField_WrongName)
1008 {
1009 AbckitFile *file = nullptr;
1010 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
1011
1012 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
1013 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
1014 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1015 ASSERT_NE(mdFinder.module, nullptr);
1016 auto *module = mdFinder.module;
1017
1018 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno2"};
1019 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
1020 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1021 ASSERT_NE(aiFinder.ai, nullptr);
1022 auto *ai = aiFinder.ai;
1023
1024 auto *newAIF = new AbckitCoreAnnotationInterfaceField();
1025 newAIF->ai = ai;
1026 newAIF->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
1027 auto *newArktsAIF = new AbckitArktsAnnotationInterfaceField {newAIF};
1028
1029 g_implArkM->annotationInterfaceRemoveField(g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai),
1030 newArktsAIF);
1031 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1032
1033 delete newArktsAIF;
1034 delete newAIF;
1035
1036 g_impl->closeFile(file);
1037 }
1038 } // namespace libabckit::test
1039