13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include <gtest/gtest.h> 173af6ab5fSopenharmony_ci#include <algorithm> 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "checker/ETSAnalyzer.h" 203af6ab5fSopenharmony_ci#include "checker/ETSchecker.h" 213af6ab5fSopenharmony_ci#include "compiler/core/compilerImpl.h" 223af6ab5fSopenharmony_ci#include "compiler/core/ETSCompiler.h" 233af6ab5fSopenharmony_ci#include "compiler/core/ETSemitter.h" 243af6ab5fSopenharmony_ci#include "compiler/core/ETSGen.h" 253af6ab5fSopenharmony_ci#include "compiler/core/regSpiller.h" 263af6ab5fSopenharmony_ci#include "compiler/lowering/phase.h" 273af6ab5fSopenharmony_ci#include "es2panda.h" 283af6ab5fSopenharmony_ci#include "mem/arena_allocator.h" 293af6ab5fSopenharmony_ci#include "mem/pool_manager.h" 303af6ab5fSopenharmony_ci#include "public/public.h" 313af6ab5fSopenharmony_ci#include "util/arktsconfig.h" 323af6ab5fSopenharmony_ci#include "util/generateBin.h" 333af6ab5fSopenharmony_ci#include "varbinder/ETSBinder.h" 343af6ab5fSopenharmony_ci#include "test/utils/panda_executable_path_getter.h" 353af6ab5fSopenharmony_ci#include "checker/types/globalTypesHolder.h" 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_cinamespace ark::es2panda { 383af6ab5fSopenharmony_ci 393af6ab5fSopenharmony_ciclass UnionNormalizationTest : public testing::Test { 403af6ab5fSopenharmony_cipublic: 413af6ab5fSopenharmony_ci UnionNormalizationTest() 423af6ab5fSopenharmony_ci : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER)), 433af6ab5fSopenharmony_ci publicContext_ {std::make_unique<public_lib::Context>()}, 443af6ab5fSopenharmony_ci program_ {parser::Program::NewProgram<varbinder::ETSBinder>(Allocator())}, 453af6ab5fSopenharmony_ci es2pandaPath_ {test::utils::PandaExecutablePathGetter {}.Get()} 463af6ab5fSopenharmony_ci { 473af6ab5fSopenharmony_ci } 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ci ~UnionNormalizationTest() override = default; 503af6ab5fSopenharmony_ci 513af6ab5fSopenharmony_ci static void SetUpTestCase() 523af6ab5fSopenharmony_ci { 533af6ab5fSopenharmony_ci constexpr auto COMPILER_SIZE = operator""_MB(256ULL); 543af6ab5fSopenharmony_ci mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); 553af6ab5fSopenharmony_ci PoolManager::Initialize(); 563af6ab5fSopenharmony_ci } 573af6ab5fSopenharmony_ci 583af6ab5fSopenharmony_ci ArenaAllocator *Allocator() 593af6ab5fSopenharmony_ci { 603af6ab5fSopenharmony_ci return allocator_.get(); 613af6ab5fSopenharmony_ci } 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_ci parser::Program *Program() 643af6ab5fSopenharmony_ci { 653af6ab5fSopenharmony_ci return &program_; 663af6ab5fSopenharmony_ci } 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ci checker::ETSChecker *Checker() 693af6ab5fSopenharmony_ci { 703af6ab5fSopenharmony_ci return &checker_; 713af6ab5fSopenharmony_ci } 723af6ab5fSopenharmony_ci 733af6ab5fSopenharmony_ci void InitializeChecker(std::string_view fileName, std::string_view src) 743af6ab5fSopenharmony_ci { 753af6ab5fSopenharmony_ci auto es2pandaPathPtr = es2pandaPath_.c_str(); 763af6ab5fSopenharmony_ci ASSERT(es2pandaPathPtr); 773af6ab5fSopenharmony_ci 783af6ab5fSopenharmony_ci InitializeChecker<parser::ETSParser, varbinder::ETSBinder, checker::ETSChecker, checker::ETSAnalyzer, 793af6ab5fSopenharmony_ci compiler::ETSCompiler, compiler::ETSGen, compiler::StaticRegSpiller, 803af6ab5fSopenharmony_ci compiler::ETSFunctionEmitter, compiler::ETSEmitter>(&es2pandaPathPtr, fileName, src, 813af6ab5fSopenharmony_ci &checker_, &program_); 823af6ab5fSopenharmony_ci } 833af6ab5fSopenharmony_ci 843af6ab5fSopenharmony_ci template <typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter, typename AstCompiler> 853af6ab5fSopenharmony_ci public_lib::Context::CodeGenCb MakeCompileJob() 863af6ab5fSopenharmony_ci { 873af6ab5fSopenharmony_ci return [this](public_lib::Context *context, varbinder::FunctionScope *scope, 883af6ab5fSopenharmony_ci compiler::ProgramElement *programElement) -> void { 893af6ab5fSopenharmony_ci RegSpiller regSpiller; 903af6ab5fSopenharmony_ci AstCompiler astcompiler; 913af6ab5fSopenharmony_ci CodeGen cg(allocator_.get(), ®Spiller, context, scope, programElement, &astcompiler); 923af6ab5fSopenharmony_ci FunctionEmitter funcEmitter(&cg, programElement); 933af6ab5fSopenharmony_ci funcEmitter.Generate(); 943af6ab5fSopenharmony_ci }; 953af6ab5fSopenharmony_ci } 963af6ab5fSopenharmony_ci 973af6ab5fSopenharmony_ci template <typename Parser, typename VarBinder, typename Checker, typename Analyzer, typename AstCompiler, 983af6ab5fSopenharmony_ci typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter> 993af6ab5fSopenharmony_ci void InitializeChecker(const char **argv, std::string_view fileName, std::string_view src, 1003af6ab5fSopenharmony_ci checker::ETSChecker *checker, parser::Program *program) 1013af6ab5fSopenharmony_ci { 1023af6ab5fSopenharmony_ci auto options = std::make_unique<ark::es2panda::util::Options>(); 1033af6ab5fSopenharmony_ci if (!options->Parse(1, argv)) { 1043af6ab5fSopenharmony_ci std::cerr << options->ErrorMsg() << std::endl; 1053af6ab5fSopenharmony_ci return; 1063af6ab5fSopenharmony_ci } 1073af6ab5fSopenharmony_ci 1083af6ab5fSopenharmony_ci ark::Logger::ComponentMask mask {}; 1093af6ab5fSopenharmony_ci mask.set(ark::Logger::Component::ES2PANDA); 1103af6ab5fSopenharmony_ci ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask); 1113af6ab5fSopenharmony_ci 1123af6ab5fSopenharmony_ci Compiler compiler(options->Extension(), options->ThreadCount()); 1133af6ab5fSopenharmony_ci SourceFile input(fileName, src, options->ParseModule()); 1143af6ab5fSopenharmony_ci compiler::CompilationUnit unit {input, *options, 0, options->Extension()}; 1153af6ab5fSopenharmony_ci auto getPhases = compiler::GetPhaseList(ScriptExtension::ETS); 1163af6ab5fSopenharmony_ci 1173af6ab5fSopenharmony_ci program->MarkEntry(); 1183af6ab5fSopenharmony_ci auto parser = 1193af6ab5fSopenharmony_ci Parser(program, unit.options.CompilerOptions(), static_cast<parser::ParserStatus>(unit.rawParserStatus)); 1203af6ab5fSopenharmony_ci auto analyzer = Analyzer(checker); 1213af6ab5fSopenharmony_ci checker->SetAnalyzer(&analyzer); 1223af6ab5fSopenharmony_ci 1233af6ab5fSopenharmony_ci auto *varbinder = program->VarBinder(); 1243af6ab5fSopenharmony_ci varbinder->SetProgram(program); 1253af6ab5fSopenharmony_ci 1263af6ab5fSopenharmony_ci varbinder->SetContext(publicContext_.get()); 1273af6ab5fSopenharmony_ci 1283af6ab5fSopenharmony_ci auto emitter = Emitter(publicContext_.get()); 1293af6ab5fSopenharmony_ci 1303af6ab5fSopenharmony_ci auto config = public_lib::ConfigImpl {}; 1313af6ab5fSopenharmony_ci publicContext_->config = &config; 1323af6ab5fSopenharmony_ci publicContext_->config->options = &unit.options; 1333af6ab5fSopenharmony_ci publicContext_->sourceFile = &unit.input; 1343af6ab5fSopenharmony_ci publicContext_->allocator = allocator_.get(); 1353af6ab5fSopenharmony_ci publicContext_->parser = &parser; 1363af6ab5fSopenharmony_ci publicContext_->checker = checker; 1373af6ab5fSopenharmony_ci publicContext_->analyzer = publicContext_->checker->GetAnalyzer(); 1383af6ab5fSopenharmony_ci publicContext_->emitter = &emitter; 1393af6ab5fSopenharmony_ci publicContext_->parserProgram = program; 1403af6ab5fSopenharmony_ci 1413af6ab5fSopenharmony_ci parser.ParseScript(unit.input, unit.options.CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB); 1423af6ab5fSopenharmony_ci for (auto *phase : getPhases) { 1433af6ab5fSopenharmony_ci if (!phase->Apply(publicContext_.get(), program)) { 1443af6ab5fSopenharmony_ci return; 1453af6ab5fSopenharmony_ci } 1463af6ab5fSopenharmony_ci } 1473af6ab5fSopenharmony_ci } 1483af6ab5fSopenharmony_ci 1493af6ab5fSopenharmony_ci static checker::Type *FindClassType(varbinder::ETSBinder *varbinder, std::string_view className) 1503af6ab5fSopenharmony_ci { 1513af6ab5fSopenharmony_ci auto classDefs = varbinder->AsETSBinder()->GetRecordTable()->ClassDefinitions(); 1523af6ab5fSopenharmony_ci auto baseClass = std::find_if(classDefs.begin(), classDefs.end(), [className](ir::ClassDefinition *cdef) { 1533af6ab5fSopenharmony_ci return cdef->Ident()->Name().Is(className); 1543af6ab5fSopenharmony_ci }); 1553af6ab5fSopenharmony_ci if (baseClass == classDefs.end()) { 1563af6ab5fSopenharmony_ci return nullptr; 1573af6ab5fSopenharmony_ci } 1583af6ab5fSopenharmony_ci return (*baseClass)->TsType(); 1593af6ab5fSopenharmony_ci } 1603af6ab5fSopenharmony_ci 1613af6ab5fSopenharmony_ci static checker::Type *FindTypeAlias(checker::ETSChecker *checker, std::string_view aliasName) 1623af6ab5fSopenharmony_ci { 1633af6ab5fSopenharmony_ci auto *foundVar = 1643af6ab5fSopenharmony_ci checker->Scope()->FindLocal(aliasName, varbinder::ResolveBindingOptions::TYPE_ALIASES)->AsLocalVariable(); 1653af6ab5fSopenharmony_ci if (foundVar == nullptr) { 1663af6ab5fSopenharmony_ci return nullptr; 1673af6ab5fSopenharmony_ci } 1683af6ab5fSopenharmony_ci return foundVar->Declaration()->Node()->AsTSTypeAliasDeclaration()->TypeAnnotation()->TsType(); 1693af6ab5fSopenharmony_ci } 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(UnionNormalizationTest); 1723af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(UnionNormalizationTest); 1733af6ab5fSopenharmony_ci 1743af6ab5fSopenharmony_ciprotected: 1753af6ab5fSopenharmony_ci static constexpr uint8_t SIZE2 = 2; 1763af6ab5fSopenharmony_ci static constexpr uint8_t SIZE3 = 3; 1773af6ab5fSopenharmony_ci static constexpr uint8_t IDX0 = 0; 1783af6ab5fSopenharmony_ci static constexpr uint8_t IDX1 = 1; 1793af6ab5fSopenharmony_ci static constexpr uint8_t IDX2 = 2; 1803af6ab5fSopenharmony_ci 1813af6ab5fSopenharmony_ciprivate: 1823af6ab5fSopenharmony_ci std::unique_ptr<ArenaAllocator> allocator_; 1833af6ab5fSopenharmony_ci std::unique_ptr<public_lib::Context> publicContext_; 1843af6ab5fSopenharmony_ci parser::Program program_; 1853af6ab5fSopenharmony_ci std::string es2pandaPath_; 1863af6ab5fSopenharmony_ci checker::ETSChecker checker_; 1873af6ab5fSopenharmony_ci}; 1883af6ab5fSopenharmony_ci 1893af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, UnionWithObject) 1903af6ab5fSopenharmony_ci{ 1913af6ab5fSopenharmony_ci // Test normalization: int | Object | string ==> Object 1923af6ab5fSopenharmony_ci InitializeChecker("_.sts", ""); 1933af6ab5fSopenharmony_ci 1943af6ab5fSopenharmony_ci auto checker = Checker(); 1953af6ab5fSopenharmony_ci ASSERT(checker); 1963af6ab5fSopenharmony_ci 1973af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 1983af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalIntType()); 1993af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalETSObjectType()); 2003af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalETSStringBuiltinType()); 2013af6ab5fSopenharmony_ci 2023af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 2033af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 2043af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 2053af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSObjectType()); 2063af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType, checker->GlobalETSObjectType()); 2073af6ab5fSopenharmony_ci} 2083af6ab5fSopenharmony_ci 2093af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, UnionWithIdenticalTypes1) 2103af6ab5fSopenharmony_ci{ 2113af6ab5fSopenharmony_ci // Test normalization: number | Base | string | number ==> number | Base | string 2123af6ab5fSopenharmony_ci InitializeChecker("_.sts", "class Base {}"); 2133af6ab5fSopenharmony_ci 2143af6ab5fSopenharmony_ci auto program = Program(); 2153af6ab5fSopenharmony_ci ASSERT(program); 2163af6ab5fSopenharmony_ci 2173af6ab5fSopenharmony_ci auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); 2183af6ab5fSopenharmony_ci ASSERT_NE(baseType, nullptr); 2193af6ab5fSopenharmony_ci 2203af6ab5fSopenharmony_ci auto checker = Checker(); 2213af6ab5fSopenharmony_ci ASSERT(checker); 2223af6ab5fSopenharmony_ci 2233af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 2243af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 2253af6ab5fSopenharmony_ci unionConstituents.emplace_back(baseType); 2263af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalBuiltinETSStringType()); 2273af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 2283af6ab5fSopenharmony_ci 2293af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 2303af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 2313af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 2323af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSUnionType()); 2333af6ab5fSopenharmony_ci auto *const unionType = normalizedType->AsETSUnionType(); 2343af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE3); 2353af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 2363af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), baseType); 2373af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker->GlobalBuiltinETSStringType()); 2383af6ab5fSopenharmony_ci} 2393af6ab5fSopenharmony_ci 2403af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, DISABLED_UnionWithIdenticalTypes2) 2413af6ab5fSopenharmony_ci{ 2423af6ab5fSopenharmony_ci // Test normalization: Base | int | Base | double | short | number ==> Base | number 2433af6ab5fSopenharmony_ci InitializeChecker("_.sts", "class Base {}"); 2443af6ab5fSopenharmony_ci 2453af6ab5fSopenharmony_ci auto program = Program(); 2463af6ab5fSopenharmony_ci ASSERT(program); 2473af6ab5fSopenharmony_ci 2483af6ab5fSopenharmony_ci auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); 2493af6ab5fSopenharmony_ci ASSERT_NE(baseType, nullptr); 2503af6ab5fSopenharmony_ci 2513af6ab5fSopenharmony_ci auto checker = Checker(); 2523af6ab5fSopenharmony_ci ASSERT(checker); 2533af6ab5fSopenharmony_ci 2543af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 2553af6ab5fSopenharmony_ci unionConstituents.emplace_back(baseType); 2563af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalIntType()); 2573af6ab5fSopenharmony_ci unionConstituents.emplace_back(baseType); 2583af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 2593af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalShortType()); 2603af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 2613af6ab5fSopenharmony_ci 2623af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 2633af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 2643af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 2653af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSUnionType()); 2663af6ab5fSopenharmony_ci auto *const unionType = normalizedType->AsETSUnionType(); 2673af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); 2683af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), baseType); 2693af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 2703af6ab5fSopenharmony_ci} 2713af6ab5fSopenharmony_ci 2723af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, DISABLED_UnionWithNumeric1) 2733af6ab5fSopenharmony_ci{ 2743af6ab5fSopenharmony_ci // Test normalization: boolean | int | double | short ==> boolean | double 2753af6ab5fSopenharmony_ci InitializeChecker("_.sts", ""); 2763af6ab5fSopenharmony_ci 2773af6ab5fSopenharmony_ci auto checker = Checker(); 2783af6ab5fSopenharmony_ci ASSERT(checker); 2793af6ab5fSopenharmony_ci 2803af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 2813af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalETSBooleanType()); 2823af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalIntType()); 2833af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 2843af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalShortType()); 2853af6ab5fSopenharmony_ci 2863af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 2873af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 2883af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 2893af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSUnionType()); 2903af6ab5fSopenharmony_ci auto *const unionType = normalizedType->AsETSUnionType(); 2913af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); 2923af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalETSBooleanBuiltinType()); 2933af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 2943af6ab5fSopenharmony_ci} 2953af6ab5fSopenharmony_ci 2963af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, DISABLED_UnionWithNumeric2) 2973af6ab5fSopenharmony_ci{ 2983af6ab5fSopenharmony_ci // Test normalization: string | int | Base | double | short ==> string | Base | double 2993af6ab5fSopenharmony_ci InitializeChecker("_.sts", "class Base {}"); 3003af6ab5fSopenharmony_ci 3013af6ab5fSopenharmony_ci auto program = Program(); 3023af6ab5fSopenharmony_ci ASSERT(program); 3033af6ab5fSopenharmony_ci 3043af6ab5fSopenharmony_ci auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); 3053af6ab5fSopenharmony_ci ASSERT_NE(baseType, nullptr); 3063af6ab5fSopenharmony_ci 3073af6ab5fSopenharmony_ci auto checker = Checker(); 3083af6ab5fSopenharmony_ci ASSERT(checker); 3093af6ab5fSopenharmony_ci 3103af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 3113af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalBuiltinETSStringType()); 3123af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalIntType()); 3133af6ab5fSopenharmony_ci unionConstituents.emplace_back(baseType); 3143af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalDoubleType()); 3153af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalShortType()); 3163af6ab5fSopenharmony_ci 3173af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 3183af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 3193af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 3203af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSUnionType()); 3213af6ab5fSopenharmony_ci auto *const unionType = normalizedType->AsETSUnionType(); 3223af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE3); 3233af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); 3243af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), baseType); 3253af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX2), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 3263af6ab5fSopenharmony_ci} 3273af6ab5fSopenharmony_ci 3283af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, UnionWithSubTypes) 3293af6ab5fSopenharmony_ci{ 3303af6ab5fSopenharmony_ci // Test 4 cases of normalization 3313af6ab5fSopenharmony_ci static constexpr std::string_view SRC = 3323af6ab5fSopenharmony_ci "\ 3333af6ab5fSopenharmony_ci class Base {}\ 3343af6ab5fSopenharmony_ci class Derived1 extends Base {}\ 3353af6ab5fSopenharmony_ci class Derived2 extends Base {}\ 3363af6ab5fSopenharmony_ci "; 3373af6ab5fSopenharmony_ci InitializeChecker("_.sts", SRC); 3383af6ab5fSopenharmony_ci 3393af6ab5fSopenharmony_ci auto program = Program(); 3403af6ab5fSopenharmony_ci ASSERT(program); 3413af6ab5fSopenharmony_ci 3423af6ab5fSopenharmony_ci auto *const baseType = FindClassType(program->VarBinder()->AsETSBinder(), "Base"); 3433af6ab5fSopenharmony_ci ASSERT_NE(baseType, nullptr); 3443af6ab5fSopenharmony_ci auto *const derived1Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived1"); 3453af6ab5fSopenharmony_ci ASSERT_NE(derived1Type, nullptr); 3463af6ab5fSopenharmony_ci auto *const derived2Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived2"); 3473af6ab5fSopenharmony_ci ASSERT_NE(derived2Type, nullptr); 3483af6ab5fSopenharmony_ci 3493af6ab5fSopenharmony_ci auto checker = Checker(); 3503af6ab5fSopenharmony_ci ASSERT(checker); 3513af6ab5fSopenharmony_ci 3523af6ab5fSopenharmony_ci // Test normalization: Derived1 | Base ==> Base 3533af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents1(checker->Allocator()->Adapter()); 3543af6ab5fSopenharmony_ci unionConstituents1.emplace_back(derived1Type); 3553af6ab5fSopenharmony_ci unionConstituents1.emplace_back(baseType); 3563af6ab5fSopenharmony_ci 3573af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 3583af6ab5fSopenharmony_ci auto *const normalizedType1 = checker->CreateETSUnionType(std::move(unionConstituents1)); 3593af6ab5fSopenharmony_ci ASSERT_NE(normalizedType1, nullptr); 3603af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType1->IsETSObjectType()); 3613af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType1, baseType); 3623af6ab5fSopenharmony_ci 3633af6ab5fSopenharmony_ci // Test normalization: Base | Derived2 ==> Base 3643af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents2(checker->Allocator()->Adapter()); 3653af6ab5fSopenharmony_ci unionConstituents2.emplace_back(baseType); 3663af6ab5fSopenharmony_ci unionConstituents2.emplace_back(derived2Type); 3673af6ab5fSopenharmony_ci 3683af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 3693af6ab5fSopenharmony_ci auto *const normalizedType2 = checker->CreateETSUnionType(std::move(unionConstituents2)); 3703af6ab5fSopenharmony_ci ASSERT_NE(normalizedType2, nullptr); 3713af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType2->IsETSObjectType()); 3723af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType2, baseType); 3733af6ab5fSopenharmony_ci 3743af6ab5fSopenharmony_ci // Test normalization: Derived1 | Derived2 ==> Derived1 | Derived2 3753af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents3(checker->Allocator()->Adapter()); 3763af6ab5fSopenharmony_ci unionConstituents3.emplace_back(derived1Type); 3773af6ab5fSopenharmony_ci unionConstituents3.emplace_back(derived2Type); 3783af6ab5fSopenharmony_ci 3793af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 3803af6ab5fSopenharmony_ci auto *const normalizedType3 = checker->CreateETSUnionType(std::move(unionConstituents3)); 3813af6ab5fSopenharmony_ci ASSERT_NE(normalizedType3, nullptr); 3823af6ab5fSopenharmony_ci auto *const unionType = normalizedType3->AsETSUnionType(); 3833af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); 3843af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), derived1Type); 3853af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), derived2Type); 3863af6ab5fSopenharmony_ci 3873af6ab5fSopenharmony_ci // Test normalization: Derived2 | Base | Derived1 ==> Base 3883af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents4(checker->Allocator()->Adapter()); 3893af6ab5fSopenharmony_ci unionConstituents4.emplace_back(derived1Type); 3903af6ab5fSopenharmony_ci unionConstituents4.emplace_back(baseType); 3913af6ab5fSopenharmony_ci unionConstituents4.emplace_back(derived2Type); 3923af6ab5fSopenharmony_ci 3933af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 3943af6ab5fSopenharmony_ci auto *const normalizedType4 = checker->CreateETSUnionType(std::move(unionConstituents4)); 3953af6ab5fSopenharmony_ci ASSERT_NE(normalizedType4, nullptr); 3963af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType4->IsETSObjectType()); 3973af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType4, baseType); 3983af6ab5fSopenharmony_ci} 3993af6ab5fSopenharmony_ci 4003af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, DISABLED_UnionLinearization) 4013af6ab5fSopenharmony_ci{ 4023af6ab5fSopenharmony_ci // Test 3 cases of normalization 4033af6ab5fSopenharmony_ci static constexpr std::string_view SRC = 4043af6ab5fSopenharmony_ci "\ 4053af6ab5fSopenharmony_ci class Base {}\ 4063af6ab5fSopenharmony_ci class Derived1 extends Base {}\ 4073af6ab5fSopenharmony_ci class Derived2 extends Base {}\ 4083af6ab5fSopenharmony_ci type UT = int | string\ 4093af6ab5fSopenharmony_ci \ 4103af6ab5fSopenharmony_ci type UT1 = int | (int | string) | number\ 4113af6ab5fSopenharmony_ci type UT2 = int | UT | number\ 4123af6ab5fSopenharmony_ci type UT3 = int | (Derived2 | Base) | Derived1 | (string | number | short) | (int | string)\ 4133af6ab5fSopenharmony_ci "; 4143af6ab5fSopenharmony_ci InitializeChecker("_.sts", SRC); 4153af6ab5fSopenharmony_ci 4163af6ab5fSopenharmony_ci auto program = Program(); 4173af6ab5fSopenharmony_ci ASSERT(program); 4183af6ab5fSopenharmony_ci 4193af6ab5fSopenharmony_ci auto *varbinder = program->VarBinder()->AsETSBinder(); 4203af6ab5fSopenharmony_ci auto *const baseType = FindClassType(varbinder, "Base"); 4213af6ab5fSopenharmony_ci ASSERT_NE(baseType, nullptr); 4223af6ab5fSopenharmony_ci auto *const derived1Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived1"); 4233af6ab5fSopenharmony_ci ASSERT_NE(derived1Type, nullptr); 4243af6ab5fSopenharmony_ci auto *const derived2Type = FindClassType(program->VarBinder()->AsETSBinder(), "Derived2"); 4253af6ab5fSopenharmony_ci ASSERT_NE(derived2Type, nullptr); 4263af6ab5fSopenharmony_ci 4273af6ab5fSopenharmony_ci auto checker = Checker(); 4283af6ab5fSopenharmony_ci ASSERT(checker); 4293af6ab5fSopenharmony_ci 4303af6ab5fSopenharmony_ci // Test normalization: int | (int | string) | number ==> string | number 4313af6ab5fSopenharmony_ci auto *const ut1Type = FindTypeAlias(checker, "UT1"); 4323af6ab5fSopenharmony_ci ASSERT_NE(ut1Type, nullptr); 4333af6ab5fSopenharmony_ci ASSERT_TRUE(ut1Type->IsETSUnionType()); 4343af6ab5fSopenharmony_ci auto *ut1 = ut1Type->AsETSUnionType(); 4353af6ab5fSopenharmony_ci ASSERT_EQ(ut1->ConstituentTypes().size(), SIZE2); 4363af6ab5fSopenharmony_ci ASSERT_EQ(ut1->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); 4373af6ab5fSopenharmony_ci ASSERT_EQ(ut1->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 4383af6ab5fSopenharmony_ci 4393af6ab5fSopenharmony_ci // Test normalization: int | UT | number ==> string | number 4403af6ab5fSopenharmony_ci auto *const ut2Type = FindTypeAlias(checker, "UT2"); 4413af6ab5fSopenharmony_ci ASSERT_NE(ut2Type, nullptr); 4423af6ab5fSopenharmony_ci ASSERT_TRUE(ut2Type->IsETSUnionType()); 4433af6ab5fSopenharmony_ci auto *ut2 = ut2Type->AsETSUnionType(); 4443af6ab5fSopenharmony_ci ASSERT_EQ(ut2->ConstituentTypes().size(), SIZE2); 4453af6ab5fSopenharmony_ci ASSERT_EQ(ut2->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); 4463af6ab5fSopenharmony_ci ASSERT_EQ(ut2->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 4473af6ab5fSopenharmony_ci 4483af6ab5fSopenharmony_ci // Test normalization: 4493af6ab5fSopenharmony_ci // int | (Derived2 | Base) | Derived1 | (string | number | short) | (int | string) ==> Base | string | number 4503af6ab5fSopenharmony_ci auto *const ut3Type = FindTypeAlias(checker, "UT3"); 4513af6ab5fSopenharmony_ci ASSERT_NE(ut3Type, nullptr); 4523af6ab5fSopenharmony_ci ASSERT_TRUE(ut3Type->IsETSUnionType()); 4533af6ab5fSopenharmony_ci auto *ut3 = ut3Type->AsETSUnionType(); 4543af6ab5fSopenharmony_ci ASSERT_EQ(ut3->ConstituentTypes().size(), SIZE3); 4553af6ab5fSopenharmony_ci ASSERT_EQ(ut3->ConstituentTypes().at(IDX0), baseType); 4563af6ab5fSopenharmony_ci ASSERT_EQ(ut3->ConstituentTypes().at(IDX1), checker->GlobalBuiltinETSStringType()); 4573af6ab5fSopenharmony_ci ASSERT_EQ(ut3->ConstituentTypes().at(IDX2), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 4583af6ab5fSopenharmony_ci} 4593af6ab5fSopenharmony_ci 4603af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, UnionStringLiterals1) 4613af6ab5fSopenharmony_ci{ 4623af6ab5fSopenharmony_ci InitializeChecker("_.sts", ""); 4633af6ab5fSopenharmony_ci 4643af6ab5fSopenharmony_ci auto checker = Checker(); 4653af6ab5fSopenharmony_ci ASSERT(checker); 4663af6ab5fSopenharmony_ci 4673af6ab5fSopenharmony_ci // Test normalization: string | "abc" ==> string 4683af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents1(checker->Allocator()->Adapter()); 4693af6ab5fSopenharmony_ci unionConstituents1.emplace_back(checker->GlobalBuiltinETSStringType()); 4703af6ab5fSopenharmony_ci unionConstituents1.emplace_back(checker->CreateETSStringLiteralType("abc")); 4713af6ab5fSopenharmony_ci 4723af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 4733af6ab5fSopenharmony_ci auto *const normalizedType1 = checker->CreateETSUnionType(std::move(unionConstituents1)); 4743af6ab5fSopenharmony_ci ASSERT_NE(normalizedType1, nullptr); 4753af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType1->IsETSObjectType()); 4763af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType1, checker->GlobalBuiltinETSStringType()); 4773af6ab5fSopenharmony_ci 4783af6ab5fSopenharmony_ci // Test normalization: "abc" | string | string ==> string 4793af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents2(checker->Allocator()->Adapter()); 4803af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->CreateETSStringLiteralType("abc")); 4813af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->GlobalBuiltinETSStringType()); 4823af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->GlobalBuiltinETSStringType()); 4833af6ab5fSopenharmony_ci 4843af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 4853af6ab5fSopenharmony_ci auto *const normalizedType2 = checker->CreateETSUnionType(std::move(unionConstituents2)); 4863af6ab5fSopenharmony_ci ASSERT_NE(normalizedType2, nullptr); 4873af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType2->IsETSObjectType()); 4883af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType2, checker->GlobalBuiltinETSStringType()); 4893af6ab5fSopenharmony_ci 4903af6ab5fSopenharmony_ci // Test normalization: number | "abc" | string | "xy" ==> number | string 4913af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents3(checker->Allocator()->Adapter()); 4923af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->GlobalDoubleType()); 4933af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("abc")); 4943af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->GlobalBuiltinETSStringType()); 4953af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("xy")); 4963af6ab5fSopenharmony_ci 4973af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 4983af6ab5fSopenharmony_ci auto *const normalizedType3 = checker->CreateETSUnionType(std::move(unionConstituents3)); 4993af6ab5fSopenharmony_ci ASSERT_NE(normalizedType3, nullptr); 5003af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType3->IsETSUnionType()); 5013af6ab5fSopenharmony_ci auto *const unionType = normalizedType3->AsETSUnionType(); 5023af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().size(), SIZE2); 5033af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX0), checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 5043af6ab5fSopenharmony_ci ASSERT_EQ(unionType->ConstituentTypes().at(IDX1), checker->GlobalBuiltinETSStringType()); 5053af6ab5fSopenharmony_ci 5063af6ab5fSopenharmony_ci // Test normalization: "abcd" | "abcd" | "abcd" ==> "abcd" 5073af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents4(checker->Allocator()->Adapter()); 5083af6ab5fSopenharmony_ci unionConstituents4.emplace_back(checker->CreateETSStringLiteralType("abcd")); 5093af6ab5fSopenharmony_ci unionConstituents4.emplace_back(checker->CreateETSStringLiteralType("abcd")); 5103af6ab5fSopenharmony_ci unionConstituents4.emplace_back(checker->CreateETSStringLiteralType("abcd")); 5113af6ab5fSopenharmony_ci 5123af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 5133af6ab5fSopenharmony_ci auto *const normalizedType4 = checker->CreateETSUnionType(std::move(unionConstituents4)); 5143af6ab5fSopenharmony_ci ASSERT_NE(normalizedType4, nullptr); 5153af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType4->IsETSStringType()); 5163af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType4->AsETSStringType()->GetValue(), "abcd"); 5173af6ab5fSopenharmony_ci} 5183af6ab5fSopenharmony_ci 5193af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, UnionStringLiterals2) 5203af6ab5fSopenharmony_ci{ 5213af6ab5fSopenharmony_ci InitializeChecker("_.sts", ""); 5223af6ab5fSopenharmony_ci 5233af6ab5fSopenharmony_ci auto checker = Checker(); 5243af6ab5fSopenharmony_ci ASSERT(checker); 5253af6ab5fSopenharmony_ci 5263af6ab5fSopenharmony_ci // Test absence of normalization: "ab1" | "bc2" | "cd3" ==> "ab1" | "bc2" | "cd3" 5273af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents1(checker->Allocator()->Adapter()); 5283af6ab5fSopenharmony_ci unionConstituents1.emplace_back(checker->CreateETSStringLiteralType("ab1")); 5293af6ab5fSopenharmony_ci unionConstituents1.emplace_back(checker->CreateETSStringLiteralType("bc2")); 5303af6ab5fSopenharmony_ci unionConstituents1.emplace_back(checker->CreateETSStringLiteralType("cd3")); 5313af6ab5fSopenharmony_ci 5323af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 5333af6ab5fSopenharmony_ci auto *const normalizedType1 = checker->CreateETSUnionType(std::move(unionConstituents1)); 5343af6ab5fSopenharmony_ci ASSERT_NE(normalizedType1, nullptr); 5353af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType1->IsETSUnionType()); 5363af6ab5fSopenharmony_ci auto *const unionType1 = normalizedType1->AsETSUnionType(); 5373af6ab5fSopenharmony_ci ASSERT_EQ(unionType1->ConstituentTypes().size(), SIZE3); 5383af6ab5fSopenharmony_ci ASSERT_TRUE(unionType1->ConstituentTypes().at(IDX0)->IsETSStringType()); 5393af6ab5fSopenharmony_ci ASSERT_EQ(unionType1->ConstituentTypes().at(IDX0)->AsETSStringType()->GetValue(), "ab1"); 5403af6ab5fSopenharmony_ci ASSERT_TRUE(unionType1->ConstituentTypes().at(IDX1)->IsETSStringType()); 5413af6ab5fSopenharmony_ci ASSERT_EQ(unionType1->ConstituentTypes().at(IDX1)->AsETSStringType()->GetValue(), "bc2"); 5423af6ab5fSopenharmony_ci ASSERT_TRUE(unionType1->ConstituentTypes().at(IDX2)->IsETSStringType()); 5433af6ab5fSopenharmony_ci ASSERT_EQ(unionType1->ConstituentTypes().at(IDX2)->AsETSStringType()->GetValue(), "cd3"); 5443af6ab5fSopenharmony_ci 5453af6ab5fSopenharmony_ci // Test normalization: "ab1" | "bc2" | "ab1" ==> "ab1" | "bc2" 5463af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents2(checker->Allocator()->Adapter()); 5473af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->CreateETSStringLiteralType("ab1")); 5483af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->CreateETSStringLiteralType("bc2")); 5493af6ab5fSopenharmony_ci unionConstituents2.emplace_back(checker->CreateETSStringLiteralType("ab1")); 5503af6ab5fSopenharmony_ci 5513af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 5523af6ab5fSopenharmony_ci auto *const normalizedType2 = checker->CreateETSUnionType(std::move(unionConstituents2)); 5533af6ab5fSopenharmony_ci ASSERT_NE(normalizedType2, nullptr); 5543af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType2->IsETSUnionType()); 5553af6ab5fSopenharmony_ci auto *const unionType2 = normalizedType2->AsETSUnionType(); 5563af6ab5fSopenharmony_ci ASSERT_EQ(unionType2->ConstituentTypes().size(), SIZE2); 5573af6ab5fSopenharmony_ci ASSERT_TRUE(unionType2->ConstituentTypes().at(IDX0)->IsETSStringType()); 5583af6ab5fSopenharmony_ci ASSERT_EQ(unionType2->ConstituentTypes().at(IDX0)->AsETSStringType()->GetValue(), "ab1"); 5593af6ab5fSopenharmony_ci ASSERT_TRUE(unionType2->ConstituentTypes().at(IDX1)->IsETSStringType()); 5603af6ab5fSopenharmony_ci ASSERT_EQ(unionType2->ConstituentTypes().at(IDX1)->AsETSStringType()->GetValue(), "bc2"); 5613af6ab5fSopenharmony_ci 5623af6ab5fSopenharmony_ci // Test absence of normalization: "ab1" | "bc2" | "cd3" | string | int ==> string | int 5633af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents3(checker->Allocator()->Adapter()); 5643af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("ab1")); 5653af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("bc2")); 5663af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->CreateETSStringLiteralType("cd3")); 5673af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->GlobalBuiltinETSStringType()); 5683af6ab5fSopenharmony_ci unionConstituents3.emplace_back(checker->GlobalIntType()); 5693af6ab5fSopenharmony_ci 5703af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 5713af6ab5fSopenharmony_ci auto *const normalizedType3 = checker->CreateETSUnionType(std::move(unionConstituents3)); 5723af6ab5fSopenharmony_ci ASSERT_NE(normalizedType3, nullptr); 5733af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType3->IsETSUnionType()); 5743af6ab5fSopenharmony_ci auto *const unionType3 = normalizedType3->AsETSUnionType(); 5753af6ab5fSopenharmony_ci ASSERT_EQ(unionType3->ConstituentTypes().size(), SIZE2); 5763af6ab5fSopenharmony_ci ASSERT_EQ(unionType3->ConstituentTypes().at(IDX0), checker->GlobalBuiltinETSStringType()); 5773af6ab5fSopenharmony_ci ASSERT_EQ(unionType3->ConstituentTypes().at(IDX1), checker->GetGlobalTypesHolder()->GlobalIntegerBuiltinType()); 5783af6ab5fSopenharmony_ci} 5793af6ab5fSopenharmony_ci 5803af6ab5fSopenharmony_ciTEST_F(UnionNormalizationTest, DISABLED_UnionWithNever) 5813af6ab5fSopenharmony_ci{ 5823af6ab5fSopenharmony_ci // Test normalization: int | never | number ==> number 5833af6ab5fSopenharmony_ci InitializeChecker("_.sts", ""); 5843af6ab5fSopenharmony_ci 5853af6ab5fSopenharmony_ci auto checker = Checker(); 5863af6ab5fSopenharmony_ci ASSERT(checker); 5873af6ab5fSopenharmony_ci 5883af6ab5fSopenharmony_ci ArenaVector<checker::Type *> unionConstituents(checker->Allocator()->Adapter()); 5893af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GlobalIntType()); 5903af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalBuiltinNeverType()); 5913af6ab5fSopenharmony_ci unionConstituents.emplace_back(checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 5923af6ab5fSopenharmony_ci 5933af6ab5fSopenharmony_ci // Create union type, which will be normalized inside creation function 5943af6ab5fSopenharmony_ci auto *const normalizedType = checker->CreateETSUnionType(std::move(unionConstituents)); 5953af6ab5fSopenharmony_ci ASSERT_NE(normalizedType, nullptr); 5963af6ab5fSopenharmony_ci ASSERT_TRUE(normalizedType->IsETSObjectType()); 5973af6ab5fSopenharmony_ci ASSERT_EQ(normalizedType, checker->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); 5983af6ab5fSopenharmony_ci} 5993af6ab5fSopenharmony_ci 6003af6ab5fSopenharmony_ci} // namespace ark::es2panda 601