14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/base/json_stringifier.h" 174514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 184514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 194514f5e3Sopenharmony_ci 204514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 214514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base; 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::test { 244514f5e3Sopenharmony_ciclass JsonStringifierTest : public BaseTestWithScope<false> { 254514f5e3Sopenharmony_ci}; 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_cistatic JSTaggedValue CreateBaseJSObject(JSThread *thread, const CString keyCStr) 284514f5e3Sopenharmony_ci{ 294514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 304514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 314514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction()); 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci JSHandle<JSObject> jsObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc)); 354514f5e3Sopenharmony_ci EXPECT_TRUE(*jsObject != nullptr); 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey1(factory->NewFromASCII(&keyCStr[0])); 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue1(thread, JSTaggedValue(1)); // 1 : test case 394514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey1, handleValue1); 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ci CString str2 = "x"; 424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey2(factory->NewFromASCII(str2)); 434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue2(thread, JSTaggedValue(3.6)); // 3.6 : test case 444514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey2, handleValue2); 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_ci CString str3 = "y"; 474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey3(factory->NewFromASCII(str3)); 484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue3(factory->NewFromASCII("abc")); 494514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey3, handleValue3); 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci return jsObject.GetTaggedValue(); 524514f5e3Sopenharmony_ci} 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_cistatic JSTaggedValue TestForStringfy1([[maybe_unused]] EcmaRuntimeCallInfo *argv) 554514f5e3Sopenharmony_ci{ 564514f5e3Sopenharmony_ci // false: test case 574514f5e3Sopenharmony_ci return JSTaggedValue(JSTaggedValue::False()); 584514f5e3Sopenharmony_ci} 594514f5e3Sopenharmony_ci 604514f5e3Sopenharmony_cistatic JSTaggedValue TestForStringfy2([[maybe_unused]] EcmaRuntimeCallInfo *argv) 614514f5e3Sopenharmony_ci{ 624514f5e3Sopenharmony_ci // 10.12: test case 634514f5e3Sopenharmony_ci return JSTaggedValue(10.12); 644514f5e3Sopenharmony_ci} 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci/** 674514f5e3Sopenharmony_ci * @tc.name: Stringify_001 684514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 694514f5e3Sopenharmony_ci * the first parameter of the ECMAObject,the second parameter is JSFunction,the third parameter 704514f5e3Sopenharmony_ci * is Undefined.if the second parameter is JSFunction,the return value is the parameter stringification 714514f5e3Sopenharmony_ci * after through "call" function. 724514f5e3Sopenharmony_ci * @tc.type: FUNC 734514f5e3Sopenharmony_ci * @tc.require: 744514f5e3Sopenharmony_ci */ 754514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_001) 764514f5e3Sopenharmony_ci{ 774514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 784514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z")); 814514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc1 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy1)); 824514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc2 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy2)); 834514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer1(thread, handleFunc1.GetTaggedValue()); 854514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer2(thread, handleFunc2.GetTaggedValue()); 864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined()); 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci JsonStringifier stringifier1(thread); 894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleValue, handleReplacer1, handleGap); 904514f5e3Sopenharmony_ci EXPECT_TRUE(resultString1->IsString()); 914514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr1(resultString1); 924514f5e3Sopenharmony_ci EXPECT_STREQ("false", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str()); 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci JsonStringifier stringifier2(thread); 954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString2 = stringifier2.Stringify(handleValue, handleReplacer2, handleGap); 964514f5e3Sopenharmony_ci EXPECT_TRUE(resultString2->IsString()); 974514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr2(resultString2); 984514f5e3Sopenharmony_ci EXPECT_STREQ("10.12", EcmaStringAccessor(handleEcmaStr2).ToCString().c_str()); 994514f5e3Sopenharmony_ci} 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci/** 1024514f5e3Sopenharmony_ci * @tc.name: Stringify_002 1034514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 1044514f5e3Sopenharmony_ci * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter 1054514f5e3Sopenharmony_ci * is Number.This situation will stringize parameters through "SerializeJSONObject" function. 1064514f5e3Sopenharmony_ci * @tc.type: FUNC 1074514f5e3Sopenharmony_ci * @tc.require: 1084514f5e3Sopenharmony_ci */ 1094514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_002) 1104514f5e3Sopenharmony_ci{ 1114514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z")); 1144514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 1154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 1164514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue(static_cast<int32_t>(10))); 1174514f5e3Sopenharmony_ci 1184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 1194514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 1204514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 1214514f5e3Sopenharmony_ci EXPECT_STREQ("{\n \"z\": 1,\n \"x\": 3.6,\n \"y\": \"abc\"\n}", 1224514f5e3Sopenharmony_ci EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 1234514f5e3Sopenharmony_ci} 1244514f5e3Sopenharmony_ci 1254514f5e3Sopenharmony_ci/** 1264514f5e3Sopenharmony_ci * @tc.name: Stringify_003 1274514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 1284514f5e3Sopenharmony_ci * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter 1294514f5e3Sopenharmony_ci * is String,This situation will stringize parameters through "SerializeJSONObject" function. 1304514f5e3Sopenharmony_ci * @tc.type: FUNC 1314514f5e3Sopenharmony_ci * @tc.require: 1324514f5e3Sopenharmony_ci */ 1334514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_003) 1344514f5e3Sopenharmony_ci{ 1354514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1364514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z")); 1394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt")); 1404514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 1434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 1444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue()); 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 1474514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 1484514f5e3Sopenharmony_ci JSHandle<EcmaString> resultStr = 1494514f5e3Sopenharmony_ci factory->NewFromASCII("{\ntttt\"z\": 1,\ntttt\"x\": 3.6,\ntttt\"y\": \"abc\"\n}"); 1504514f5e3Sopenharmony_ci EXPECT_EQ(EcmaStringAccessor::Compare(instance, resultStr, JSHandle<EcmaString>(resultString)), 0); 1514514f5e3Sopenharmony_ci} 1524514f5e3Sopenharmony_ci 1534514f5e3Sopenharmony_ci/** 1544514f5e3Sopenharmony_ci * @tc.name: Stringify_004 1554514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 1564514f5e3Sopenharmony_ci * the first parameter of the ECMAObject,the second parameter is JSArray,the third parameter 1574514f5e3Sopenharmony_ci * is String.This situation will stringize parameters through "SerializeJSONObject" function. 1584514f5e3Sopenharmony_ci * @tc.type: FUNC 1594514f5e3Sopenharmony_ci * @tc.require: 1604514f5e3Sopenharmony_ci */ 1614514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_004) 1624514f5e3Sopenharmony_ci{ 1634514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1644514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleObj1 = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z")); 1674514f5e3Sopenharmony_ci 1684514f5e3Sopenharmony_ci JSArray *handleArr = 1694514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 1704514f5e3Sopenharmony_ci JSHandle<JSObject> handleObj2(thread, handleArr); 1714514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0)); 1724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("z")); 1734514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey0, handleValue0); 1744514f5e3Sopenharmony_ci 1754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1)); 1764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue1(factory->NewFromASCII("x")); 1774514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey1, handleValue1); 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt")); 1804514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 1814514f5e3Sopenharmony_ci 1824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj1.GetTaggedValue()); 1834514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, handleObj2.GetTaggedValue()); 1844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue()); 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 1874514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 1884514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 1894514f5e3Sopenharmony_ci EXPECT_STREQ("{\ntttt\"z\": 1,\ntttt\"x\": 3.6\n}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 1904514f5e3Sopenharmony_ci} 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_ci/** 1934514f5e3Sopenharmony_ci * @tc.name: Stringify_005 1944514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 1954514f5e3Sopenharmony_ci * the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter 1964514f5e3Sopenharmony_ci * is Undefined.This situation will stringize the first parameter through "SerializeJSONObject" function. 1974514f5e3Sopenharmony_ci * @tc.type: FUNC 1984514f5e3Sopenharmony_ci * @tc.require: 1994514f5e3Sopenharmony_ci */ 2004514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_005) 2014514f5e3Sopenharmony_ci{ 2024514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 2034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z")); 2044514f5e3Sopenharmony_ci 2054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 2064514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 2074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined()); 2084514f5e3Sopenharmony_ci 2094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 2104514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 2114514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 2124514f5e3Sopenharmony_ci EXPECT_STREQ("{\"z\":1,\"x\":3.6,\"y\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2134514f5e3Sopenharmony_ci} 2144514f5e3Sopenharmony_ci 2154514f5e3Sopenharmony_ci/** 2164514f5e3Sopenharmony_ci * @tc.name: Stringify_006 2174514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 2184514f5e3Sopenharmony_ci * the first parameter of the JSArray,the second parameter is Undefined,the third parameter 2194514f5e3Sopenharmony_ci * is String,This situation will stringize parameters through "SerializeJSArray" function. 2204514f5e3Sopenharmony_ci * @tc.type: FUNC 2214514f5e3Sopenharmony_ci * @tc.require: 2224514f5e3Sopenharmony_ci */ 2234514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_006) 2244514f5e3Sopenharmony_ci{ 2254514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2264514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci JSArray *handleArr = 2294514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 2304514f5e3Sopenharmony_ci JSHandle<JSObject> handleObj(thread, handleArr); 2314514f5e3Sopenharmony_ci 2324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0)); 2334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("json")); 2344514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey0, handleValue0); 2354514f5e3Sopenharmony_ci 2364514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1)); 2374514f5e3Sopenharmony_ci PropertyDescriptor handleDesc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(100)), true, true, true); 2384514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc); 2394514f5e3Sopenharmony_ci 2404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2)); 2414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc")); 2424514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2); 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt")); 2454514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2464514f5e3Sopenharmony_ci 2474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 2484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 2494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue()); 2504514f5e3Sopenharmony_ci 2514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 2524514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 2534514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 2544514f5e3Sopenharmony_ci EXPECT_STREQ("[\ntttt\"json\",\ntttt100,\ntttt\"abc\"\n]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2554514f5e3Sopenharmony_ci} 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ci/** 2584514f5e3Sopenharmony_ci * @tc.name: Stringify_007 2594514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 2604514f5e3Sopenharmony_ci * the first parameter of the JSObject,the second parameter is Undefined,the third parameter 2614514f5e3Sopenharmony_ci * is Undefined.This situation will stringize the first parameter through "SerializeJSArray" function. 2624514f5e3Sopenharmony_ci * @tc.type: FUNC 2634514f5e3Sopenharmony_ci * @tc.require: 2644514f5e3Sopenharmony_ci */ 2654514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_007) 2664514f5e3Sopenharmony_ci{ 2674514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2684514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 2694514f5e3Sopenharmony_ci 2704514f5e3Sopenharmony_ci JSArray *handleArr = 2714514f5e3Sopenharmony_ci JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 2724514f5e3Sopenharmony_ci JSHandle<JSObject> handleObj(thread, handleArr); 2734514f5e3Sopenharmony_ci 2744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0)); 2754514f5e3Sopenharmony_ci PropertyDescriptor handleDesc0(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), true, true, true); 2764514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, handleObj, handleKey0, handleDesc0); 2774514f5e3Sopenharmony_ci 2784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1)); 2794514f5e3Sopenharmony_ci PropertyDescriptor handleDesc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(3.6)), true, true, true); 2804514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc1); 2814514f5e3Sopenharmony_ci 2824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2)); 2834514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc")); 2844514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2); 2854514f5e3Sopenharmony_ci 2864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 2874514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 2884514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined()); 2894514f5e3Sopenharmony_ci 2904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 2914514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 2924514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 2934514f5e3Sopenharmony_ci EXPECT_STREQ("[1,3.6,\"abc\"]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2944514f5e3Sopenharmony_ci} 2954514f5e3Sopenharmony_ci 2964514f5e3Sopenharmony_ci/** 2974514f5e3Sopenharmony_ci * @tc.name: Stringify_008 2984514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "Stringify" function is within expectations 2994514f5e3Sopenharmony_ci * the first parameter of the JSObject,the second parameter is Undefined,the third parameter 3004514f5e3Sopenharmony_ci * is Undefined.This situation will stringize the first parameter through "SerializePrimitiveRef" 3014514f5e3Sopenharmony_ci * function. 3024514f5e3Sopenharmony_ci * @tc.type: FUNC 3034514f5e3Sopenharmony_ci * @tc.require: 3044514f5e3Sopenharmony_ci */ 3054514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_008) 3064514f5e3Sopenharmony_ci{ 3074514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3084514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 3094514f5e3Sopenharmony_ci 3104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 3114514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("\"\\\b\f\n\r\t")); 3124514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> handlePrimitiveRef = factory->NewJSString(handleStr, undefined); 3134514f5e3Sopenharmony_ci JSHandle<JSObject> handleObj(thread, handlePrimitiveRef.GetTaggedValue()); 3144514f5e3Sopenharmony_ci 3154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue()); 3164514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 3174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined()); 3184514f5e3Sopenharmony_ci 3194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 3204514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 3214514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 3224514f5e3Sopenharmony_ci EXPECT_STREQ("\"\\\"\\\\\\b\\f\\n\\r\\t\"", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3234514f5e3Sopenharmony_ci} 3244514f5e3Sopenharmony_ci 3254514f5e3Sopenharmony_cistatic void* Detach(void *param1, void *param2, void *hint, void *detachData) 3264514f5e3Sopenharmony_ci{ 3274514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "detach is running"; 3284514f5e3Sopenharmony_ci if (param1 == nullptr && param2 == nullptr) { 3294514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "detach: two params is nullptr"; 3304514f5e3Sopenharmony_ci } 3314514f5e3Sopenharmony_ci if (hint == nullptr && detachData) { 3324514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "detach: hint is nullptr"; 3334514f5e3Sopenharmony_ci } 3344514f5e3Sopenharmony_ci return nullptr; 3354514f5e3Sopenharmony_ci} 3364514f5e3Sopenharmony_ci 3374514f5e3Sopenharmony_cistatic void* Attach([[maybe_unused]] void *enginePointer, [[maybe_unused]] void *buffer, [[maybe_unused]] void *hint, 3384514f5e3Sopenharmony_ci [[maybe_unused]] void *attachData) 3394514f5e3Sopenharmony_ci{ 3404514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "attach is running"; 3414514f5e3Sopenharmony_ci return nullptr; 3424514f5e3Sopenharmony_ci} 3434514f5e3Sopenharmony_ci 3444514f5e3Sopenharmony_cistatic panda::JSNApi::NativeBindingInfo* CreateNativeBindingInfo(void* attach, void* detach) 3454514f5e3Sopenharmony_ci{ 3464514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "CreateNativeBindingInfo"; 3474514f5e3Sopenharmony_ci auto info = panda::JSNApi::NativeBindingInfo::CreateNewInstance(); 3484514f5e3Sopenharmony_ci info->attachFunc = attach; 3494514f5e3Sopenharmony_ci info->detachFunc = detach; 3504514f5e3Sopenharmony_ci return info; 3514514f5e3Sopenharmony_ci} 3524514f5e3Sopenharmony_ci 3534514f5e3Sopenharmony_ciHWTEST_F_L0(JsonStringifierTest, Stringify_009) 3544514f5e3Sopenharmony_ci{ 3554514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3564514f5e3Sopenharmony_ci JsonStringifier stringifier(thread); 3574514f5e3Sopenharmony_ci 3584514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 3594514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 3604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction()); 3614514f5e3Sopenharmony_ci JSHandle<JSObject> jsObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc)); 3624514f5e3Sopenharmony_ci EXPECT_TRUE(*jsObject != nullptr); 3634514f5e3Sopenharmony_ci 3644514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(factory->NewFromASCII("key1")); 3654514f5e3Sopenharmony_ci auto info = CreateNativeBindingInfo(reinterpret_cast<void*>(Attach), reinterpret_cast<void*>(Detach)); 3664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value1(factory->NewJSNativePointer(reinterpret_cast<void*>(info))); 3674514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), key1, value1); 3684514f5e3Sopenharmony_ci 3694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key2")); 3704514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(factory->NewFromASCII("abc")); 3714514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), key2, value2); 3724514f5e3Sopenharmony_ci 3734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleValue(thread, jsObject.GetTaggedValue()); 3744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined()); 3754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined()); 3764514f5e3Sopenharmony_ci 3774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); 3784514f5e3Sopenharmony_ci EXPECT_TRUE(resultString->IsString()); 3794514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(resultString); 3804514f5e3Sopenharmony_ci EXPECT_STREQ("{\"key1\":{},\"key2\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3814514f5e3Sopenharmony_ci} 3824514f5e3Sopenharmony_ci} // namespace panda::test 383