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,ReturnTypeInLambda)29 TEST_F(ASTVerifierTest, ReturnTypeInLambda)
30 {
31 ASTVerifier verifier {Allocator()};
32 char const *text = R"(
33 function main(): void {
34 let x: () => void = ()=> {}
35 }
36 )";
37
38 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
39 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
40 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
41
42 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
43
44 InvariantNameSet checks;
45 checks.insert("EveryChildHasValidParentForAll");
46 const auto &messages = verifier.Verify(ast, checks);
47 ASSERT_EQ(messages.size(), 0);
48
49 impl_->DestroyContext(ctx);
50 }
51
TEST_F(ASTVerifierTest,TSThisType)52 TEST_F(ASTVerifierTest, TSThisType)
53 {
54 ASTVerifier verifier {Allocator()};
55 char const *text = R"(
56 class A {
57 foo(a?: Number): this { return this; }
58 }
59
60 function main () {}
61 )";
62
63 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
64 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
65 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
66
67 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
68
69 InvariantNameSet checks;
70 checks.insert("EveryChildHasValidParentForAll");
71 const auto &messages = verifier.Verify(ast, checks);
72 ASSERT_EQ(messages.size(), 0);
73
74 impl_->DestroyContext(ctx);
75 }
76
TEST_F(ASTVerifierTest,TupleFieldInInterface)77 TEST_F(ASTVerifierTest, TupleFieldInInterface)
78 {
79 ASTVerifier verifier {Allocator()};
80 char const *text = R"(
81 interface I {
82 field: [String, String]
83 }
84 )";
85
86 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
87 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
88 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
89
90 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
91
92 InvariantNameSet checks;
93 checks.insert("EveryChildHasValidParentForAll");
94 const auto &messages = verifier.Verify(ast, checks);
95 ASSERT_EQ(messages.size(), 0);
96
97 impl_->DestroyContext(ctx);
98 }
99
100 } // namespace
101