• 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 "annotation.h"
17 #include "util/options.h"
18 #include <cstddef>
19 #include <iostream>
20 #include <ostream>
21 #include <string>
22 #include <utility>
23 #include <variant>
24 #include <vector>
25 #include "assembly-program.h"
26 #include "test/unit/annotations/annotations_emit_test.h"
27 
28 namespace ark::es2panda::compiler::test {
29 
30 class StandardEmitTest : public AnnotationEmitTest {
31 public:
32     StandardEmitTest() = default;
33 
34     ~StandardEmitTest() override = default;
35 
RunAnnotationEmitTest(const std::string_view text)36     void RunAnnotationEmitTest(const std::string_view text)
37     {
38         auto program = GetCurrentProgram(text);
39         ASSERT_NE(program, nullptr);
40 
41         CheckAnnotations(program.get());
42         CheckClassAnnotations(program.get());
43         CheckFunctionAnnotations(program.get());
44         CheckLiteralArrayTable(program.get());
45     }
46 
CheckAnnotations(pandasm::Program * program)47     void CheckAnnotations(pandasm::Program *program)
48     {
49         const std::string annoName = "ClassAuthor";
50         const std::vector<std::pair<std::string, std::string>> expectedAnnotations = {
51             {"authorName", "Jim"},
52             {"authorAge", "35.000000"},
53             {"testBool", "0"},
54             {"favorColor", "1"},
55             {"color", "ETSGLOBAL$ClassAuthor$color$0"},
56             {"reviewers", "ETSGLOBAL$ClassAuthor$reviewers$1"},
57             {"reviewersAge", "ETSGLOBAL$ClassAuthor$reviewersAge$2"},
58             {"testBools", "ETSGLOBAL$ClassAuthor$testBools$3"},
59             {"mutiArray", "ETSGLOBAL$ClassAuthor$mutiArray$7"},
60         };
61         CheckAnnoDecl(program, annoName, expectedAnnotations);
62     }
63 
CheckClassAnnotations(pandasm::Program * program)64     void CheckClassAnnotations(pandasm::Program *program)
65     {
66         const std::string className = "MyClass";
67         const AnnotationMap expectedClassAnnotations = {
68             {"ClassAuthor",
69              {
70                  {"authorName", "Jim"},
71                  {"authorAge", "35.000000"},
72                  {"testBool", "0"},
73                  {"favorColor", "1"},
74                  {"color", "MyClass$ClassAuthor$color$12"},
75                  {"reviewers", "MyClass$ClassAuthor$reviewers$14"},
76                  {"reviewersAge", "MyClass$ClassAuthor$reviewersAge$15"},
77                  {"testBools", "MyClass$ClassAuthor$testBools$13"},
78                  {"mutiArray", "MyClass$ClassAuthor$mutiArray$11"},
79              }},
80         };
81         AnnotationEmitTest::CheckClassAnnotations(program, className, expectedClassAnnotations);
82     }
83 
CheckFunctionAnnotations(pandasm::Program * program)84     void CheckFunctionAnnotations(pandasm::Program *program)
85     {
86         const std::string functionName = "MyClass.foo:void;";
87         const AnnotationMap expectedFuncAnnotations = {
88             {"ClassAuthor",
89              {
90                  {"mutiArray", "foo$ClassAuthor$mutiArray$19"},
91                  {"color", "foo$ClassAuthor$color$20"},
92                  {"testBools", "foo$ClassAuthor$testBools$21"},
93                  {"reviewers", "foo$ClassAuthor$reviewers$22"},
94                  {"favorColor", "1"},
95                  {"testBool", "0"},
96                  {"reviewersAge", "foo$ClassAuthor$reviewersAge$23"},
97                  {"authorAge", "35.000000"},
98                  {"authorName", "Jim"},
99              }},
100         };
101         AnnotationEmitTest::CheckFunctionAnnotations(program, functionName, expectedFuncAnnotations);
102     }
103 
CheckLiteralArrayTable(pandasm::Program * program)104     void CheckLiteralArrayTable(pandasm::Program *program)
105     {
106         std::vector<std::pair<std::string, std::vector<AnnotationValueType>>> expectedLiteralArrayTable = {
107             {"ETSGLOBAL$ClassAuthor$color$0", std::vector<AnnotationValueType> {COLOR_OPTION_0, COLOR_OPTION_1}},
108             {"ETSGLOBAL$ClassAuthor$reviewers$1",
109              std::vector<AnnotationValueType> {std::string("Bob"), std::string("Jim"), std::string("Tom")}},
110             {"ETSGLOBAL$ClassAuthor$reviewersAge$2", std::vector<AnnotationValueType> {AGE_18, AGE_21, AGE_32}},
111             {"ETSGLOBAL$ClassAuthor$testBools$3", std::vector<AnnotationValueType> {false, true, false}},
112             {"ETSGLOBAL$ClassAuthor$mutiArray$4", std::vector<AnnotationValueType> {VALUE_1, VALUE_2, VALUE_3}},
113             {"ETSGLOBAL$ClassAuthor$mutiArray$5", std::vector<AnnotationValueType> {VALUE_4, VALUE_5, VALUE_6}},
114             {"ETSGLOBAL$ClassAuthor$mutiArray$6", std::vector<AnnotationValueType> {VALUE_7, VALUE_8, VALUE_9}},
115             {"ETSGLOBAL$ClassAuthor$mutiArray$7",
116              std::vector<AnnotationValueType> {std::string("ETSGLOBAL$ClassAuthor$mutiArray$4"),
117                                                std::string("ETSGLOBAL$ClassAuthor$mutiArray$5"),
118                                                std::string("ETSGLOBAL$ClassAuthor$mutiArray$6")}},
119             {"MyClass$ClassAuthor$color$12", std::vector<AnnotationValueType> {COLOR_OPTION_0, COLOR_OPTION_1}},
120             {"MyClass$ClassAuthor$reviewers$14",
121              std::vector<AnnotationValueType> {std::string("Bob"), std::string("Jim"), std::string("Tom")}},
122             {"MyClass$ClassAuthor$reviewersAge$15", std::vector<AnnotationValueType> {AGE_18, AGE_21, AGE_32}},
123             {"MyClass$ClassAuthor$testBools$13", std::vector<AnnotationValueType> {false, true, false}},
124             {"MyClass$ClassAuthor$mutiArray$8", std::vector<AnnotationValueType> {VALUE_1, VALUE_2, VALUE_3}},
125             {"MyClass$ClassAuthor$mutiArray$9", std::vector<AnnotationValueType> {VALUE_4, VALUE_5, VALUE_6}},
126             {"MyClass$ClassAuthor$mutiArray$10", std::vector<AnnotationValueType> {VALUE_7, VALUE_8, VALUE_9}},
127             {"MyClass$ClassAuthor$mutiArray$11",
128              std::vector<AnnotationValueType> {std::string("MyClass$ClassAuthor$mutiArray$8"),
129                                                std::string("MyClass$ClassAuthor$mutiArray$9"),
130                                                std::string("MyClass$ClassAuthor$mutiArray$10")}},
131             {"foo$ClassAuthor$color$20", std::vector<AnnotationValueType> {COLOR_OPTION_0, COLOR_OPTION_1}},
132             {"foo$ClassAuthor$reviewers$22",
133              std::vector<AnnotationValueType> {std::string("Bob"), std::string("Jim"), std::string("Tom")}},
134             {"foo$ClassAuthor$reviewersAge$23", std::vector<AnnotationValueType> {AGE_18, AGE_21, AGE_32}},
135             {"foo$ClassAuthor$testBools$21", std::vector<AnnotationValueType> {false, true, false}},
136             {"foo$ClassAuthor$mutiArray$16", std::vector<AnnotationValueType> {VALUE_1, VALUE_2, VALUE_3}},
137             {"foo$ClassAuthor$mutiArray$17", std::vector<AnnotationValueType> {VALUE_4, VALUE_5, VALUE_6}},
138             {"foo$ClassAuthor$mutiArray$18", std::vector<AnnotationValueType> {VALUE_7, VALUE_8, VALUE_9}},
139             {"foo$ClassAuthor$mutiArray$19",
140              std::vector<AnnotationValueType> {std::string("foo$ClassAuthor$mutiArray$16"),
141                                                std::string("foo$ClassAuthor$mutiArray$17"),
142                                                std::string("foo$ClassAuthor$mutiArray$18")}},
143         };
144 
145         AnnotationEmitTest::CheckLiteralArrayTable(program, expectedLiteralArrayTable);
146     }
147 
148 private:
149     NO_COPY_SEMANTIC(StandardEmitTest);
150     NO_MOVE_SEMANTIC(StandardEmitTest);
151 };
152 
TEST_F(StandardEmitTest,standard_test)153 TEST_F(StandardEmitTest, standard_test)
154 {
155     std::string_view text = R"(
156         enum Color{RED, BLUE, GREEN}
157 
158         @interface ClassAuthor {
159         authorName: string = "Jim"
160         authorAge: number = 35
161         testBool: boolean = false
162         favorColor: Color = Color.BLUE
163         color: Color[] = [Color.RED, Color.BLUE]
164         reviewers: string[] = ["Bob", "Jim", "Tom"]
165         reviewersAge: number[] = [18, 21, 32]
166         testBools: boolean[] = [false, true, false]
167         mutiArray: number[][] = [
168             [1, 2, 3],
169             [4, 5, 6],
170             [7, 8, 9]
171         ]
172     }
173     @ClassAuthor()
174     class MyClass {
175         @ClassAuthor()
176         foo() {}
177     })";
178 
179     RunAnnotationEmitTest(text);
180 }
181 
182 }  // namespace ark::es2panda::compiler::test