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