• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AnnotationsforInterface : public AnnotationEmitTest {
25 public:
26     AnnotationsforInterface() = default;
27 
28     ~AnnotationsforInterface() 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         CheckInterfaceAnnotations(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 
CheckInterfaceAnnotations(pandasm::Program * program)53     void CheckInterfaceAnnotations(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 getter = "A.<get>x:i32;";
70         const std::string setter = "A.<set>x:i32;void;";
71         const std::string funcName = "A.foo:void;";
72         const AnnotationMap expectedFuncAnnotations1 = {
73             {"Anno",
74              {
75                  {"authorName", ""},
76                  {"authorAge", "0"},
77                  {"testBool", "0"},
78              }},
79         };
80         const AnnotationMap expectedFuncAnnotations2 = {
81             {"Anno",
82              {
83                  {"authorName", "John"},
84                  {"authorAge", "23"},
85                  {"testBool", "0"},
86              }},
87         };
88         AnnotationEmitTest::CheckFunctionAnnotations(program, getter, false, expectedFuncAnnotations1);
89         AnnotationEmitTest::CheckFunctionAnnotations(program, setter, false, expectedFuncAnnotations1);
90         AnnotationEmitTest::CheckFunctionAnnotations(program, funcName, false, expectedFuncAnnotations2);
91     }
92 
93 private:
94     NO_COPY_SEMANTIC(AnnotationsforInterface);
95     NO_MOVE_SEMANTIC(AnnotationsforInterface);
96 };
97 
TEST_F(AnnotationsforInterface,annotations_for_interface)98 TEST_F(AnnotationsforInterface, annotations_for_interface)
99 {
100     std::string_view text = R"(
101     @interface Anno {
102         authorName: string = ""
103         authorAge: int = 0
104         testBool: boolean = false
105     }
106 
107     @Anno({
108         authorName: "Mike",
109         authorAge: 18,
110         testBool: true
111     })
112     interface A {
113         @Anno
114         x:int
115 
116         @Anno({
117             authorName: "John",
118             authorAge: 23,
119             testBool: false
120         })
121         foo()
122     })";
123 
124     RunAnnotationEmitTest(text);
125 }
126 
127 }  // namespace ark::es2panda::compiler::test