• 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 #include "ir/expressions/identifier.h"
19 #include "ir/astNode.h"
20 #include "parser/ETSparser.h"
21 
22 #include <gtest/gtest.h>
23 
24 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
25 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
26 using ark::es2panda::ir::AstNode;
27 
28 namespace {
TEST_F(ASTVerifierTest,LabelsHaveReferences)29 TEST_F(ASTVerifierTest, LabelsHaveReferences)
30 {
31     ASTVerifier verifier {Allocator()};
32 
33     char const *text = R"(
34         function main(): void {
35         loop: for (let i = 0; i < 10; i++) {
36         break loop;
37         }
38         }
39     )";
40 
41     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
42     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
43     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
44 
45     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
46 
47     InvariantNameSet checks;
48     checks.insert("IdentifierHasVariableForAll");
49     const auto &messages = verifier.Verify(ast, checks);
50     ASSERT_EQ(messages.size(), 0);
51 
52     impl_->DestroyContext(ctx);
53 }
54 
TEST_F(ASTVerifierTest,ExtensionFunction)55 TEST_F(ASTVerifierTest, ExtensionFunction)
56 {
57     ASTVerifier verifier {Allocator()};
58 
59     char const *text = R"(
60         class Fruit {
61             name(): void {
62             }
63         }
64 
65         function Fruit.name(id: int): void {
66         }
67 
68         function test() {
69         let fruit = new Fruit();
70         fruit.name()
71         }
72     )";
73 
74     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
75     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
76     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
77 
78     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
79 
80     InvariantNameSet checks;
81     checks.insert("IdentifierHasVariableForAll");
82     const auto &messages = verifier.Verify(ast, checks);
83     ASSERT_EQ(messages.size(), 0);
84 
85     impl_->DestroyContext(ctx);
86 }
87 
TEST_F(ASTVerifierTest,Imports)88 TEST_F(ASTVerifierTest, Imports)
89 {
90     ASTVerifier verifier {Allocator()};
91 
92     char const *text = R"(
93         import { PI } from "std/math";
94         import { A } from "dynamic_js_import_tests"
95         import default_imported from "import_tests/modules/default_export";
96         import * as Time from "std/time";
97     )";
98 
99     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
100     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
101     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
102 
103     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
104 
105     InvariantNameSet checks;
106     checks.insert("IdentifierHasVariableForAll");
107     const auto &messages = verifier.Verify(ast, checks);
108     ASSERT_EQ(messages.size(), 0);
109 
110     impl_->DestroyContext(ctx);
111 }
112 
TEST_F(ASTVerifierTest,OptionalLambdas)113 TEST_F(ASTVerifierTest, OptionalLambdas)
114 {
115     ASTVerifier verifier {Allocator()};
116 
117     char const *text = R"(
118         function main(): void {
119             let d = (c?: int) => {
120             }
121         }
122     )";
123 
124     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
125     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
126     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
127 
128     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
129 
130     InvariantNameSet checks;
131     checks.insert("IdentifierHasVariableForAll");
132     const auto &messages = verifier.Verify(ast, checks);
133     ASSERT_EQ(messages.size(), 0);
134 
135     impl_->DestroyContext(ctx);
136 }
137 }  // namespace
138