1 /*
2 * Copyright (c) 2021-2024 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/json_helper.h"
17 #include "ecmascript/base/json_stringifier.h"
18 #include "ecmascript/js_api/js_api_hashmap.h"
19 #include "ecmascript/js_api/js_api_hashset.h"
20 #include "ecmascript/js_array.h"
21 #include "ecmascript/js_map.h"
22 #include "ecmascript/js_set.h"
23 #include "ecmascript/linked_hash_table.h"
24 #include "ecmascript/object_factory.h"
25 #include "ecmascript/tests/test_helper.h"
26 #include "ecmascript/shared_objects/js_shared_map.h"
27 #include "ecmascript/shared_objects/js_shared_set.h"
28 #include "ecmascript/tests/ecma_test_common.h"
29
30 using namespace panda::ecmascript;
31 using namespace panda::ecmascript::base;
32
33 namespace panda::test {
34 class JsonStringifierTest : public BaseTestWithScope<false> {
35 public:
36 using TransformType = base::JsonHelper::TransformType;
37 };
38
CreateBaseJSObject(JSThread * thread,const CString keyCStr)39 static JSTaggedValue CreateBaseJSObject(JSThread *thread, const CString keyCStr)
40 {
41 EcmaVM *ecmaVM = thread->GetEcmaVM();
42 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
43 ObjectFactory *factory = ecmaVM->GetFactory();
44 JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction());
45
46 JSHandle<JSObject> jsObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
47 EXPECT_TRUE(*jsObject != nullptr);
48
49 JSHandle<JSTaggedValue> handleKey1(factory->NewFromASCII(&keyCStr[0]));
50 JSHandle<JSTaggedValue> handleValue1(thread, JSTaggedValue(1)); // 1 : test case
51 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey1, handleValue1);
52
53 CString str2 = "x";
54 JSHandle<JSTaggedValue> handleKey2(factory->NewFromASCII(str2));
55 JSHandle<JSTaggedValue> handleValue2(thread, JSTaggedValue(3.6)); // 3.6 : test case
56 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey2, handleValue2);
57
58 CString str3 = "y";
59 JSHandle<JSTaggedValue> handleKey3(factory->NewFromASCII(str3));
60 JSHandle<JSTaggedValue> handleValue3(factory->NewFromASCII("abc"));
61 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey3, handleValue3);
62
63 return jsObject.GetTaggedValue();
64 }
65
CreateSharedMap(JSThread * thread)66 static JSHandle<JSSharedMap> CreateSharedMap(JSThread *thread)
67 {
68 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
69 JSHandle<JSTaggedValue> proto = globalEnv->GetSharedMapPrototype();
70 auto emptySLayout = thread->GlobalConstants()->GetHandledEmptySLayoutInfo();
71 auto vm = thread->GetEcmaVM();
72 ObjectFactory *factory = vm->GetFactory();
73 JSHandle<JSHClass> mapClass = factory->NewSEcmaHClass(JSSharedMap::SIZE, 0,
74 JSType::JS_SHARED_MAP, proto,
75 emptySLayout);
76 JSHandle<JSObject> obj = factory->NewSharedOldSpaceJSObjectWithInit(mapClass);
77 JSHandle<JSSharedMap> jsMap = JSHandle<JSSharedMap>::Cast(obj);
78 JSHandle<LinkedHashMap> linkedMap(
79 LinkedHashMap::Create(thread, LinkedHashMap::MIN_CAPACITY, MemSpaceKind::SHARED));
80 jsMap->SetLinkedMap(thread, linkedMap);
81 jsMap->SetModRecord(0);
82 return jsMap;
83 }
84
CreateJSSharedSet(JSThread * thread)85 static JSHandle<JSSharedSet> CreateJSSharedSet(JSThread *thread)
86 {
87 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
88 auto vm = thread->GetEcmaVM();
89 ObjectFactory *factory = vm->GetFactory();
90 JSHandle<JSTaggedValue> proto = globalEnv->GetSFunctionPrototype();
91 auto emptySLayout = thread->GlobalConstants()->GetHandledEmptySLayoutInfo();
92 JSHandle<JSHClass> setClass = factory->NewSEcmaHClass(JSSharedSet::SIZE, 0,
93 JSType::JS_SHARED_SET, proto, emptySLayout);
94 JSHandle<JSSharedSet> jsSet = JSHandle<JSSharedSet>::Cast(factory->NewJSObjectWithInit(setClass));
95 JSHandle<LinkedHashSet> linkedSet(
96 LinkedHashSet::Create(thread, LinkedHashSet::MIN_CAPACITY, MemSpaceKind::SHARED));
97 jsSet->SetLinkedSet(thread, linkedSet);
98 jsSet->SetModRecord(0);
99 return jsSet;
100 }
101
CreateJSMap(JSThread * thread)102 static JSHandle<JSMap> CreateJSMap(JSThread *thread)
103 {
104 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
105 auto vm = thread->GetEcmaVM();
106 ObjectFactory *factory = vm->GetFactory();
107 JSHandle<JSTaggedValue> constructor = globalEnv->GetBuiltinsMapFunction();
108 JSHandle<JSMap> map =
109 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
110 JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
111 map->SetLinkedMap(thread, linkedMap);
112 return JSHandle<JSMap>(thread, *map);
113 }
114
CreateJSSet(JSThread * thread)115 static JSHandle<JSSet> CreateJSSet(JSThread *thread)
116 {
117 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
118 JSHandle<JSTaggedValue> proto = globalEnv->GetFunctionPrototype();
119 auto vm = thread->GetEcmaVM();
120 ObjectFactory *factory = vm->GetFactory();
121 JSHandle<JSHClass> hclass = factory->NewEcmaHClass(JSSet::SIZE, JSType::JS_SET, proto);
122 JSHandle<JSObject> jsSetObject = factory->NewJSObjectWithInit(hclass);
123 JSHandle<JSSet> jsSet = JSHandle<JSSet>::Cast(jsSetObject);
124 JSHandle<LinkedHashSet> linkedSet(LinkedHashSet::Create(thread));
125 jsSet->SetLinkedSet(thread, linkedSet);
126 return jsSet;
127 }
128
CreateHashMap(JSThread * thread)129 static JSAPIHashMap *CreateHashMap(JSThread *thread)
130 {
131 return EcmaContainerCommon::CreateHashMap(thread);
132 }
133
CreateHashSet(JSThread * thread)134 static JSAPIHashSet *CreateHashSet(JSThread *thread)
135 {
136 return EcmaContainerCommon::CreateHashSet(thread);
137 }
138
TestForStringfy1(EcmaRuntimeCallInfo * argv)139 static JSTaggedValue TestForStringfy1([[maybe_unused]] EcmaRuntimeCallInfo *argv)
140 {
141 // false: test case
142 return JSTaggedValue(JSTaggedValue::False());
143 }
144
TestForStringfy2(EcmaRuntimeCallInfo * argv)145 static JSTaggedValue TestForStringfy2([[maybe_unused]] EcmaRuntimeCallInfo *argv)
146 {
147 // 10.12: test case
148 return JSTaggedValue(10.12);
149 }
150
151 /**
152 * @tc.name: Stringify_001
153 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
154 * the first parameter of the ECMAObject,the second parameter is JSFunction,the third parameter
155 * is Undefined.if the second parameter is JSFunction,the return value is the parameter stringification
156 * after through "call" function.
157 * @tc.type: FUNC
158 * @tc.require:
159 */
HWTEST_F_L0(JsonStringifierTest,Stringify_001)160 HWTEST_F_L0(JsonStringifierTest, Stringify_001)
161 {
162 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
163 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
164
165 JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
166 JSHandle<JSFunction> handleFunc1 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy1));
167 JSHandle<JSFunction> handleFunc2 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy2));
168 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
169 JSHandle<JSTaggedValue> handleReplacer1(thread, handleFunc1.GetTaggedValue());
170 JSHandle<JSTaggedValue> handleReplacer2(thread, handleFunc2.GetTaggedValue());
171 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
172
173 JsonStringifier stringifier1(thread);
174 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleValue, handleReplacer1, handleGap);
175 EXPECT_TRUE(resultString1->IsString());
176 JSHandle<EcmaString> handleEcmaStr1(resultString1);
177 EXPECT_STREQ("false", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
178
179 JsonStringifier stringifier2(thread);
180 JSHandle<JSTaggedValue> resultString2 = stringifier2.Stringify(handleValue, handleReplacer2, handleGap);
181 EXPECT_TRUE(resultString2->IsString());
182 JSHandle<EcmaString> handleEcmaStr2(resultString2);
183 EXPECT_STREQ("10.12", EcmaStringAccessor(handleEcmaStr2).ToCString().c_str());
184 }
185
186 /**
187 * @tc.name: Stringify_002
188 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
189 * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
190 * is Number.This situation will stringize parameters through "SerializeJSONObject" function.
191 * @tc.type: FUNC
192 * @tc.require:
193 */
HWTEST_F_L0(JsonStringifierTest,Stringify_002)194 HWTEST_F_L0(JsonStringifierTest, Stringify_002)
195 {
196 JsonStringifier stringifier(thread);
197
198 JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
199 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
200 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
201 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue(static_cast<int32_t>(10)));
202
203 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
204 EXPECT_TRUE(resultString->IsString());
205 JSHandle<EcmaString> handleEcmaStr(resultString);
206 EXPECT_STREQ("{\n \"z\": 1,\n \"x\": 3.6,\n \"y\": \"abc\"\n}",
207 EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
208 }
209
210 /**
211 * @tc.name: Stringify_003
212 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
213 * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
214 * is String,This situation will stringize parameters through "SerializeJSONObject" function.
215 * @tc.type: FUNC
216 * @tc.require:
217 */
HWTEST_F_L0(JsonStringifierTest,Stringify_003)218 HWTEST_F_L0(JsonStringifierTest, Stringify_003)
219 {
220 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
221 JsonStringifier stringifier(thread);
222
223 JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
224 JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
225 JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
226
227 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
228 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
229 JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
230
231 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
232 EXPECT_TRUE(resultString->IsString());
233 JSHandle<EcmaString> resultStr =
234 factory->NewFromASCII("{\ntttt\"z\": 1,\ntttt\"x\": 3.6,\ntttt\"y\": \"abc\"\n}");
235 EXPECT_EQ(EcmaStringAccessor::Compare(instance, resultStr, JSHandle<EcmaString>(resultString)), 0);
236 }
237
238 /**
239 * @tc.name: Stringify_004
240 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
241 * the first parameter of the ECMAObject,the second parameter is JSArray,the third parameter
242 * is String.This situation will stringize parameters through "SerializeJSONObject" function.
243 * @tc.type: FUNC
244 * @tc.require:
245 */
HWTEST_F_L0(JsonStringifierTest,Stringify_004)246 HWTEST_F_L0(JsonStringifierTest, Stringify_004)
247 {
248 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
249 JsonStringifier stringifier(thread);
250
251 JSHandle<JSTaggedValue> handleObj1 = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
252
253 JSArray *handleArr =
254 JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
255 JSHandle<JSObject> handleObj2(thread, handleArr);
256 JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
257 JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("z"));
258 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey0, handleValue0);
259
260 JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
261 JSHandle<JSTaggedValue> handleValue1(factory->NewFromASCII("x"));
262 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey1, handleValue1);
263
264 JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
265 JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
266
267 JSHandle<JSTaggedValue> handleValue(thread, handleObj1.GetTaggedValue());
268 JSHandle<JSTaggedValue> handleReplacer(thread, handleObj2.GetTaggedValue());
269 JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
270
271 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
272 EXPECT_TRUE(resultString->IsString());
273 JSHandle<EcmaString> handleEcmaStr(resultString);
274 EXPECT_STREQ("{\ntttt\"z\": 1,\ntttt\"x\": 3.6\n}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
275 }
276
277 /**
278 * @tc.name: Stringify_005
279 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
280 * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
281 * is Undefined.This situation will stringize the first parameter through "SerializeJSONObject" function.
282 * @tc.type: FUNC
283 * @tc.require:
284 */
HWTEST_F_L0(JsonStringifierTest,Stringify_005)285 HWTEST_F_L0(JsonStringifierTest, Stringify_005)
286 {
287 JsonStringifier stringifier(thread);
288 JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
289
290 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
291 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
292 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
293
294 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
295 EXPECT_TRUE(resultString->IsString());
296 JSHandle<EcmaString> handleEcmaStr(resultString);
297 EXPECT_STREQ("{\"z\":1,\"x\":3.6,\"y\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
298 }
299
300 /**
301 * @tc.name: Stringify_006
302 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
303 * the first parameter of the JSArray,the second parameter is Undefined,the third parameter
304 * is String,This situation will stringize parameters through "SerializeJSArray" function.
305 * @tc.type: FUNC
306 * @tc.require:
307 */
HWTEST_F_L0(JsonStringifierTest,Stringify_006)308 HWTEST_F_L0(JsonStringifierTest, Stringify_006)
309 {
310 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
311 JsonStringifier stringifier(thread);
312
313 JSArray *handleArr =
314 JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
315 JSHandle<JSObject> handleObj(thread, handleArr);
316
317 JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
318 JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("json"));
319 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey0, handleValue0);
320
321 JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
322 PropertyDescriptor handleDesc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(100)), true, true, true);
323 JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc);
324
325 JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2));
326 JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc"));
327 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2);
328
329 JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
330 JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
331
332 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
333 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
334 JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
335
336 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
337 EXPECT_TRUE(resultString->IsString());
338 JSHandle<EcmaString> handleEcmaStr(resultString);
339 EXPECT_STREQ("[\ntttt\"json\",\ntttt100,\ntttt\"abc\"\n]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
340 }
341
342 /**
343 * @tc.name: Stringify_007
344 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
345 * the first parameter of the JSObject,the second parameter is Undefined,the third parameter
346 * is Undefined.This situation will stringize the first parameter through "SerializeJSArray" function.
347 * @tc.type: FUNC
348 * @tc.require:
349 */
HWTEST_F_L0(JsonStringifierTest,Stringify_007)350 HWTEST_F_L0(JsonStringifierTest, Stringify_007)
351 {
352 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
353 JsonStringifier stringifier(thread);
354
355 JSArray *handleArr =
356 JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
357 JSHandle<JSObject> handleObj(thread, handleArr);
358
359 JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
360 PropertyDescriptor handleDesc0(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), true, true, true);
361 JSArray::DefineOwnProperty(thread, handleObj, handleKey0, handleDesc0);
362
363 JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
364 PropertyDescriptor handleDesc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(3.6)), true, true, true);
365 JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc1);
366
367 JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2));
368 JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc"));
369 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2);
370
371 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
372 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
373 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
374
375 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
376 EXPECT_TRUE(resultString->IsString());
377 JSHandle<EcmaString> handleEcmaStr(resultString);
378 EXPECT_STREQ("[1,3.6,\"abc\"]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
379 }
380
381 /**
382 * @tc.name: Stringify_008
383 * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
384 * the first parameter of the JSObject,the second parameter is Undefined,the third parameter
385 * is Undefined.This situation will stringize the first parameter through "SerializePrimitiveRef"
386 * function.
387 * @tc.type: FUNC
388 * @tc.require:
389 */
HWTEST_F_L0(JsonStringifierTest,Stringify_008)390 HWTEST_F_L0(JsonStringifierTest, Stringify_008)
391 {
392 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
393 JsonStringifier stringifier(thread);
394
395 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
396 JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("\"\\\b\f\n\r\t"));
397 JSHandle<JSPrimitiveRef> handlePrimitiveRef = factory->NewJSString(handleStr, undefined);
398 JSHandle<JSObject> handleObj(thread, handlePrimitiveRef.GetTaggedValue());
399
400 JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
401 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
402 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
403
404 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
405 EXPECT_TRUE(resultString->IsString());
406 JSHandle<EcmaString> handleEcmaStr(resultString);
407 EXPECT_STREQ("\"\\\"\\\\\\b\\f\\n\\r\\t\"", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
408 }
409
Detach(void * param1,void * param2,void * hint,void * detachData)410 static void* Detach(void *param1, void *param2, void *hint, void *detachData)
411 {
412 GTEST_LOG_(INFO) << "detach is running";
413 if (param1 == nullptr && param2 == nullptr) {
414 GTEST_LOG_(INFO) << "detach: two params is nullptr";
415 }
416 if (hint == nullptr && detachData) {
417 GTEST_LOG_(INFO) << "detach: hint is nullptr";
418 }
419 return nullptr;
420 }
421
Attach(void * enginePointer,void * buffer,void * hint,void * attachData)422 static void* Attach([[maybe_unused]] void *enginePointer, [[maybe_unused]] void *buffer, [[maybe_unused]] void *hint,
423 [[maybe_unused]] void *attachData)
424 {
425 GTEST_LOG_(INFO) << "attach is running";
426 return nullptr;
427 }
428
CreateNativeBindingInfo(void * attach,void * detach)429 static panda::JSNApi::NativeBindingInfo* CreateNativeBindingInfo(void* attach, void* detach)
430 {
431 GTEST_LOG_(INFO) << "CreateNativeBindingInfo";
432 auto info = panda::JSNApi::NativeBindingInfo::CreateNewInstance();
433 info->attachFunc = attach;
434 info->detachFunc = detach;
435 return info;
436 }
437
HWTEST_F_L0(JsonStringifierTest,Stringify_009)438 HWTEST_F_L0(JsonStringifierTest, Stringify_009)
439 {
440 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
441 JsonStringifier stringifier(thread);
442
443 EcmaVM *ecmaVM = thread->GetEcmaVM();
444 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
445 JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction());
446 JSHandle<JSObject> jsObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
447 EXPECT_TRUE(*jsObject != nullptr);
448
449 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
450 auto info = CreateNativeBindingInfo(reinterpret_cast<void*>(Attach), reinterpret_cast<void*>(Detach));
451 JSHandle<JSTaggedValue> value1(factory->NewJSNativePointer(reinterpret_cast<void*>(info)));
452 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), key1, value1);
453
454 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
455 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("abc"));
456 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), key2, value2);
457
458 JSHandle<JSTaggedValue> handleValue(thread, jsObject.GetTaggedValue());
459 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
460 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
461
462 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
463 EXPECT_TRUE(resultString->IsString());
464 JSHandle<EcmaString> handleEcmaStr(resultString);
465 EXPECT_STREQ("{\"key1\":{},\"key2\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
466 }
467
HWTEST_F_L0(JsonStringifierTest,Stringify_010)468 HWTEST_F_L0(JsonStringifierTest, Stringify_010)
469 {
470 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
471 JsonStringifier stringifier(thread, TransformType::SENDABLE);
472 JSHandle<JSSharedMap> sharedMap = CreateSharedMap(thread);
473 JSHandle<JSTaggedValue> handleMap = JSHandle<JSTaggedValue>(sharedMap);
474 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
475 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
476 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleMap, handleReplacer, handleGap);
477 EXPECT_TRUE(resultString->IsString());
478 JSHandle<EcmaString> handleEcmaStr(resultString);
479 EXPECT_STREQ("{}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
480
481 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
482 JSHandle<JSSharedMap> sharedMap1 = CreateSharedMap(thread);
483 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
484 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
485 JSSharedMap::Set(thread, sharedMap1, key1, value1);
486
487 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
488 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
489 JSSharedMap::Set(thread, sharedMap1, key2, value2);
490
491 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(sharedMap1);
492 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
493 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
494 EXPECT_TRUE(*handleMap1 != nullptr);
495 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleMap1, handleReplacer1, handleGap1);
496 EXPECT_TRUE(resultString1->IsString());
497 JSHandle<EcmaString> handleEcmaStr1(resultString1);
498 EXPECT_STREQ("{\"key1\":\"abc\",\"key2\":\"val\"}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
499 }
500
HWTEST_F_L0(JsonStringifierTest,Stringify_011)501 HWTEST_F_L0(JsonStringifierTest, Stringify_011)
502 {
503 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
504 JsonStringifier stringifier(thread, TransformType::SENDABLE);
505 JSHandle<JSMap> jsMap = CreateJSMap(thread);
506 JSHandle<JSTaggedValue> handleMap = JSHandle<JSTaggedValue>(jsMap);
507 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
508 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
509 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleMap, handleReplacer, handleGap);
510 EXPECT_TRUE(resultString->IsString());
511 JSHandle<EcmaString> handleEcmaStr(resultString);
512 EXPECT_STREQ("{}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
513
514 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
515 JSHandle<JSMap> jsMap1 = CreateJSMap(thread);
516 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
517 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
518 JSMap::Set(thread, jsMap1, key1, value1);
519
520 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
521 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
522 JSMap::Set(thread, jsMap1, key2, value2);
523
524 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(jsMap1);
525 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
526 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
527 EXPECT_TRUE(*handleMap1 != nullptr);
528 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleMap1, handleReplacer1, handleGap1);
529 EXPECT_TRUE(resultString1->IsString());
530 JSHandle<EcmaString> handleEcmaStr1(resultString1);
531 EXPECT_STREQ("{\"key1\":\"abc\",\"key2\":\"val\"}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
532 }
533
HWTEST_F_L0(JsonStringifierTest,Stringify_012)534 HWTEST_F_L0(JsonStringifierTest, Stringify_012)
535 {
536 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
537 JsonStringifier stringifier(thread, TransformType::SENDABLE);
538 JSHandle<JSSharedSet> sharedSet = CreateJSSharedSet(thread);
539 JSHandle<JSTaggedValue> handleSet = JSHandle<JSTaggedValue>(sharedSet);
540 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
541 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
542 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleSet, handleReplacer, handleGap);
543 EXPECT_TRUE(resultString->IsString());
544 JSHandle<EcmaString> handleEcmaStr(resultString);
545 EXPECT_STREQ("[]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
546
547 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
548 JSHandle<JSSharedSet> sharedSet1 = CreateJSSharedSet(thread);
549 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
550 JSSharedSet::Add(thread, sharedSet1, value1);
551
552 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
553 JSSharedSet::Add(thread, sharedSet1, value2);
554
555 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(sharedSet1);
556 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
557 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
558 EXPECT_TRUE(*handleSet1 != nullptr);
559 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleSet1, handleReplacer1, handleGap1);
560 EXPECT_TRUE(resultString1->IsString());
561 JSHandle<EcmaString> handleEcmaStr1(resultString1);
562 EXPECT_STREQ("[\"abc\",\"val\"]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
563 }
564
HWTEST_F_L0(JsonStringifierTest,Stringify_013)565 HWTEST_F_L0(JsonStringifierTest, Stringify_013)
566 {
567 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
568 JsonStringifier stringifier(thread, TransformType::SENDABLE);
569 JSHandle<JSSet> jsSet = CreateJSSet(thread);
570 JSHandle<JSTaggedValue> handleSet = JSHandle<JSTaggedValue>(jsSet);
571 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
572 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
573 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleSet, handleReplacer, handleGap);
574 EXPECT_TRUE(resultString->IsString());
575 JSHandle<EcmaString> handleEcmaStr(resultString);
576 EXPECT_STREQ("[]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
577
578 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
579 JSHandle<JSSet> jsSet1 = CreateJSSet(thread);
580 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
581 JSSet::Add(thread, jsSet1, value1);
582
583 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
584 JSSet::Add(thread, jsSet1, value2);
585
586 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(jsSet1);
587 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
588 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
589 EXPECT_TRUE(*handleSet1 != nullptr);
590 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleSet1, handleReplacer1, handleGap1);
591 EXPECT_TRUE(resultString1->IsString());
592 JSHandle<EcmaString> handleEcmaStr1(resultString1);
593 EXPECT_STREQ("[\"abc\",\"val\"]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
594 }
595
HWTEST_F_L0(JsonStringifierTest,Stringify_014)596 HWTEST_F_L0(JsonStringifierTest, Stringify_014)
597 {
598 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
599 JsonStringifier stringifier(thread, TransformType::SENDABLE);
600 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap(thread));
601 JSHandle<JSTaggedValue> handleMap = JSHandle<JSTaggedValue>(hashMap);
602 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
603 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
604 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleMap, handleReplacer, handleGap);
605 EXPECT_TRUE(resultString->IsString());
606 JSHandle<EcmaString> handleEcmaStr(resultString);
607 EXPECT_STREQ("{}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
608
609 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
610 JSHandle<JSAPIHashMap> hashMap1(thread, CreateHashMap(thread));
611 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
612 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
613 JSAPIHashMap::Set(thread, hashMap1, key1, value1);
614
615 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
616 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
617 JSAPIHashMap::Set(thread, hashMap1, key2, value2);
618
619 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(hashMap1);
620 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
621 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
622 EXPECT_TRUE(*handleMap1 != nullptr);
623 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleMap1, handleReplacer1, handleGap1);
624 EXPECT_TRUE(resultString1->IsString());
625 JSHandle<EcmaString> handleEcmaStr1(resultString1);
626 EXPECT_STRNE("{}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
627 }
628
HWTEST_F_L0(JsonStringifierTest,Stringify_015)629 HWTEST_F_L0(JsonStringifierTest, Stringify_015)
630 {
631 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
632 JsonStringifier stringifier(thread, TransformType::SENDABLE);
633 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet(thread));
634 JSHandle<JSTaggedValue> handleSet = JSHandle<JSTaggedValue>(hashSet);
635 JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
636 JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
637 JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleSet, handleReplacer, handleGap);
638 EXPECT_TRUE(resultString->IsString());
639 JSHandle<EcmaString> handleEcmaStr(resultString);
640 EXPECT_STREQ("[]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
641
642 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
643 JSHandle<JSAPIHashSet> hashSet1(thread, CreateHashSet(thread));
644 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
645 JSAPIHashSet::Add(thread, hashSet1, value1);
646
647 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
648 JSAPIHashSet::Add(thread, hashSet1, value2);
649
650 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(hashSet1);
651 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
652 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
653 EXPECT_TRUE(*handleSet1 != nullptr);
654 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleSet1, handleReplacer1, handleGap1);
655 EXPECT_TRUE(resultString1->IsString());
656 JSHandle<EcmaString> handleEcmaStr1(resultString1);
657 EXPECT_STRNE("[]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
658 }
659
HWTEST_F_L0(JsonStringifierTest,Stringify_016)660 HWTEST_F_L0(JsonStringifierTest, Stringify_016)
661 {
662 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
663
664 JsonStringifier stringifier(thread, TransformType::SENDABLE);
665 JSHandle<JSSharedMap> sharedMap1 = CreateSharedMap(thread);
666 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
667 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
668 JSSharedMap::Set(thread, sharedMap1, key1, value1);
669 JSSharedMap::Clear(thread, sharedMap1);
670
671 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
672 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
673 JSSharedMap::Set(thread, sharedMap1, key2, value2);
674 JSSharedMap::Set(thread, sharedMap1, key1, value1);
675
676 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(sharedMap1);
677 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
678 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
679 EXPECT_TRUE(*handleMap1 != nullptr);
680 JSHandle<JSTaggedValue> resultString1 = stringifier.Stringify(handleMap1, handleReplacer1, handleGap1);
681 EXPECT_TRUE(resultString1->IsString());
682 JSHandle<EcmaString> handleEcmaStr1(resultString1);
683 EXPECT_STREQ("{\"key2\":\"val\",\"key1\":\"abc\"}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
684 }
685
HWTEST_F_L0(JsonStringifierTest,Stringify_017)686 HWTEST_F_L0(JsonStringifierTest, Stringify_017)
687 {
688 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
689 JsonStringifier stringifier(thread, TransformType::SENDABLE);
690 JSHandle<JSSharedSet> sharedSet1 = CreateJSSharedSet(thread);
691 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
692 JSSharedSet::Add(thread, sharedSet1, value1);
693 JSSharedSet::Clear(thread, sharedSet1);
694
695 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
696 JSSharedSet::Add(thread, sharedSet1, value2);
697 JSSharedSet::Add(thread, sharedSet1, value1);
698
699 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(sharedSet1);
700 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
701 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
702 EXPECT_TRUE(*handleSet1 != nullptr);
703 JSHandle<JSTaggedValue> resultString1 = stringifier.Stringify(handleSet1, handleReplacer1, handleGap1);
704 EXPECT_TRUE(resultString1->IsString());
705 JSHandle<EcmaString> handleEcmaStr1(resultString1);
706 EXPECT_STREQ("[\"val\",\"abc\"]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
707 }
708
HWTEST_F_L0(JsonStringifierTest,Stringify_018)709 HWTEST_F_L0(JsonStringifierTest, Stringify_018)
710 {
711 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
712
713 JsonStringifier stringifier(thread, TransformType::SENDABLE);
714 JSHandle<JSSharedMap> sharedMap1 = CreateSharedMap(thread);
715 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
716 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
717 JSSharedMap::Set(thread, sharedMap1, key1, value1);
718
719 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
720 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
721 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
722 JSSharedMap::Set(thread, sharedMap1, key2, undefined);
723 JSSharedMap::Set(thread, sharedMap1, undefined, value2);
724
725 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(sharedMap1);
726 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
727 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
728 EXPECT_TRUE(*handleMap1 != nullptr);
729 JSHandle<JSTaggedValue> resultString1 = stringifier.Stringify(handleMap1, handleReplacer1, handleGap1);
730 EXPECT_TRUE(resultString1->IsString());
731 JSHandle<EcmaString> handleEcmaStr1(resultString1);
732 EXPECT_STREQ("{\"key1\":\"abc\",\"undefined\":\"val\"}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
733 }
734
HWTEST_F_L0(JsonStringifierTest,Stringify_019)735 HWTEST_F_L0(JsonStringifierTest, Stringify_019)
736 {
737 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
738 JsonStringifier stringifier(thread, TransformType::SENDABLE);
739 JSHandle<JSSharedSet> sharedSet1 = CreateJSSharedSet(thread);
740 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
741 JSSharedSet::Add(thread, sharedSet1, value1);
742
743 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("val"));
744 JSSharedSet::Add(thread, sharedSet1, value2);
745
746 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
747 JSSharedSet::Add(thread, sharedSet1, undefined);
748
749 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(sharedSet1);
750 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
751 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
752 EXPECT_TRUE(*handleSet1 != nullptr);
753 JSHandle<JSTaggedValue> resultString1 = stringifier.Stringify(handleSet1, handleReplacer1, handleGap1);
754 EXPECT_TRUE(resultString1->IsString());
755 JSHandle<EcmaString> handleEcmaStr1(resultString1);
756 EXPECT_STREQ("[\"abc\",\"val\",null]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
757 }
758
HWTEST_F_L0(JsonStringifierTest,Stringify_020)759 HWTEST_F_L0(JsonStringifierTest, Stringify_020)
760 {
761 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
762 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
763 JSHandle<JSAPIHashMap> hashMap1(thread, CreateHashMap(thread));
764 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1"));
765 JSHandle<JSTaggedValue> value1(factory->NewFromASCII("abc"));
766 JSAPIHashMap::Set(thread, hashMap1, key1, value1);
767
768 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2"));
769 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue::Undefined());
770 JSAPIHashMap::Set(thread, hashMap1, key2, value2);
771
772 JSHandle<JSTaggedValue> handleMap1 = JSHandle<JSTaggedValue>(hashMap1);
773 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
774 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
775 EXPECT_TRUE(*handleMap1 != nullptr);
776 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleMap1, handleReplacer1, handleGap1);
777 EXPECT_TRUE(resultString1->IsString());
778 JSHandle<EcmaString> handleEcmaStr1(resultString1);
779 EXPECT_STREQ("{\"key1\":\"abc\"}", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
780 }
781
HWTEST_F_L0(JsonStringifierTest,Stringify_021)782 HWTEST_F_L0(JsonStringifierTest, Stringify_021)
783 {
784 JsonStringifier stringifier1(thread, TransformType::SENDABLE);
785 JSHandle<JSAPIHashSet> hashSet1(thread, CreateHashSet(thread));
786 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue::Undefined());
787 JSAPIHashSet::Add(thread, hashSet1, value1);
788
789 JSHandle<JSTaggedValue> handleSet1 = JSHandle<JSTaggedValue>(hashSet1);
790 JSHandle<JSTaggedValue> handleReplacer1(thread, JSTaggedValue::Undefined());
791 JSHandle<JSTaggedValue> handleGap1(thread, JSTaggedValue::Undefined());
792 EXPECT_TRUE(*handleSet1 != nullptr);
793 JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleSet1, handleReplacer1, handleGap1);
794 EXPECT_TRUE(resultString1->IsString());
795 JSHandle<EcmaString> handleEcmaStr1(resultString1);
796 EXPECT_STREQ("[null]", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
797 }
798 } // namespace panda::test