• 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 
16 #include "ast_verifier_test.h"
17 #include "checker/ETSchecker.h"
18 
19 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
20 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
21 using ark::es2panda::ir::ETSScript;
22 
TEST_F(ASTVerifierTest,ProtectedAccessTestCorrect)23 TEST_F(ASTVerifierTest, ProtectedAccessTestCorrect)
24 {
25     ASTVerifier verifier {Allocator()};
26 
27     char const *text = R"(
28         class A {
29             public a: int = 1;
30         }
31         class B extends A {
32             public b: int = this.a;
33         }
34     )";
35     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
36     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
37     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
38 
39     auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
40 
41     ast->AsETSScript()
42         ->Statements()[1]
43         ->AsClassDeclaration()
44         ->Definition()
45         ->AsClassDefinition()
46         ->Body()[0]
47         ->AsClassProperty()
48         ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
49 
50     InvariantNameSet checks;
51     checks.insert("ModifierAccessValidForAll");
52     const auto &messages = verifier.Verify(ast, checks);
53 
54     ASSERT_EQ(messages.size(), 0);
55 
56     impl_->DestroyContext(ctx);
57 }
58