1 /**
2 * Copyright (c) 2021-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 <gtest/gtest.h>
17 #include <algorithm>
18 #include "macros.h"
19
20 #include "test/utils/node_creator.h"
21 #include "test/utils/scope_init_test.h"
22 #include "compiler/lowering/scopesInit/scopesInitPhase.h"
23 #include "varbinder/tsBinding.h"
24 #include "varbinder/ETSBinder.h"
25
26 namespace ark::es2panda {
27
28 class ScopesInitPhaseTest : public test::utils::ScopeInitTest {
29 public:
30 ~ScopesInitPhaseTest() override = default;
31
ScopesInitPhaseTest()32 ScopesInitPhaseTest()
33 : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER)), nodeGen_(allocator_.get())
34 {
35 }
36
Allocator()37 ArenaAllocator *Allocator()
38 {
39 return allocator_.get();
40 }
41
NodeGen()42 gtests::NodeGenerator &NodeGen()
43 {
44 return nodeGen_;
45 }
46
47 NO_COPY_SEMANTIC(ScopesInitPhaseTest);
48 NO_MOVE_SEMANTIC(ScopesInitPhaseTest);
49
50 private:
51 std::unique_ptr<ArenaAllocator> allocator_;
52 gtests::NodeGenerator nodeGen_;
53 };
54
TEST_F(ScopesInitPhaseTest,TestForUpdateLoop)55 TEST_F(ScopesInitPhaseTest, TestForUpdateLoop)
56 {
57 /*
58 * for (int x = 0; x < 10; x++ ) { let x; }
59 */
60 auto varbinder = varbinder::VarBinder(Allocator());
61 auto forNode = NodeGen().CreateForUpdate();
62 compiler::InitScopesPhaseETS::RunExternalNode(forNode, &varbinder);
63
64 auto blockScope = forNode->Body()->AsBlockStatement()->Scope();
65 auto loopScope = forNode->Scope();
66 auto parScope = loopScope->Parent();
67 ASSERT_EQ(blockScope->Parent(), loopScope);
68
69 const auto &scopeBindings = blockScope->Bindings();
70 const auto &parBindings = parScope->Bindings();
71
72 ASSERT_EQ(scopeBindings.size(), 1);
73 ASSERT_EQ(parBindings.size(), 1);
74
75 auto parName = forNode->Init()->AsVariableDeclaration()->Declarators()[0]->Id()->AsIdentifier();
76 auto name = BodyToFirstName(forNode->Body());
77 ASSERT_EQ(scopeBindings.begin()->first, name->Name());
78 ASSERT_EQ(parBindings.begin()->first, parName->Name());
79 ASSERT_EQ(scopeBindings.begin()->second, name->Variable());
80 ASSERT_EQ(parBindings.begin()->second, parName->Variable());
81 ASSERT_NE(parName->Variable(), name->Variable());
82 }
83
TEST_F(ScopesInitPhaseTest,CreateWhile)84 TEST_F(ScopesInitPhaseTest, CreateWhile)
85 {
86 /*
87 * while (x < 10) { let x; }
88 */
89 auto varbinder = varbinder::VarBinder(Allocator());
90 auto whileNode = NodeGen().CreateWhile();
91
92 compiler::InitScopesPhaseETS::RunExternalNode(whileNode, &varbinder);
93
94 auto whileScope = whileNode->Scope();
95 auto bodyScope = whileNode->Body()->AsBlockStatement()->Scope();
96 ASSERT_EQ(bodyScope->Parent(), whileScope);
97
98 const auto &bodyBindings = bodyScope->Bindings();
99 auto name = BodyToFirstName(whileNode->Body());
100 ASSERT_EQ(bodyBindings.size(), 1);
101 ASSERT_EQ(bodyBindings.begin()->first, name->Name());
102 ASSERT_EQ(bodyBindings.begin()->second, name->Variable());
103 }
104
105 } // namespace ark::es2panda
106