/* * 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/base/error_helper.h" #include "ecmascript/global_env.h" #include "ecmascript/tests/test_helper.h" using namespace panda::ecmascript; using namespace panda::ecmascript::base; namespace panda::test { class ErrorHelperTest : public BaseTestWithScope { }; HWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_001) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle errorFunc = env->GetErrorFunction(); JSHandle evalErrorFunc = env->GetEvalErrorFunction(); JSHandle typeErrorFunc = env->GetTypeErrorFunction(); JSHandle rangeErrorFunc = env->GetRangeErrorFunction(); JSHandle errorObj = factory->NewJSObjectByConstructor(JSHandle(errorFunc), errorFunc); JSHandle evalErrorObj = factory->NewJSObjectByConstructor(JSHandle(evalErrorFunc), evalErrorFunc); JSHandle typeErrorObj = factory->NewJSObjectByConstructor(JSHandle(typeErrorFunc), typeErrorFunc); JSHandle rangeErrorObj = factory->NewJSObjectByConstructor(JSHandle(rangeErrorFunc), rangeErrorFunc); EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); argv->SetFunction(JSTaggedValue::Undefined()); argv->SetThis(JSTaggedValue(*errorObj)); auto prev = TestHelper::SetupFrame(thread, argv); JSHandle error(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*evalErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle evalError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::EVAL_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*typeErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle typeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::TYPE_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*rangeErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle rangeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::RANGE_ERROR)); TestHelper::TearDownFrame(thread, prev); EcmaStringAccessor errorStrAcc(JSHandle::Cast(error)); EcmaStringAccessor evalErrorStrAcc(JSHandle::Cast(evalError)); EcmaStringAccessor typeErrorStrAcc(JSHandle::Cast(typeError)); EcmaStringAccessor rangeErrorStrAcc(JSHandle::Cast(rangeError)); EXPECT_STREQ(errorStrAcc.ToCString().c_str(), "Error"); EXPECT_STREQ(evalErrorStrAcc.ToCString().c_str(), "EvalError"); EXPECT_STREQ(typeErrorStrAcc.ToCString().c_str(), "TypeError"); EXPECT_STREQ(rangeErrorStrAcc.ToCString().c_str(), "RangeError"); } HWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_002) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle uriErrorFunc = env->GetURIErrorFunction(); JSHandle oomErrorFunc = env->GetOOMErrorFunction(); JSHandle syntaxErrorFunc = env->GetSyntaxErrorFunction(); JSHandle referenceErrorFunc = env->GetReferenceErrorFunction(); JSHandle aggregateErrorFunc = env->GetAggregateErrorFunction(); JSHandle terminationErrorFunc = env->GetTerminationErrorFunction(); JSHandle uriErrorObj = factory->NewJSObjectByConstructor(JSHandle(uriErrorFunc), uriErrorFunc); JSHandle oomErrorObj = factory->NewJSObjectByConstructor(JSHandle(oomErrorFunc), oomErrorFunc); JSHandle syntaxErrorObj = factory->NewJSObjectByConstructor(JSHandle(syntaxErrorFunc), syntaxErrorFunc); JSHandle referenceErrorObj = factory->NewJSObjectByConstructor(JSHandle(referenceErrorFunc), referenceErrorFunc); JSHandle aggregateErrorObj = factory->NewJSObjectByConstructor(JSHandle(aggregateErrorFunc), aggregateErrorFunc); JSHandle terminationErrorObj = factory->NewJSObjectByConstructor(JSHandle(terminationErrorFunc), terminationErrorFunc); EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); argv->SetFunction(JSTaggedValue::Undefined()); argv->SetThis(JSTaggedValue(*uriErrorObj)); auto prev = TestHelper::SetupFrame(thread, argv); JSHandle uriError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::URI_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*oomErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle oomError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::OOM_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*terminationErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle terminationError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::TERMINATION_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*syntaxErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle syntaxError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::SYNTAX_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*referenceErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle referenceError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::REFERENCE_ERROR)); TestHelper::TearDownFrame(thread, prev); argv->SetThis(JSTaggedValue(*aggregateErrorObj)); prev = TestHelper::SetupFrame(thread, argv); JSHandle aggregateError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::AGGREGATE_ERROR)); TestHelper::TearDownFrame(thread, prev); EcmaStringAccessor uriErrorStrAcc(JSHandle::Cast(uriError)); EcmaStringAccessor oomErrorStrAcc(JSHandle::Cast(oomError)); EcmaStringAccessor syntaxErrorStrAcc(JSHandle::Cast(syntaxError)); EcmaStringAccessor referenceErrorStrAcc(JSHandle::Cast(referenceError)); EcmaStringAccessor aggregateErrorStrAcc(JSHandle::Cast(aggregateError)); EXPECT_STREQ(uriErrorStrAcc.ToCString().c_str(), "URIError"); EXPECT_STREQ(oomErrorStrAcc.ToCString().c_str(), "OutOfMemoryError"); EXPECT_STREQ(syntaxErrorStrAcc.ToCString().c_str(), "SyntaxError"); EXPECT_STREQ(referenceErrorStrAcc.ToCString().c_str(), "ReferenceError"); EXPECT_STREQ(aggregateErrorStrAcc.ToCString().c_str(), "AggregateError"); } HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_001) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle msgKey = thread->GlobalConstants()->GetHandledMessageString(); JSHandle nameKey = thread->GlobalConstants()->GetHandledNameString(); JSHandle error(env->GetErrorFunction()); JSHandle evalError(env->GetEvalErrorFunction()); JSHandle typeError(env->GetTypeErrorFunction()); JSHandle errorMsg(factory->NewFromASCII("You have an Error!")); EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 6); argv1->SetFunction(error.GetTaggedValue()); argv1->SetThis(JSTaggedValue(*error)); argv1->SetCallArg(0, errorMsg.GetTaggedValue()); auto prev1 = TestHelper::SetupFrame(thread, argv1); JSHandle errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR)); TestHelper::TearDownFrame(thread, prev1); JSHandle errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue()); JSHandle errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(errorMsgValue)).ToCString().c_str(), "You have an Error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(errorNameValue)).ToCString().c_str(), "Error"); JSHandle evalErrorMsg(factory->NewFromASCII("You have an eval error!")); EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*evalError), 6); argv2->SetFunction(evalError.GetTaggedValue()); argv2->SetThis(JSTaggedValue(*evalError)); argv2->SetCallArg(0, evalErrorMsg.GetTaggedValue()); auto prev2 = TestHelper::SetupFrame(thread, argv2); JSHandle evalErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::EVAL_ERROR)); TestHelper::TearDownFrame(thread, prev2); JSHandle evalMsgValue(JSObject::GetProperty(thread, evalErrorResult, msgKey).GetValue()); JSHandle evalNameValue(JSObject::GetProperty(thread, evalErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(evalMsgValue)).ToCString().c_str(), "You have an eval error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(evalNameValue)).ToCString().c_str(), "EvalError"); JSHandle typeErrorMsg(factory->NewFromASCII("You have a type error!")); EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 6); argv3->SetFunction(typeError.GetTaggedValue()); argv3->SetThis(JSTaggedValue(*typeError)); argv3->SetCallArg(0, typeErrorMsg.GetTaggedValue()); auto prev3 = TestHelper::SetupFrame(thread, argv3); JSHandle typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::TYPE_ERROR)); TestHelper::TearDownFrame(thread, prev3); JSHandle typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue()); JSHandle typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(typeMsgValue)).ToCString().c_str(), "You have a type error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(typeNameValue)).ToCString().c_str(), "TypeError"); } HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_002) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle msgKey = thread->GlobalConstants()->GetHandledMessageString(); JSHandle nameKey = thread->GlobalConstants()->GetHandledNameString(); JSHandle rangeError(env->GetRangeErrorFunction()); JSHandle uriError(env->GetURIErrorFunction()); JSHandle oomError(env->GetOOMErrorFunction()); JSHandle rangeErrorMsg(factory->NewFromASCII("You have an range error!")); EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*rangeError), 6); argv1->SetFunction(rangeError.GetTaggedValue()); argv1->SetThis(JSTaggedValue(*rangeError)); argv1->SetCallArg(0, rangeErrorMsg.GetTaggedValue()); auto prev1 = TestHelper::SetupFrame(thread, argv1); JSHandle rangeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::RANGE_ERROR)); TestHelper::TearDownFrame(thread, prev1); JSHandle rangeMsgValue(JSObject::GetProperty(thread, rangeErrorResult, msgKey).GetValue()); JSHandle rangeNameValue(JSObject::GetProperty(thread, rangeErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(rangeMsgValue)).ToCString().c_str(), "You have an range error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(rangeNameValue)).ToCString().c_str(), "RangeError"); JSHandle uriErrorMsg(factory->NewFromASCII("You have an uri error!")); EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*uriError), 6); argv2->SetFunction(uriError.GetTaggedValue()); argv2->SetThis(JSTaggedValue(*uriError)); argv2->SetCallArg(0, uriErrorMsg.GetTaggedValue()); auto prev2 = TestHelper::SetupFrame(thread, argv2); JSHandle uriErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::URI_ERROR)); TestHelper::TearDownFrame(thread, prev2); JSHandle uriMsgValue(JSObject::GetProperty(thread, uriErrorResult, msgKey).GetValue()); JSHandle uriNameValue(JSObject::GetProperty(thread, uriErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(uriMsgValue)).ToCString().c_str(), "You have an uri error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(uriNameValue)).ToCString().c_str(), "URIError"); JSHandle oomErrorMsg(factory->NewFromASCII("You have an out of memory error!")); EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*oomError), 6); argv3->SetFunction(oomError.GetTaggedValue()); argv3->SetThis(JSTaggedValue(*oomError)); argv3->SetCallArg(0, oomErrorMsg.GetTaggedValue()); auto prev3 = TestHelper::SetupFrame(thread, argv3); JSHandle oomErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::OOM_ERROR)); TestHelper::TearDownFrame(thread, prev3); JSHandle oomMsgValue(JSObject::GetProperty(thread, oomErrorResult, msgKey).GetValue()); JSHandle oomNameValue(JSObject::GetProperty(thread, oomErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(oomMsgValue)).ToCString().c_str(), "You have an out of memory error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(oomNameValue)).ToCString().c_str(), "OutOfMemoryError"); } HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_003) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle msgKey = thread->GlobalConstants()->GetHandledMessageString(); JSHandle nameKey = thread->GlobalConstants()->GetHandledNameString(); JSHandle syntaxError(env->GetSyntaxErrorFunction()); JSHandle referenceError(env->GetReferenceErrorFunction()); JSHandle aggregateError(env->GetAggregateErrorFunction()); JSHandle syntaxErrorMsg(factory->NewFromASCII("You have an syntax error!")); EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*syntaxError), 6); argv1->SetFunction(syntaxError.GetTaggedValue()); argv1->SetThis(JSTaggedValue(*syntaxError)); argv1->SetCallArg(0, syntaxErrorMsg.GetTaggedValue()); auto prev1 = TestHelper::SetupFrame(thread, argv1); JSHandle syntaxErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::SYNTAX_ERROR)); TestHelper::TearDownFrame(thread, prev1); JSHandle syntaxMsgValue(JSObject::GetProperty(thread, syntaxErrorResult, msgKey).GetValue()); JSHandle syntaxNameValue(JSObject::GetProperty(thread, syntaxErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(syntaxMsgValue)).ToCString().c_str(), "You have an syntax error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(syntaxNameValue)).ToCString().c_str(), "SyntaxError"); JSHandle referenceErrorMsg(factory->NewFromASCII("You have an reference error!")); EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*referenceError), 6); argv2->SetFunction(referenceError.GetTaggedValue()); argv2->SetThis(JSTaggedValue(*referenceError)); argv2->SetCallArg(0, referenceErrorMsg.GetTaggedValue()); auto prev2 = TestHelper::SetupFrame(thread, argv2); JSHandle referenceErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::REFERENCE_ERROR)); TestHelper::TearDownFrame(thread, prev2); JSHandle referenceMsgValue(JSObject::GetProperty(thread, referenceErrorResult, msgKey).GetValue()); JSHandle referenceNameValue( JSObject::GetProperty(thread, referenceErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(referenceMsgValue)).ToCString().c_str(), "You have an reference error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(referenceNameValue)).ToCString().c_str(), "ReferenceError"); JSHandle aggregateErrorMsg(factory->NewFromASCII("You have an aggregate error!")); EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*aggregateError), 6); argv3->SetFunction(aggregateError.GetTaggedValue()); argv3->SetThis(JSTaggedValue(*aggregateError)); argv3->SetCallArg(0, aggregateErrorMsg.GetTaggedValue()); auto prev3 = TestHelper::SetupFrame(thread, argv3); JSHandle aggregateErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::AGGREGATE_ERROR)); TestHelper::TearDownFrame(thread, prev3); JSHandle aggregateMsgValue(JSObject::GetProperty(thread, aggregateErrorResult, msgKey).GetValue()); JSHandle aggregateNameValue( JSObject::GetProperty(thread, aggregateErrorResult, nameKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(aggregateMsgValue)).ToCString().c_str(), "You have an aggregate error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(aggregateNameValue)).ToCString().c_str(), "AggregateError"); } HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_004) { auto factory = instance->GetFactory(); auto env = instance->GetGlobalEnv(); JSHandle msgKey = thread->GlobalConstants()->GetHandledMessageString(); JSHandle nameKey = thread->GlobalConstants()->GetHandledNameString(); JSHandle causeKey = thread->GlobalConstants()->GetHandledCauseString(); JSHandle error(env->GetErrorFunction()); JSHandle typeError(env->GetTypeErrorFunction()); JSHandle objFun = env->GetObjectFunction(); JSHandle optionsObj = factory->NewJSObjectByConstructor(JSHandle(objFun), objFun); JSHandle causeValue(factory->NewFromASCII("error cause")); // test error cause JSObject::SetProperty(thread, optionsObj, causeKey, causeValue); JSHandle errorMsg(factory->NewFromASCII("You have an Error!")); EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 8); // 8 means 2 call args argv1->SetFunction(error.GetTaggedValue()); argv1->SetThis(JSTaggedValue(*error)); argv1->SetCallArg(0, errorMsg.GetTaggedValue()); argv1->SetCallArg(1, optionsObj.GetTaggedValue()); auto prev1 = TestHelper::SetupFrame(thread, argv1); JSHandle errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR)); TestHelper::TearDownFrame(thread, prev1); JSHandle errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue()); JSHandle errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue()); JSHandle errorCauseValue(JSObject::GetProperty(thread, errorResult, causeKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(errorMsgValue)).ToCString().c_str(), "You have an Error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(errorNameValue)).ToCString().c_str(), "Error"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(errorCauseValue)).ToCString().c_str(), "error cause"); JSHandle typeErrorMsg(factory->NewFromASCII("You have a type error!")); EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 8); // 8 means 2 call args argv2->SetFunction(typeError.GetTaggedValue()); argv2->SetThis(JSTaggedValue(*typeError)); argv2->SetCallArg(0, typeErrorMsg.GetTaggedValue()); argv2->SetCallArg(1, optionsObj.GetTaggedValue()); auto prev2 = TestHelper::SetupFrame(thread, argv2); JSHandle typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::TYPE_ERROR)); TestHelper::TearDownFrame(thread, prev2); JSHandle typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue()); JSHandle typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue()); JSHandle typeCauseValue(JSObject::GetProperty(thread, typeErrorResult, causeKey).GetValue()); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(typeMsgValue)).ToCString().c_str(), "You have a type error!"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(typeNameValue)).ToCString().c_str(), "TypeError"); EXPECT_STREQ(EcmaStringAccessor(JSHandle::Cast(typeCauseValue)).ToCString().c_str(), "error cause"); } } // namespace panda::test