• 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 #include "public/es2panda_lib.h"
19 
20 #include <gtest/gtest.h>
21 
22 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
23 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
24 using ark::es2panda::ir::AstNode;
25 
26 namespace {
27 struct TestData {
28     char const *program;
29 };
30 
31 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
32 class InfiniteLoopTests : public ASTVerifierTest, public testing::WithParamInterface<TestData> {};
33 
34 // clang-format off
35 INSTANTIATE_TEST_SUITE_P(,
36     InfiniteLoopTests,
37     testing::Values(
38         TestData {
39             R"(
40                 function main () {
41                     do {
42                     } while (2 > 1)
43                 }
44             )",
45         },
46         TestData {
47             R"(
48                 function main () {
49                     for (;;) {}
50                 }
51             )",
52         },
53         TestData {
54             R"(
55                 function main () {
56                     for (; 2 > 1;) {}
57                 }
58             )",
59         }));
60 // clang-format on
61 
TEST_P(InfiniteLoopTests,InfiniteLoop)62 TEST_P(InfiniteLoopTests, InfiniteLoop)
63 {
64     ASTVerifier verifier {Allocator()};
65     TestData data = GetParam();
66 
67     char const *text = data.program;
68 
69     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets");
70     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
71     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
72 
73     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
74 
75     InvariantNameSet checks;
76     checks.insert("CheckInfiniteLoopForAll");
77     const auto &messages = verifier.Verify(ast, checks);
78 
79     // Expecting warning
80     ASSERT_EQ(messages.size(), 1);
81     ASSERT_EQ(messages[0].Cause(), "INFINITE LOOP");
82 
83     impl_->DestroyContext(ctx);
84 }
85 }  // namespace
86