• 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 "ir/astNode.h"
18 
19 #include <gtest/gtest.h>
20 
21 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
22 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
23 using ark::es2panda::ir::AstNode;
24 
25 namespace {
26 
TEST_F(ASTVerifierTest,ForInLoopScope)27 TEST_F(ASTVerifierTest, ForInLoopScope)
28 {
29     ASTVerifier verifier {Allocator()};
30 
31     char const *text = R"(
32         function main(): void {
33             let res = 0
34             let arr: int[] = [1, 2, 3]
35             for (let i of arr) {
36                 res += i
37             }
38         }
39     )";
40 
41     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
42     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
43     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
44 
45     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
46 
47     InvariantNameSet checks;
48     checks.insert("CheckScopeDeclarationForAll");
49     const auto &messages = verifier.Verify(ast, checks);
50 
51     // Expecting no warnings
52     ASSERT_EQ(messages.size(), 0);
53 
54     impl_->DestroyContext(ctx);
55 }
56 
TEST_F(ASTVerifierTest,PartialClass)57 TEST_F(ASTVerifierTest, PartialClass)
58 {
59     ASTVerifier verifier {Allocator()};
60 
61     char const *text = R"(
62         export class A {
63             private static readonly param: int = 0;
64         }
65     )";
66 
67     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
68     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
69     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
70 
71     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
72 
73     InvariantNameSet checks;
74     checks.insert("CheckScopeDeclarationForAll");
75     const auto &messages = verifier.Verify(ast, checks);
76 
77     // Expecting no warnings
78     ASSERT_EQ(messages.size(), 0);
79 
80     impl_->DestroyContext(ctx);
81 }
82 
TEST_F(ASTVerifierTest,TryCatch)83 TEST_F(ASTVerifierTest, TryCatch)
84 {
85     ASTVerifier verifier {Allocator()};
86 
87     char const *text = R"(
88         function main(): void {
89             let catchCode = 0;
90 
91             try {
92                 throw new NullPointerError();
93             } catch (e: NullPointerError) {
94                 catchCode = 1;
95             }
96         }
97     )";
98 
99     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
100     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
101     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
102 
103     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
104 
105     InvariantNameSet checks;
106     checks.insert("CheckScopeDeclarationForAll");
107     const auto &messages = verifier.Verify(ast, checks);
108 
109     // Expecting no warnings
110     ASSERT_EQ(messages.size(), 0);
111 
112     impl_->DestroyContext(ctx);
113 }
114 
115 }  // namespace
116