• 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 NormalLoopTests : public ASTVerifierTest, public testing::WithParamInterface<TestData> {};
33 
34 // clang-format off
35 INSTANTIATE_TEST_SUITE_P(,
36     NormalLoopTests,
37     testing::Values(
38         TestData {
39             R"(
40                 function main() {
41                     let counter = 0;
42                     while (counter < 10) {
43                         counter = counter + 1;
44                     }
45                 }
46             )",
47         },
48         TestData {
49             R"(
50                 function main() {
51                     let counter = 0;
52                     do {
53                         counter = counter + 1
54                     } while (counter < 10)
55                 }
56             )",
57         },
58         TestData {
59             R"(
60                 function main() {
61                     for (let i = 0; i < 10; ++i) {}
62                 }
63             )",
64         }));
65 // clang-format on
66 
TEST_P(NormalLoopTests,NormalLoop)67 TEST_P(NormalLoopTests, NormalLoop)
68 {
69     ASTVerifier verifier {Allocator()};
70 
71     TestData data = GetParam();
72     char const *text = data.program;
73 
74     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets");
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("CheckInfiniteLoopForAll");
82     const auto &messages = verifier.Verify(ast, checks);
83 
84     // Expecting no warnings
85     ASSERT_EQ(messages.size(), 0);
86 
87     impl_->DestroyContext(ctx);
88 }
89 }  // namespace
90