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 #ifndef ES2PANDA_TEST_LOWERINGS_LOWERING_TEST_H 17 #define ES2PANDA_TEST_LOWERINGS_LOWERING_TEST_H 18 19 #include <gtest/gtest.h> 20 #include "test/unit/node_creator.h" 21 #include "test/utils/panda_executable_path_getter.h" 22 #include "parser/program/program.h" 23 24 namespace ark::es2panda { 25 26 class LoweringTest : public testing::Test { 27 public: LoweringTest()28 LoweringTest() 29 { 30 impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); 31 auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get(); 32 std::array argv = {es2pandaPath.c_str()}; 33 cfg_ = impl_->CreateConfig(argv.size(), argv.data()); 34 allocator_ = new ark::ArenaAllocator(ark::SpaceType::SPACE_TYPE_COMPILER); 35 } 36 ~LoweringTest()37 ~LoweringTest() override 38 { 39 if (ctx_ != nullptr) { 40 impl_->DestroyContext(ctx_); 41 } 42 delete allocator_; 43 impl_->DestroyConfig(cfg_); 44 } 45 Allocator()46 ark::ArenaAllocator *Allocator() 47 { 48 return allocator_; 49 } 50 SetupContext(const char * text,es2panda_ContextState state)51 parser::Program *SetupContext(const char *text, es2panda_ContextState state) 52 { 53 ASSERT(ctx_ == nullptr); 54 ctx_ = impl_->CreateContextFromString(cfg_, text, "dummy.sts"); 55 impl_->ProceedToState(ctx_, state); 56 ASSERT(impl_->ContextState(ctx_) == state); 57 58 return reinterpret_cast<parser::Program *>(impl_->ContextProgram(ctx_)); 59 } 60 61 NO_COPY_SEMANTIC(LoweringTest); 62 NO_MOVE_SEMANTIC(LoweringTest); 63 64 protected: 65 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 66 es2panda_Impl const *impl_ {}; 67 es2panda_Config *cfg_ {}; 68 ark::ArenaAllocator *allocator_ {}; 69 70 es2panda_Context *ctx_ {}; 71 // NOLINTEND(misc-non-private-member-variables-in-classes) 72 }; 73 74 } // namespace ark::es2panda 75 76 #endif // ES2PANDA_TEST_LOWERINGS_LOWERING_TEST_H 77