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 <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,DISABLED_ClassAddAnnotation_WrongTargets)657 TEST_F(LibAbcKitModifyApiAnnotationsTests, DISABLED_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::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno3"};
678 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
679 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
680 ASSERT_NE(aiFinder.ai, nullptr);
681 auto ai = aiFinder.ai;
682
683 EXPECT_TRUE(ai->owningModule != klass->owningModule);
684
685 ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
686
687 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
688 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai);
689 g_implArkM->classAddAnnotation(g_implArkI->coreClassToArktsClass(klass), &annoCreateParams);
690 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
691
692 g_impl->closeFile(file);
693 }
694
695 // Test: test-kind=api, api=ArktsModifyApiImpl::classRemoveAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassRemoveAnnotation)696 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassRemoveAnnotation)
697 {
698 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
699 ABCKIT_ABC_DIR
700 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
701 "foo", ModifyClassRemoveAnnotation);
702 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
703 }
704
705 // Test: test-kind=api, api=ArktsModifyApiImpl::classRemoveAnnotation, abc-kind=ArkTS1, category=negative, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,DISABLED_ClassRemoveAnnotation_WrongTargets)706 TEST_F(LibAbcKitModifyApiAnnotationsTests, DISABLED_ClassRemoveAnnotation_WrongTargets)
707 {
708 // Test is disabled
709 // Annotation imports are not supported yet
710
711 AbckitFile *file = nullptr;
712 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
713
714 helpers::ModuleByNameContext mdlFinder = {nullptr, "annotations_dynamic"};
715 g_implI->fileEnumerateModules(file, &mdlFinder, helpers::ModuleByNameFinder);
716 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
717 ASSERT_NE(mdlFinder.module, nullptr);
718 auto *module = mdlFinder.module;
719
720 helpers::ClassByNameContext clsFinder = {nullptr, "A"};
721 g_implI->moduleEnumerateClasses(module, &clsFinder, helpers::ClassByNameFinder);
722 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
723 ASSERT_NE(clsFinder.klass, nullptr);
724 auto klass = clsFinder.klass;
725
726 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno4"};
727 g_implI->classEnumerateAnnotations(klass, &annoFinder, helpers::AnnotationByNameFinder);
728 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
729 ASSERT_NE(annoFinder.anno, nullptr);
730 auto anno = annoFinder.anno;
731
732 EXPECT_TRUE(anno->ai->owningModule != klass->owningModule);
733
734 anno->ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
735
736 g_implArkM->classRemoveAnnotation(g_implArkI->coreClassToArktsClass(klass),
737 g_implArkI->coreAnnotationToArktsAnnotation(anno));
738 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
739
740 g_impl->closeFile(file);
741 }
742
743 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,ClassRemoveAnnotation_WrongName)744 TEST_F(LibAbcKitModifyApiAnnotationsTests, ClassRemoveAnnotation_WrongName)
745 {
746 AbckitFile *file = nullptr;
747 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
748
749 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
750 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
751 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
752 ASSERT_NE(mdFinder.module, nullptr);
753 auto *module = mdFinder.module;
754
755 helpers::ClassByNameContext clsFinder = {nullptr, "A"};
756 g_implI->moduleEnumerateClasses(module, &clsFinder, helpers::ClassByNameFinder);
757 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
758 ASSERT_NE(clsFinder.klass, nullptr);
759 auto *klass = clsFinder.klass;
760
761 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno1"};
762 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
763 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
764 ASSERT_NE(aiFinder.ai, nullptr);
765 auto *ai = aiFinder.ai;
766
767 auto *newAnno = new AbckitCoreAnnotation();
768 newAnno->ai = ai;
769 newAnno->owner = klass;
770 newAnno->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
771 auto *newArktsAnno = new AbckitArktsAnnotation {newAnno};
772
773 g_implArkM->classRemoveAnnotation(g_implArkI->coreClassToArktsClass(klass), newArktsAnno);
774 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
775
776 delete newArktsAnno;
777 delete newAnno;
778
779 g_impl->closeFile(file);
780 }
781
782 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionAddAnnotation)783 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionAddAnnotation)
784 {
785 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
786 ABCKIT_ABC_DIR
787 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
788 "foo", ModifyFunctionAddAnnotation);
789 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
790 }
791
792 // Test: test-kind=api, api=ArktsModifyApiImpl::functionAddAnnotation, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitModifyApiAnnotationsTests,DISABLED_FunctionAddAnnotation_WrongTargets)793 TEST_F(LibAbcKitModifyApiAnnotationsTests, DISABLED_FunctionAddAnnotation_WrongTargets)
794 {
795 // Test is disabled
796 // Annotation imports are not supported yet
797
798 AbckitFile *file = nullptr;
799 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
800
801 helpers::ModuleByNameContext mdlFinder = {nullptr, "annotations_dynamic"};
802 g_implI->fileEnumerateModules(file, &mdlFinder, helpers::ModuleByNameFinder);
803 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
804 ASSERT_NE(mdlFinder.module, nullptr);
805 auto *module = mdlFinder.module;
806
807 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno3"};
808 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
809 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
810 ASSERT_NE(aiFinder.ai, nullptr);
811 auto ai = aiFinder.ai;
812
813 auto method = helpers::FindMethodByName(file, "foo");
814 ASSERT_NE(method, nullptr);
815
816 EXPECT_TRUE(ai->owningModule != method->owningModule);
817 ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
818
819 struct AbckitArktsAnnotationCreateParams annoCreateParams {};
820 annoCreateParams.ai = g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai);
821 g_implArkM->functionAddAnnotation(g_implArkI->coreFunctionToArktsFunction(method), &annoCreateParams);
822 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
823
824 g_impl->closeFile(file);
825 }
826
827 // Test: test-kind=api, api=ArktsModifyApiImpl::functionRemoveAnnotation, abc-kind=ArkTS1, category=positive,
828 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionRemoveAnnotation)829 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionRemoveAnnotation)
830 {
831 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
832 ABCKIT_ABC_DIR
833 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
834 "foo", ModifyFunctionRemoveAnnotation);
835 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
836 }
837
838 // Test: test-kind=api, api=ArktsModifyApiImpl::functionRemoveAnnotation, abc-kind=ArkTS1, category=positive,
839 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,DISABLED_FunctionRemoveAnnotation_WrongTargets)840 TEST_F(LibAbcKitModifyApiAnnotationsTests, DISABLED_FunctionRemoveAnnotation_WrongTargets)
841 {
842 // Test is disabled
843 // Annotation imports are not supported yet
844
845 AbckitFile *file = nullptr;
846 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
847
848 auto method = helpers::FindMethodByName(file, "bar");
849 ASSERT_NE(method, nullptr);
850
851 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno4"};
852 g_implI->functionEnumerateAnnotations(method, &annoFinder, helpers::AnnotationByNameFinder);
853 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
854 ASSERT_NE(annoFinder.anno, nullptr);
855 auto anno = annoFinder.anno;
856
857 EXPECT_TRUE(anno->ai->owningModule != method->owningModule);
858
859 anno->ai->owningModule->target = ABCKIT_TARGET_ARK_TS_V2;
860
861 g_implArkM->functionRemoveAnnotation(g_implArkI->coreFunctionToArktsFunction(method),
862 g_implArkI->coreAnnotationToArktsAnnotation(anno));
863 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_WRONG_TARGET);
864
865 g_impl->closeFile(file);
866 }
867
868 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationAddAnnotationElement, abc-kind=ArkTS1, category=positive,
869 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,FunctionRemoveAnnotation_WrongName)870 TEST_F(LibAbcKitModifyApiAnnotationsTests, FunctionRemoveAnnotation_WrongName)
871 {
872 AbckitFile *file = nullptr;
873 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
874
875 auto *method = helpers::FindMethodByName(file, "bar");
876 ASSERT_NE(method, nullptr);
877
878 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
879 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
880 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
881 ASSERT_NE(mdFinder.module, nullptr);
882 auto *module = mdFinder.module;
883
884 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno2"};
885 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
886 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
887 ASSERT_NE(aiFinder.ai, nullptr);
888 auto *ai = aiFinder.ai;
889
890 auto *newAnno = new AbckitCoreAnnotation();
891 newAnno->ai = ai;
892 newAnno->owner = method;
893 newAnno->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
894 auto *newArktsAnno = new AbckitArktsAnnotation {newAnno};
895
896 g_implArkM->functionRemoveAnnotation(g_implArkI->coreFunctionToArktsFunction(method), newArktsAnno);
897 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
898
899 delete newArktsAnno;
900 delete newAnno;
901
902 g_impl->closeFile(file);
903 }
904
905 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationAddAnnotationElement, abc-kind=ArkTS1, category=positive,
906 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationAddAnnotationElement)907 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationAddAnnotationElement)
908 {
909 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
910 ABCKIT_ABC_DIR
911 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
912 "bar", ModifyAnnotationAddAnnotationElement);
913 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
914 }
915
916 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationRemoveAnnotationElement, abc-kind=ArkTS1, category=positive,
917 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement)918 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement)
919 {
920 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
921 ABCKIT_ABC_DIR
922 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
923 "bar", ModifyAnnotationRemoveAnnotationElement);
924 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
925 }
926
927 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationRemoveAnnotationElement, abc-kind=ArkTS1, category=positive,
928 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement_2)929 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement_2)
930 {
931 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
932 ABCKIT_ABC_DIR
933 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
934 "bar", TestAnnotationRemoveAnnotationElement);
935 }
936
937 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceAddField, abc-kind=ArkTS1, category=positive,
938 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationRemoveAnnotationElement_WrongName)939 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationRemoveAnnotationElement_WrongName)
940 {
941 AbckitFile *file = nullptr;
942 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
943
944 auto *method = helpers::FindMethodByName(file, "bar");
945 ASSERT_NE(method, nullptr);
946
947 helpers::AnnotationByNameContext annoFinder = {nullptr, "Anno1"};
948 g_implI->functionEnumerateAnnotations(method, &annoFinder, helpers::AnnotationByNameFinder);
949 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
950 ASSERT_NE(annoFinder.anno, nullptr);
951 auto *anno = annoFinder.anno;
952
953 auto *newAnnoElem = new AbckitCoreAnnotationElement();
954 newAnnoElem->ann = anno;
955 newAnnoElem->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
956 auto *newArktsAnnoElem = new AbckitArktsAnnotationElement {newAnnoElem};
957
958 g_implArkM->annotationRemoveAnnotationElement(g_implArkI->coreAnnotationToArktsAnnotation(anno), newArktsAnnoElem);
959 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
960
961 delete newArktsAnnoElem;
962 delete newAnnoElem;
963
964 g_impl->closeFile(file);
965 }
966
967 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceAddField, abc-kind=ArkTS1, category=positive,
968 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceAddField)969 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceAddField)
970 {
971 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
972 ABCKIT_ABC_DIR
973 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
974 "bar", ModifyAnnotationInterfaceAddField);
975 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
976 }
977
978 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=positive,
979 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField)980 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField)
981 {
982 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
983 ABCKIT_ABC_DIR
984 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
985 "bar", ModifyAnnotationInterfaceRemoveField);
986 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
987 }
988
989 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=positive,
990 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField_2)991 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField_2)
992 {
993 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc",
994 ABCKIT_ABC_DIR
995 "ut/extensions/arkts/modify_api/annotations/annotations_dynamic_modified.abc",
996 "bar", TestAnnotationInterfaceRemoveField);
997 }
998
999 // Test: test-kind=api, api=ArktsModifyApiImpl::annotationInterfaceRemoveField, abc-kind=ArkTS1, category=negative,
1000 // extension=c
TEST_F(LibAbcKitModifyApiAnnotationsTests,AnnotationInterfaceRemoveField_WrongName)1001 TEST_F(LibAbcKitModifyApiAnnotationsTests, AnnotationInterfaceRemoveField_WrongName)
1002 {
1003 AbckitFile *file = nullptr;
1004 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/extensions/arkts/modify_api/annotations/annotations_dynamic.abc", &file);
1005
1006 helpers::ModuleByNameContext mdFinder = {nullptr, "annotations_dynamic"};
1007 g_implI->fileEnumerateModules(file, &mdFinder, helpers::ModuleByNameFinder);
1008 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1009 ASSERT_NE(mdFinder.module, nullptr);
1010 auto *module = mdFinder.module;
1011
1012 helpers::AnnotationInterfaceByNameContext aiFinder = {nullptr, "Anno2"};
1013 g_implI->moduleEnumerateAnnotationInterfaces(module, &aiFinder, helpers::AnnotationInterfaceByNameFinder);
1014 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1015 ASSERT_NE(aiFinder.ai, nullptr);
1016 auto *ai = aiFinder.ai;
1017
1018 auto *newAIF = new AbckitCoreAnnotationInterfaceField();
1019 newAIF->ai = ai;
1020 newAIF->name = g_implM->createString(file, "wrongName", sizeof("wrongName"));
1021 auto *newArktsAIF = new AbckitArktsAnnotationInterfaceField {newAIF};
1022
1023 g_implArkM->annotationInterfaceRemoveField(g_implArkI->coreAnnotationInterfaceToArktsAnnotationInterface(ai),
1024 newArktsAIF);
1025 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1026
1027 delete newArktsAIF;
1028 delete newAIF;
1029
1030 g_impl->closeFile(file);
1031 }
1032 } // namespace libabckit::test
1033