• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "ast_verifier_test.h"
17 #include "checker/ETSchecker.h"
18 
19 namespace {
20 
21 using ark::es2panda::compiler::ast_verifier::ModifierAccessValid;
22 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative1)23 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative1)
24 {
25     char const *text = R"(
26         class Base {
27             public a: int = 1;
28         }
29         function main(): void {
30             let base: Base = new Base();
31             let a = base.a;
32         }
33     )";
34     CONTEXT(ES2PANDA_STATE_CHECKED, text)
35     {
36         GetAst()
37             ->AsETSModule()
38             ->Statements()[1]
39             ->AsClassDeclaration()
40             ->Definition()
41             ->AsClassDefinition()
42             ->Body()[0]
43             ->AsClassProperty()
44             ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
45 
46         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
47     }
48 }
49 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative2)50 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative2)
51 {
52     char const *text = R"(
53         class Base {
54             public a: int = 1;
55         }
56         class Derived extends Base {}
57         function main(): void {
58             let derived: Derived = new Derived();
59             let a = derived.a;
60         }
61     )";
62     CONTEXT(ES2PANDA_STATE_CHECKED, text)
63     {
64         GetAst()
65             ->AsETSModule()
66             ->Statements()[1]
67             ->AsClassDeclaration()
68             ->Definition()
69             ->AsClassDefinition()
70             ->Body()[0]
71             ->AsClassProperty()
72             ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
73 
74         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
75     }
76 }
77 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative3)78 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative3)
79 {
80     char const *text = R"(
81         class Base {
82             public a: int = 1;
83         }
84         class Derived extends Base {}
85         function main(): void {
86             let derived: Base = new Derived();
87             let a = derived.a;
88         }
89     )";
90     CONTEXT(ES2PANDA_STATE_CHECKED, text)
91     {
92         GetAst()
93             ->AsETSModule()
94             ->Statements()[1]
95             ->AsClassDeclaration()
96             ->Definition()
97             ->AsClassDefinition()
98             ->Body()[0]
99             ->AsClassProperty()
100             ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
101 
102         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
103     }
104 }
105 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative4)106 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative4)
107 {
108     char const *text = R"(
109         class Base {
110             public a: int = 1;
111             public protectedMethod() {
112                 this.a = 2;
113             }
114         }
115         function main(): void {
116             let base: Base = new Base();
117             base.protectedMethod();
118         }
119     )";
120     CONTEXT(ES2PANDA_STATE_CHECKED, text)
121     {
122         GetAst()
123             ->AsETSModule()
124             ->Statements()[0]
125             ->AsClassDeclaration()
126             ->Definition()
127             ->AsClassDefinition()
128             ->Body()[1]
129             ->AsClassElement()
130             ->Value()
131             ->AsFunctionExpression()
132             ->Function()
133             ->AsScriptFunction()
134             ->Body()
135             ->AsBlockStatement()
136             ->Statements()[1]
137             ->AsExpressionStatement()
138             ->GetExpression()
139             ->AsCallExpression()
140             ->Signature()
141             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
142 
143         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
144     }
145 }
146 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative5)147 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative5)
148 {
149     char const *text = R"(
150         class Base {
151             public a: int = 1;
152             public protectedMethod() {
153                 this.a = 2;
154             }
155         }
156         class Derived extends Base {}
157         function main(): void {
158             let derived: Derived = new Derived();
159             derived.protectedMethod();
160         }
161     )";
162     CONTEXT(ES2PANDA_STATE_CHECKED, text)
163     {
164         GetAst()
165             ->AsETSModule()
166             ->Statements()[0]
167             ->AsClassDeclaration()
168             ->Definition()
169             ->AsClassDefinition()
170             ->Body()[1]
171             ->AsClassElement()
172             ->Value()
173             ->AsFunctionExpression()
174             ->Function()
175             ->AsScriptFunction()
176             ->Body()
177             ->AsBlockStatement()
178             ->Statements()[1]
179             ->AsExpressionStatement()
180             ->GetExpression()
181             ->AsCallExpression()
182             ->Signature()
183             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
184 
185         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
186     }
187 }
188 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative6)189 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative6)
190 {
191     char const *text = R"(
192         class Base {
193             public a: int = 1;
194             public protectedMethod() {
195                 this.a = 2;
196             }
197         }
198         class Derived extends Base {}
199         function main(): void {
200             let derived: Base = new Derived();
201             derived.protectedMethod();
202         }
203     )";
204     CONTEXT(ES2PANDA_STATE_CHECKED, text)
205     {
206         GetAst()
207             ->AsETSModule()
208             ->Statements()[0]
209             ->AsClassDeclaration()
210             ->Definition()
211             ->AsClassDefinition()
212             ->Body()[1]
213             ->AsClassElement()
214             ->Value()
215             ->AsFunctionExpression()
216             ->Function()
217             ->AsScriptFunction()
218             ->Body()
219             ->AsBlockStatement()
220             ->Statements()[1]
221             ->AsExpressionStatement()
222             ->GetExpression()
223             ->AsCallExpression()
224             ->Signature()
225             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
226 
227         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
228     }
229 }
230 
231 }  // namespace
232