14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_collator.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_array.h" 194514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_collator.h" 214514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 244514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::test { 274514f5e3Sopenharmony_ciusing BuiltinsArray = ecmascript::builtins::BuiltinsArray; 284514f5e3Sopenharmony_ciclass BuiltinsCollatorTest : public BaseTestWithScope<true> { 294514f5e3Sopenharmony_ci}; 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_cistatic JSTaggedValue CollatorConstructor(JSThread *thread, std::vector<JSTaggedValue>& args, 324514f5e3Sopenharmony_ci JSHandle<JSFunction>& target) 334514f5e3Sopenharmony_ci{ 344514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*target), 8); 354514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(target.GetTaggedValue()); 364514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 374514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 384514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(i, args[i]); 394514f5e3Sopenharmony_ci } 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 424514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsCollator::CollatorConstructor(ecmaRuntimeCallInfo); 434514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 444514f5e3Sopenharmony_ci return result; 454514f5e3Sopenharmony_ci} 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_cienum class AlgorithmType { 484514f5e3Sopenharmony_ci COLLATOR_SUPPORTED_LOCALES_OF, 494514f5e3Sopenharmony_ci COLLATOR_RESOLVED_OPTIONS, 504514f5e3Sopenharmony_ci COLLATOR_COMPARE, 514514f5e3Sopenharmony_ci ARRAY_SORT, 524514f5e3Sopenharmony_ci ARRAY_JOIN, 534514f5e3Sopenharmony_ci ARRAY_TOSTR, 544514f5e3Sopenharmony_ci}; 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_cistatic JSTaggedValue CollatorAlgorithm(JSThread *thread, std::vector<JSTaggedValue>& args, int32_t maxArgLen, 574514f5e3Sopenharmony_ci AlgorithmType type, JSTaggedValue thisValue = JSTaggedValue::Undefined()) 584514f5e3Sopenharmony_ci{ 594514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), maxArgLen); 604514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 614514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(thisValue); 624514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 634514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(i, args[i]); 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 664514f5e3Sopenharmony_ci JSTaggedValue result; 674514f5e3Sopenharmony_ci switch (type) { 684514f5e3Sopenharmony_ci case AlgorithmType::COLLATOR_SUPPORTED_LOCALES_OF: 694514f5e3Sopenharmony_ci result = BuiltinsCollator::SupportedLocalesOf(ecmaRuntimeCallInfo); 704514f5e3Sopenharmony_ci break; 714514f5e3Sopenharmony_ci case AlgorithmType::COLLATOR_RESOLVED_OPTIONS: 724514f5e3Sopenharmony_ci result = BuiltinsCollator::ResolvedOptions(ecmaRuntimeCallInfo); 734514f5e3Sopenharmony_ci break; 744514f5e3Sopenharmony_ci case AlgorithmType::COLLATOR_COMPARE: 754514f5e3Sopenharmony_ci result = BuiltinsCollator::Compare(ecmaRuntimeCallInfo); 764514f5e3Sopenharmony_ci break; 774514f5e3Sopenharmony_ci case AlgorithmType::ARRAY_SORT: 784514f5e3Sopenharmony_ci result = BuiltinsArray::Sort(ecmaRuntimeCallInfo); 794514f5e3Sopenharmony_ci break; 804514f5e3Sopenharmony_ci case AlgorithmType::ARRAY_JOIN: 814514f5e3Sopenharmony_ci result = BuiltinsArray::Join(ecmaRuntimeCallInfo); 824514f5e3Sopenharmony_ci break; 834514f5e3Sopenharmony_ci case AlgorithmType::ARRAY_TOSTR: 844514f5e3Sopenharmony_ci result = BuiltinsArray::ToString(ecmaRuntimeCallInfo); 854514f5e3Sopenharmony_ci break; 864514f5e3Sopenharmony_ci default: 874514f5e3Sopenharmony_ci break; 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 904514f5e3Sopenharmony_ci return result; 914514f5e3Sopenharmony_ci} 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, CollatorConstructor) 944514f5e3Sopenharmony_ci{ 954514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 964514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 974514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetCollatorFunction()); 984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> usageKey = thread->GlobalConstants()->GetHandledUsageString(); 1014514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString(); 1024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericKey = thread->GlobalConstants()->GetHandledNumericString(); 1034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> caseFirstKey = thread->GlobalConstants()->GetHandledCaseFirstString(); 1044514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sensitivityKey = thread->GlobalConstants()->GetHandledSensitivityString(); 1054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> ignorePunctuationKey = thread->GlobalConstants()->GetHandledIgnorePunctuationString(); 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> usageValue(factory->NewFromASCII("search")); 1084514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeMatcherValue(factory->NewFromASCII("lookup")); 1094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericValue(factory->NewFromASCII("true")); 1104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> caseFirstValue(factory->NewFromASCII("lower")); 1114514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sensitivityValue(factory->NewFromASCII("variant")); 1124514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> ignorePunctuationValue(factory->NewFromASCII("true")); 1134514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localesString(factory->NewFromASCII("en-Latn-US")); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 1164514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, usageKey, usageValue); 1174514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue); 1184514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, caseFirstKey, caseFirstValue); 1194514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, sensitivityKey, sensitivityValue); 1204514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, ignorePunctuationKey, ignorePunctuationValue); 1214514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, numericKey, numericValue); 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{localesString.GetTaggedValue(), optionsObj.GetTaggedValue()}; 1244514f5e3Sopenharmony_ci auto result = CollatorConstructor(thread, vals, newTarget); 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSCollator()); 1274514f5e3Sopenharmony_ci} 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_cistatic JSTaggedValue JSCollatorCreateWithLocaleTest(JSThread *thread, JSHandle<JSTaggedValue> &locale) 1304514f5e3Sopenharmony_ci{ 1314514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1324514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetCollatorFunction()); 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localesString = locale; 1354514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{localesString.GetTaggedValue(), JSTaggedValue::Undefined()}; 1364514f5e3Sopenharmony_ci auto result = CollatorConstructor(thread, vals, newTarget); 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSCollator()); 1394514f5e3Sopenharmony_ci return result; 1404514f5e3Sopenharmony_ci} 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_cistatic JSTaggedValue JSCollatorCreateWithLocaleAndOptionsTest(JSThread *thread, JSHandle<JSTaggedValue> &locale) 1434514f5e3Sopenharmony_ci{ 1444514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1454514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1464514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetCollatorFunction()); 1474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 1484514f5e3Sopenharmony_ci 1494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localesString = locale; 1504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> usageKey = thread->GlobalConstants()->GetHandledUsageString(); 1514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sensitivityKey = thread->GlobalConstants()->GetHandledSensitivityString(); 1524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> usageValue(factory->NewFromASCII("search")); 1534514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sensitivityValue(factory->NewFromASCII("base")); 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 1564514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, usageKey, usageValue); 1574514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, sensitivityKey, sensitivityValue); 1584514f5e3Sopenharmony_ci 1594514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{localesString.GetTaggedValue(), optionsObj.GetTaggedValue()}; 1604514f5e3Sopenharmony_ci auto result = CollatorConstructor(thread, vals, newTarget); 1614514f5e3Sopenharmony_ci 1624514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSCollator()); 1634514f5e3Sopenharmony_ci return result; 1644514f5e3Sopenharmony_ci} 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci// compare with sort(de) 1674514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, Compare_001) 1684514f5e3Sopenharmony_ci{ 1694514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1704514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de")); 1714514f5e3Sopenharmony_ci JSHandle<JSCollator> jsCollator = JSHandle<JSCollator>(thread, JSCollatorCreateWithLocaleTest(thread, locale)); 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{}; 1744514f5e3Sopenharmony_ci auto result1 = CollatorAlgorithm(thread, vals, 4, AlgorithmType::COLLATOR_COMPARE, jsCollator.GetTaggedValue()); 1754514f5e3Sopenharmony_ci JSHandle<JSFunction> jsFunction(thread, result1); 1764514f5e3Sopenharmony_ci 1774514f5e3Sopenharmony_ci JSArray *jsArray = 1784514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 1794514f5e3Sopenharmony_ci JSHandle<JSObject> jsObject(thread, jsArray); 1804514f5e3Sopenharmony_ci 1814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0)); 1824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value0(factory->NewFromASCII("Z")); 1834514f5e3Sopenharmony_ci PropertyDescriptor desc0(thread, value0, true, true, true); 1844514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key0, desc0); 1854514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1)); 1864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value1(factory->NewFromASCII("a")); 1874514f5e3Sopenharmony_ci PropertyDescriptor desc1(thread, value1, true, true, true); 1884514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key1, desc1); 1894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2)); 1904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(factory->NewFromUtf8("ä")); 1914514f5e3Sopenharmony_ci PropertyDescriptor desc2(thread, value2, true, true, true); 1924514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key2, desc2); 1934514f5e3Sopenharmony_ci 1944514f5e3Sopenharmony_ci std::vector<JSTaggedValue> arrayVals{jsFunction.GetTaggedValue()}; 1954514f5e3Sopenharmony_ci auto result2 = CollatorAlgorithm(thread, arrayVals, 6, AlgorithmType::ARRAY_SORT, jsObject.GetTaggedValue()); 1964514f5e3Sopenharmony_ci 1974514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultArr = 1984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<JSTaggedType>(result2.GetRawData()))); 1994514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::SameValue(JSArray::GetProperty(thread, resultArr, key0).GetValue(), value1), true); 2004514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::SameValue(JSArray::GetProperty(thread, resultArr, key1).GetValue(), value2), true); 2014514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::SameValue(JSArray::GetProperty(thread, resultArr, key2).GetValue(), value0), true); 2024514f5e3Sopenharmony_ci} 2034514f5e3Sopenharmony_ci 2044514f5e3Sopenharmony_ci// // compare with sort(sv) 2054514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, Compare_002) 2064514f5e3Sopenharmony_ci{ 2074514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2084514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("sv")); 2094514f5e3Sopenharmony_ci JSHandle<JSCollator> jsCollator = JSHandle<JSCollator>(thread, JSCollatorCreateWithLocaleTest(thread, locale)); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{}; 2124514f5e3Sopenharmony_ci auto result1 = CollatorAlgorithm(thread, vals, 4, AlgorithmType::COLLATOR_COMPARE, jsCollator.GetTaggedValue()); 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ci JSHandle<JSFunction> jsFunction(thread, result1); 2154514f5e3Sopenharmony_ci JSArray *jsArray = 2164514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 2174514f5e3Sopenharmony_ci JSHandle<JSObject> jsObject(thread, jsArray); 2184514f5e3Sopenharmony_ci 2194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0)); 2204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value0(factory->NewFromASCII("Z")); 2214514f5e3Sopenharmony_ci PropertyDescriptor desc0(thread, value0, true, true, true); 2224514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key0, desc0); 2234514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1)); 2244514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value1(factory->NewFromASCII("a")); 2254514f5e3Sopenharmony_ci PropertyDescriptor desc1(thread, value1, true, true, true); 2264514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key1, desc1); 2274514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2)); 2284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(factory->NewFromUtf8("ä")); 2294514f5e3Sopenharmony_ci PropertyDescriptor desc2(thread, value2, true, true, true); 2304514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, key2, desc2); 2314514f5e3Sopenharmony_ci 2324514f5e3Sopenharmony_ci std::vector<JSTaggedValue> arrayVals{jsFunction.GetTaggedValue()}; 2334514f5e3Sopenharmony_ci auto result2 = CollatorAlgorithm(thread, arrayVals, 6, AlgorithmType::ARRAY_SORT, jsObject.GetTaggedValue()); 2344514f5e3Sopenharmony_ci JSHandle<JSObject> resultObj(thread, result2); 2354514f5e3Sopenharmony_ci 2364514f5e3Sopenharmony_ci JSHandle<EcmaString> str = thread->GetEcmaVM()->GetFactory()->NewFromUtf8("a,Z,ä"); 2374514f5e3Sopenharmony_ci std::vector<JSTaggedValue> arrayVals2{}; 2384514f5e3Sopenharmony_ci auto result = CollatorAlgorithm(thread, arrayVals2, 4, AlgorithmType::ARRAY_JOIN, resultObj.GetTaggedValue()); 2394514f5e3Sopenharmony_ci 2404514f5e3Sopenharmony_ci JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 2414514f5e3Sopenharmony_ci EXPECT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, str), 0); 2424514f5e3Sopenharmony_ci} 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci// compare with options("search") 2454514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, Compare_003) 2464514f5e3Sopenharmony_ci{ 2474514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("sv")); 2494514f5e3Sopenharmony_ci JSHandle<JSCollator> jsCollator = 2504514f5e3Sopenharmony_ci JSHandle<JSCollator>(thread, JSCollatorCreateWithLocaleAndOptionsTest(thread, locale)); 2514514f5e3Sopenharmony_ci 2524514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{}; 2534514f5e3Sopenharmony_ci auto result1 = CollatorAlgorithm(thread, vals, 4, AlgorithmType::COLLATOR_COMPARE, jsCollator.GetTaggedValue()); 2544514f5e3Sopenharmony_ci 2554514f5e3Sopenharmony_ci JSHandle<JSFunction> jsFunction(thread, result1); 2564514f5e3Sopenharmony_ci JSArray *jsArray = 2574514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 2584514f5e3Sopenharmony_ci JSHandle<JSObject> jsObject(thread, jsArray); 2594514f5e3Sopenharmony_ci 2604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value0(factory->NewFromUtf8("Congrès")); 2614514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value1(factory->NewFromASCII("congres")); 2624514f5e3Sopenharmony_ci PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(jsFunction), true, true, true); 2634514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> joinKey(factory->NewFromASCII("join")); 2644514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, jsObject, joinKey, desc); 2654514f5e3Sopenharmony_ci 2664514f5e3Sopenharmony_ci std::vector<JSTaggedValue> arrayVals{value0.GetTaggedValue(), value1.GetTaggedValue()}; 2674514f5e3Sopenharmony_ci auto result2 = CollatorAlgorithm(thread, arrayVals, 8, AlgorithmType::ARRAY_TOSTR, jsObject.GetTaggedValue()); 2684514f5e3Sopenharmony_ci 2694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultHandle(thread, result2); 2704514f5e3Sopenharmony_ci EXPECT_EQ(resultHandle->GetInt(), 0); // Congrès and congres is matching 2714514f5e3Sopenharmony_ci} 2724514f5e3Sopenharmony_ci 2734514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, ResolvedOptions) 2744514f5e3Sopenharmony_ci{ 2754514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2764514f5e3Sopenharmony_ci auto globalConst = thread->GlobalConstants(); 2774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de")); 2784514f5e3Sopenharmony_ci JSHandle<JSCollator> jsCollator = JSHandle<JSCollator>(thread, JSCollatorCreateWithLocaleTest(thread, locale)); 2794514f5e3Sopenharmony_ci 2804514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{}; 2814514f5e3Sopenharmony_ci auto result = 2824514f5e3Sopenharmony_ci CollatorAlgorithm(thread, vals, 4, AlgorithmType::COLLATOR_RESOLVED_OPTIONS, jsCollator.GetTaggedValue()); 2834514f5e3Sopenharmony_ci 2844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultObj = 2854514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<JSTaggedType>(result.GetRawData()))); 2864514f5e3Sopenharmony_ci // judge whether the properties of the object are the same as those of jscollator tag 2874514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString(); 2884514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::SameValue(JSObject::GetProperty(thread, resultObj, localeKey).GetValue(), locale), true); 2894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> usageKey = globalConst->GetHandledUsageString(); 2904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> defaultUsageValue(factory->NewFromASCII("sort")); 2914514f5e3Sopenharmony_ci EXPECT_EQ( 2924514f5e3Sopenharmony_ci JSTaggedValue::SameValue(JSObject::GetProperty(thread, resultObj, usageKey).GetValue(), defaultUsageValue), 2934514f5e3Sopenharmony_ci true); 2944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handledCaseFirstKey = globalConst->GetHandledCaseFirstString(); 2954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handledCaseFirstValue(factory->NewFromASCII("upper")); 2964514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::SameValue(JSObject::GetProperty(thread, resultObj, handledCaseFirstKey).GetValue(), 2974514f5e3Sopenharmony_ci handledCaseFirstValue), 2984514f5e3Sopenharmony_ci true); 2994514f5e3Sopenharmony_ci} 3004514f5e3Sopenharmony_ci 3014514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsCollatorTest, SupportedLocalesOf) 3024514f5e3Sopenharmony_ci{ 3034514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3044514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 3064514f5e3Sopenharmony_ci 3074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString(); 3084514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeMatcherValue(factory->NewFromASCII("lookup")); 3094514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 3104514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue); 3114514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("id-u-co-pinyin-de-ID")); 3124514f5e3Sopenharmony_ci 3134514f5e3Sopenharmony_ci std::vector<JSTaggedValue> vals{locale.GetTaggedValue(), optionsObj.GetTaggedValue()}; 3144514f5e3Sopenharmony_ci auto resultArr = CollatorAlgorithm(thread, vals, 8, AlgorithmType::COLLATOR_SUPPORTED_LOCALES_OF); 3154514f5e3Sopenharmony_ci 3164514f5e3Sopenharmony_ci JSHandle<JSArray> resultHandle(thread, resultArr); 3174514f5e3Sopenharmony_ci JSHandle<TaggedArray> elements(thread, resultHandle->GetElements()); 3184514f5e3Sopenharmony_ci EXPECT_EQ(elements->GetLength(), 1U); 3194514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0)); 3204514f5e3Sopenharmony_ci EXPECT_STREQ("id-u-co-pinyin-de-id", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3214514f5e3Sopenharmony_ci} 3224514f5e3Sopenharmony_ci} // namespace panda::test 323