• 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 AnnotationRetentionPolicy : public AnnotationEmitTest {
25 public:
26     AnnotationRetentionPolicy() = default;
27 
28     ~AnnotationRetentionPolicy() 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         CheckRecordAnnotations(program.get());
36         CheckFunctionAnnotations(program.get());
37         CheckClasspropertyAnnotations(program.get());
38         CheckFunctionParameterAnnotations(program.get());
39     }
40 
CheckRecordAnnotations(pandasm::Program * program)41     void CheckRecordAnnotations(pandasm::Program *program)
42     {
43         const std::string recordName1 = "A";
44         AnnotationEmitTest::CheckRecordWithoutAnnotations(program, recordName1);
45         const std::string recordName2 = "NS";
46         AnnotationEmitTest::CheckRecordWithoutAnnotations(program, recordName2, true);
47     }
48 
CheckFunctionAnnotations(pandasm::Program * program)49     void CheckFunctionAnnotations(pandasm::Program *program)
50     {
51         const std::string foo1 = "ETSGLOBAL.foo:f64;void;";
52         const std::string foo2 = "A.foo:std.core.String;void;";
53         AnnotationEmitTest::CheckFunctionWithoutAnnotations(program, foo1, true);
54         AnnotationEmitTest::CheckFunctionWithoutAnnotations(program, foo2, false);
55     }
56 
CheckClasspropertyAnnotations(pandasm::Program * program)57     void CheckClasspropertyAnnotations(pandasm::Program *program)
58     {
59         const std::string recordName = "A";
60         const std::string fieldName = "a";
61         AnnotationEmitTest::CheckClassFieldWithoutAnnotations(program, recordName, fieldName);
62     }
63 
CheckFunctionParameterAnnotations(pandasm::Program * program)64     void CheckFunctionParameterAnnotations(pandasm::Program *program)
65     {
66         const std::string foo1 = "ETSGLOBAL.foo:f64;void;";
67         const std::string foo2 = "A.foo:std.core.String;void;";
68         AnnotationEmitTest::CheckFunctionParameterWithoutAnnotations(program, foo1, true, 0);
69         AnnotationEmitTest::CheckFunctionParameterWithoutAnnotations(program, foo2, false, 0);
70     }
71 
72 private:
73     NO_COPY_SEMANTIC(AnnotationRetentionPolicy);
74     NO_MOVE_SEMANTIC(AnnotationRetentionPolicy);
75 };
76 
TEST_F(AnnotationRetentionPolicy,annotations_retention_policy)77 TEST_F(AnnotationRetentionPolicy, annotations_retention_policy)
78 {
79     std::string_view text = R"(
80     @Retention("SOURCE")
81     @interface Anno{}
82 
83     @Anno()
84     function foo(@Anno() a: number) {}
85 
86     @Anno()
87     class A {
88         @Anno()
89         a:string = ""
90 
91         @Anno()
92         foo(@Anno() a: string) {}
93     }
94 
95     @Anno()
96     namespace NS{})";
97 
98     RunAnnotationEmitTest(text);
99 }
100 
101 }  // namespace ark::es2panda::compiler::test