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
16 #include "ast_verifier_test.h"
17 #include "ir/astNode.h"
18
19 #include <gtest/gtest.h>
20
21 using ark::es2panda::compiler::ast_verifier::CheckStructDeclaration;
22
23 namespace {
24
TEST_F(ASTVerifierTest,StructDeclaration)25 TEST_F(ASTVerifierTest, StructDeclaration)
26 {
27 char const *text = R"(
28 struct A {
29 x : number = 0
30 }
31 )";
32
33 CONTEXT(ES2PANDA_STATE_PARSED, text)
34 {
35 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
36 }
37 }
38
TEST_F(ASTVerifierTest,StructWithAnnotation)39 TEST_F(ASTVerifierTest, StructWithAnnotation)
40 {
41 char const *text = R"(
42 @interface Anno {
43 name : string = "Tom";
44 }
45
46 @Anno
47 struct A {
48 a : number = 1
49 }
50 )";
51 CONTEXT(ES2PANDA_STATE_PARSED, text)
52 {
53 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
54 }
55 }
56
TEST_F(ASTVerifierTest,StructWithComponent)57 TEST_F(ASTVerifierTest, StructWithComponent)
58 {
59 char const *text = R"(
60 @Component
61 struct A {
62 a : number = 1
63 }
64 )";
65 CONTEXT(ES2PANDA_STATE_PARSED, text)
66 {
67 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
68 }
69 }
70
TEST_F(ASTVerifierTest,StructExtendsStruct)71 TEST_F(ASTVerifierTest, StructExtendsStruct)
72 {
73 char const *text = R"(
74 struct A {
75 }
76 struct B extends A {
77 }
78 )";
79 CONTEXT(ES2PANDA_STATE_PARSED, text)
80 {
81 ASSERT_TRUE(Verify<CheckStructDeclaration>(
82 ExpectVerifierMessage {"Struct declaration should", "Struct declaration should"}));
83 }
84 }
85
TEST_F(ASTVerifierTest,StructExtendsClass)86 TEST_F(ASTVerifierTest, StructExtendsClass)
87 {
88 char const *text = R"(
89 class A {
90 }
91 struct B extends A {
92 }
93 )";
94 CONTEXT(ES2PANDA_STATE_PARSED, text)
95 {
96 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
97 }
98 }
99
TEST_F(ASTVerifierTest,StructExtendsEnum)100 TEST_F(ASTVerifierTest, StructExtendsEnum)
101 {
102 char const *text = R"(
103 enum Color {
104 Red,
105 Green,
106 Blue
107 }
108 struct B extends Color {
109 }
110 )";
111 CONTEXT(ES2PANDA_STATE_PARSED, text)
112 {
113 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
114 }
115 }
116
TEST_F(ASTVerifierTest,StructImplementInterface)117 TEST_F(ASTVerifierTest, StructImplementInterface)
118 {
119 char const *text = R"(
120 interface A {
121 }
122 struct B implements A {
123 }
124 )";
125 CONTEXT(ES2PANDA_STATE_PARSED, text)
126 {
127 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
128 }
129 }
130
TEST_F(ASTVerifierTest,StructExtendsInterface)131 TEST_F(ASTVerifierTest, StructExtendsInterface)
132 {
133 char const *text = R"(
134 interface A {
135 }
136 struct B extends A {
137 }
138 )";
139 CONTEXT(ES2PANDA_STATE_PARSED, text)
140 {
141 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
142 }
143 }
144
TEST_F(ASTVerifierTest,StructExport)145 TEST_F(ASTVerifierTest, StructExport)
146 {
147 char const *text = R"(
148 export struct A {
149 }
150 )";
151 CONTEXT(ES2PANDA_STATE_PARSED, text)
152 {
153 ASSERT_TRUE(Verify<CheckStructDeclaration>(ExpectVerifierMessage {"Struct declaration should"}));
154 }
155 }
156
TEST_F(ASTVerifierTest,StructInStruct)157 TEST_F(ASTVerifierTest, StructInStruct)
158 {
159 char const *text = R"(
160 Struct A {
161 Struct B {
162 }
163 }
164 )";
165
166 CONTEXT(ES2PANDA_STATE_PARSED, text)
167 {
168 ASSERT_TRUE(GetImpl()->IsAnyError(GetContext()));
169 }
170 }
171
172 } // namespace
173