Files
ark_js_runtime/ecmascript/builtins/tests/builtins_displaynames_test.cpp
g00416891 f11810349e New EcmaRuntimeCallInfo in stack pointer
Signed-off-by: g00416891 <guobingbing3@huawei.com>
Change-Id: I466a4107e6e073ffecea12e6986c112731d2d3bf
2022-07-05 14:54:43 +08:00

277 lines
13 KiB
C++

/*
* Copyright (c) 2022 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/builtins/builtins_displaynames.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_displaynames.h"
#include "ecmascript/tests/test_helper.h"
using namespace panda::ecmascript;
using namespace panda::ecmascript::builtins;
namespace panda::test {
class BuiltinsDisplayNamesTest : public testing::Test {
public:
static void SetUpTestCase()
{
GTEST_LOG_(INFO) << "SetUpTestCase";
}
static void TearDownTestCase()
{
GTEST_LOG_(INFO) << "TearDownCase";
}
void SetUp() override
{
JSRuntimeOptions options;
#if PANDA_TARGET_LINUX
// for consistency requirement, use ohos_icu4j/data/icudt67l.dat as icu-data-path
options.SetIcuDataPath(ICU_PATH);
#endif
options.SetEnableForceGC(true);
instance = JSNApi::CreateEcmaVM(options);
instance->SetEnableForceGC(true);
ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
thread = instance->GetJSThread();
scope = new EcmaHandleScope(thread);
}
void TearDown() override
{
TestHelper::DestroyEcmaVMWithScope(instance, scope);
}
EcmaVM *instance {nullptr};
EcmaHandleScope *scope {nullptr};
JSThread *thread {nullptr};
};
// new DisplayNames(locales, options)
HWTEST_F_L0(BuiltinsDisplayNamesTest, DisplayNamesConstructor)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSFunction> newTarget(env->GetDisplayNamesFunction());
JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
JSHandle<JSTaggedValue> typeKey = thread->GlobalConstants()->GetHandledTypeString();
JSHandle<JSTaggedValue> styleKey = thread->GlobalConstants()->GetHandledStyleString();
JSHandle<JSTaggedValue> fallbackKey = thread->GlobalConstants()->GetHandledFallbackString();
JSHandle<JSTaggedValue> localeString(factory->NewFromASCII("en"));
JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("language"));
JSHandle<JSTaggedValue> styleValue(factory->NewFromASCII("narrow"));
JSHandle<JSTaggedValue> fallbackValue(factory->NewFromASCII("code"));
JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
JSObject::SetProperty(thread, optionsObj, typeKey, typeValue);
JSObject::SetProperty(thread, optionsObj, styleKey, styleValue);
JSObject::SetProperty(thread, optionsObj, fallbackKey, fallbackValue);
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetCallArg(0, localeString.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::DisplayNamesConstructor(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_TRUE(result.IsJSDisplayNames());
}
static JSTaggedValue JSDisplayNamesCreateWithOptionTest(JSThread *thread, JSHandle<JSTaggedValue> &locale,
JSHandle<JSTaggedValue> &typeValue)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSFunction> newTarget(env->GetDisplayNamesFunction());
JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
JSHandle<JSTaggedValue> typeKey = thread->GlobalConstants()->GetHandledTypeString();
JSObject::SetProperty(thread, optionsObj, typeKey, typeValue);
JSHandle<JSTaggedValue> localesString = locale;
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetCallArg(0, localesString.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::DisplayNamesConstructor(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_TRUE(result.IsJSDisplayNames());
return result;
}
// Of(fr, type(language))
HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_001)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en"));
JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("language"));
JSHandle<JSDisplayNames> jsDisplayNames =
JSHandle<JSDisplayNames>(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue));
JSHandle<JSTaggedValue> stringValue(factory->NewFromASCII("fr"));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_TRUE(result.IsString());
JSHandle<EcmaString> handleEcmaStr(thread, result);
EXPECT_STREQ("French", CString(handleEcmaStr->GetCString().get()).c_str());
}
// Of(419, type(region))
HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_002)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en"));
JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("region"));
JSHandle<JSDisplayNames> jsDisplayNames =
JSHandle<JSDisplayNames>(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue));
JSHandle<JSTaggedValue> stringValue(factory->NewFromASCII("419"));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_TRUE(result.IsString());
JSHandle<EcmaString> handleEcmaStr(thread, result);
EXPECT_STREQ("Latin America", CString(handleEcmaStr->GetCString().get()).c_str());
}
// Of(EUR, type(currency))
HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_003)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en"));
JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("currency"));
JSHandle<JSDisplayNames> jsDisplayNames =
JSHandle<JSDisplayNames>(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue));
JSHandle<JSTaggedValue> stringValue(factory->NewFromASCII("EUR"));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_TRUE(result.IsString());
JSHandle<EcmaString> handleEcmaStr(thread, result);
EXPECT_STREQ("Euro", CString(handleEcmaStr->GetCString().get()).c_str());
}
// SupportedLocalesOf("best fit")
HWTEST_F_L0(BuiltinsDisplayNamesTest, SupportedLocalesOf_001)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("id-u-co-pinyin-de-ID"));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
// set the tag is default value
ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::Undefined());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue resultArr = BuiltinsDisplayNames::SupportedLocalesOf(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
JSHandle<JSArray> resultHandle(thread, resultArr);
JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
EXPECT_EQ(elements->GetLength(), 1U);
JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
EXPECT_STREQ("id-u-co-pinyin-de-id", CString(handleEcmaStr->GetCString().get()).c_str());
}
// SupportedLocalesOf("look up")
HWTEST_F_L0(BuiltinsDisplayNamesTest, SupportedLocalesOf_002)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
JSHandle<JSTaggedValue> localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString();
JSHandle<JSTaggedValue> localeMatcherValue(factory->NewFromASCII("lookup"));
JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue);
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de-DE-u-co-phonebk"));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue resultArr = BuiltinsDisplayNames::SupportedLocalesOf(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
JSHandle<JSArray> resultHandle(thread, resultArr);
JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
EXPECT_EQ(elements->GetLength(), 1U);
JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
EXPECT_STREQ("de-DE-u-co-phonebk", CString(handleEcmaStr->GetCString().get()).c_str());
}
HWTEST_F_L0(BuiltinsDisplayNamesTest, ResolvedOptions)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
auto globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de-DE"));
JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("currency"));
JSHandle<JSDisplayNames> jsDisplayNames =
JSHandle<JSDisplayNames>(thread, JSDisplayNamesCreateWithOptionTest(
thread, locale, typeValue));
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
JSTaggedValue result = BuiltinsDisplayNames::ResolvedOptions(ecmaRuntimeCallInfo);
TestHelper::TearDownFrame(thread, prev);
JSHandle<JSTaggedValue> resultObj =
JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<JSTaggedType>(result.GetRawData())));
// judge whether the properties of the object are the same as those of jsdatetimeformat tag
JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString();
JSHandle<JSTaggedValue> localeValue(factory->NewFromASCII("de-DE"));
EXPECT_EQ(JSTaggedValue::SameValue(
JSObject::GetProperty(thread, resultObj, localeKey).GetValue(), localeValue), true);
JSHandle<JSTaggedValue> typeKey = globalConst->GetHandledTypeString();
EXPECT_EQ(JSTaggedValue::SameValue(
JSObject::GetProperty(thread, resultObj, typeKey).GetValue(), typeValue), true);
}
}