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/base/error_helper.h"
17#include "ecmascript/global_env.h"
18#include "ecmascript/tests/test_helper.h"
19
20using namespace panda::ecmascript;
21using namespace panda::ecmascript::base;
22
23namespace panda::test {
24class ErrorHelperTest : public BaseTestWithScope<false> {
25};
26
27HWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_001)
28{
29    auto factory = instance->GetFactory();
30    auto env = instance->GetGlobalEnv();
31    JSHandle<JSTaggedValue> errorFunc = env->GetErrorFunction();
32    JSHandle<JSTaggedValue> evalErrorFunc = env->GetEvalErrorFunction();
33    JSHandle<JSTaggedValue> typeErrorFunc = env->GetTypeErrorFunction();
34    JSHandle<JSTaggedValue> rangeErrorFunc = env->GetRangeErrorFunction();
35    JSHandle<JSObject> errorObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(errorFunc), errorFunc);
36    JSHandle<JSObject> evalErrorObj =
37        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(evalErrorFunc), evalErrorFunc);
38    JSHandle<JSObject> typeErrorObj =
39        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(typeErrorFunc), typeErrorFunc);
40    JSHandle<JSObject> rangeErrorObj =
41        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(rangeErrorFunc), rangeErrorFunc);
42
43    EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
44    argv->SetFunction(JSTaggedValue::Undefined());
45    argv->SetThis(JSTaggedValue(*errorObj));
46    auto prev = TestHelper::SetupFrame(thread, argv);
47    JSHandle<JSTaggedValue> error(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::ERROR));
48    TestHelper::TearDownFrame(thread, prev);
49
50    argv->SetThis(JSTaggedValue(*evalErrorObj));
51    prev = TestHelper::SetupFrame(thread, argv);
52    JSHandle<JSTaggedValue> evalError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::EVAL_ERROR));
53    TestHelper::TearDownFrame(thread, prev);
54
55    argv->SetThis(JSTaggedValue(*typeErrorObj));
56    prev = TestHelper::SetupFrame(thread, argv);
57    JSHandle<JSTaggedValue> typeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::TYPE_ERROR));
58    TestHelper::TearDownFrame(thread, prev);
59
60    argv->SetThis(JSTaggedValue(*rangeErrorObj));
61    prev = TestHelper::SetupFrame(thread, argv);
62    JSHandle<JSTaggedValue> rangeError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::RANGE_ERROR));
63    TestHelper::TearDownFrame(thread, prev);
64
65    EcmaStringAccessor errorStrAcc(JSHandle<EcmaString>::Cast(error));
66    EcmaStringAccessor evalErrorStrAcc(JSHandle<EcmaString>::Cast(evalError));
67    EcmaStringAccessor typeErrorStrAcc(JSHandle<EcmaString>::Cast(typeError));
68    EcmaStringAccessor rangeErrorStrAcc(JSHandle<EcmaString>::Cast(rangeError));
69    EXPECT_STREQ(errorStrAcc.ToCString().c_str(), "Error");
70    EXPECT_STREQ(evalErrorStrAcc.ToCString().c_str(), "EvalError");
71    EXPECT_STREQ(typeErrorStrAcc.ToCString().c_str(), "TypeError");
72    EXPECT_STREQ(rangeErrorStrAcc.ToCString().c_str(), "RangeError");
73}
74
75HWTEST_F_L0(ErrorHelperTest, ErrorCommonToString_002)
76{
77    auto factory = instance->GetFactory();
78    auto env = instance->GetGlobalEnv();
79    JSHandle<JSTaggedValue> uriErrorFunc = env->GetURIErrorFunction();
80    JSHandle<JSTaggedValue> oomErrorFunc = env->GetOOMErrorFunction();
81    JSHandle<JSTaggedValue> syntaxErrorFunc = env->GetSyntaxErrorFunction();
82    JSHandle<JSTaggedValue> referenceErrorFunc = env->GetReferenceErrorFunction();
83    JSHandle<JSTaggedValue> aggregateErrorFunc = env->GetAggregateErrorFunction();
84    JSHandle<JSTaggedValue> terminationErrorFunc = env->GetTerminationErrorFunction();
85    JSHandle<JSObject> uriErrorObj =
86        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(uriErrorFunc), uriErrorFunc);
87    JSHandle<JSObject> oomErrorObj =
88        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(oomErrorFunc), oomErrorFunc);
89    JSHandle<JSObject> syntaxErrorObj =
90        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(syntaxErrorFunc), syntaxErrorFunc);
91    JSHandle<JSObject> referenceErrorObj =
92        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(referenceErrorFunc), referenceErrorFunc);
93    JSHandle<JSObject> aggregateErrorObj =
94        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(aggregateErrorFunc), aggregateErrorFunc);
95    JSHandle<JSObject> terminationErrorObj =
96        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(terminationErrorFunc), terminationErrorFunc);
97
98    EcmaRuntimeCallInfo* argv = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
99    argv->SetFunction(JSTaggedValue::Undefined());
100    argv->SetThis(JSTaggedValue(*uriErrorObj));
101    auto prev = TestHelper::SetupFrame(thread, argv);
102    JSHandle<JSTaggedValue> uriError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::URI_ERROR));
103    TestHelper::TearDownFrame(thread, prev);
104
105    argv->SetThis(JSTaggedValue(*oomErrorObj));
106    prev = TestHelper::SetupFrame(thread, argv);
107    JSHandle<JSTaggedValue> oomError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::OOM_ERROR));
108    TestHelper::TearDownFrame(thread, prev);
109
110    argv->SetThis(JSTaggedValue(*terminationErrorObj));
111    prev = TestHelper::SetupFrame(thread, argv);
112    JSHandle<JSTaggedValue> terminationError(thread,
113        ErrorHelper::ErrorCommonToString(argv, ErrorType::TERMINATION_ERROR));
114    TestHelper::TearDownFrame(thread, prev);
115
116    argv->SetThis(JSTaggedValue(*syntaxErrorObj));
117    prev = TestHelper::SetupFrame(thread, argv);
118    JSHandle<JSTaggedValue> syntaxError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::SYNTAX_ERROR));
119    TestHelper::TearDownFrame(thread, prev);
120
121    argv->SetThis(JSTaggedValue(*referenceErrorObj));
122    prev = TestHelper::SetupFrame(thread, argv);
123    JSHandle<JSTaggedValue> referenceError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::REFERENCE_ERROR));
124    TestHelper::TearDownFrame(thread, prev);
125
126    argv->SetThis(JSTaggedValue(*aggregateErrorObj));
127    prev = TestHelper::SetupFrame(thread, argv);
128    JSHandle<JSTaggedValue> aggregateError(thread, ErrorHelper::ErrorCommonToString(argv, ErrorType::AGGREGATE_ERROR));
129    TestHelper::TearDownFrame(thread, prev);
130
131    EcmaStringAccessor uriErrorStrAcc(JSHandle<EcmaString>::Cast(uriError));
132    EcmaStringAccessor oomErrorStrAcc(JSHandle<EcmaString>::Cast(oomError));
133    EcmaStringAccessor syntaxErrorStrAcc(JSHandle<EcmaString>::Cast(syntaxError));
134    EcmaStringAccessor referenceErrorStrAcc(JSHandle<EcmaString>::Cast(referenceError));
135    EcmaStringAccessor aggregateErrorStrAcc(JSHandle<EcmaString>::Cast(aggregateError));
136    EXPECT_STREQ(uriErrorStrAcc.ToCString().c_str(), "URIError");
137    EXPECT_STREQ(oomErrorStrAcc.ToCString().c_str(), "OutOfMemoryError");
138    EXPECT_STREQ(syntaxErrorStrAcc.ToCString().c_str(), "SyntaxError");
139    EXPECT_STREQ(referenceErrorStrAcc.ToCString().c_str(), "ReferenceError");
140    EXPECT_STREQ(aggregateErrorStrAcc.ToCString().c_str(), "AggregateError");
141}
142
143HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_001)
144{
145    auto factory = instance->GetFactory();
146    auto env = instance->GetGlobalEnv();
147    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
148    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
149
150    JSHandle<JSFunction> error(env->GetErrorFunction());
151    JSHandle<JSFunction> evalError(env->GetEvalErrorFunction());
152    JSHandle<JSFunction> typeError(env->GetTypeErrorFunction());
153
154    JSHandle<JSTaggedValue> errorMsg(factory->NewFromASCII("You have an Error!"));
155    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 6);
156    argv1->SetFunction(error.GetTaggedValue());
157    argv1->SetThis(JSTaggedValue(*error));
158    argv1->SetCallArg(0, errorMsg.GetTaggedValue());
159    auto prev1 = TestHelper::SetupFrame(thread, argv1);
160    JSHandle<JSTaggedValue> errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR));
161    TestHelper::TearDownFrame(thread, prev1);
162    JSHandle<JSTaggedValue> errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue());
163    JSHandle<JSTaggedValue> errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue());
164    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorMsgValue)).ToCString().c_str(),
165                 "You have an Error!");
166    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString().c_str(), "Error");
167
168    JSHandle<JSTaggedValue> evalErrorMsg(factory->NewFromASCII("You have an eval error!"));
169    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*evalError), 6);
170    argv2->SetFunction(evalError.GetTaggedValue());
171    argv2->SetThis(JSTaggedValue(*evalError));
172    argv2->SetCallArg(0, evalErrorMsg.GetTaggedValue());
173    auto prev2 = TestHelper::SetupFrame(thread, argv2);
174    JSHandle<JSTaggedValue> evalErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::EVAL_ERROR));
175    TestHelper::TearDownFrame(thread, prev2);
176    JSHandle<JSTaggedValue> evalMsgValue(JSObject::GetProperty(thread, evalErrorResult, msgKey).GetValue());
177    JSHandle<JSTaggedValue> evalNameValue(JSObject::GetProperty(thread, evalErrorResult, nameKey).GetValue());
178    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(evalMsgValue)).ToCString().c_str(),
179                 "You have an eval error!");
180    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(evalNameValue)).ToCString().c_str(), "EvalError");
181
182    JSHandle<JSTaggedValue> typeErrorMsg(factory->NewFromASCII("You have a type error!"));
183    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 6);
184    argv3->SetFunction(typeError.GetTaggedValue());
185    argv3->SetThis(JSTaggedValue(*typeError));
186    argv3->SetCallArg(0, typeErrorMsg.GetTaggedValue());
187    auto prev3 = TestHelper::SetupFrame(thread, argv3);
188    JSHandle<JSTaggedValue> typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::TYPE_ERROR));
189    TestHelper::TearDownFrame(thread, prev3);
190    JSHandle<JSTaggedValue> typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue());
191    JSHandle<JSTaggedValue> typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue());
192    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeMsgValue)).ToCString().c_str(),
193                 "You have a type error!");
194    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString().c_str(), "TypeError");
195}
196
197HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_002)
198{
199    auto factory = instance->GetFactory();
200    auto env = instance->GetGlobalEnv();
201    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
202    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
203
204    JSHandle<JSFunction> rangeError(env->GetRangeErrorFunction());
205    JSHandle<JSFunction> uriError(env->GetURIErrorFunction());
206    JSHandle<JSFunction> oomError(env->GetOOMErrorFunction());
207
208    JSHandle<JSTaggedValue> rangeErrorMsg(factory->NewFromASCII("You have an range error!"));
209    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*rangeError), 6);
210    argv1->SetFunction(rangeError.GetTaggedValue());
211    argv1->SetThis(JSTaggedValue(*rangeError));
212    argv1->SetCallArg(0, rangeErrorMsg.GetTaggedValue());
213    auto prev1 = TestHelper::SetupFrame(thread, argv1);
214    JSHandle<JSTaggedValue> rangeErrorResult(thread,
215                                             ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::RANGE_ERROR));
216    TestHelper::TearDownFrame(thread, prev1);
217    JSHandle<JSTaggedValue> rangeMsgValue(JSObject::GetProperty(thread, rangeErrorResult, msgKey).GetValue());
218    JSHandle<JSTaggedValue> rangeNameValue(JSObject::GetProperty(thread, rangeErrorResult, nameKey).GetValue());
219    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(rangeMsgValue)).ToCString().c_str(),
220                 "You have an range error!");
221    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(rangeNameValue)).ToCString().c_str(), "RangeError");
222
223    JSHandle<JSTaggedValue> uriErrorMsg(factory->NewFromASCII("You have an uri error!"));
224    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*uriError), 6);
225    argv2->SetFunction(uriError.GetTaggedValue());
226    argv2->SetThis(JSTaggedValue(*uriError));
227    argv2->SetCallArg(0, uriErrorMsg.GetTaggedValue());
228    auto prev2 = TestHelper::SetupFrame(thread, argv2);
229    JSHandle<JSTaggedValue> uriErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::URI_ERROR));
230    TestHelper::TearDownFrame(thread, prev2);
231    JSHandle<JSTaggedValue> uriMsgValue(JSObject::GetProperty(thread, uriErrorResult, msgKey).GetValue());
232    JSHandle<JSTaggedValue> uriNameValue(JSObject::GetProperty(thread, uriErrorResult, nameKey).GetValue());
233    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriMsgValue)).ToCString().c_str(),
234                 "You have an uri error!");
235    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriNameValue)).ToCString().c_str(), "URIError");
236
237    JSHandle<JSTaggedValue> oomErrorMsg(factory->NewFromASCII("You have an out of memory error!"));
238    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*oomError), 6);
239    argv3->SetFunction(oomError.GetTaggedValue());
240    argv3->SetThis(JSTaggedValue(*oomError));
241    argv3->SetCallArg(0, oomErrorMsg.GetTaggedValue());
242    auto prev3 = TestHelper::SetupFrame(thread, argv3);
243    JSHandle<JSTaggedValue> oomErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::OOM_ERROR));
244    TestHelper::TearDownFrame(thread, prev3);
245    JSHandle<JSTaggedValue> oomMsgValue(JSObject::GetProperty(thread, oomErrorResult, msgKey).GetValue());
246    JSHandle<JSTaggedValue> oomNameValue(JSObject::GetProperty(thread, oomErrorResult, nameKey).GetValue());
247    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomMsgValue)).ToCString().c_str(),
248                 "You have an out of memory error!");
249    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomNameValue)).ToCString().c_str(), "OutOfMemoryError");
250}
251
252HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_003)
253{
254    auto factory = instance->GetFactory();
255    auto env = instance->GetGlobalEnv();
256    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
257    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
258
259    JSHandle<JSFunction> syntaxError(env->GetSyntaxErrorFunction());
260    JSHandle<JSFunction> referenceError(env->GetReferenceErrorFunction());
261    JSHandle<JSFunction> aggregateError(env->GetAggregateErrorFunction());
262
263    JSHandle<JSTaggedValue> syntaxErrorMsg(factory->NewFromASCII("You have an syntax error!"));
264    EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*syntaxError), 6);
265    argv1->SetFunction(syntaxError.GetTaggedValue());
266    argv1->SetThis(JSTaggedValue(*syntaxError));
267    argv1->SetCallArg(0, syntaxErrorMsg.GetTaggedValue());
268    auto prev1 = TestHelper::SetupFrame(thread, argv1);
269    JSHandle<JSTaggedValue> syntaxErrorResult(thread,
270                                              ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::SYNTAX_ERROR));
271    TestHelper::TearDownFrame(thread, prev1);
272    JSHandle<JSTaggedValue> syntaxMsgValue(JSObject::GetProperty(thread, syntaxErrorResult, msgKey).GetValue());
273    JSHandle<JSTaggedValue> syntaxNameValue(JSObject::GetProperty(thread, syntaxErrorResult, nameKey).GetValue());
274    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxMsgValue)).ToCString().c_str(),
275                 "You have an syntax error!");
276    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxNameValue)).ToCString().c_str(), "SyntaxError");
277
278    JSHandle<JSTaggedValue> referenceErrorMsg(factory->NewFromASCII("You have an reference error!"));
279    EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*referenceError), 6);
280    argv2->SetFunction(referenceError.GetTaggedValue());
281    argv2->SetThis(JSTaggedValue(*referenceError));
282    argv2->SetCallArg(0, referenceErrorMsg.GetTaggedValue());
283    auto prev2 = TestHelper::SetupFrame(thread, argv2);
284    JSHandle<JSTaggedValue> referenceErrorResult(thread,
285        ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::REFERENCE_ERROR));
286    TestHelper::TearDownFrame(thread, prev2);
287    JSHandle<JSTaggedValue> referenceMsgValue(JSObject::GetProperty(thread, referenceErrorResult, msgKey).GetValue());
288    JSHandle<JSTaggedValue> referenceNameValue(
289        JSObject::GetProperty(thread, referenceErrorResult, nameKey).GetValue());
290    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceMsgValue)).ToCString().c_str(),
291                 "You have an reference error!");
292    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceNameValue)).ToCString().c_str(),
293                 "ReferenceError");
294
295    JSHandle<JSTaggedValue> aggregateErrorMsg(factory->NewFromASCII("You have an aggregate error!"));
296    EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*aggregateError), 6);
297    argv3->SetFunction(aggregateError.GetTaggedValue());
298    argv3->SetThis(JSTaggedValue(*aggregateError));
299    argv3->SetCallArg(0, aggregateErrorMsg.GetTaggedValue());
300    auto prev3 = TestHelper::SetupFrame(thread, argv3);
301    JSHandle<JSTaggedValue> aggregateErrorResult(thread,
302        ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::AGGREGATE_ERROR));
303    TestHelper::TearDownFrame(thread, prev3);
304    JSHandle<JSTaggedValue> aggregateMsgValue(JSObject::GetProperty(thread, aggregateErrorResult, msgKey).GetValue());
305    JSHandle<JSTaggedValue> aggregateNameValue(
306        JSObject::GetProperty(thread, aggregateErrorResult, nameKey).GetValue());
307    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateMsgValue)).ToCString().c_str(),
308                 "You have an aggregate error!");
309    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateNameValue)).ToCString().c_str(),
310                 "AggregateError");
311}
312
313HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_004)
314{
315    auto factory = instance->GetFactory();
316    auto env = instance->GetGlobalEnv();
317    JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
318    JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
319    JSHandle<JSTaggedValue> causeKey = thread->GlobalConstants()->GetHandledCauseString();
320
321    JSHandle<JSFunction> error(env->GetErrorFunction());
322    JSHandle<JSFunction> typeError(env->GetTypeErrorFunction());
323    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
324    JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
325    JSHandle<JSTaggedValue> causeValue(factory->NewFromASCII("error cause")); // test error cause
326    JSObject::SetProperty(thread, optionsObj, causeKey, causeValue);
327
328    JSHandle<JSTaggedValue> errorMsg(factory->NewFromASCII("You have an Error!"));
329    EcmaRuntimeCallInfo *argv1 =
330        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 8); // 8 means 2 call args
331    argv1->SetFunction(error.GetTaggedValue());
332    argv1->SetThis(JSTaggedValue(*error));
333    argv1->SetCallArg(0, errorMsg.GetTaggedValue());
334    argv1->SetCallArg(1, optionsObj.GetTaggedValue());
335    auto prev1 = TestHelper::SetupFrame(thread, argv1);
336    JSHandle<JSTaggedValue> errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR));
337    TestHelper::TearDownFrame(thread, prev1);
338    JSHandle<JSTaggedValue> errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue());
339    JSHandle<JSTaggedValue> errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue());
340    JSHandle<JSTaggedValue> errorCauseValue(JSObject::GetProperty(thread, errorResult, causeKey).GetValue());
341    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorMsgValue)).ToCString().c_str(),
342                 "You have an Error!");
343    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString().c_str(), "Error");
344    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorCauseValue)).ToCString().c_str(), "error cause");
345
346    JSHandle<JSTaggedValue> typeErrorMsg(factory->NewFromASCII("You have a type error!"));
347    EcmaRuntimeCallInfo *argv2 =
348        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 8); // 8 means 2 call args
349    argv2->SetFunction(typeError.GetTaggedValue());
350    argv2->SetThis(JSTaggedValue(*typeError));
351    argv2->SetCallArg(0, typeErrorMsg.GetTaggedValue());
352    argv2->SetCallArg(1, optionsObj.GetTaggedValue());
353    auto prev2 = TestHelper::SetupFrame(thread, argv2);
354    JSHandle<JSTaggedValue> typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::TYPE_ERROR));
355    TestHelper::TearDownFrame(thread, prev2);
356    JSHandle<JSTaggedValue> typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue());
357    JSHandle<JSTaggedValue> typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue());
358    JSHandle<JSTaggedValue> typeCauseValue(JSObject::GetProperty(thread, typeErrorResult, causeKey).GetValue());
359    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeMsgValue)).ToCString().c_str(),
360                 "You have a type error!");
361    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString().c_str(), "TypeError");
362    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeCauseValue)).ToCString().c_str(), "error cause");
363}
364}  // namespace panda::test
365