1 /*
2 * Copyright (c) 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 ModuleAnnotations : public AnnotationEmitTest {
25 public:
26 ModuleAnnotations() = default;
27
28 ~ModuleAnnotations() override = default;
29
RunAnnotationEmitTest(const std::string_view text,const std::function<void (pandasm::Program * program)> & test)30 void RunAnnotationEmitTest(const std::string_view text, const std::function<void(pandasm::Program *program)> &test)
31 {
32 auto program = GetCurrentProgram(text);
33 ASSERT_NE(program, nullptr);
34
35 test(program.get());
36 }
37
38 private:
39 NO_COPY_SEMANTIC(ModuleAnnotations);
40 NO_MOVE_SEMANTIC(ModuleAnnotations);
41 };
42
TEST_F(ModuleAnnotations,module_annotation)43 TEST_F(ModuleAnnotations, module_annotation)
44 {
45 std::string_view text = R"(
46 class A {}
47 export class ExportedClass {}
48 namespace B {}
49 export namespace ExportedNamespace {}
50 )";
51
52 auto test = [this](pandasm::Program *program) {
53 AnnotationEmitTest::CheckModuleAnnotation(program, "ETSGLOBAL", true, {"ExportedClass", "ExportedNamespace"});
54 AnnotationEmitTest::CheckModuleAnnotation(program, "ExportedClass", false);
55 AnnotationEmitTest::CheckModuleAnnotation(program, "ExportedNamespace", true, {});
56 };
57 RunAnnotationEmitTest(text, test);
58 }
59
TEST_F(ModuleAnnotations,module_annotation_nested)60 TEST_F(ModuleAnnotations, module_annotation_nested)
61 {
62 std::string_view text = R"(
63 export namespace ExportedNamespace {
64 class A {}
65 export class B {}
66 namespace C {}
67 export namespace D {
68 export class A {}
69 }
70 }
71 )";
72
73 auto test = [this](pandasm::Program *program) {
74 AnnotationEmitTest::CheckModuleAnnotation(program, "ETSGLOBAL", true, {"ExportedNamespace"});
75 AnnotationEmitTest::CheckModuleAnnotation(program, "ExportedNamespace", true,
76 {"ExportedNamespace.B", "ExportedNamespace.D"});
77 AnnotationEmitTest::CheckModuleAnnotation(program, "ExportedNamespace.D", true, {"ExportedNamespace.D.A"});
78 };
79 RunAnnotationEmitTest(text, test);
80 }
81
82 } // namespace ark::es2panda::compiler::test