• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 MutipleAnnotationsforClass : public AnnotationEmitTest {
25 public:
26     MutipleAnnotationsforClass() = default;
27 
28     ~MutipleAnnotationsforClass() 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         CheckLiteralArrayTable(program.get());
38     }
39 
CheckAnnotations(pandasm::Program * program)40     void CheckAnnotations(pandasm::Program *program)
41     {
42         const std::string annoName = "Anno2";
43         const std::vector<std::pair<std::string, std::string>> expectedAnnotations = {
44             {
45                 {"favorColor", "1"},
46                 {"color", "ETSGLOBAL$Anno2$color$0"},
47                 {"reviewers", "ETSGLOBAL$Anno2$reviewers$1"},
48             },
49         };
50         CheckAnnoDecl(program, annoName, expectedAnnotations);
51     }
52 
CheckClassAnnotations(pandasm::Program * program)53     void CheckClassAnnotations(pandasm::Program *program)
54     {
55         const std::string className = "A";
56         const AnnotationMap expectedClassAnnotations = {
57             {"Anno1",
58              {
59                  {"authorName", "Mike"},
60                  {"authorAge", "18"},
61                  {"testBool", "1"},
62              }},
63             {"Anno2",
64              {
65                  {"favorColor", "1"},
66                  {"color", "A$Anno2$color$2"},
67                  {"reviewers", "A$Anno2$reviewers$3"},
68              }},
69             {"Anno3",
70              {
71                  {"reviewersAge", "A$Anno3$reviewersAge$4"},
72                  {"testBools", "A$Anno3$testBools$5"},
73                  {"mutiArray", "A$Anno3$mutiArray$9"},
74              }},
75         };
76         AnnotationEmitTest::CheckClassAnnotations(program, className, expectedClassAnnotations);
77     }
78 
CheckLiteralArrayTable(pandasm::Program * program)79     void CheckLiteralArrayTable(pandasm::Program *program)
80     {
81         std::vector<std::pair<std::string, std::vector<AnnotationValueType>>> expectedLiteralArrayTable = {
82             {"ETSGLOBAL$Anno2$color$0", std::vector<AnnotationValueType> {COLOR_0, COLOR_1}},
83             {"ETSGLOBAL$Anno2$reviewers$1",
84              std::vector<AnnotationValueType> {std::string("Bob"), std::string("Jim"), std::string("Tom")}},
85             {"A$Anno2$color$2", std::vector<AnnotationValueType> {COLOR_0, COLOR_1}},
86             {"A$Anno2$reviewers$3",
87              std::vector<AnnotationValueType> {std::string("Bob"), std::string("Jim"), std::string("Tom")}},
88             {"A$Anno3$reviewersAge$4",
89              std::vector<AnnotationValueType> {REVIEWER_AGE_19, REVIEWER_AGE_20, REVIEWER_AGE_24}},
90             {"A$Anno3$testBools$5", std::vector<AnnotationValueType> {true, true, true}},
91             {"A$Anno3$mutiArray$6", std::vector<AnnotationValueType> {VALUE_9, VALUE_8, VALUE_7}},
92             {"A$Anno3$mutiArray$7", std::vector<AnnotationValueType> {VALUE_6, VALUE_5, VALUE_4}},
93             {"A$Anno3$mutiArray$8", std::vector<AnnotationValueType> {VALUE_3, VALUE_2, VALUE_1}},
94             {"A$Anno3$mutiArray$9",
95              std::vector<AnnotationValueType> {std::string("A$Anno3$mutiArray$6"), std::string("A$Anno3$mutiArray$7"),
96                                                std::string("A$Anno3$mutiArray$8")}}};
97 
98         AnnotationEmitTest::CheckLiteralArrayTable(program, expectedLiteralArrayTable);
99     }
100 
101 private:
102     NO_COPY_SEMANTIC(MutipleAnnotationsforClass);
103     NO_MOVE_SEMANTIC(MutipleAnnotationsforClass);
104 };
105 
TEST_F(MutipleAnnotationsforClass,mutiple_annotations_for_class)106 TEST_F(MutipleAnnotationsforClass, mutiple_annotations_for_class)
107 {
108     std::string_view text = R"(
109         enum Color{RED, BLUE, GREEN}
110 
111         @interface Anno1 {
112             authorName: string
113             authorAge: number
114             testBool: boolean
115         }
116 
117         @interface Anno2 {
118             favorColor: Color = Color.BLUE
119             color: Color[] = [Color.RED, Color.BLUE]
120             reviewers: string[] = ["Bob", "Jim", "Tom"]
121         }
122 
123         @interface Anno3 {
124             reviewersAge: number[]
125             testBools: boolean[]
126             mutiArray: number[][]
127         }
128 
129         @Anno1({
130             authorName: "Mike",
131             authorAge: 18,
132             testBool: true
133         })
134         @Anno2
135         @Anno3({
136             reviewersAge: [19, 20, 24],
137             testBools: [true, true, true],
138             mutiArray: [
139                 [9, 8, 7],
140                 [6, 5, 4],
141                 [3, 2, 1]
142             ]
143         })
144         class A {}
145     )";
146 
147     RunAnnotationEmitTest(text);
148 }
149 
150 }  // namespace ark::es2panda::compiler::test