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 #include <string_view>
19
20 #include "libabckit/include/c/abckit.h"
21 #include "helpers/helpers.h"
22 #include "libabckit/include/c/metadata_core.h"
23
24 namespace libabckit::test {
25
26 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29
30 class LibAbcKitInspectApiAnnotationsTests : public ::testing::Test {};
31
32 struct AnnoInfo {
33 AbckitFile *file = nullptr;
34 AbckitCoreAnnotation *anno = nullptr;
35 std::string a;
36 std::string b;
37 std::string d;
38 };
39
40 struct AnnoInterfaceInfo {
41 AbckitFile *file = nullptr;
42 AbckitCoreAnnotationInterface *annoIf = nullptr;
43 AbckitCoreAnnotation *anno = nullptr;
44 };
45
46 template <bool IS_CHECK_VALUE>
ProcessAnnotationsInner(std::string_view name,AbckitCoreAnnotationElement * annoElement,AnnoInfo * newData2)47 static void ProcessAnnotationsInner(std::string_view name, AbckitCoreAnnotationElement *annoElement, AnnoInfo *newData2)
48 {
49 if (name == "a") {
50 newData2->a = name;
51 if constexpr (IS_CHECK_VALUE) {
52 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
53 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
54
55 auto vs = g_implI->valueGetDouble(val);
56 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
57 EXPECT_TRUE(vs == 10U);
58 }
59 }
60
61 if (name == "b") {
62 newData2->b = name;
63 if constexpr (IS_CHECK_VALUE) {
64 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
65 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
66
67 auto vs = g_implI->arrayValueGetLiteralArray(val);
68 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
69 g_implI->literalArrayEnumerateElements(vs, nullptr, [](AbckitFile *, AbckitLiteral *v, void *) {
70 double value = g_implI->literalGetDouble(v);
71 EXPECT_TRUE(value == 1 || value == 2U || value == 3U);
72 return true;
73 });
74 }
75 }
76
77 if (name == "d") {
78 newData2->d = name;
79 if constexpr (IS_CHECK_VALUE) {
80 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
81 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
82
83 auto vs = g_implI->valueGetU1(val);
84 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
85 EXPECT_TRUE(vs == true);
86 }
87 }
88
89 if (name == "str") {
90 if constexpr (IS_CHECK_VALUE) {
91 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
92 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
93
94 auto str = g_implI->valueGetString(val);
95 auto vs = helpers::AbckitStringToString(str);
96 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
97 EXPECT_TRUE(vs == "DEF");
98 }
99 }
100 }
101
102 template <bool IS_CHECK_VALUE, bool IS_CHECK_ANNOTATION, bool IS_CHECK_CONTEXT>
ProcessAnnotations(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)103 static void ProcessAnnotations(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
104 {
105 AnnoInfo newData;
106 g_implI->functionEnumerateAnnotations(method, &newData, [](AbckitCoreAnnotation *anno, void *data1) {
107 auto *newData1 = reinterpret_cast<AnnoInfo *>(data1);
108 newData1->anno = anno;
109 newData1->file = g_implI->annotationGetFile(anno);
110 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
111 g_implI->annotationEnumerateElements(anno, data1, [](AbckitCoreAnnotationElement *annoElement, void *data2) {
112 auto abckitstr = g_implI->annotationElementGetName(annoElement);
113 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
114
115 auto name = helpers::AbckitStringToString(abckitstr);
116
117 auto *newData2 = reinterpret_cast<AnnoInfo *>(data2);
118
119 if constexpr (IS_CHECK_ANNOTATION) {
120 [[maybe_unused]] auto gotAnno = g_implI->annotationElementGetAnnotation(annoElement);
121 EXPECT_TRUE(newData2->anno == gotAnno);
122 }
123
124 if constexpr (IS_CHECK_CONTEXT) {
125 [[maybe_unused]] auto gotCtx = g_implI->annotationElementGetFile(annoElement);
126 EXPECT_TRUE(newData2->file == gotCtx);
127 }
128
129 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "str");
130
131 ProcessAnnotationsInner<IS_CHECK_VALUE>(name, annoElement, newData2);
132
133 return true;
134 });
135 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
136
137 return true;
138 });
139 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
140 }
141
__anon6c29206d0402(AbckitCoreAnnotationElement *annoElement, void *data2) 142 static auto g_processClassEnumCb = [](AbckitCoreAnnotationElement *annoElement, void *data2) {
143 auto abckitstr = g_implI->annotationElementGetName(annoElement);
144 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
145
146 auto name = helpers::AbckitStringToString(abckitstr);
147
148 auto *newData2 = reinterpret_cast<AnnoInfo *>(data2);
149
150 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "str");
151 if (name == "a") {
152 newData2->a = name;
153 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
154 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
155
156 auto vs = g_implI->valueGetDouble(val);
157 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
158 EXPECT_TRUE(vs == 20U);
159 }
160
161 if (name == "b") {
162 newData2->b = name;
163 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
164 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
165
166 auto vs = g_implI->arrayValueGetLiteralArray(val);
167 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
168 g_implI->literalArrayEnumerateElements(vs, nullptr, [](AbckitFile *, AbckitLiteral *v, void *) {
169 [[maybe_unused]] double value = g_implI->literalGetDouble(v);
170 EXPECT_TRUE(value == 13U || value == 10U);
171 return true;
172 });
173 }
174
175 if (name == "d") {
176 newData2->d = name;
177 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
178 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
179
180 auto vs = g_implI->valueGetU1(val);
181 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
182 EXPECT_TRUE(vs == true);
183 }
184
185 if (name == "str") {
186 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
187 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
188
189 auto str = g_implI->valueGetString(val);
190 auto vs = helpers::AbckitStringToString(str);
191 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
192 EXPECT_TRUE(vs == "ABC");
193 }
194
195 return true;
196 };
197
ProcessClass(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)198 static void ProcessClass(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
199 {
200 AnnoInfo newData;
201 auto klass = g_implI->functionGetParentClass(method);
202 g_implI->classEnumerateAnnotations(klass, &newData, [](AbckitCoreAnnotation *anno, void *data1) {
203 auto *newData1 = reinterpret_cast<AnnoInfo *>(data1);
204 newData1->anno = anno;
205 newData1->file = g_implI->annotationGetFile(anno);
206 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
207 g_implI->annotationEnumerateElements(anno, data1, g_processClassEnumCb);
208 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
209
210 return true;
211 });
212 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
213 }
214
215 template <bool IS_CHECK_NAME, bool IS_CHECK_CONTEXT>
ProcessAnnotationInterface(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)216 static void ProcessAnnotationInterface(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
217 {
218 AnnoInterfaceInfo newData;
219 g_implI->functionEnumerateAnnotations(method, &newData, [](AbckitCoreAnnotation *anno, void *data1) {
220 auto *newData1 = reinterpret_cast<AnnoInterfaceInfo *>(data1);
221 newData1->anno = anno;
222 newData1->annoIf = g_implI->annotationGetInterface(anno);
223 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
224 EXPECT_TRUE(newData1->annoIf != nullptr);
225 auto file = g_implI->annotationGetFile(anno);
226 if constexpr (IS_CHECK_CONTEXT) {
227 EXPECT_TRUE(file == g_implI->annotationInterfaceGetFile(newData1->annoIf));
228 }
229 if constexpr (IS_CHECK_NAME) {
230 auto str = g_implI->annotationInterfaceGetName(newData1->annoIf);
231 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
232 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
233 auto name = helpers::AbckitStringToString(str);
234 EXPECT_TRUE(name == "Anno");
235 }
236
237 return true;
238 });
239 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
240 }
241
242 template <bool IS_CHECK_MODULE>
ProcessModule(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)243 static void ProcessModule(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
244 {
245 auto module = g_implI->functionGetModule(method);
246 g_implI->moduleEnumerateAnnotationInterfaces(module, module, [](AbckitCoreAnnotationInterface *annoI, void *data) {
247 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
248
249 auto str = g_implI->annotationInterfaceGetName(annoI);
250
251 if constexpr (IS_CHECK_MODULE) {
252 auto curModule = g_implI->annotationInterfaceGetModule(annoI);
253 EXPECT_TRUE(curModule == data);
254 return true;
255 }
256
257 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
258 auto name = helpers::AbckitStringToString(str);
259
260 EXPECT_TRUE(name == "Anno");
261
262 return true;
263 });
264 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
265 }
266
267 template <bool IS_CHECK_TYPE>
NameIsCheckType(std::string_view name,AbckitCoreAnnotationInterfaceField * fld)268 static void NameIsCheckType(std::string_view name, AbckitCoreAnnotationInterfaceField *fld)
269 {
270 if constexpr (IS_CHECK_TYPE) {
271 auto type = g_implI->annotationInterfaceFieldGetType(fld);
272 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
273 if (name == "a") {
274 EXPECT_TRUE(ABCKIT_TYPE_ID_F64 == g_implI->typeGetTypeId(type));
275 }
276 if (name == "b") {
277 EXPECT_TRUE(ABCKIT_TYPE_ID_F64 == g_implI->typeGetTypeId(type));
278 }
279 if (name == "d") {
280 EXPECT_TRUE(ABCKIT_TYPE_ID_U1 == g_implI->typeGetTypeId(type));
281 }
282 }
283 }
284
285 template <bool IS_CHECK_CONTEXT, bool IS_CHECK_NAME, bool IS_CHECK_INTERFACE, bool IS_CHECK_TYPE, bool IS_CHECK_VALUE>
__anon6c29206d0902(AbckitCoreAnnotationInterfaceField *fld, void *data2) 286 static auto g_processAnnotationInterfaceFieldsEnumeratorCb = [](AbckitCoreAnnotationInterfaceField *fld, void *data2) {
287 auto *newData2 = reinterpret_cast<AnnoInterfaceInfo *>(data2);
288
289 if constexpr (IS_CHECK_INTERFACE) {
290 auto annoIf = g_implI->annotationInterfaceFieldGetInterface(fld);
291 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
292 EXPECT_TRUE(newData2->annoIf == annoIf);
293 }
294
295 auto file = g_implI->annotationInterfaceFieldGetFile(fld);
296 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
297
298 if constexpr (IS_CHECK_CONTEXT) {
299 EXPECT_TRUE(newData2->file == file);
300 }
301
302 auto str = g_implI->annotationInterfaceFieldGetName(fld);
303 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
304 auto name = helpers::AbckitStringToString(str);
305 if constexpr (IS_CHECK_NAME) {
306 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "str");
307 }
308
309 NameIsCheckType<IS_CHECK_TYPE>(name, fld);
310
311 if constexpr (IS_CHECK_VALUE) {
312 auto val = g_implI->annotationInterfaceFieldGetDefaultValue(fld);
313 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
314 if (name == "a") {
315 auto valueDb = g_implI->valueGetDouble(val);
316 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
317 EXPECT_TRUE(valueDb == 3U);
318 }
319 if (name == "b") {
320 auto vs = g_implI->arrayValueGetLiteralArray(val);
321 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
322 g_implI->literalArrayEnumerateElements(vs, nullptr, [](AbckitFile *, AbckitLiteral *v, void *) {
323 double value = g_implI->literalGetDouble(v);
324 EXPECT_TRUE(value == 13U || value == 9U);
325 return true;
326 });
327 }
328 if (name == "d") {
329 auto valueB = g_implI->valueGetU1(val);
330 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
331 EXPECT_TRUE(valueB == false);
332 }
333 if (name == "str") {
334 auto str = g_implI->valueGetString(val);
335 auto valueStr = helpers::AbckitStringToString(str);
336 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
337 EXPECT_TRUE(valueStr == "Hello");
338 }
339 }
340
341 return true;
342 };
343
344 template <bool IS_CHECK_CONTEXT, bool IS_CHECK_NAME, bool IS_CHECK_INTERFACE, bool IS_CHECK_TYPE, bool IS_CHECK_VALUE>
ProcessAnnotationInterfaceFields(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)345 static void ProcessAnnotationInterfaceFields(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
346 {
347 AnnoInterfaceInfo newData;
348 g_implI->functionEnumerateAnnotations(method, &newData, [](AbckitCoreAnnotation *anno, void *data1) {
349 auto *newData1 = reinterpret_cast<AnnoInterfaceInfo *>(data1);
350 newData1->anno = anno;
351 newData1->annoIf = g_implI->annotationGetInterface(anno);
352 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
353 EXPECT_TRUE(newData1->annoIf != nullptr);
354 auto file = g_implI->annotationInterfaceGetFile(newData1->annoIf);
355 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
356 newData1->file = file;
357
358 auto str = g_implI->annotationInterfaceGetName(newData1->annoIf);
359 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
360
361 auto name = helpers::AbckitStringToString(str);
362 if (name != "Anno") {
363 return true;
364 }
365
366 g_implI->annotationInterfaceEnumerateFields(
367 newData1->annoIf, newData1,
368 g_processAnnotationInterfaceFieldsEnumeratorCb<IS_CHECK_CONTEXT, IS_CHECK_INTERFACE, IS_CHECK_NAME,
369 IS_CHECK_TYPE, IS_CHECK_VALUE>);
370
371 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
372 return true;
373 });
374 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
375 }
376
ProcessAnnotationElementsInner(std::string_view name,AbckitCoreAnnotationElement * annoElement)377 static void ProcessAnnotationElementsInner(std::string_view name, AbckitCoreAnnotationElement *annoElement)
378 {
379 if (name == "a") {
380 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
381 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
382
383 auto vs = g_implI->valueGetDouble(val);
384 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
385 EXPECT_TRUE(vs == 10U);
386 }
387
388 if (name == "b") {
389 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
390 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
391
392 auto vs = g_implI->arrayValueGetLiteralArray(val);
393 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
394 g_implI->literalArrayEnumerateElements(vs, nullptr, [](AbckitFile *, AbckitLiteral *v, void *) {
395 double value = g_implI->literalGetDouble(v);
396 EXPECT_TRUE(value == 1 || value == 2U || value == 3U);
397 return true;
398 });
399 }
400
401 if (name == "d") {
402 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
403 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
404
405 auto vs = g_implI->valueGetU1(val);
406 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
407 EXPECT_TRUE(vs == true);
408 }
409
410 if (name == "str") {
411 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
412 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
413
414 auto str = g_implI->valueGetString(val);
415 auto vs = helpers::AbckitStringToString(str);
416 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
417 EXPECT_TRUE(vs == "DEF");
418 }
419 }
420
ProcessMethodAnnotations(AbckitFile *,AbckitCoreFunction * method,AbckitGraph *)421 static void ProcessMethodAnnotations(AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
422 {
423 g_implI->functionEnumerateAnnotations(method, nullptr, [](AbckitCoreAnnotation *anno, void *) {
424 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
425 g_implI->annotationEnumerateElements(anno, nullptr, [](AbckitCoreAnnotationElement *annoElement, void *) {
426 auto abckitstr = g_implI->annotationElementGetName(annoElement);
427 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
428
429 auto name = helpers::AbckitStringToString(abckitstr);
430 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "str");
431
432 ProcessAnnotationElementsInner(name, annoElement);
433
434 return true;
435 });
436 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
437
438 return true;
439 });
440 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
441 }
442
ProcessClassAnnotationsInner(std::string_view name,AbckitCoreAnnotationElement * annoElement)443 static void ProcessClassAnnotationsInner(std::string_view name, AbckitCoreAnnotationElement *annoElement)
444 {
445 if (name == "a") {
446 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
447 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
448
449 auto vs = g_implI->valueGetDouble(val);
450 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
451 EXPECT_TRUE(vs == 20U);
452 }
453
454 if (name == "b") {
455 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
456 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
457
458 auto vs = g_implI->arrayValueGetLiteralArray(val);
459 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
460 g_implI->literalArrayEnumerateElements(vs, nullptr, [](AbckitFile *, AbckitLiteral *v, void *) {
461 double value = g_implI->literalGetDouble(v);
462 EXPECT_TRUE(value == 13U || value == 10U);
463 return true;
464 });
465 }
466
467 if (name == "d") {
468 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
469 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
470
471 auto vs = g_implI->valueGetU1(val);
472 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
473 EXPECT_TRUE(vs == true);
474 }
475
476 if (name == "str") {
477 AbckitValue *val = g_implI->annotationElementGetValue(annoElement);
478 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
479
480 auto str = g_implI->valueGetString(val);
481 auto vs = helpers::AbckitStringToString(str);
482 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
483 EXPECT_TRUE(vs == "ABC");
484 }
485 }
486
ProcessClassAnnotations(AbckitFile * file,AbckitCoreFunction * method,AbckitGraph *)487 static void ProcessClassAnnotations(AbckitFile *file, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
488 {
489 auto klass = g_implI->functionGetParentClass(method);
490 g_implI->classEnumerateAnnotations(klass, file, [](AbckitCoreAnnotation *anno, void *) {
491 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
492 g_implI->annotationEnumerateElements(anno, nullptr, [](AbckitCoreAnnotationElement *annoElement, void *) {
493 auto abckitstr = g_implI->annotationElementGetName(annoElement);
494 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
495
496 auto name = helpers::AbckitStringToString(abckitstr);
497
498 EXPECT_TRUE(name == "a" || name == "b" || name == "d" || name == "str");
499
500 ProcessClassAnnotationsInner(name, annoElement);
501
502 return true;
503 });
504 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
505
506 return true;
507 });
508 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
509 }
510
ProcessAnnotationElements(AbckitFile * file,AbckitCoreFunction * method,AbckitGraph * graph)511 static void ProcessAnnotationElements(AbckitFile *file, AbckitCoreFunction *method, AbckitGraph *graph)
512 {
513 ProcessMethodAnnotations(file, method, graph);
514 ProcessClassAnnotations(file, method, graph);
515 }
516
AnnotationGetFile(AbckitFile * file,AbckitCoreFunction * method,AbckitGraph *)517 static void AnnotationGetFile(AbckitFile *file, AbckitCoreFunction *method, AbckitGraph * /*graph*/)
518 {
519 g_implI->functionEnumerateAnnotations(method, file, [](AbckitCoreAnnotation *anno, void *data1) {
520 auto file1 = reinterpret_cast<AbckitFile *>(data1);
521 auto annoFile = g_implI->annotationGetFile(anno);
522 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
523 EXPECT_EQ(file1, annoFile);
524 return true;
525 });
526 auto klass = g_implI->functionGetParentClass(method);
527 g_implI->classEnumerateAnnotations(klass, file, [](AbckitCoreAnnotation *anno, void *data1) {
528 auto file1 = reinterpret_cast<AbckitFile *>(data1);
529 auto annoFile = g_implI->annotationGetFile(anno);
530 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
531 EXPECT_EQ(file1, annoFile);
532 return true;
533 });
534 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
535 }
536
537 // Test: test-kind=api, api=InspectApiImpl::annotationElementGetName, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationElementGetName)538 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationElementGetName)
539 {
540 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
541 "foo", ProcessAnnotations<false, false, false>);
542 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
543 }
544
545 // Test: test-kind=api, api=InspectApiImpl::annotationElementGetValue, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationElementGetValue)546 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationElementGetValue)
547 {
548 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
549 "foo", ProcessAnnotations<true, false, false>);
550 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
551 }
552
553 // Test: test-kind=api, api=InspectApiImpl::annotationElementGetAnnotation, abc-kind=ArkTS1, category=positive,
554 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationElementGetAnnotation)555 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationElementGetAnnotation)
556 {
557 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
558 "foo", ProcessAnnotations<false, true, false>);
559 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
560 }
561
562 // Test: test-kind=api, api=InspectApiImpl::annotationElementGetFile, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationElementGetFile)563 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationElementGetFile)
564 {
565 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
566 "foo", ProcessAnnotations<false, false, true>);
567 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
568 }
569
570 // Test: test-kind=api, api=InspectApiImpl::classEnumerateAnnotations, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,ClassEnumerateAnnotations)571 TEST_F(LibAbcKitInspectApiAnnotationsTests, ClassEnumerateAnnotations)
572 {
573 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
574 "A", ProcessClass);
575 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
576 }
577
578 // Test: test-kind=api, api=InspectApiImpl::annotationGetInterface, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationGetInterface)579 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationGetInterface)
580 {
581 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
582 "foo", ProcessAnnotationInterface<false, false>);
583 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
584 }
585
586 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceGetName, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceGetName)587 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceGetName)
588 {
589 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
590 "foo", ProcessAnnotationInterface<true, false>);
591 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
592 }
593
594 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceGetFile, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceGetFile)595 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceGetFile)
596 {
597 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
598 "foo", ProcessAnnotationInterface<false, true>);
599 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
600 }
601
602 // Test: test-kind=api, api=InspectApiImpl::moduleEnumerateAnnotationInterfaces, abc-kind=ArkTS1, category=positive,
603 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,ModuleEnumerateAnnotationInterfaces)604 TEST_F(LibAbcKitInspectApiAnnotationsTests, ModuleEnumerateAnnotationInterfaces)
605 {
606 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
607 "foo", ProcessModule<false>);
608 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
609 }
610
611 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceGetModule, abc-kind=ArkTS1, category=positive,
612 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceGetModule)613 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceGetModule)
614 {
615 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
616 "foo", ProcessModule<true>);
617 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
618 }
619
620 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceEnumerateFields, abc-kind=ArkTS1, category=positive,
621 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceEnumerateFields)622 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceEnumerateFields)
623 {
624 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
625 "foo", ProcessAnnotationInterfaceFields<false, false, false, false, false>);
626 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
627 }
628
629 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceFieldGetFile, abc-kind=ArkTS1, category=positive,
630 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceFieldGetFile)631 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceFieldGetFile)
632 {
633 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
634 "foo", ProcessAnnotationInterfaceFields<true, false, false, false, false>);
635 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
636 }
637
638 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceFieldGetName, abc-kind=ArkTS1, category=positive,
639 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceFieldGetName)640 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceFieldGetName)
641 {
642 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
643 "foo", ProcessAnnotationInterfaceFields<false, true, false, false, false>);
644 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
645 }
646
647 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceFieldGetInterface, abc-kind=ArkTS1, category=positive,
648 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceFieldGetInterface)649 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceFieldGetInterface)
650 {
651 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
652 "foo", ProcessAnnotationInterfaceFields<false, false, true, false, false>);
653 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
654 }
655
656 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceFieldGetType, abc-kind=ArkTS1, category=positive,
657 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceFieldGetType)658 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceFieldGetType)
659 {
660 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
661 "foo", ProcessAnnotationInterfaceFields<false, false, false, true, false>);
662 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
663 }
664
665 // Test: test-kind=api, api=InspectApiImpl::annotationInterfaceFieldGetDefaultValue, abc-kind=ArkTS1, category=positive,
666 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationInterfaceFieldGetDefaultValue)667 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationInterfaceFieldGetDefaultValue)
668 {
669 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
670 "foo", ProcessAnnotationInterfaceFields<false, false, false, false, true>);
671 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
672 }
673
674 // Test: test-kind=api, api=InspectApiImpl::annotationEnumerateElements, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationEnumerateElements)675 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationEnumerateElements)
676 {
677 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
678 "foo", ProcessAnnotationElements);
679 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
680 }
681
682 // Test: test-kind=api, api=InspectApiImpl::annotationGetFile, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,AnnotationGetFile)683 TEST_F(LibAbcKitInspectApiAnnotationsTests, AnnotationGetFile)
684 {
685 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
686 "foo", AnnotationGetFile);
687 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
688 }
689
690 // Test: test-kind=api, api=InspectApiImpl::functionEnumerateAnnotations, abc-kind=ArkTS1, category=positive,
691 // extension=c
TEST_F(LibAbcKitInspectApiAnnotationsTests,FunctionEnumerateAnnotations)692 TEST_F(LibAbcKitInspectApiAnnotationsTests, FunctionEnumerateAnnotations)
693 {
694 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/annotations/annotations_inspect_dynamic.abc",
695 "foo", ProcessMethodAnnotations);
696 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
697 }
698
699 } // namespace libabckit::test
700