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 #include <gtest/gtest.h>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 #include "assembly-program.h"
20 #include "test/unit/annotations/annotations_emit_test.h"
21
22 namespace ark::es2panda::compiler::test {
23
24 class AnnotationsforClass : public AnnotationEmitTest {
25 public:
26 AnnotationsforClass() = default;
27
28 ~AnnotationsforClass() override = default;
29
RunAnnotationEmitTest(const std::string_view text)30 void RunAnnotationEmitTest(const std::string_view text)
31 {
32 auto program = GetCurrentProgram(text);
33 ASSERT_NE(program, nullptr);
34
35 CheckAnnotations(program.get());
36 CheckClassAnnotations(program.get());
37 CheckFunctionAnnotations(program.get());
38 }
39
CheckAnnotations(pandasm::Program * program)40 void CheckAnnotations(pandasm::Program *program)
41 {
42 const std::string annoName = "Anno";
43 const std::vector<std::pair<std::string, std::string>> expectedAnnotations = {
44 {
45 {"authorName", ""},
46 {"authorAge", "0"},
47 {"testBool", "0"},
48 },
49 };
50 AnnotationEmitTest::CheckAnnoDecl(program, annoName, expectedAnnotations);
51 }
52
CheckClassAnnotations(pandasm::Program * program)53 void CheckClassAnnotations(pandasm::Program *program)
54 {
55 const std::string recordName = "A";
56 const AnnotationMap expectedClassAnnotations = {
57 {"Anno",
58 {
59 {"authorName", "Mike"},
60 {"authorAge", "18"},
61 {"testBool", "1"},
62 }},
63 };
64 AnnotationEmitTest::CheckRecordAnnotations(program, recordName, expectedClassAnnotations);
65 }
66
CheckFunctionAnnotations(pandasm::Program * program)67 void CheckFunctionAnnotations(pandasm::Program *program)
68 {
69 const std::string funcName = "A.foo:void;";
70 const AnnotationMap expectedFuncAnnotations = {
71 {"Anno",
72 {
73 {"authorName", "John"},
74 {"authorAge", "23"},
75 {"testBool", "0"},
76 }},
77 };
78 AnnotationEmitTest::CheckFunctionAnnotations(program, funcName, false, expectedFuncAnnotations);
79 }
80
CheckClassPropertyAnnotations(pandasm::Program * program)81 void CheckClassPropertyAnnotations(pandasm::Program *program)
82 {
83 const std::string record = "A";
84 const std::string fieldName = "x";
85 const AnnotationMap expectedFuncAnnotations = {
86 {"Anno",
87 {
88 {"authorName", ""},
89 {"authorAge", "0"},
90 {"testBool", "0"},
91 }},
92 };
93 AnnotationEmitTest::CheckClassFieldAnnotations(program, record, fieldName, expectedFuncAnnotations);
94 }
95
96 private:
97 NO_COPY_SEMANTIC(AnnotationsforClass);
98 NO_MOVE_SEMANTIC(AnnotationsforClass);
99 };
100
TEST_F(AnnotationsforClass,annotations_for_class)101 TEST_F(AnnotationsforClass, annotations_for_class)
102 {
103 std::string_view text = R"(
104 @interface Anno {
105 authorName: string = ""
106 authorAge: int = 0
107 testBool: boolean = false
108 }
109
110 @Anno({
111 authorName: "Mike",
112 authorAge: 18,
113 testBool: true
114 })
115 class A {
116 @Anno
117 x:int = 1
118
119 @Anno({
120 authorName: "John",
121 authorAge: 23,
122 testBool: false
123 })
124 foo() {}
125 })";
126
127 RunAnnotationEmitTest(text);
128 }
129
130 } // namespace ark::es2panda::compiler::test