diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index 04be788b8c546ea40a8acb52aad192a8aacd2b3f..4261aa7faf1e7f59f027eca055d17027588a8f7b 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -444,6 +444,7 @@ set(ES2PANDA_LIB_SRC util/path.cpp util/pathHandler.cpp util/ustring.cpp + test/utils/panda_executable_path_getter.cpp ) panda_add_library(es2panda-lib ${PANDA_DEFAULT_LIB_TYPE} ${ES2PANDA_LIB_SRC}) diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 9974f49fa4431c93caa38a44a4ce8324ef9b7059..c8479d029118958bd835aeba3d924609e5ef284a 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -394,6 +394,9 @@ extern "C" void DestroyConfig(es2panda_Config *config) mem::MemConfig::Finalize(); auto *cfg = reinterpret_cast(config); + if (cfg == nullptr) { + return; + } delete cfg->options; delete cfg; diff --git a/ets2panda/test/unit/ast_dumper_test.cpp b/ets2panda/test/unit/ast_dumper_test.cpp index 2dedb2e6e64ed47004e014cb6beea2b87cc126bd..237336996d78d0922f6c0a78ec4b1d9602a6bcd8 100644 --- a/ets2panda/test/unit/ast_dumper_test.cpp +++ b/ets2panda/test/unit/ast_dumper_test.cpp @@ -30,50 +30,30 @@ #include "util/generateBin.h" #include "util/options.h" #include "libpandabase/mem/mem.h" +#include "test/utils/panda_executable_path_getter.h" -class ASTDumperTest : public testing::Test { -public: - ASTDumperTest() - { - constexpr auto COMPILER_SIZE = 268435456; +namespace { - ark::mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); - ark::PoolManager::Initialize(ark::PoolType::MMAP); - } - ~ASTDumperTest() override +struct TestParams { + explicit TestParams(std::string_view testSrc, std::string testArgsList = std::string {}, + int testArgsCount = ARGS_COUNT_DEFAULT, std::string_view testFileName = FILE_NAME_DEFAULT) + : src {testSrc}, argsList {std::move(testArgsList)}, argsCount {testArgsCount}, fileName {testFileName} { - ark::PoolManager::Finalize(); - ark::mem::MemConfig::Finalize(); - }; - - static ark::pandasm::Program *GetProgram(int argc, const char **argv, std::string_view fileName, - std::string_view src) - { - auto options = std::make_unique(); - if (!options->Parse(argc, argv)) { - std::cerr << options->ErrorMsg() << std::endl; - return nullptr; - } - - ark::Logger::ComponentMask mask {}; - mask.set(ark::Logger::Component::ES2PANDA); - ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask); - - ark::es2panda::Compiler compiler(options->Extension(), options->ThreadCount()); - ark::es2panda::SourceFile input(fileName, src, options->ParseModule()); - - return compiler.Compile(input, options->CompilerOptions()); } - NO_COPY_SEMANTIC(ASTDumperTest); - NO_MOVE_SEMANTIC(ASTDumperTest); + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + std::string_view src; + std::string argsList; + int argsCount; + std::string_view fileName; -private: + static constexpr int ARGS_COUNT_DEFAULT = 1; + static constexpr std::string_view FILE_NAME_DEFAULT = "dummy.ets"; + // NOLINTEND(misc-non-private-member-variables-in-classes) }; -TEST_F(ASTDumperTest, DumpJsonSimple) +TestParams DumpJsonSimple() { - static constexpr std::string_view FILE_NAME = "dummy.ets"; static constexpr std::string_view SRC = "\ function main(args: String[]): int {\ @@ -82,22 +62,11 @@ TEST_F(ASTDumperTest, DumpJsonSimple) return a + b;\ }"; - int argc = 1; - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - - auto program = std::unique_ptr {GetProgram(argc, &argv, FILE_NAME, SRC)}; - - ASSERT_NE(program, nullptr); - - auto dumpStr = program->JsonDump(); - - ASSERT_FALSE(dumpStr.empty()); + return TestParams {SRC}; } -TEST_F(ASTDumperTest, DumpJsonUTF16Char) +TestParams DumpJsonUTF16Char() { - static constexpr std::string_view FILE_NAME = "dummy.ets"; static constexpr std::string_view SRC = "\ function main(args: String[]): int {\ @@ -108,22 +77,11 @@ TEST_F(ASTDumperTest, DumpJsonUTF16Char) return 0;\ }"; - int argc = 1; - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - - auto program = std::unique_ptr {GetProgram(argc, &argv, FILE_NAME, SRC)}; - - ASSERT_NE(program, nullptr); - - auto dumpStr = program->JsonDump(); - - ASSERT_FALSE(dumpStr.empty()); + return TestParams {SRC}; } -TEST_F(ASTDumperTest, DumpEtsSrcSimple) +TestParams DumpEtsSrcSimple() { - static constexpr std::string_view FILE_NAME = "dummy.ets"; static constexpr std::string_view SRC = "\ function main(args: String[]): int {\ @@ -132,18 +90,74 @@ TEST_F(ASTDumperTest, DumpEtsSrcSimple) return a + b;\ }"; - int argc = 1; - const char *argv = - "../../../bin/es2panda " + auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get(); + auto argsList = + es2pandaPath + "--extension=ets " - "--dump-ets-src-before-phases=\"plugins-after-parse:lambda-lowering:checker:plugins-after-check:generate-ts-" + "--dump-ets-src-before-phases=\"plugins-after-parse:lambda-lowering:checker:plugins-after-check:generate-" + "ts-" "declarations:op-assignment:tuple-lowering:union-property-access:plugins-after-lowering\""; - auto program = std::unique_ptr {GetProgram(argc, &argv, FILE_NAME, SRC)}; + return TestParams {SRC, argsList}; +} - ASSERT_NE(program, nullptr); +} // namespace - auto dumpStr = program->JsonDump(); +class ASTDumperTest : public testing::TestWithParam { +public: + ASTDumperTest() + { + constexpr auto COMPILER_SIZE = 268435456; + + ark::mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); + ark::PoolManager::Initialize(ark::PoolType::MMAP); + } + + ~ASTDumperTest() override + { + ark::PoolManager::Finalize(); + ark::mem::MemConfig::Finalize(); + }; + + static ark::pandasm::Program *GetProgram(std::string_view src, const char **argsList, int argsCount, + std::string_view fileName) + { + auto options = std::make_unique(); + if (!options->Parse(argsCount, argsList)) { + std::cerr << options->ErrorMsg() << std::endl; + return nullptr; + } + + ark::Logger::ComponentMask mask {}; + mask.set(ark::Logger::Component::ES2PANDA); + ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask); + + ark::es2panda::Compiler compiler(options->Extension(), options->ThreadCount()); + ark::es2panda::SourceFile input(fileName, src, options->ParseModule()); + + return compiler.Compile(input, options->CompilerOptions()); + } + + NO_COPY_SEMANTIC(ASTDumperTest); + NO_MOVE_SEMANTIC(ASTDumperTest); +}; + +TEST_P(ASTDumperTest, CheckNoDump) +{ + auto param = GetParam(); + if (param.argsList.empty()) { + param.argsList = test::utils::PandaExecutablePathGetter {}.Get(); + } + + auto argsListPtr = param.argsList.c_str(); + + auto program = + std::unique_ptr {GetProgram(param.src, &argsListPtr, param.argsCount, param.fileName)}; + ASSERT(program); - ASSERT_FALSE(dumpStr.empty()); + auto dumpStr = program->JsonDump(); + ASSERT(!dumpStr.empty()); } + +INSTANTIATE_TEST_SUITE_P(ASTDumperTestParamList, ASTDumperTest, + ::testing::Values(DumpJsonSimple(), DumpJsonUTF16Char(), DumpEtsSrcSimple())); diff --git a/ets2panda/test/unit/checker_test.cpp b/ets2panda/test/unit/checker_test.cpp index 580a01d4722d34dfb33ce11b8aecf66d63f6c6c1..2f21e1def2ab71cff376fe3cfa09053862473896 100644 --- a/ets2panda/test/unit/checker_test.cpp +++ b/ets2panda/test/unit/checker_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Device Co., Ltd. 2021 - 2023. All rights reserved. + * Copyright (c) Huawei Device Co., Ltd. 2021 - 2024. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,13 +17,16 @@ #include "macros.h" #include "public/es2panda_lib.h" +#include "test/utils/panda_executable_path_getter.h" + class CheckerTest : public testing::Test { public: CheckerTest() { impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get(); // NOLINTNEXTLINE(modernize-avoid-c-arrays) - char const *argv[] = {"../../../bin/es2panda test"}; + char const *argv[] = {es2pandaPath.c_str()}; cfg_ = impl_->CreateConfig(1, argv); } diff --git a/ets2panda/test/unit/public/ast_verifier_test.cpp b/ets2panda/test/unit/public/ast_verifier_test.cpp index 3fcd7907f2926c5e70b920a49e4a820a788965c6..1ca8571522f001822753b449bf41ffa48bb977d5 100644 --- a/ets2panda/test/unit/public/ast_verifier_test.cpp +++ b/ets2panda/test/unit/public/ast_verifier_test.cpp @@ -22,6 +22,7 @@ #include "parser/ETSparser.h" #include "varbinder/ETSBinder.h" #include "compiler/core/ASTVerifier.h" +#include "test/utils/panda_executable_path_getter.h" #include @@ -56,8 +57,9 @@ public: ASTVerifierTest() { impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get(); // NOLINTNEXTLINE(modernize-avoid-c-arrays) - char const *argv[] = {"../../../bin/es2panda test"}; + char const *argv[] = {es2pandaPath.c_str()}; cfg_ = impl_->CreateConfig(1, argv); allocator_ = new ark::ArenaAllocator(ark::SpaceType::SPACE_TYPE_COMPILER); } diff --git a/ets2panda/test/unit/public/es2panda_public_test.cpp b/ets2panda/test/unit/public/es2panda_public_test.cpp index 2987e79dc316da3dc0b54db362345bc1f860ee49..e397717cc13851503669d2f216c0590f9d8d7648 100644 --- a/ets2panda/test/unit/public/es2panda_public_test.cpp +++ b/ets2panda/test/unit/public/es2panda_public_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,14 +16,16 @@ #include #include "macros.h" #include "public/es2panda_lib.h" +#include "test/utils/panda_executable_path_getter.h" class Es2PandaLibTest : public testing::Test { public: Es2PandaLibTest() { impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get(); // NOLINTNEXTLINE(modernize-avoid-c-arrays) - char const *argv[] = {"../../../bin/es2panda"}; + char const *argv[] = {es2pandaPath.c_str()}; cfg_ = impl_->CreateConfig(1, argv); } diff --git a/ets2panda/test/unit/union_normalization_test.cpp b/ets2panda/test/unit/union_normalization_test.cpp index 038b7e99bf5f94d9e40832259410f8ff79447498..72224aa6fa4642997f91d8975c17410933f85b9d 100644 --- a/ets2panda/test/unit/union_normalization_test.cpp +++ b/ets2panda/test/unit/union_normalization_test.cpp @@ -31,15 +31,18 @@ #include "util/arktsconfig.h" #include "util/generateBin.h" #include "varbinder/ETSBinder.h" +#include "test/utils/panda_executable_path_getter.h" namespace ark::es2panda { class UnionNormalizationTest : public testing::Test { public: UnionNormalizationTest() + : allocator_(std::make_unique(SpaceType::SPACE_TYPE_COMPILER)), + publicContext_ {std::make_unique()}, + program_ {parser::Program::NewProgram(Allocator())}, + es2pandaPath_ {test::utils::PandaExecutablePathGetter {}.Get()} { - allocator_ = std::make_unique(SpaceType::SPACE_TYPE_COMPILER); - publicContext_ = std::make_unique(); } ~UnionNormalizationTest() override = default; @@ -56,12 +59,25 @@ public: return allocator_.get(); } - void InitializeChecker(const char **argv, std::string_view fileName, std::string_view src, - checker::ETSChecker *checker, parser::Program *program) + parser::Program *Program() + { + return &program_; + } + + checker::ETSChecker *Checker() + { + return &checker_; + } + + void InitializeChecker(std::string_view fileName, std::string_view src) { + auto es2pandaPathPtr = es2pandaPath_.c_str(); + ASSERT(es2pandaPathPtr); + InitializeChecker(argv, fileName, src, checker, program); + compiler::ETSFunctionEmitter, compiler::ETSEmitter>(&es2pandaPathPtr, fileName, src, + &checker_, &program_); } template @@ -164,194 +180,203 @@ protected: private: std::unique_ptr allocator_; std::unique_ptr publicContext_; + parser::Program program_; + std::string es2pandaPath_; + checker::ETSChecker checker_; }; TEST_F(UnionNormalizationTest, UnionWithObject) { // Test normalization: int | Object | string ==> Object - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "", &checker, &program); + InitializeChecker("_.ets", ""); - ArenaVector unionConstituents(checker.Allocator()->Adapter()); - unionConstituents.emplace_back(checker.GlobalIntType()); - unionConstituents.emplace_back(checker.GetGlobalTypesHolder()->GlobalETSObjectType()); - unionConstituents.emplace_back(checker.GetGlobalTypesHolder()->GlobalETSStringBuiltinType()); + auto checker = Checker(); + ASSERT(checker); + + ArenaVector unionConstituents(checker->Allocator()->Adapter()); + unionConstituents.emplace_back(checker->GlobalIntType()); + unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalETSObjectType()); + unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalETSStringBuiltinType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSObjectType()); - ASSERT_EQ(normalizedType, checker.GlobalETSObjectType()); + ASSERT_EQ(normalizedType, checker->GlobalETSObjectType()); } TEST_F(UnionNormalizationTest, UnionWithIdenticalTypes1) { // Test normalization: number | Base | string | number ==> number | Base | string - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "class Base {}", &checker, &program); + InitializeChecker("_.ets", "class Base {}"); + + auto program = Program(); + ASSERT(program); - auto *const baseType = FindClassType(program.VarBinder()->AsETSBinder(), "Base"); + auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); ASSERT_NE(baseType, nullptr); - ArenaVector unionConstituents(checker.Allocator()->Adapter()); - unionConstituents.emplace_back(checker.GlobalDoubleType()); + auto checker = Checker(); + ASSERT(checker); + + ArenaVector unionConstituents(checker->Allocator()->Adapter()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); unionConstituents.emplace_back(baseType); - unionConstituents.emplace_back(checker.GlobalBuiltinETSStringType()); - unionConstituents.emplace_back(checker.GlobalDoubleType()); + unionConstituents.emplace_back(checker->GlobalBuiltinETSStringType()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSUnionType()); auto *const unionType = normalizedType->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE3); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), baseType); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker.GlobalBuiltinETSStringType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker->GlobalBuiltinETSStringType()); } TEST_F(UnionNormalizationTest, DISABLED_UnionWithIdenticalTypes2) { // Test normalization: Base | int | Base | double | short | number ==> Base | number - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "class Base {}", &checker, &program); + InitializeChecker("_.ets", "class Base {}"); - auto *const baseType = FindClassType(program.VarBinder()->AsETSBinder(), "Base"); + auto program = Program(); + ASSERT(program); + + auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); ASSERT_NE(baseType, nullptr); - ArenaVector unionConstituents(checker.Allocator()->Adapter()); + auto checker = Checker(); + ASSERT(checker); + + ArenaVector unionConstituents(checker->Allocator()->Adapter()); unionConstituents.emplace_back(baseType); - unionConstituents.emplace_back(checker.GlobalIntType()); + unionConstituents.emplace_back(checker->GlobalIntType()); unionConstituents.emplace_back(baseType); - unionConstituents.emplace_back(checker.GlobalDoubleType()); - unionConstituents.emplace_back(checker.GlobalShortType()); - unionConstituents.emplace_back(checker.GlobalDoubleType()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); + unionConstituents.emplace_back(checker->GlobalShortType()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSUnionType()); auto *const unionType = normalizedType->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), baseType); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); } TEST_F(UnionNormalizationTest, DISABLED_UnionWithNumeric1) { // Test normalization: boolean | int | double | short ==> boolean | double - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "", &checker, &program); - - ArenaVector unionConstituents(checker.Allocator()->Adapter()); - unionConstituents.emplace_back(checker.GlobalETSBooleanType()); - unionConstituents.emplace_back(checker.GlobalIntType()); - unionConstituents.emplace_back(checker.GlobalDoubleType()); - unionConstituents.emplace_back(checker.GlobalShortType()); + InitializeChecker("_.ets", ""); + + auto checker = Checker(); + ASSERT(checker); + + ArenaVector unionConstituents(checker->Allocator()->Adapter()); + unionConstituents.emplace_back(checker->GlobalETSBooleanType()); + unionConstituents.emplace_back(checker->GlobalIntType()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); + unionConstituents.emplace_back(checker->GlobalShortType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSUnionType()); auto *const unionType = normalizedType->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker.GetGlobalTypesHolder()->GlobalETSBooleanBuiltinType()); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalETSBooleanBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); } TEST_F(UnionNormalizationTest, DISABLED_UnionWithNumeric2) { // Test normalization: string | int | Base | double | short ==> string | Base | double - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "class Base {}", &checker, &program); + InitializeChecker("_.ets", "class Base {}"); + + auto program = Program(); + ASSERT(program); - auto *const baseType = FindClassType(program.VarBinder()->AsETSBinder(), "Base"); + auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); ASSERT_NE(baseType, nullptr); - ArenaVector unionConstituents(checker.Allocator()->Adapter()); - unionConstituents.emplace_back(checker.GlobalBuiltinETSStringType()); - unionConstituents.emplace_back(checker.GlobalIntType()); + auto checker = Checker(); + ASSERT(checker); + + ArenaVector unionConstituents(checker->Allocator()->Adapter()); + unionConstituents.emplace_back(checker->GlobalBuiltinETSStringType()); + unionConstituents.emplace_back(checker->GlobalIntType()); unionConstituents.emplace_back(baseType); - unionConstituents.emplace_back(checker.GlobalDoubleType()); - unionConstituents.emplace_back(checker.GlobalShortType()); + unionConstituents.emplace_back(checker->GlobalDoubleType()); + unionConstituents.emplace_back(checker->GlobalShortType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSUnionType()); auto *const unionType = normalizedType->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE3); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker.GlobalBuiltinETSStringType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), baseType); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); } TEST_F(UnionNormalizationTest, UnionWithSubTypes) { // Test 4 cases of normalization - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); static constexpr std::string_view SRC = "\ class Base {}\ class Derived1 extends Base {}\ class Derived2 extends Base {}\ "; - InitializeChecker(&argv, "_.ets", SRC, &checker, &program); + InitializeChecker("_.ets", SRC); - auto *const baseType = FindClassType(program.VarBinder()->AsETSBinder(), "Base"); + auto program = Program(); + ASSERT(program); + + auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); ASSERT_NE(baseType, nullptr); - auto *const derived1Type = FindClassType(program.VarBinder()->AsETSBinder(), "Derived1"); + auto *const derived1Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived1"); ASSERT_NE(derived1Type, nullptr); - auto *const derived2Type = FindClassType(program.VarBinder()->AsETSBinder(), "Derived2"); + auto *const derived2Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived2"); ASSERT_NE(derived2Type, nullptr); + auto checker = Checker(); + ASSERT(checker); + // Test normalization: Derived1 | Base ==> Base - ArenaVector unionConstituents1(checker.Allocator()->Adapter()); + ArenaVector unionConstituents1(checker->Allocator()->Adapter()); unionConstituents1.emplace_back(derived1Type); unionConstituents1.emplace_back(baseType); // Create union type, which will be normalized inside creation function - auto *const normalizedType1 = checker.CreateETSUnionType(std::move(unionConstituents1)); + auto *const normalizedType1 = checker->CreateETSUnionType(std::move(unionConstituents1)); ASSERT_NE(normalizedType1, nullptr); ASSERT_TRUE(normalizedType1->IsETSObjectType()); ASSERT_EQ(normalizedType1, baseType); // Test normalization: Base | Derived2 ==> Base - ArenaVector unionConstituents2(checker.Allocator()->Adapter()); + ArenaVector unionConstituents2(checker->Allocator()->Adapter()); unionConstituents2.emplace_back(baseType); unionConstituents2.emplace_back(derived2Type); // Create union type, which will be normalized inside creation function - auto *const normalizedType2 = checker.CreateETSUnionType(std::move(unionConstituents2)); + auto *const normalizedType2 = checker->CreateETSUnionType(std::move(unionConstituents2)); ASSERT_NE(normalizedType2, nullptr); ASSERT_TRUE(normalizedType2->IsETSObjectType()); ASSERT_EQ(normalizedType2, baseType); // Test normalization: Derived1 | Derived2 ==> Derived1 | Derived2 - ArenaVector unionConstituents3(checker.Allocator()->Adapter()); + ArenaVector unionConstituents3(checker->Allocator()->Adapter()); unionConstituents3.emplace_back(derived1Type); unionConstituents3.emplace_back(derived2Type); // Create union type, which will be normalized inside creation function - auto *const normalizedType3 = checker.CreateETSUnionType(std::move(unionConstituents3)); + auto *const normalizedType3 = checker->CreateETSUnionType(std::move(unionConstituents3)); ASSERT_NE(normalizedType3, nullptr); auto *const unionType = normalizedType3->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); @@ -359,13 +384,13 @@ TEST_F(UnionNormalizationTest, UnionWithSubTypes) ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), derived2Type); // Test normalization: Derived2 | Base | Derived1 ==> Base - ArenaVector unionConstituents4(checker.Allocator()->Adapter()); + ArenaVector unionConstituents4(checker->Allocator()->Adapter()); unionConstituents4.emplace_back(derived1Type); unionConstituents4.emplace_back(baseType); unionConstituents4.emplace_back(derived2Type); // Create union type, which will be normalized inside creation function - auto *const normalizedType4 = checker.CreateETSUnionType(std::move(unionConstituents4)); + auto *const normalizedType4 = checker->CreateETSUnionType(std::move(unionConstituents4)); ASSERT_NE(normalizedType4, nullptr); ASSERT_TRUE(normalizedType4->IsETSObjectType()); ASSERT_EQ(normalizedType4, baseType); @@ -374,10 +399,6 @@ TEST_F(UnionNormalizationTest, UnionWithSubTypes) TEST_F(UnionNormalizationTest, DISABLED_UnionLinearization) { // Test 3 cases of normalization - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); static constexpr std::string_view SRC = "\ class Base {}\ @@ -389,113 +410,117 @@ TEST_F(UnionNormalizationTest, DISABLED_UnionLinearization) type UT2 = int | UT | number\ type UT3 = int | (Derived2 | Base) | Derived1 | (string | number | short) | (int | string)\ "; - InitializeChecker(&argv, "_.ets", SRC, &checker, &program); + InitializeChecker("_.ets", SRC); + + auto program = Program(); + ASSERT(program); - auto *varbinder = program.VarBinder()->AsETSBinder(); + auto *varbinder = program->VarBinder()->AsETSBinder(); auto *const baseType = FindClassType(varbinder, "Base"); ASSERT_NE(baseType, nullptr); - auto *const derived1Type = FindClassType(program.VarBinder()->AsETSBinder(), "Derived1"); + auto *const derived1Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived1"); ASSERT_NE(derived1Type, nullptr); - auto *const derived2Type = FindClassType(program.VarBinder()->AsETSBinder(), "Derived2"); + auto *const derived2Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived2"); ASSERT_NE(derived2Type, nullptr); + auto checker = Checker(); + ASSERT(checker); + // Test normalization: int | (int | string) | number ==> string | number - auto *const ut1Type = FindTypeAlias(&checker, "UT1"); + auto *const ut1Type = FindTypeAlias(checker, "UT1"); ASSERT_NE(ut1Type, nullptr); ASSERT_TRUE(ut1Type->IsETSUnionType()); auto *ut1 = ut1Type->AsETSUnionType(); ASSERT_EQ(ut1->ConstituentTypes().size(), SIZE2); - ASSERT_EQ(ut1->ConstituentTypes().at(IDX0), checker.GlobalBuiltinETSStringType()); - ASSERT_EQ(ut1->ConstituentTypes().at(IDX1), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(ut1->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); + ASSERT_EQ(ut1->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); // Test normalization: int | UT | number ==> string | number - auto *const ut2Type = FindTypeAlias(&checker, "UT2"); + auto *const ut2Type = FindTypeAlias(checker, "UT2"); ASSERT_NE(ut2Type, nullptr); ASSERT_TRUE(ut2Type->IsETSUnionType()); auto *ut2 = ut2Type->AsETSUnionType(); ASSERT_EQ(ut2->ConstituentTypes().size(), SIZE2); - ASSERT_EQ(ut2->ConstituentTypes().at(IDX0), checker.GlobalBuiltinETSStringType()); - ASSERT_EQ(ut2->ConstituentTypes().at(IDX1), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(ut2->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); + ASSERT_EQ(ut2->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); // Test normalization: // int | (Derived2 | Base) | Derived1 | (string | number | short) | (int | string) ==> Base | string | number - auto *const ut3Type = FindTypeAlias(&checker, "UT3"); + auto *const ut3Type = FindTypeAlias(checker, "UT3"); ASSERT_NE(ut3Type, nullptr); ASSERT_TRUE(ut3Type->IsETSUnionType()); auto *ut3 = ut3Type->AsETSUnionType(); ASSERT_EQ(ut3->ConstituentTypes().size(), SIZE3); ASSERT_EQ(ut3->ConstituentTypes().at(IDX0), baseType); - ASSERT_EQ(ut3->ConstituentTypes().at(IDX1), checker.GlobalBuiltinETSStringType()); - ASSERT_EQ(ut3->ConstituentTypes().at(IDX2), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(ut3->ConstituentTypes().at(IDX1), checker->GlobalBuiltinETSStringType()); + ASSERT_EQ(ut3->ConstituentTypes().at(IDX2), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); } TEST_F(UnionNormalizationTest, UnionStringLiterals) { - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "", &checker, &program); + InitializeChecker("_.ets", ""); + + auto checker = Checker(); + ASSERT(checker); // Test normalization: string | "abc" ==> string - ArenaVector unionConstituents1(checker.Allocator()->Adapter()); - unionConstituents1.emplace_back(checker.GlobalBuiltinETSStringType()); - unionConstituents1.emplace_back(checker.CreateETSStringLiteralType("abc")); + ArenaVector unionConstituents1(checker->Allocator()->Adapter()); + unionConstituents1.emplace_back(checker->GlobalBuiltinETSStringType()); + unionConstituents1.emplace_back(checker->CreateETSStringLiteralType("abc")); // Create union type, which will be normalized inside creation function - auto *const normalizedType1 = checker.CreateETSUnionType(std::move(unionConstituents1)); + auto *const normalizedType1 = checker->CreateETSUnionType(std::move(unionConstituents1)); ASSERT_NE(normalizedType1, nullptr); ASSERT_TRUE(normalizedType1->IsETSObjectType()); - ASSERT_EQ(normalizedType1, checker.GlobalBuiltinETSStringType()); + ASSERT_EQ(normalizedType1, checker->GlobalBuiltinETSStringType()); // Test normalization: "abc" | string | string ==> string - ArenaVector unionConstituents2(checker.Allocator()->Adapter()); - unionConstituents2.emplace_back(checker.CreateETSStringLiteralType("abc")); - unionConstituents2.emplace_back(checker.GlobalBuiltinETSStringType()); - unionConstituents2.emplace_back(checker.GlobalBuiltinETSStringType()); + ArenaVector unionConstituents2(checker->Allocator()->Adapter()); + unionConstituents2.emplace_back(checker->CreateETSStringLiteralType("abc")); + unionConstituents2.emplace_back(checker->GlobalBuiltinETSStringType()); + unionConstituents2.emplace_back(checker->GlobalBuiltinETSStringType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType2 = checker.CreateETSUnionType(std::move(unionConstituents2)); + auto *const normalizedType2 = checker->CreateETSUnionType(std::move(unionConstituents2)); ASSERT_NE(normalizedType2, nullptr); ASSERT_TRUE(normalizedType2->IsETSObjectType()); - ASSERT_EQ(normalizedType2, checker.GlobalBuiltinETSStringType()); + ASSERT_EQ(normalizedType2, checker->GlobalBuiltinETSStringType()); // Test normalization: number | "abc" | string | "xy" ==> number | string - ArenaVector unionConstituents3(checker.Allocator()->Adapter()); - unionConstituents3.emplace_back(checker.GlobalDoubleType()); - unionConstituents3.emplace_back(checker.CreateETSStringLiteralType("abc")); - unionConstituents3.emplace_back(checker.GlobalBuiltinETSStringType()); - unionConstituents3.emplace_back(checker.CreateETSStringLiteralType("xy")); + ArenaVector unionConstituents3(checker->Allocator()->Adapter()); + unionConstituents3.emplace_back(checker->GlobalDoubleType()); + unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("abc")); + unionConstituents3.emplace_back(checker->GlobalBuiltinETSStringType()); + unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("xy")); // Create union type, which will be normalized inside creation function - auto *const normalizedType3 = checker.CreateETSUnionType(std::move(unionConstituents3)); + auto *const normalizedType3 = checker->CreateETSUnionType(std::move(unionConstituents3)); ASSERT_NE(normalizedType3, nullptr); ASSERT_TRUE(normalizedType3->IsETSUnionType()); auto *const unionType = normalizedType3->AsETSUnionType(); ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); - ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker.GlobalBuiltinETSStringType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GlobalBuiltinETSStringType()); } TEST_F(UnionNormalizationTest, DISABLED_UnionWithNever) { // Test normalization: int | never | number ==> number - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - const char *argv = "../../../bin/es2panda"; - checker::ETSChecker checker; - auto program = parser::Program::NewProgram(Allocator()); - InitializeChecker(&argv, "_.ets", "", &checker, &program); + InitializeChecker("_.ets", ""); + + auto checker = Checker(); + ASSERT(checker); - ArenaVector unionConstituents(checker.Allocator()->Adapter()); - unionConstituents.emplace_back(checker.GlobalIntType()); - unionConstituents.emplace_back(checker.GetGlobalTypesHolder()->GlobalBuiltinNeverType()); - unionConstituents.emplace_back(checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ArenaVector unionConstituents(checker->Allocator()->Adapter()); + unionConstituents.emplace_back(checker->GlobalIntType()); + unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalBuiltinNeverType()); + unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); // Create union type, which will be normalized inside creation function - auto *const normalizedType = checker.CreateETSUnionType(std::move(unionConstituents)); + auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); ASSERT_NE(normalizedType, nullptr); ASSERT_TRUE(normalizedType->IsETSObjectType()); - ASSERT_EQ(normalizedType, checker.GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); + ASSERT_EQ(normalizedType, checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); } } // namespace ark::es2panda diff --git a/ets2panda/test/utils/panda_executable_path_getter.cpp b/ets2panda/test/utils/panda_executable_path_getter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d4af4a940f0d2c46f1c6d007e3ed38228f25918 --- /dev/null +++ b/ets2panda/test/utils/panda_executable_path_getter.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "macros.h" +#include "panda_executable_path_getter.h" + +namespace test::utils { + +std::string PandaExecutablePathGetter::Get() const +{ +#ifdef BUILD_FOLDER + return BUILD_FOLDER + std::string("/bin/es2panda test"); +#else + ASSERT_PRINT(false, "BUILD FOLDER not set"); + return std::string {}; +#endif +} + +} // namespace test::utils diff --git a/ets2panda/test/utils/panda_executable_path_getter.h b/ets2panda/test/utils/panda_executable_path_getter.h new file mode 100644 index 0000000000000000000000000000000000000000..824de3530ea658ad7827af587024542be5a6f0f1 --- /dev/null +++ b/ets2panda/test/utils/panda_executable_path_getter.h @@ -0,0 +1,32 @@ +#ifndef PANDA_EXECUTABLE_PATH_GETTER +#define PANDA_EXECUTABLE_PATH_GETTER + +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace test::utils { + +class PandaExecutablePathGetter { +public: + PandaExecutablePathGetter() = default; + + std::string Get() const; +}; + +} // namespace test::utils + +#endif