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 AnnotationsforNamespace : public AnnotationEmitTest {
25 public:
26 AnnotationsforNamespace() = default;
27
28 ~AnnotationsforNamespace() 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 CheckNamespaceAnnotations(program.get());
37 }
38
CheckAnnotations(pandasm::Program * program)39 void CheckAnnotations(pandasm::Program *program)
40 {
41 const std::string annoName = "Anno";
42 const std::vector<std::pair<std::string, std::string>> expectedAnnotations = {
43 {
44 {"authorName", ""},
45 {"authorAge", "0"},
46 {"testBool", "0"},
47 },
48 };
49 AnnotationEmitTest::CheckAnnoDecl(program, annoName, expectedAnnotations);
50 }
51
CheckNamespaceAnnotations(pandasm::Program * program)52 void CheckNamespaceAnnotations(pandasm::Program *program)
53 {
54 const std::string recordName1 = "A";
55 const AnnotationMap expectedClassAnnotations1 = {
56 {"Anno",
57 {
58 {"authorName", "Mike"},
59 {"authorAge", "18"},
60 {"testBool", "1"},
61 }},
62 };
63 const std::string recordName2 = "A.B";
64 const AnnotationMap expectedClassAnnotations2 = {
65 {"Anno",
66 {
67 {"authorName", ""},
68 {"authorAge", "0"},
69 {"testBool", "0"},
70 }},
71 };
72 AnnotationEmitTest::CheckRecordAnnotations(program, recordName1, expectedClassAnnotations1);
73 AnnotationEmitTest::CheckRecordAnnotations(program, recordName2, expectedClassAnnotations2);
74 }
75
76 private:
77 NO_COPY_SEMANTIC(AnnotationsforNamespace);
78 NO_MOVE_SEMANTIC(AnnotationsforNamespace);
79 };
80
TEST_F(AnnotationsforNamespace,annotations_for_namespace)81 TEST_F(AnnotationsforNamespace, annotations_for_namespace)
82 {
83 std::string_view text = R"(
84 @interface Anno {
85 authorName: string = ""
86 authorAge: int = 0
87 testBool: boolean = false
88 }
89
90 @Anno({
91 authorName: "Mike",
92 authorAge: 18,
93 testBool: true
94 })
95 namespace A {
96 @Anno()
97 namespace B {}
98 })";
99
100 RunAnnotationEmitTest(text);
101 }
102
103 } // namespace ark::es2panda::compiler::test