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 while (true) {}
42 }
43 )",
44 },
45 TestData {
46 R"(
47 function main () {
48 while (2 > 1) {}
49 }
50 )",
51 },
52 TestData {
53 R"(
54 function main () {
55 do {
56 } while (true)
57 }
58 )",
59 }
60 ));
61 // clang-format on
62
TEST_P(InfiniteLoopTests,InfiniteLoop)63 TEST_P(InfiniteLoopTests, InfiniteLoop)
64 {
65 ASTVerifier verifier {Allocator()};
66 TestData data = GetParam();
67
68 char const *text = data.program;
69
70 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets");
71 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
72 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
73
74 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
75
76 InvariantNameSet checks;
77 checks.insert("CheckInfiniteLoopForAll");
78 const auto &messages = verifier.Verify(ast, checks);
79
80 // Expecting warning
81 ASSERT_EQ(messages.size(), 1);
82 ASSERT_EQ(messages[0].Cause(), "INFINITE LOOP");
83
84 impl_->DestroyContext(ctx);
85 }
86 } // namespace
87