• 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 <cstdint>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 #include "assembly-program.h"
21 #include "test/unit/annotations/annotations_emit_test.h"
22 
23 namespace ark::es2panda::compiler::test {
24 
25 class AnnotationsforFunction : public AnnotationEmitTest {
26 public:
27     AnnotationsforFunction() = default;
28 
29     ~AnnotationsforFunction() override = default;
30 
RunAnnotationEmitTest(const std::string_view text)31     void RunAnnotationEmitTest(const std::string_view text)
32     {
33         auto program = GetCurrentProgram(text);
34         ASSERT_NE(program, nullptr);
35 
36         CheckAnnotations(program.get());
37         CheckFunctionAnnotations(program.get());
38         CheckFunctionParameterAnnotations(program.get());
39     }
40 
CheckAnnotations(pandasm::Program * program)41     void CheckAnnotations(pandasm::Program *program)
42     {
43         const std::string annoName = "Anno";
44         const std::vector<std::pair<std::string, std::string>> expectedAnnotations = {
45             {{"a", "1.000000"}},
46         };
47         AnnotationEmitTest::CheckAnnoDecl(program, annoName, expectedAnnotations);
48     }
49 
CheckFunctionAnnotations(pandasm::Program * program)50     void CheckFunctionAnnotations(pandasm::Program *program)
51     {
52         const std::string funcName1 = "ETSGLOBAL.foo:std.core.Double;void;";
53         const std::string funcName2 = "ETSGLOBAL.foo:void;";
54         const AnnotationMap expectedFuncAnnotations = {
55             {"Anno", {{"a", "1.000000"}}},
56         };
57         AnnotationEmitTest::CheckFunctionAnnotations(program, funcName1, true, expectedFuncAnnotations);
58         AnnotationEmitTest::CheckFunctionAnnotations(program, funcName2, true, expectedFuncAnnotations);
59     }
60 
CheckFunctionParameterAnnotations(pandasm::Program * program)61     void CheckFunctionParameterAnnotations(pandasm::Program *program)
62     {
63         const std::string funcName1 = "ETSGLOBAL.foo:std.core.Double;void;";
64         const uint32_t paramIndex1 = 0;
65         const AnnotationMap expectedFuncAnnotations1 = {
66             {"Anno", {{"a", "1.000000"}}},
67         };
68         const std::string funcName2 = "A.bar:f64;f64[];void;";
69         // Index 0 is the parameter 'this'
70         const uint32_t paramIndex2 = 1;
71         const uint32_t paramIndex3 = 2;
72         const AnnotationMap expectedFuncAnnotations2 = {
73             {"Anno",
74              {
75                  {"a", "2.000000"},
76              }},
77         };
78         const AnnotationMap expectedFuncAnnotations3 = {
79             {"Anno",
80              {
81                  {"a", "3.000000"},
82              }},
83         };
84         AnnotationEmitTest::CheckFunctionParameterAnnotations(program, funcName1, true, paramIndex1,
85                                                               expectedFuncAnnotations1);
86         AnnotationEmitTest::CheckFunctionParameterAnnotations(program, funcName2, false, paramIndex2,
87                                                               expectedFuncAnnotations2);
88         AnnotationEmitTest::CheckFunctionParameterAnnotations(program, funcName2, false, paramIndex3,
89                                                               expectedFuncAnnotations3);
90     }
91 
92 private:
93     NO_COPY_SEMANTIC(AnnotationsforFunction);
94     NO_MOVE_SEMANTIC(AnnotationsforFunction);
95 };
96 
97 // #22952
TEST_F(AnnotationsforFunction,DISABLED_annotations_for_function)98 TEST_F(AnnotationsforFunction, DISABLED_annotations_for_function)
99 {
100     std::string_view text = R"(
101     @interface Anno {
102         a:number = 1
103     }
104 
105     @Anno
106     function foo(@Anno a ?: number) {}
107 
108     class A {
109         bar(@Anno(2) a : number, @Anno(3) ...rest : number[]) {}
110     })";
111 
112     RunAnnotationEmitTest(text);
113 }
114 
115 }  // namespace ark::es2panda::compiler::test