From f6cec779bb3af2ff6250a7910cf8945b34880228 Mon Sep 17 00:00:00 2001 From: Gongyuhang <517563583@qq.com> Date: Wed, 20 Oct 2021 12:40:12 +0800 Subject: [PATCH] Add a cpp that tests the public functions of EcmaModule class, ModuleManager class and ModuleStack class. Signed-off-by: Gongyuhang <517563583@qq.com> --- ecmascript/tests/BUILD.gn | 29 ++ ecmascript/tests/ecma_module_test.cpp | 548 ++++++++++++++++++++++++++ 2 files changed, 577 insertions(+) create mode 100644 ecmascript/tests/ecma_module_test.cpp diff --git a/ecmascript/tests/BUILD.gn b/ecmascript/tests/BUILD.gn index 653a46d87a..c8abe686b6 100644 --- a/ecmascript/tests/BUILD.gn +++ b/ecmascript/tests/BUILD.gn @@ -98,6 +98,33 @@ host_unittest_action("DumpTest") { } } +host_unittest_action("EcmaModuleTest") { + module_out_path = module_output_path + + sources = [ + # test file + "ecma_module_test.cpp", + ] + + configs = [ + "//ark/js_runtime:ecma_test_config", + "//ark/js_runtime:ark_jsruntime_public_config", # should add before + # arkruntime_public_config + "//ark/js_runtime:ark_jsruntime_common_config", + "$ark_root/runtime:arkruntime_public_config", + ] + + deps = [ + "$ark_root/libpandabase:libarkbase", + "//ark/js_runtime:libark_jsruntime_test", + sdk_libc_secshared_dep, + ] + + if (!is_standard_system) { + deps += [ "$ark_root/runtime:libarkruntime" ] + } +} + host_unittest_action("EcmaStringTest") { module_out_path = module_output_path @@ -889,6 +916,7 @@ group("unittest") { ":AssertScopeTest", ":BuiltinsTest", ":DumpTest", + ":EcmaModuleTest", ":EcmaStringTest", ":GcTest", ":GlueRegsTest", @@ -929,6 +957,7 @@ group("host_unittest") { ":AssertScopeTestAction", ":BuiltinsTestAction", ":DumpTestAction", + ":EcmaModuleTestAction", ":EcmaStringTestAction", ":GcTestAction", ":GlueRegsTestAction", diff --git a/ecmascript/tests/ecma_module_test.cpp b/ecmascript/tests/ecma_module_test.cpp new file mode 100644 index 0000000000..f214c0bc4e --- /dev/null +++ b/ecmascript/tests/ecma_module_test.cpp @@ -0,0 +1,548 @@ +/* + * Copyright (c) 2021 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 "ecmascript/ecma_module.h" +#include "ecmascript/global_env.h" +#include "ecmascript/js_locale.h" +#include "ecmascript/tagged_dictionary.h" +#include "ecmascript/tests/test_helper.h" + +using namespace panda::ecmascript; + +namespace panda::test { +class EcmaModuleTest : public testing::Test { +public: + static void SetUpTestCase() + { + GTEST_LOG_(INFO) << "SetUpTestCase"; + } + + static void TesrDownTestCase() + { + GTEST_LOG_(INFO) << "TearDownCase"; + } + + void SetUp() override + { + TestHelper::CreateEcmaVMWithScope(instance, thread, scope); + } + + void TearDown() override + { + TestHelper::DestroyEcmaVMWithScope(instance, scope); + } + PandaVM *instance {nullptr}; + ecmascript::EcmaHandleScope *scope {nullptr}; + JSThread *thread {nullptr}; +}; + +EcmaModule *EcmaModuleCreate(JSThread *thread) +{ + ObjectFactory *objectFactory = thread->GetEcmaVM()->GetFactory(); + JSHandle handleEcmaModule = objectFactory->NewEmptyEcmaModule(); + return *handleEcmaModule; +} + +/* + * Feature: EcmaModule + * Function: AddItem + * SubFunction: GetItem + * FunctionPoints: Add Item To EcmaModule + * CaseDescription: Add an item for a EcmaModule that has called "SetNameDictionary" function in the HWTEST_F_L0, and + * check its value through 'GetItem' function. + */ +HWTEST_F_L0(EcmaModuleTest, AddItem_001) +{ + int numOfElementsDict = 4; + CString cStrItemName = "key1"; + int intItemValue = 1; + JSHandle handleEcmaModule(thread, EcmaModuleCreate(thread)); + JSHandle handleNameDict(thread, NameDictionary::Create(thread, numOfElementsDict)); + JSHandle handleTagValItemName( + thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName)); + JSHandle handleTagValItemValue(thread, JSTaggedValue(intItemValue)); + + handleEcmaModule->SetNameDictionary(thread, handleNameDict); // Call SetNameDictionary in HWTEST_F_L0 + EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue); + EXPECT_EQ(handleEcmaModule->GetItem(thread, handleTagValItemName)->GetNumber(), intItemValue); +} + +/* + * Feature: EcmaModule + * Function: AddItem + * SubFunction: GetItem + * FunctionPoints: Add Item To EcmaModule + * CaseDescription: Add an item for a EcmaModule that has not called "SetNameDictionary" function in the HWTEST_F_L0, + * and check its value through 'GetItem' function. + */ +HWTEST_F_L0(EcmaModuleTest, AddItem_002) +{ + CString cStrItemName = "cStrItemName"; + int intItemValue = 1; + JSHandle handleEcmaModule(thread, EcmaModuleCreate(thread)); + JSHandle handleTagValItemName( + thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName)); + JSHandle handleTagValItemValue(thread, JSTaggedValue(intItemValue)); + + // This EcmaModule calls 'SetNameDictionary' function through 'AddItem' function of "ecma_module.cpp". + EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue); + EXPECT_EQ(handleEcmaModule->GetItem(thread, handleTagValItemName)->GetNumber(), intItemValue); +} + +/* + * Feature: EcmaModule + * Function: RemoveItem + * SubFunction: AddItem/GetItem + * FunctionPoints: Remove Item From EcmaModule + * CaseDescription: Add an item for an empty EcmaModule through 'AddItem' function, then remove the item from the + * EcmaModule through 'RemoveItem' function, finally check whether the item obtained from the + * EcmaModule through 'GetItem' function is undefined. + */ +HWTEST_F_L0(EcmaModuleTest, RemoveItem) +{ + ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory(); + + CString cStrItemName = "cStrItemName"; + int intItemValue = 1; + JSHandle handleEcmaModule(thread, EcmaModuleCreate(thread)); + JSHandle handleTagValItemName(objFactory->NewFromCanBeCompressString(cStrItemName)); + JSHandle handleTagValItemValue(thread, JSTaggedValue(intItemValue)); + + EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue); + EcmaModule::RemoveItem(thread, handleEcmaModule, handleTagValItemName); + EXPECT_TRUE(handleEcmaModule->GetItem(thread, handleTagValItemName)->IsUndefined()); +} + +/* + * Feature: EcmaModule + * Function: SetNameDictionary + * SubFunction: NameDictionary::Put/GetNameDictionary + * FunctionPoints: Set NameDictionary For EcmaModule + * CaseDescription: Create a source key, a source value, a source NameDictionary and a target EcmaModule, change the + * NameDictionary through 'NameDictionary::Put' function, set the changed source NameDictionary as + * this target EcmaModule's NameDictionary through 'SetNameDictionary' function, check whether the + * result returned through 'GetNameDictionary' function from the target EcmaModule are within + * expectations. + */ +HWTEST_F_L0(EcmaModuleTest, SetNameDictionary) +{ + ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory(); + + int numOfElementsDict = 4; + JSHandle handleNameDict(thread, NameDictionary::Create(thread, numOfElementsDict)); + JSHandle handleObjFunc = thread->GetEcmaVM()->GetGlobalEnv()->GetObjectFunction(); + CString keyArray1 = "hello1"; + JSHandle stringKey1 = objFactory->NewFromCanBeCompressString(keyArray1); + JSHandle key1(stringKey1); + JSHandle value1(objFactory + ->NewJSObjectByConstructor(JSHandle(handleObjFunc), handleObjFunc)); + JSHandle handleNameDictionaryFrom(thread, + NameDictionary::Put(thread, handleNameDict, key1, value1, PropertyAttributes::Default())); + JSHandle handleEcmaModule(thread, EcmaModuleCreate(thread)); + + handleEcmaModule->SetNameDictionary(thread, handleNameDictionaryFrom); + JSHandle handleNameDictionaryTo(thread, + NameDictionary::Cast(handleEcmaModule->GetNameDictionary().GetTaggedObject())); + EXPECT_EQ(handleNameDictionaryTo->EntriesCount(), 1); + int entry1 = handleNameDictionaryTo->FindEntry(key1.GetTaggedValue()); + EXPECT_TRUE(key1.GetTaggedValue() == handleNameDictionaryTo->GetKey(entry1)); + EXPECT_TRUE(value1.GetTaggedValue() == handleNameDictionaryTo->GetValue(entry1)); +} + +/* + * Feature: ModuleManager + * Function: AddModule + * SubFunction: AddItem/GetModule/GetItem + * FunctionPoints: Add EcmaModule To ModuleManager + * CaseDescription: Create 2 source EcmaModules that both add an item, create a ModuleManager that add the 2 source + * EcmaModule, check whether the items of EcmaModules obtained from the ModuleManager through + * 'GetModule' function and 'GetItem' function are within expectations. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_AddModule) +{ + ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory(); + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + int numOfElementsDict1 = 4; + int numOfElementsDict2 = 4; + CString cStrItemName1 = "cStrItemName1"; + CString cStrItemName2 = "cStrItemName2"; + int intItemValue1 = 1; + int intItemValue2 = 2; + JSHandle handleNameDict1(thread, NameDictionary::Create(thread, numOfElementsDict1)); + JSHandle handleNameDict2(thread, NameDictionary::Create(thread, numOfElementsDict2)); + JSHandle handleItemName1( + thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName1)); + JSHandle handleItemName2( + thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName2)); + JSHandle handleItemValue1(thread, JSTaggedValue(intItemValue1)); + JSHandle handleItemValue2(thread, JSTaggedValue(intItemValue2)); + JSHandle handleEcmaModuleAddFrom1(thread, EcmaModuleCreate(thread)); + JSHandle handleEcmaModuleAddFrom2(thread, EcmaModuleCreate(thread)); + handleEcmaModuleAddFrom1->SetNameDictionary(thread, handleNameDict1); + handleEcmaModuleAddFrom2->SetNameDictionary(thread, handleNameDict2); + + EcmaModule::AddItem(thread, handleEcmaModuleAddFrom1, handleItemName1, handleItemValue1); + JSHandle handleTagValEcmaModuleAddFrom1(thread, handleEcmaModuleAddFrom1.GetTaggedValue()); + std::string stdStrNameEcmaModuleAdd1 = "NameEcmaModule1"; + JSHandle handleTagValNameEcmaModuleAdd1(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd1)); + EcmaModule::AddItem(thread, handleEcmaModuleAddFrom2, handleItemName2, handleItemValue2); + JSHandle handleTagValEcmaModuleAddFrom2(thread, handleEcmaModuleAddFrom2.GetTaggedValue()); + std::string stdStrNameEcmaModuleAdd2 = "NameEcmaModule2"; + JSHandle handleTagValNameEcmaModuleAdd2(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd2)); + + moduleManager->AddModule(handleTagValNameEcmaModuleAdd1, handleTagValEcmaModuleAddFrom1); + moduleManager->AddModule(handleTagValNameEcmaModuleAdd2, handleTagValEcmaModuleAddFrom2); + JSHandle handleTagValEcmaModuleGet1 = moduleManager->GetModule(thread, + handleTagValNameEcmaModuleAdd1); + JSHandle handleTagValEcmaModuleGet2 = moduleManager->GetModule(thread, + handleTagValNameEcmaModuleAdd2); + EXPECT_EQ(JSHandle::Cast(handleTagValEcmaModuleGet1)->GetItem(thread, handleItemName1)->GetNumber(), + intItemValue1); + EXPECT_EQ(JSHandle::Cast(handleTagValEcmaModuleGet2)->GetItem(thread, handleItemName2)->GetNumber(), + intItemValue2); +} + +/* + * Feature: ModuleManager + * Function: RemoveModule + * SubFunction: AddItem/GetModule/AddModule/GetItem + * FunctionPoints: Remove EcmaModule From ModuleManager + * CaseDescription: Create two source EcmaModules that add different items, create a ModuleManager that add the two + * source EcmaModules, check whether the properties of the EcmaModules obtained from the ModuleManager + * through 'GetModule' function are within expectations while removing EcmaModules from the + * ModuleManager through 'RemoveModule' function one by one. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_RemoveModule) +{ + ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory(); + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + std::string stdStrNameEcmaModuleAdd1 = "NameEcmaModule1"; + std::string stdStrNameEcmaModuleAdd2 = "NameEcmaModule2"; + int intItemValue1 = 1; + int intItemValue2 = 2; + int numOfElementsDict1 = 4; + int numOfElementsDict2 = 4; + JSHandle handleTagValNameEcmaModuleAdd1(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd1)); + JSHandle handleTagValNameEcmaModuleAdd2(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd2)); + JSHandle handleTagValItemName1(objFactory->NewFromCanBeCompressString("name1")); + JSHandle handleTagValItemName2(objFactory->NewFromCanBeCompressString("name2")); + JSHandle handleTagValItemValue1(thread, JSTaggedValue(intItemValue1)); + JSHandle handleTagValItemValue2(thread, JSTaggedValue(intItemValue2)); + JSHandle handleEcmaModuleAddFrom1(thread, EcmaModuleCreate(thread)); + JSHandle handleEcmaModuleAddFrom2(thread, EcmaModuleCreate(thread)); + JSHandle handleNameDict1(thread, NameDictionary::Create(thread, numOfElementsDict1)); + JSHandle handleNameDict2(thread, NameDictionary::Create(thread, numOfElementsDict2)); + handleEcmaModuleAddFrom1->SetNameDictionary(thread, handleNameDict1); + handleEcmaModuleAddFrom2->SetNameDictionary(thread, handleNameDict2); + EcmaModule::AddItem(thread, handleEcmaModuleAddFrom1, handleTagValItemName1, handleTagValItemValue1); + EcmaModule::AddItem(thread, handleEcmaModuleAddFrom2, handleTagValItemName2, handleTagValItemValue2); + JSHandle handleTaggedValueEcmaModuleAddFrom1(thread, handleEcmaModuleAddFrom1.GetTaggedValue()); + JSHandle handleTaggedValueEcmaModuleAddFrom2(thread, handleEcmaModuleAddFrom2.GetTaggedValue()); + + moduleManager->AddModule(handleTagValNameEcmaModuleAdd1, handleTaggedValueEcmaModuleAddFrom1); + moduleManager->AddModule(handleTagValNameEcmaModuleAdd2, handleTaggedValueEcmaModuleAddFrom2); + EXPECT_EQ(JSHandle::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1)) + ->GetItem(thread, handleTagValItemName1)->GetNumber(), intItemValue1); + EXPECT_EQ(JSHandle::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2)) + ->GetItem(thread, handleTagValItemName2)->GetNumber(), intItemValue2); + + moduleManager->RemoveModule(handleTagValNameEcmaModuleAdd1); + EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1)->IsUndefined()); + EXPECT_EQ(JSHandle::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2)) + ->GetItem(thread, handleTagValItemName2)->GetNumber(), intItemValue2); + + moduleManager->RemoveModule(handleTagValNameEcmaModuleAdd2); + EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1)->IsUndefined()); + EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2)->IsUndefined()); +} + +/* + * Feature: ModuleManager + * Function: SetCurrentExportModuleName + * SubFunction: GetCurrentExportModuleName + * FunctionPoints: Get Current ExportModuleName Of ModuleManager + * CaseDescription: Create a ModuleManager, check whether the ExportModuleName obtained from the ModuleManager through + * 'GetCurrentExportModuleName' function is within expectations while changing the Current + * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_SetCurrentExportModuleName) +{ + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + std::string_view strViewNameEcmaModule1 = "NameEcmaModule1"; + std::string_view strViewNameEcmaModule2 = "NameEcmaModule2"; + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1); + EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str()); + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2); + EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str()); +} + +/* + * Feature: ModuleManager + * Function: GetPrevExportModuleName + * SubFunction: SetCurrentExportModuleName + * FunctionPoints: Get Previous ExportModuleName Of ModuleManager + * CaseDescription: Create a ModuleManager, check whether the previous ExportModuleName obtained from the ModuleManager + * through 'GetPrevExportModuleName' function is within expectations while changing the Current + * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_GetPrevExportModuleName) +{ + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + std::string_view strViewNameEcmaModule1 = "NameEcmaModule1"; + std::string_view strViewNameEcmaModule2 = "NameEcmaModule2"; + std::string_view strViewNameEcmaModule3 = "NameEcmaModule3"; + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1); + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2); + EXPECT_STREQ(moduleManager->GetPrevExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str()); + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule3); + EXPECT_STREQ(moduleManager->GetPrevExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str()); +} + +/* + * Feature: ModuleManager + * Function: RestoreCurrentExportModuleName + * SubFunction: SetCurrentExportModuleName/GetCurrentExportModuleName + * FunctionPoints: Restore Current ExportModuleName Of ModuleManager + * CaseDescription: Create a ModuleManager, check whether the current ExportModuleName obtained from the ModuleManager + * through 'GetCurrentExportModuleName' function is within expectations while changing the Current + * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function and + * 'RestoreCurrentExportModuleName' function. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_RestoreCurrentExportModuleName) +{ + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + std::string_view strViewNameEcmaModule1 = "NameEcmaModule1"; + std::string_view strViewNameEcmaModule2 = "NameEcmaModule2"; + std::string_view strViewNameEcmaModule3 = "NameEcmaModule3"; + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1); + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2); + moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule3); + EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule3).c_str()); + moduleManager->RestoreCurrentExportModuleName(); + EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str()); + moduleManager->RestoreCurrentExportModuleName(); + EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str()); +} + +/* + * Feature: ModuleManager + * Function: AddModuleItem + * SubFunction: SetCurrentExportModuleName/GetModule/GetModuleItem + * FunctionPoints: Add ModuleItem For Current EcmaModule Of ModuleManager + * CaseDescription: Create a ModuleManager, set the current EcmaModule for the ModuleManager through + * 'SetCurrentExportModuleName' function, add source ModuleItems for the current EcmaModule Of the + * ModuleManager, check whether the ModuleItems obtained through 'GetModuleItem' function from the + * ModuleManager are within expectations. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_AddModuleItem) +{ + ObjectFactory *objFactory = thread->GetEcmaVM()->GetFactory(); + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + int intItemValue11 = 11; + int intItemValue12 = 12; + int intItemValue21 = 21; + int intItemValue22 = 22; + JSHandle handleTagValItemName11(objFactory->NewFromCanBeCompressString("cStrItemName11")); + JSHandle handleTagValItemName12(objFactory->NewFromCanBeCompressString("cStrItemName12")); + JSHandle handleTagValItemName21(objFactory->NewFromCanBeCompressString("cStrItemName21")); + JSHandle handleTagValItemName22(objFactory->NewFromCanBeCompressString("cStrItemName22")); + JSHandle handleTagValItemValue11(thread, JSTaggedValue(intItemValue11)); + JSHandle handleTagValItemValue12(thread, JSTaggedValue(intItemValue12)); + JSHandle handleTagValItemValue21(thread, JSTaggedValue(intItemValue21)); + JSHandle handleTagValItemValue22(thread, JSTaggedValue(intItemValue22)); + JSHandle handleEcmaStrNameEcmaModule1 = objFactory->NewFromString("cStrNameEcmaModule1"); + JSHandle handleEcmaStrNameEcmaModule2 = objFactory->NewFromString("cStrNameEcmaModule2"); + std::string stdStrModuleFileName1 = JSLocale::ConvertToStdString(handleEcmaStrNameEcmaModule1); + std::string stdStrModuleFileName2 = JSLocale::ConvertToStdString(handleEcmaStrNameEcmaModule2); + JSHandle handleTagValEcmaModuleName1(handleEcmaStrNameEcmaModule1); + JSHandle handleTagValEcmaModuleName2(handleEcmaStrNameEcmaModule2); + + // Test when the module is created through 'NewEmptyEcmaModule' function called at HWTEST_F_L0. + JSHandle handleEcmaModule1(thread, EcmaModuleCreate(thread)); + JSHandle handleTagValEcmaModule1(thread, handleEcmaModule1.GetTaggedValue()); + moduleManager->AddModule(handleTagValEcmaModuleName1, handleTagValEcmaModule1); + moduleManager->SetCurrentExportModuleName(stdStrModuleFileName1); + moduleManager->AddModuleItem(thread, handleTagValItemName11, handleTagValItemValue11); + moduleManager->AddModuleItem(thread, handleTagValItemName12, handleTagValItemValue12); + + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName11)->GetNumber(), + intItemValue11); + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName12)->GetNumber(), + intItemValue12); + + // Test when the module is created through 'NewEmptyEcmaModule' function called at "ecma_module.cpp". + moduleManager->SetCurrentExportModuleName(stdStrModuleFileName2); + moduleManager->AddModuleItem(thread, handleTagValItemName21, handleTagValItemValue21); + moduleManager->AddModuleItem(thread, handleTagValItemName22, handleTagValItemValue22); + + JSHandle handleTagValEcmaModule2 = moduleManager->GetModule(thread, handleTagValEcmaModuleName2); + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName11)->GetNumber(), + intItemValue11); + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName12)->GetNumber(), + intItemValue12); + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule2, handleTagValItemName21)->GetNumber(), + intItemValue21); + EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule2, handleTagValItemName22)->GetNumber(), + intItemValue22); +} + +/* + * Feature: ModuleManager + * Function: CopyModule + * SubFunction: AddItem/SetCurrentExportModuleName/GetModule/GetModuleItem + * FunctionPoints: Copy EcmaModule To ModuleManager + * CaseDescription: Create two source EcmaModules and one target ModuleManager, prepare the two source EcmaModules + * through 'AddItem' function, check whether the the ModuleItems obtained through 'GetModuleItem' from + * the target ModuleManager are within expectations while changing the target ModuleManager through + * 'SetCurrentExportModuleName' function and 'CopyModule' function. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleManager_CopyModule) +{ + ObjectFactory *objFactory = thread->GetEcmaVM()->GetFactory(); + ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager(); + + int intItemValue11 = 11; + int intItemValue12 = 12; + int intItemValue21 = 21; + int intItemValue22 = 22; + std::string_view fileNameEcmaModuleCopyTo1 = "fileNameEcmaModuleCopyTo1"; + std::string_view fileNameEcmaModuleCopyTo2 = "fileNameEcmaModuleCopyTo2"; + JSHandle handleTagValItemName11(objFactory->NewFromCanBeCompressString("ItemName11")); + JSHandle handleTagValItemName12(objFactory->NewFromCanBeCompressString("ItemName12")); + JSHandle handleTagValItemName21(objFactory->NewFromCanBeCompressString("ItemName21")); + JSHandle handleTagValItemName22(objFactory->NewFromCanBeCompressString("ItemName22")); + JSHandle handleTagValItemValue11(thread, JSTaggedValue(intItemValue11)); + JSHandle handleTagValItemValue12(thread, JSTaggedValue(intItemValue12)); + JSHandle handleTagValItemValue21(thread, JSTaggedValue(intItemValue21)); + JSHandle handleTagValItemValue22(thread, JSTaggedValue(intItemValue22)); + JSHandle handleEcmaModuleCopyFrom1(thread, EcmaModuleCreate(thread)); + JSHandle handleEcmaModuleCopyFrom2(thread, EcmaModuleCreate(thread)); + JSHandle handleTagValEcmaModuleCopyFrom1(thread, handleEcmaModuleCopyFrom1.GetTaggedValue()); + JSHandle handleTagValEcmaModuleCopyFrom2(thread, handleEcmaModuleCopyFrom2.GetTaggedValue()); + EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom1, handleTagValItemName11, handleTagValItemValue11); + EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom1, handleTagValItemName12, handleTagValItemValue12); + EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom2, handleTagValItemName21, handleTagValItemValue21); + EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom2, handleTagValItemName22, handleTagValItemValue22); + + moduleManager->SetCurrentExportModuleName(fileNameEcmaModuleCopyTo1); + moduleManager->CopyModule(thread, handleTagValEcmaModuleCopyFrom1); + JSHandle handleTagValEcmaModuleCopyTo1 = moduleManager->GetModule(thread, + JSHandle::Cast(objFactory->NewFromString(CString(fileNameEcmaModuleCopyTo1)))); + EXPECT_EQ(intItemValue11, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName11)->GetNumber()); + EXPECT_EQ(intItemValue12, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName12)->GetNumber()); + + moduleManager->SetCurrentExportModuleName(fileNameEcmaModuleCopyTo2); + moduleManager->CopyModule(thread, handleTagValEcmaModuleCopyFrom2); + JSHandle handleTagValEcmaModuleCopyTo2 = moduleManager->GetModule(thread, + JSHandle::Cast(objFactory->NewFromString(CString(fileNameEcmaModuleCopyTo2)))); + EXPECT_EQ(intItemValue11, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName11)->GetNumber()); + EXPECT_EQ(intItemValue12, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName12)->GetNumber()); + EXPECT_EQ(intItemValue21, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo2, handleTagValItemName21)->GetNumber()); + EXPECT_EQ(intItemValue22, + moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo2, handleTagValItemName22)->GetNumber()); +} + +// ModuleStack +/* + * Feature: ModuleStack + * Function: PushModule + * SubFunction: GetTop + * FunctionPoints: Push EcmaModule By Name + * CaseDescription: Create a target ModuleStack, push a source EcmaModule name at the back of the name vector of the + * target ModuleStack, check whether the returned EcmaModule name obtained through 'GetTop' function + * from the target ModuleStack is the same with the source EcmaModule name. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleStack_PushModule) +{ + ModuleStack moduleStack; + + CString cStrPushNo1 = "cStrPushNo1"; + CString cStrPushNo2 = "cStrPushNo2"; + CString cStrPushNo3 = "cStrPushNo3"; + moduleStack.PushModule(cStrPushNo1); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo1.c_str()); + moduleStack.PushModule(cStrPushNo2); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo2.c_str()); + moduleStack.PushModule(cStrPushNo3); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo3.c_str()); +} + +/* + * Feature: ModuleStack + * Function: PopModule + * SubFunction: PushModule/GetTop + * FunctionPoints: Pop EcmaModule At The Back + * CaseDescription: Create a target ModuleStack, push some source EcmaModule names at the back of the name vector of + * the target ModuleStack, check whether the returned EcmaModule name obtained through 'GetTop' + * function from the target ModuleStack now and the returned EcmaModule name obtained through 'GetTop' + * function from the target ModuleStack after the target ModuleStack calls 'PopModule' function are + * within expectations. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleStack_PopModule) +{ + ModuleStack moduleStack; + + CString cStrPushNo1 = "cStrPushNo1"; + CString cStrPushNo2 = "cStrPushNo2"; + CString cStrPushNo3 = "cStrPushNo3"; + moduleStack.PushModule(cStrPushNo1); + moduleStack.PushModule(cStrPushNo2); + moduleStack.PushModule(cStrPushNo3); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo3.c_str()); + moduleStack.PopModule(); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo2.c_str()); + moduleStack.PopModule(); + EXPECT_STREQ(moduleStack.GetTop().c_str(), cStrPushNo1.c_str()); + moduleStack.PopModule(); +} + +/* + * Feature: ModuleStack + * Function: GetPrevModule + * SubFunction: PushModule/GetTop/PopModule + * FunctionPoints: Get Previous Module + * CaseDescription: Create a target ModuleStack, push some source EcmaModule names at the back of the name vector of + * the target ModuleStack, check whether the returned EcmaModule name obtained through 'GetPrevModule' + * function from the target ModuleStack now and the returned EcmaModule name obtained through + * 'GetPrevModule' function from the target ModuleStack after the target ModuleStack calls 'PushModule' + * function or 'PopModule' function are within expectations. + */ +HWTEST_F_L0(EcmaModuleTest, ModuleStack_GetPrevModule) +{ + ModuleStack moduleStack; + + CString cStrPushNo1 = "cStrPushNo1"; + CString cStrPushNo2 = "cStrPushNo2"; + CString cStrPushNo3 = "cStrPushNo3"; + moduleStack.PushModule(cStrPushNo1); + moduleStack.PushModule(cStrPushNo2); + EXPECT_STREQ(moduleStack.GetPrevModule().c_str(), cStrPushNo1.c_str()); + moduleStack.PushModule(cStrPushNo3); + EXPECT_STREQ(moduleStack.GetPrevModule().c_str(), cStrPushNo2.c_str()); + moduleStack.PopModule(); + EXPECT_STREQ(moduleStack.GetPrevModule().c_str(), cStrPushNo1.c_str()); +} +} // namespace panda::ecmascript -- Gitee