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
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::base;
22
23 namespace panda::test {
24 class ErrorHelperTest : public BaseTestWithScope<false> {
25 };
26
HWTEST_F_L0(ErrorHelperTest,ErrorCommonToString_001)27 HWTEST_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(thread).c_str(), "Error");
70 EXPECT_STREQ(evalErrorStrAcc.ToCString(thread).c_str(), "EvalError");
71 EXPECT_STREQ(typeErrorStrAcc.ToCString(thread).c_str(), "TypeError");
72 EXPECT_STREQ(rangeErrorStrAcc.ToCString(thread).c_str(), "RangeError");
73 }
74
HWTEST_F_L0(ErrorHelperTest,ErrorCommonToString_002)75 HWTEST_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(thread).c_str(), "URIError");
137 EXPECT_STREQ(oomErrorStrAcc.ToCString(thread).c_str(), "OutOfMemoryError");
138 EXPECT_STREQ(syntaxErrorStrAcc.ToCString(thread).c_str(), "SyntaxError");
139 EXPECT_STREQ(referenceErrorStrAcc.ToCString(thread).c_str(), "ReferenceError");
140 EXPECT_STREQ(aggregateErrorStrAcc.ToCString(thread).c_str(), "AggregateError");
141 }
142
HWTEST_F_L0(ErrorHelperTest,ErrorCommonConstructor_001)143 HWTEST_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(thread).c_str(),
165 "You have an Error!");
166 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString(thread).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(thread).c_str(),
179 "You have an eval error!");
180 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(evalNameValue)).ToCString(thread).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(thread).c_str(),
193 "You have a type error!");
194 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString(thread).c_str(), "TypeError");
195 }
196
HWTEST_F_L0(ErrorHelperTest,ErrorCommonConstructor_002)197 HWTEST_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(thread).c_str(),
220 "You have an range error!");
221 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(rangeNameValue)).ToCString(thread).c_str(),
222 "RangeError");
223
224 JSHandle<JSTaggedValue> uriErrorMsg(factory->NewFromASCII("You have an uri error!"));
225 EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*uriError), 6);
226 argv2->SetFunction(uriError.GetTaggedValue());
227 argv2->SetThis(JSTaggedValue(*uriError));
228 argv2->SetCallArg(0, uriErrorMsg.GetTaggedValue());
229 auto prev2 = TestHelper::SetupFrame(thread, argv2);
230 JSHandle<JSTaggedValue> uriErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::URI_ERROR));
231 TestHelper::TearDownFrame(thread, prev2);
232 JSHandle<JSTaggedValue> uriMsgValue(JSObject::GetProperty(thread, uriErrorResult, msgKey).GetValue());
233 JSHandle<JSTaggedValue> uriNameValue(JSObject::GetProperty(thread, uriErrorResult, nameKey).GetValue());
234 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriMsgValue)).ToCString(thread).c_str(),
235 "You have an uri error!");
236 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(uriNameValue)).ToCString(thread).c_str(), "URIError");
237
238 JSHandle<JSTaggedValue> oomErrorMsg(factory->NewFromASCII("You have an out of memory error!"));
239 EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*oomError), 6);
240 argv3->SetFunction(oomError.GetTaggedValue());
241 argv3->SetThis(JSTaggedValue(*oomError));
242 argv3->SetCallArg(0, oomErrorMsg.GetTaggedValue());
243 auto prev3 = TestHelper::SetupFrame(thread, argv3);
244 JSHandle<JSTaggedValue> oomErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::OOM_ERROR));
245 TestHelper::TearDownFrame(thread, prev3);
246 JSHandle<JSTaggedValue> oomMsgValue(JSObject::GetProperty(thread, oomErrorResult, msgKey).GetValue());
247 JSHandle<JSTaggedValue> oomNameValue(JSObject::GetProperty(thread, oomErrorResult, nameKey).GetValue());
248 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomMsgValue)).ToCString(thread).c_str(),
249 "You have an out of memory error!");
250 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(oomNameValue)).ToCString(thread).c_str(),
251 "OutOfMemoryError");
252 }
253
HWTEST_F_L0(ErrorHelperTest,ErrorCommonConstructor_003)254 HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_003)
255 {
256 auto factory = instance->GetFactory();
257 auto env = instance->GetGlobalEnv();
258 JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
259 JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
260
261 JSHandle<JSFunction> syntaxError(env->GetSyntaxErrorFunction());
262 JSHandle<JSFunction> referenceError(env->GetReferenceErrorFunction());
263 JSHandle<JSFunction> aggregateError(env->GetAggregateErrorFunction());
264
265 JSHandle<JSTaggedValue> syntaxErrorMsg(factory->NewFromASCII("You have an syntax error!"));
266 EcmaRuntimeCallInfo *argv1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*syntaxError), 6);
267 argv1->SetFunction(syntaxError.GetTaggedValue());
268 argv1->SetThis(JSTaggedValue(*syntaxError));
269 argv1->SetCallArg(0, syntaxErrorMsg.GetTaggedValue());
270 auto prev1 = TestHelper::SetupFrame(thread, argv1);
271 JSHandle<JSTaggedValue> syntaxErrorResult(thread,
272 ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::SYNTAX_ERROR));
273 TestHelper::TearDownFrame(thread, prev1);
274 JSHandle<JSTaggedValue> syntaxMsgValue(JSObject::GetProperty(thread, syntaxErrorResult, msgKey).GetValue());
275 JSHandle<JSTaggedValue> syntaxNameValue(JSObject::GetProperty(thread, syntaxErrorResult, nameKey).GetValue());
276 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxMsgValue)).ToCString(thread).c_str(),
277 "You have an syntax error!");
278 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(syntaxNameValue)).ToCString(thread).c_str(),
279 "SyntaxError");
280
281 JSHandle<JSTaggedValue> referenceErrorMsg(factory->NewFromASCII("You have an reference error!"));
282 EcmaRuntimeCallInfo *argv2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*referenceError), 6);
283 argv2->SetFunction(referenceError.GetTaggedValue());
284 argv2->SetThis(JSTaggedValue(*referenceError));
285 argv2->SetCallArg(0, referenceErrorMsg.GetTaggedValue());
286 auto prev2 = TestHelper::SetupFrame(thread, argv2);
287 JSHandle<JSTaggedValue> referenceErrorResult(thread,
288 ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::REFERENCE_ERROR));
289 TestHelper::TearDownFrame(thread, prev2);
290 JSHandle<JSTaggedValue> referenceMsgValue(JSObject::GetProperty(thread, referenceErrorResult, msgKey).GetValue());
291 JSHandle<JSTaggedValue> referenceNameValue(
292 JSObject::GetProperty(thread, referenceErrorResult, nameKey).GetValue());
293 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceMsgValue)).ToCString(thread).c_str(),
294 "You have an reference error!");
295 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(referenceNameValue)).ToCString(thread).c_str(),
296 "ReferenceError");
297
298 JSHandle<JSTaggedValue> aggregateErrorMsg(factory->NewFromASCII("You have an aggregate error!"));
299 EcmaRuntimeCallInfo *argv3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*aggregateError), 6);
300 argv3->SetFunction(aggregateError.GetTaggedValue());
301 argv3->SetThis(JSTaggedValue(*aggregateError));
302 argv3->SetCallArg(0, aggregateErrorMsg.GetTaggedValue());
303 auto prev3 = TestHelper::SetupFrame(thread, argv3);
304 JSHandle<JSTaggedValue> aggregateErrorResult(thread,
305 ErrorHelper::ErrorCommonConstructor(argv3, ErrorType::AGGREGATE_ERROR));
306 TestHelper::TearDownFrame(thread, prev3);
307 JSHandle<JSTaggedValue> aggregateMsgValue(JSObject::GetProperty(thread, aggregateErrorResult, msgKey).GetValue());
308 JSHandle<JSTaggedValue> aggregateNameValue(
309 JSObject::GetProperty(thread, aggregateErrorResult, nameKey).GetValue());
310 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateMsgValue)).ToCString(thread).c_str(),
311 "You have an aggregate error!");
312 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(aggregateNameValue)).ToCString(thread).c_str(),
313 "AggregateError");
314 }
315
HWTEST_F_L0(ErrorHelperTest,ErrorCommonConstructor_004)316 HWTEST_F_L0(ErrorHelperTest, ErrorCommonConstructor_004)
317 {
318 auto factory = instance->GetFactory();
319 auto env = instance->GetGlobalEnv();
320 JSHandle<JSTaggedValue> msgKey = thread->GlobalConstants()->GetHandledMessageString();
321 JSHandle<JSTaggedValue> nameKey = thread->GlobalConstants()->GetHandledNameString();
322 JSHandle<JSTaggedValue> causeKey = thread->GlobalConstants()->GetHandledCauseString();
323
324 JSHandle<JSFunction> error(env->GetErrorFunction());
325 JSHandle<JSFunction> typeError(env->GetTypeErrorFunction());
326 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
327 JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
328 JSHandle<JSTaggedValue> causeValue(factory->NewFromASCII("error cause")); // test error cause
329 JSObject::SetProperty(thread, optionsObj, causeKey, causeValue);
330
331 JSHandle<JSTaggedValue> errorMsg(factory->NewFromASCII("You have an Error!"));
332 EcmaRuntimeCallInfo *argv1 =
333 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*error), 8); // 8 means 2 call args
334 argv1->SetFunction(error.GetTaggedValue());
335 argv1->SetThis(JSTaggedValue(*error));
336 argv1->SetCallArg(0, errorMsg.GetTaggedValue());
337 argv1->SetCallArg(1, optionsObj.GetTaggedValue());
338 auto prev1 = TestHelper::SetupFrame(thread, argv1);
339 JSHandle<JSTaggedValue> errorResult(thread, ErrorHelper::ErrorCommonConstructor(argv1, ErrorType::ERROR));
340 TestHelper::TearDownFrame(thread, prev1);
341 JSHandle<JSTaggedValue> errorMsgValue(JSObject::GetProperty(thread, errorResult, msgKey).GetValue());
342 JSHandle<JSTaggedValue> errorNameValue(JSObject::GetProperty(thread, errorResult, nameKey).GetValue());
343 JSHandle<JSTaggedValue> errorCauseValue(JSObject::GetProperty(thread, errorResult, causeKey).GetValue());
344 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorMsgValue)).ToCString(thread).c_str(),
345 "You have an Error!");
346 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorNameValue)).ToCString(thread).c_str(), "Error");
347 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(errorCauseValue)).ToCString(thread).c_str(),
348 "error cause");
349
350 JSHandle<JSTaggedValue> typeErrorMsg(factory->NewFromASCII("You have a type error!"));
351 EcmaRuntimeCallInfo *argv2 =
352 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*typeError), 8); // 8 means 2 call args
353 argv2->SetFunction(typeError.GetTaggedValue());
354 argv2->SetThis(JSTaggedValue(*typeError));
355 argv2->SetCallArg(0, typeErrorMsg.GetTaggedValue());
356 argv2->SetCallArg(1, optionsObj.GetTaggedValue());
357 auto prev2 = TestHelper::SetupFrame(thread, argv2);
358 JSHandle<JSTaggedValue> typeErrorResult(thread, ErrorHelper::ErrorCommonConstructor(argv2, ErrorType::TYPE_ERROR));
359 TestHelper::TearDownFrame(thread, prev2);
360 JSHandle<JSTaggedValue> typeMsgValue(JSObject::GetProperty(thread, typeErrorResult, msgKey).GetValue());
361 JSHandle<JSTaggedValue> typeNameValue(JSObject::GetProperty(thread, typeErrorResult, nameKey).GetValue());
362 JSHandle<JSTaggedValue> typeCauseValue(JSObject::GetProperty(thread, typeErrorResult, causeKey).GetValue());
363 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeMsgValue)).ToCString(thread).c_str(),
364 "You have a type error!");
365 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeNameValue)).ToCString(thread).c_str(), "TypeError");
366 EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(typeCauseValue)).ToCString(thread).c_str(),
367 "error cause");
368 }
369 } // namespace panda::test
370