14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 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/base/error_helper.h"
174514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
184514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
214514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base;
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::test {
244514f5e3Sopenharmony_ciclass ErrorHelperTest : public BaseTestWithScope<false> {
254514f5e3Sopenharmony_ci};
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_001)
284514f5e3Sopenharmony_ci{
294514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
304514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
314514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorFunc = env->GetErrorFunction();
324514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalErrorFunc = env->GetEvalErrorFunction();
334514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeErrorFunc = env->GetTypeErrorFunction();
344514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeErrorFunc = env->GetRangeErrorFunction();
354514f5e3Sopenharmony_ci    JSHandle<JSObject> errorObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(errorFunc), errorFunc);
364514f5e3Sopenharmony_ci    JSHandle<JSObject> evalErrorObj =
374514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(evalErrorFunc), evalErrorFunc);
384514f5e3Sopenharmony_ci    JSHandle<JSObject> typeErrorObj =
394514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(typeErrorFunc), typeErrorFunc);
404514f5e3Sopenharmony_ci    JSHandle<JSObject> rangeErrorObj =
414514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(rangeErrorFunc), rangeErrorFunc);
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
444514f5e3Sopenharmony_ci    argv->SetFunction(JSTaggedValue::Undefined());
454514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*errorObj));
464514f5e3Sopenharmony_ci    auto prev = TestHelper::SetupFrame(thread, argv);
474514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> error(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::ERROR));
484514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*evalErrorObj));
514514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
524514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::EVAL_ERROR));
534514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*typeErrorObj));
564514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
574514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::TYPE_ERROR));
584514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*rangeErrorObj));
614514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
624514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::RANGE_ERROR));
634514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    EcmaStringAccessor errorStrAcc(JSHandle<EcmaString>::Cast(error));
664514f5e3Sopenharmony_ci    EcmaStringAccessor evalErrorStrAcc(JSHandle<EcmaString>::Cast(evalError));
674514f5e3Sopenharmony_ci    EcmaStringAccessor typeErrorStrAcc(JSHandle<EcmaString>::Cast(typeError));
684514f5e3Sopenharmony_ci    EcmaStringAccessor rangeErrorStrAcc(JSHandle<EcmaString>::Cast(rangeError));
694514f5e3Sopenharmony_ci    EXPECT_STREQ(errorStrAcc.ToCString().c_str(), "Error");
704514f5e3Sopenharmony_ci    EXPECT_STREQ(evalErrorStrAcc.ToCString().c_str(), "EvalError");
714514f5e3Sopenharmony_ci    EXPECT_STREQ(typeErrorStrAcc.ToCString().c_str(), "TypeError");
724514f5e3Sopenharmony_ci    EXPECT_STREQ(rangeErrorStrAcc.ToCString().c_str(), "RangeError");
734514f5e3Sopenharmony_ci}
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_002)
764514f5e3Sopenharmony_ci{
774514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
784514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
794514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriErrorFunc = env->GetURIErrorFunction();
804514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomErrorFunc = env->GetOOMErrorFunction();
814514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxErrorFunc = env->GetSyntaxErrorFunction();
824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceErrorFunc = env->GetReferenceErrorFunction();
834514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateErrorFunc = env->GetAggregateErrorFunction();
844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> terminationErrorFunc = env->GetTerminationErrorFunction();
854514f5e3Sopenharmony_ci    JSHandle<JSObject> uriErrorObj =
864514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(uriErrorFunc), uriErrorFunc);
874514f5e3Sopenharmony_ci    JSHandle<JSObject> oomErrorObj =
884514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(oomErrorFunc), oomErrorFunc);
894514f5e3Sopenharmony_ci    JSHandle<JSObject> syntaxErrorObj =
904514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(syntaxErrorFunc), syntaxErrorFunc);
914514f5e3Sopenharmony_ci    JSHandle<JSObject> referenceErrorObj =
924514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(referenceErrorFunc), referenceErrorFunc);
934514f5e3Sopenharmony_ci    JSHandle<JSObject> aggregateErrorObj =
944514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(aggregateErrorFunc), aggregateErrorFunc);
954514f5e3Sopenharmony_ci    JSHandle<JSObject> terminationErrorObj =
964514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(terminationErrorFunc), terminationErrorFunc);
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
994514f5e3Sopenharmony_ci    argv->SetFunction(JSTaggedValue::Undefined());
1004514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*uriErrorObj));
1014514f5e3Sopenharmony_ci    auto prev = TestHelper::SetupFrame(thread, argv);
1024514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::URI_ERROR));
1034514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*oomErrorObj));
1064514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
1074514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::OOM_ERROR));
1084514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*terminationErrorObj));
1114514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
1124514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> terminationError(thread,
1134514f5e3Sopenharmony_ci        ErrorHelper::ErrorCommonToString(argv, ErrorType::TERMINATION_ERROR));
1144514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*syntaxErrorObj));
1174514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
1184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::SYNTAX_ERROR));
1194514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*referenceErrorObj));
1224514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
1234514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::REFERENCE_ERROR));
1244514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci    argv->SetThis(JSTaggedValue(*aggregateErrorObj));
1274514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, argv);
1284514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::AGGREGATE_ERROR));
1294514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci    EcmaStringAccessor uriErrorStrAcc(JSHandle<EcmaString>::Cast(uriError));
1324514f5e3Sopenharmony_ci    EcmaStringAccessor oomErrorStrAcc(JSHandle<EcmaString>::Cast(oomError));
1334514f5e3Sopenharmony_ci    EcmaStringAccessor syntaxErrorStrAcc(JSHandle<EcmaString>::Cast(syntaxError));
1344514f5e3Sopenharmony_ci    EcmaStringAccessor referenceErrorStrAcc(JSHandle<EcmaString>::Cast(referenceError));
1354514f5e3Sopenharmony_ci    EcmaStringAccessor aggregateErrorStrAcc(JSHandle<EcmaString>::Cast(aggregateError));
1364514f5e3Sopenharmony_ci    EXPECT_STREQ(uriErrorStrAcc.ToCString().c_str(), "URIError");
1374514f5e3Sopenharmony_ci    EXPECT_STREQ(oomErrorStrAcc.ToCString().c_str(), "OutOfMemoryError");
1384514f5e3Sopenharmony_ci    EXPECT_STREQ(syntaxErrorStrAcc.ToCString().c_str(), "SyntaxError");
1394514f5e3Sopenharmony_ci    EXPECT_STREQ(referenceErrorStrAcc.ToCString().c_str(), "ReferenceError");
1404514f5e3Sopenharmony_ci    EXPECT_STREQ(aggregateErrorStrAcc.ToCString().c_str(), "AggregateError");
1414514f5e3Sopenharmony_ci}
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_001)
1444514f5e3Sopenharmony_ci{
1454514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
1464514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
1474514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
1484514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
1494514f5e3Sopenharmony_ci
1504514f5e3Sopenharmony_ci    JSHandle<JSFunction> error(env->GetErrorFunction());
1514514f5e3Sopenharmony_ci    JSHandle<JSFunction> evalError(env->GetEvalErrorFunction());
1524514f5e3Sopenharmony_ci    JSHandle<JSFunction> typeError(env->GetTypeErrorFunction());
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorMsg(factory->NewFromASCII("You have an Error!"));
1554514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 6);
1564514f5e3Sopenharmony_ci    argv1->SetFunction(error.GetTaggedValue());
1574514f5e3Sopenharmony_ci    argv1->SetThis(JSTaggedValue(*error));
1584514f5e3Sopenharmony_ci    argv1->SetCallArg(0, errorMsg.GetTaggedValue());
1594514f5e3Sopenharmony_ci    auto prev1 = TestHelper::SetupFrame(thread, argv1);
1604514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR));
1614514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
1624514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue());
1634514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue());
1644514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorMsgValue)).ToCString().c_str(),
1654514f5e3Sopenharmony_ci                 "You have an Error!");
1664514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString().c_str(), "Error");
1674514f5e3Sopenharmony_ci
1684514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalErrorMsg(factory->NewFromASCII("You have an eval error!"));
1694514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*evalError), 6);
1704514f5e3Sopenharmony_ci    argv2->SetFunction(evalError.GetTaggedValue());
1714514f5e3Sopenharmony_ci    argv2->SetThis(JSTaggedValue(*evalError));
1724514f5e3Sopenharmony_ci    argv2->SetCallArg(0, evalErrorMsg.GetTaggedValue());
1734514f5e3Sopenharmony_ci    auto prev2 = TestHelper::SetupFrame(thread, argv2);
1744514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::EVAL_ERROR));
1754514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
1764514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalMsgValue(JSObject::GetProperty(thread, evalErrorResult, msgKey).GetValue());
1774514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> evalNameValue(JSObject::GetProperty(thread, evalErrorResult, nameKey).GetValue());
1784514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(evalMsgValue)).ToCString().c_str(),
1794514f5e3Sopenharmony_ci                 "You have an eval error!");
1804514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(evalNameValue)).ToCString().c_str(), "EvalError");
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeErrorMsg(factory->NewFromASCII("You have a type error!"));
1834514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 6);
1844514f5e3Sopenharmony_ci    argv3->SetFunction(typeError.GetTaggedValue());
1854514f5e3Sopenharmony_ci    argv3->SetThis(JSTaggedValue(*typeError));
1864514f5e3Sopenharmony_ci    argv3->SetCallArg(0, typeErrorMsg.GetTaggedValue());
1874514f5e3Sopenharmony_ci    auto prev3 = TestHelper::SetupFrame(thread, argv3);
1884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::TYPE_ERROR));
1894514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev3);
1904514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue());
1914514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue());
1924514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeMsgValue)).ToCString().c_str(),
1934514f5e3Sopenharmony_ci                 "You have a type error!");
1944514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString().c_str(), "TypeError");
1954514f5e3Sopenharmony_ci}
1964514f5e3Sopenharmony_ci
1974514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_002)
1984514f5e3Sopenharmony_ci{
1994514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
2004514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
2014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
2024514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
2034514f5e3Sopenharmony_ci
2044514f5e3Sopenharmony_ci    JSHandle<JSFunction> rangeError(env->GetRangeErrorFunction());
2054514f5e3Sopenharmony_ci    JSHandle<JSFunction> uriError(env->GetURIErrorFunction());
2064514f5e3Sopenharmony_ci    JSHandle<JSFunction> oomError(env->GetOOMErrorFunction());
2074514f5e3Sopenharmony_ci
2084514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeErrorMsg(factory->NewFromASCII("You have an range error!"));
2094514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*rangeError), 6);
2104514f5e3Sopenharmony_ci    argv1->SetFunction(rangeError.GetTaggedValue());
2114514f5e3Sopenharmony_ci    argv1->SetThis(JSTaggedValue(*rangeError));
2124514f5e3Sopenharmony_ci    argv1->SetCallArg(0, rangeErrorMsg.GetTaggedValue());
2134514f5e3Sopenharmony_ci    auto prev1 = TestHelper::SetupFrame(thread, argv1);
2144514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeErrorResult(thread,
2154514f5e3Sopenharmony_ci                                             ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::RANGE_ERROR));
2164514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
2174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeMsgValue(JSObject::GetProperty(thread, rangeErrorResult, msgKey).GetValue());
2184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rangeNameValue(JSObject::GetProperty(thread, rangeErrorResult, nameKey).GetValue());
2194514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(rangeMsgValue)).ToCString().c_str(),
2204514f5e3Sopenharmony_ci                 "You have an range error!");
2214514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(rangeNameValue)).ToCString().c_str(), "RangeError");
2224514f5e3Sopenharmony_ci
2234514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriErrorMsg(factory->NewFromASCII("You have an uri error!"));
2244514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*uriError), 6);
2254514f5e3Sopenharmony_ci    argv2->SetFunction(uriError.GetTaggedValue());
2264514f5e3Sopenharmony_ci    argv2->SetThis(JSTaggedValue(*uriError));
2274514f5e3Sopenharmony_ci    argv2->SetCallArg(0, uriErrorMsg.GetTaggedValue());
2284514f5e3Sopenharmony_ci    auto prev2 = TestHelper::SetupFrame(thread, argv2);
2294514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::URI_ERROR));
2304514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
2314514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriMsgValue(JSObject::GetProperty(thread, uriErrorResult, msgKey).GetValue());
2324514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> uriNameValue(JSObject::GetProperty(thread, uriErrorResult, nameKey).GetValue());
2334514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriMsgValue)).ToCString().c_str(),
2344514f5e3Sopenharmony_ci                 "You have an uri error!");
2354514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriNameValue)).ToCString().c_str(), "URIError");
2364514f5e3Sopenharmony_ci
2374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomErrorMsg(factory->NewFromASCII("You have an out of memory error!"));
2384514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*oomError), 6);
2394514f5e3Sopenharmony_ci    argv3->SetFunction(oomError.GetTaggedValue());
2404514f5e3Sopenharmony_ci    argv3->SetThis(JSTaggedValue(*oomError));
2414514f5e3Sopenharmony_ci    argv3->SetCallArg(0, oomErrorMsg.GetTaggedValue());
2424514f5e3Sopenharmony_ci    auto prev3 = TestHelper::SetupFrame(thread, argv3);
2434514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::OOM_ERROR));
2444514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev3);
2454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomMsgValue(JSObject::GetProperty(thread, oomErrorResult, msgKey).GetValue());
2464514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> oomNameValue(JSObject::GetProperty(thread, oomErrorResult, nameKey).GetValue());
2474514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomMsgValue)).ToCString().c_str(),
2484514f5e3Sopenharmony_ci                 "You have an out of memory error!");
2494514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomNameValue)).ToCString().c_str(), "OutOfMemoryError");
2504514f5e3Sopenharmony_ci}
2514514f5e3Sopenharmony_ci
2524514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_003)
2534514f5e3Sopenharmony_ci{
2544514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
2554514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
2564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
2574514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
2584514f5e3Sopenharmony_ci
2594514f5e3Sopenharmony_ci    JSHandle<JSFunction> syntaxError(env->GetSyntaxErrorFunction());
2604514f5e3Sopenharmony_ci    JSHandle<JSFunction> referenceError(env->GetReferenceErrorFunction());
2614514f5e3Sopenharmony_ci    JSHandle<JSFunction> aggregateError(env->GetAggregateErrorFunction());
2624514f5e3Sopenharmony_ci
2634514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxErrorMsg(factory->NewFromASCII("You have an syntax error!"));
2644514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*syntaxError), 6);
2654514f5e3Sopenharmony_ci    argv1->SetFunction(syntaxError.GetTaggedValue());
2664514f5e3Sopenharmony_ci    argv1->SetThis(JSTaggedValue(*syntaxError));
2674514f5e3Sopenharmony_ci    argv1->SetCallArg(0, syntaxErrorMsg.GetTaggedValue());
2684514f5e3Sopenharmony_ci    auto prev1 = TestHelper::SetupFrame(thread, argv1);
2694514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxErrorResult(thread,
2704514f5e3Sopenharmony_ci                                              ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::SYNTAX_ERROR));
2714514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
2724514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxMsgValue(JSObject::GetProperty(thread, syntaxErrorResult, msgKey).GetValue());
2734514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> syntaxNameValue(JSObject::GetProperty(thread, syntaxErrorResult, nameKey).GetValue());
2744514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxMsgValue)).ToCString().c_str(),
2754514f5e3Sopenharmony_ci                 "You have an syntax error!");
2764514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxNameValue)).ToCString().c_str(), "SyntaxError");
2774514f5e3Sopenharmony_ci
2784514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceErrorMsg(factory->NewFromASCII("You have an reference error!"));
2794514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*referenceError), 6);
2804514f5e3Sopenharmony_ci    argv2->SetFunction(referenceError.GetTaggedValue());
2814514f5e3Sopenharmony_ci    argv2->SetThis(JSTaggedValue(*referenceError));
2824514f5e3Sopenharmony_ci    argv2->SetCallArg(0, referenceErrorMsg.GetTaggedValue());
2834514f5e3Sopenharmony_ci    auto prev2 = TestHelper::SetupFrame(thread, argv2);
2844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceErrorResult(thread,
2854514f5e3Sopenharmony_ci        ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::REFERENCE_ERROR));
2864514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
2874514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceMsgValue(JSObject::GetProperty(thread, referenceErrorResult, msgKey).GetValue());
2884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> referenceNameValue(
2894514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, referenceErrorResult, nameKey).GetValue());
2904514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceMsgValue)).ToCString().c_str(),
2914514f5e3Sopenharmony_ci                 "You have an reference error!");
2924514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceNameValue)).ToCString().c_str(),
2934514f5e3Sopenharmony_ci                 "ReferenceError");
2944514f5e3Sopenharmony_ci
2954514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateErrorMsg(factory->NewFromASCII("You have an aggregate error!"));
2964514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*aggregateError), 6);
2974514f5e3Sopenharmony_ci    argv3->SetFunction(aggregateError.GetTaggedValue());
2984514f5e3Sopenharmony_ci    argv3->SetThis(JSTaggedValue(*aggregateError));
2994514f5e3Sopenharmony_ci    argv3->SetCallArg(0, aggregateErrorMsg.GetTaggedValue());
3004514f5e3Sopenharmony_ci    auto prev3 = TestHelper::SetupFrame(thread, argv3);
3014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateErrorResult(thread,
3024514f5e3Sopenharmony_ci        ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::AGGREGATE_ERROR));
3034514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev3);
3044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateMsgValue(JSObject::GetProperty(thread, aggregateErrorResult, msgKey).GetValue());
3054514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> aggregateNameValue(
3064514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, aggregateErrorResult, nameKey).GetValue());
3074514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateMsgValue)).ToCString().c_str(),
3084514f5e3Sopenharmony_ci                 "You have an aggregate error!");
3094514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateNameValue)).ToCString().c_str(),
3104514f5e3Sopenharmony_ci                 "AggregateError");
3114514f5e3Sopenharmony_ci}
3124514f5e3Sopenharmony_ci
3134514f5e3Sopenharmony_ciHWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_004)
3144514f5e3Sopenharmony_ci{
3154514f5e3Sopenharmony_ci    auto factory = instance->GetFactory();
3164514f5e3Sopenharmony_ci    auto env = instance->GetGlobalEnv();
3174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
3184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
3194514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> causeKey = thread->GlobalConstants()->GetHandledCauseString();
3204514f5e3Sopenharmony_ci
3214514f5e3Sopenharmony_ci    JSHandle<JSFunction> error(env->GetErrorFunction());
3224514f5e3Sopenharmony_ci    JSHandle<JSFunction> typeError(env->GetTypeErrorFunction());
3234514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
3244514f5e3Sopenharmony_ci    JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
3254514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> causeValue(factory->NewFromASCII("error cause")); // test error cause
3264514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, causeKey, causeValue);
3274514f5e3Sopenharmony_ci
3284514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorMsg(factory->NewFromASCII("You have an Error!"));
3294514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv1 =
3304514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 8); // 8 means 2 call args
3314514f5e3Sopenharmony_ci    argv1->SetFunction(error.GetTaggedValue());
3324514f5e3Sopenharmony_ci    argv1->SetThis(JSTaggedValue(*error));
3334514f5e3Sopenharmony_ci    argv1->SetCallArg(0, errorMsg.GetTaggedValue());
3344514f5e3Sopenharmony_ci    argv1->SetCallArg(1, optionsObj.GetTaggedValue());
3354514f5e3Sopenharmony_ci    auto prev1 = TestHelper::SetupFrame(thread, argv1);
3364514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR));
3374514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
3384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue());
3394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue());
3404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorCauseValue(JSObject::GetProperty(thread, errorResult, causeKey).GetValue());
3414514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorMsgValue)).ToCString().c_str(),
3424514f5e3Sopenharmony_ci                 "You have an Error!");
3434514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString().c_str(), "Error");
3444514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorCauseValue)).ToCString().c_str(), "error cause");
3454514f5e3Sopenharmony_ci
3464514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeErrorMsg(factory->NewFromASCII("You have a type error!"));
3474514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *argv2 =
3484514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 8); // 8 means 2 call args
3494514f5e3Sopenharmony_ci    argv2->SetFunction(typeError.GetTaggedValue());
3504514f5e3Sopenharmony_ci    argv2->SetThis(JSTaggedValue(*typeError));
3514514f5e3Sopenharmony_ci    argv2->SetCallArg(0, typeErrorMsg.GetTaggedValue());
3524514f5e3Sopenharmony_ci    argv2->SetCallArg(1, optionsObj.GetTaggedValue());
3534514f5e3Sopenharmony_ci    auto prev2 = TestHelper::SetupFrame(thread, argv2);
3544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::TYPE_ERROR));
3554514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
3564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue());
3574514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue());
3584514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeCauseValue(JSObject::GetProperty(thread, typeErrorResult, causeKey).GetValue());
3594514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeMsgValue)).ToCString().c_str(),
3604514f5e3Sopenharmony_ci                 "You have a type error!");
3614514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString().c_str(), "TypeError");
3624514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeCauseValue)).ToCString().c_str(), "error cause");
3634514f5e3Sopenharmony_ci}
3644514f5e3Sopenharmony_ci}  // namespace panda::test
365