1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/js_collator.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/tests/test_helper.h"
19
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class JSCollatorTest : public BaseTestWithScope<true> {
24 };
25
26 /**
27 * @tc.name: GetIcuCollatorAndCompare
28 * @tc.desc: Call "SetIcuCollator" function set IcuCollator arrributes, then check whether the return value is
29 * expected and Call "CompareStrings" function to compare the two strings to check whether the attribute
30 * setting of ICU Collator meets the requirements.
31 * @tc.type: FUNC
32 * @tc.require:
33 */
HWTEST_F_L0(JSCollatorTest,GetIcuCollatorAndCompare)34 HWTEST_F_L0(JSCollatorTest, GetIcuCollatorAndCompare)
35 {
36 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
37 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
38 // test string
39 JSHandle<EcmaString> string1 = factory->NewFromASCII("ABC");
40 JSHandle<EcmaString> string2 = factory->NewFromASCII("abc");
41 JSHandle<EcmaString> string3 = factory->NewFromASCII("cde");
42 UErrorCode status = U_ZERO_ERROR;
43 JSHandle<JSTaggedValue> ctor = env->GetCollatorFunction();
44 JSHandle<JSCollator> collator =
45 JSHandle<JSCollator>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
46 // test Collator for US and set its strength to PRIMARY
47 icu::Collator *icuCollator = icu::Collator::createInstance("US", status);
48 EXPECT_TRUE(icuCollator != nullptr);
49 icuCollator->setStrength(icu::Collator::PRIMARY);
50 // Call "SetIcuCollator" function set icu collator
51 JSCollator::SetIcuCollator(thread, collator, icuCollator, JSCollator::FreeIcuCollator);
52 icu::Collator *icuCollator1 = collator->GetIcuCollator(thread);
53 EXPECT_TRUE(icuCollator1 == icuCollator);
54 JSTaggedValue result = JSCollator::CompareStrings(thread, icuCollator1, string1, string2);
55 EXPECT_EQ(result.GetInt(), 0); // equivalent
56 result = JSCollator::CompareStrings(thread, icuCollator1, string1, string3);
57 EXPECT_EQ(result.GetInt(), -1); // less than
58 // test Collator is zh-Hans-CN locale
59 icu::Locale zhLocale("zh", "Hans", "CN");
60 icuCollator = icu::Collator::createInstance(zhLocale, status);
61 icuCollator->setStrength(icu::Collator::PRIMARY);
62 JSCollator::SetIcuCollator(thread, collator, icuCollator, JSCollator::FreeIcuCollator);
63 icu::Collator *icuCollator2 = collator->GetIcuCollator(thread);
64 EXPECT_TRUE(icuCollator2 == icuCollator);
65 result = JSCollator::CompareStrings(thread, icuCollator2, string1, string2);
66 EXPECT_EQ(result.GetInt(), 0); // equivalent
67 }
68
69 /**
70 * @tc.name: InitializeCollatorAndGetIcuCollator
71 * @tc.desc: Call "InitializeCollator" function initialize the attributes of Collator,then check whether the
72 * attributes is expected.
73 * @tc.type: FUNC
74 * @tc.require:
75 */
HWTEST_F_L0(JSCollatorTest,InitializeCollatorAndGetIcuCollator)76 HWTEST_F_L0(JSCollatorTest, InitializeCollatorAndGetIcuCollator)
77 {
78 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
79 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
80 JSHandle<JSTaggedValue> ctor = env->GetCollatorFunction();
81 JSHandle<JSCollator> collator =
82 JSHandle<JSCollator>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
83 JSHandle<JSTaggedValue> localeStr = thread->GlobalConstants()->GetHandledEnUsString();
84 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
85
86 JSHandle<JSCollator> initCollator = JSCollator::InitializeCollator(thread, collator, localeStr, undefinedHandle);
87 EXPECT_EQ(JSTaggedValue::SameValue(thread, collator.GetTaggedValue(), initCollator.GetTaggedValue()), true);
88 // check attributes
89 EXPECT_TRUE(initCollator->GetBoundCompare(thread).IsUndefined());
90 EXPECT_EQ(initCollator->GetIgnorePunctuation(), false);
91 EXPECT_EQ(initCollator->GetSensitivity(), SensitivityOption::VARIANT);
92 EXPECT_EQ(initCollator->GetCaseFirst(), CaseFirstOption::UNDEFINED);
93 EXPECT_TRUE(initCollator->GetCollation(thread).IsUndefined());
94 EXPECT_EQ(initCollator->GetUsage(), UsageOption::SORT);
95 EXPECT_TRUE(
96 JSTaggedValue::SameValue(thread, JSHandle<JSTaggedValue>(thread, initCollator->GetLocale(thread)), localeStr));
97 // check icucollator
98 icu::Collator *icuCollator = initCollator->GetIcuCollator(thread);
99 EXPECT_EQ(icuCollator->getStrength(), icu::Collator::TERTIARY);
100 }
101 } // namespace panda::test
102