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 "ir/astNode.h"
18
19 #include <gtest/gtest.h>
20
21 using ark::es2panda::compiler::ast_verifier::CheckScopeDeclaration;
22
23 namespace {
24
TEST_F(ASTVerifierTest,FunctionScope)25 TEST_F(ASTVerifierTest, FunctionScope)
26 {
27 char const *text = R"(
28 function test(a: int) {
29 console.log(a)
30 }
31 )";
32
33 CONTEXT(ES2PANDA_STATE_LOWERED, text)
34 {
35 EXPECT_TRUE(Verify<CheckScopeDeclaration>());
36 }
37 }
38
TEST_F(ASTVerifierTest,ForUpdateLoopScope)39 TEST_F(ASTVerifierTest, ForUpdateLoopScope)
40 {
41 char const *text = R"(
42 function main() {
43 for (let i = 0; i < 10; i++) {}
44 }
45 )";
46
47 CONTEXT(ES2PANDA_STATE_LOWERED, text)
48 {
49 EXPECT_TRUE(Verify<CheckScopeDeclaration>());
50 }
51 }
52
TEST_F(ASTVerifierTest,ForInLoopScope)53 TEST_F(ASTVerifierTest, ForInLoopScope)
54 {
55 char const *text = R"(
56 function main(): void {
57 let res = 0
58 let arr: int[] = [1, 2, 3]
59 for (let i of arr) {
60 res += i
61 }
62 }
63 )";
64
65 CONTEXT(ES2PANDA_STATE_LOWERED, text)
66 {
67 EXPECT_TRUE(Verify<CheckScopeDeclaration>());
68 }
69 }
70
TEST_F(ASTVerifierTest,PartialClass)71 TEST_F(ASTVerifierTest, PartialClass)
72 {
73 char const *text = R"(
74 export class A {
75 private static readonly param: int = 0;
76 }
77 )";
78
79 CONTEXT(ES2PANDA_STATE_LOWERED, text)
80 {
81 EXPECT_TRUE(Verify<CheckScopeDeclaration>());
82 }
83 }
84
TEST_F(ASTVerifierTest,TryCatch)85 TEST_F(ASTVerifierTest, TryCatch)
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 CONTEXT(ES2PANDA_STATE_LOWERED, text)
100 {
101 EXPECT_TRUE(Verify<CheckScopeDeclaration>());
102 }
103 }
104
105 } // namespace
106