• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_stringifier.h"
17 #include "ecmascript/js_array.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 JsonStringifierTest : 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 
CreateBaseJSObject(JSThread * thread,const CString keyCStr)51 static JSTaggedValue CreateBaseJSObject(JSThread *thread, const CString keyCStr)
52 {
53     EcmaVM *ecmaVM = thread->GetEcmaVM();
54     JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
55     ObjectFactory *factory = ecmaVM->GetFactory();
56     JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction());
57 
58     JSHandle<JSObject> jsObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc));
59     EXPECT_TRUE(*jsObject != nullptr);
60 
61     JSHandle<JSTaggedValue> handleKey1(factory->NewFromASCII(&keyCStr[0]));
62     JSHandle<JSTaggedValue> handleValue1(thread, JSTaggedValue(1)); // 1 : test case
63     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey1, handleValue1);
64 
65     CString str2 = "x";
66     JSHandle<JSTaggedValue> handleKey2(factory->NewFromASCII(str2));
67     JSHandle<JSTaggedValue> handleValue2(thread, JSTaggedValue(3.6)); // 3.6 : test case
68     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey2, handleValue2);
69 
70     CString str3 = "y";
71     JSHandle<JSTaggedValue> handleKey3(factory->NewFromASCII(str3));
72     JSHandle<JSTaggedValue> handleValue3(factory->NewFromASCII("abc"));
73     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsObject), handleKey3, handleValue3);
74 
75     return jsObject.GetTaggedValue();
76 }
77 
TestForStringfy1(EcmaRuntimeCallInfo * argv)78 static JSTaggedValue TestForStringfy1([[maybe_unused]] EcmaRuntimeCallInfo *argv)
79 {
80     // false: test case
81     return JSTaggedValue(JSTaggedValue::False());
82 }
83 
TestForStringfy2(EcmaRuntimeCallInfo * argv)84 static JSTaggedValue TestForStringfy2([[maybe_unused]] EcmaRuntimeCallInfo *argv)
85 {
86     // 10.12: test case
87     return JSTaggedValue(10.12);
88 }
89 
90 /**
91  * @tc.name: Stringify_001
92  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
93  *           the first parameter of the ECMAObject,the second parameter is JSFunction,the third parameter
94  *           is Undefined.if the second parameter is JSFunction,the return value is the parameter stringification
95  *           after through "call" function.
96  * @tc.type: FUNC
97  * @tc.require:
98  */
HWTEST_F_L0(JsonStringifierTest,Stringify_001)99 HWTEST_F_L0(JsonStringifierTest, Stringify_001)
100 {
101     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
102     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
103 
104     JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
105     JSHandle<JSFunction> handleFunc1 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy1));
106     JSHandle<JSFunction> handleFunc2 = factory->NewJSFunction(env, reinterpret_cast<void *>(TestForStringfy2));
107     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
108     JSHandle<JSTaggedValue> handleReplacer1(thread, handleFunc1.GetTaggedValue());
109     JSHandle<JSTaggedValue> handleReplacer2(thread, handleFunc2.GetTaggedValue());
110     JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
111 
112     JsonStringifier stringifier1(thread);
113     JSHandle<JSTaggedValue> resultString1 = stringifier1.Stringify(handleValue, handleReplacer1, handleGap);
114     EXPECT_TRUE(resultString1->IsString());
115     JSHandle<EcmaString> handleEcmaStr1(resultString1);
116     EXPECT_STREQ("false", EcmaStringAccessor(handleEcmaStr1).ToCString().c_str());
117 
118     JsonStringifier stringifier2(thread);
119     JSHandle<JSTaggedValue> resultString2 = stringifier2.Stringify(handleValue, handleReplacer2, handleGap);
120     EXPECT_TRUE(resultString2->IsString());
121     JSHandle<EcmaString> handleEcmaStr2(resultString2);
122     EXPECT_STREQ("10.12", EcmaStringAccessor(handleEcmaStr2).ToCString().c_str());
123 }
124 
125 /**
126  * @tc.name: Stringify_002
127  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
128  *           the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
129  *           is Number.This situation will stringize parameters through "SerializeJSONObject" function.
130  * @tc.type: FUNC
131  * @tc.require:
132  */
HWTEST_F_L0(JsonStringifierTest,Stringify_002)133 HWTEST_F_L0(JsonStringifierTest, Stringify_002)
134 {
135     JsonStringifier stringifier(thread);
136 
137     JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
138     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
139     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
140     JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue(static_cast<int32_t>(10)));
141 
142     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
143     EXPECT_TRUE(resultString->IsString());
144     JSHandle<EcmaString> handleEcmaStr(resultString);
145     EXPECT_STREQ("{\n          \"z\": 1,\n          \"x\": 3.6,\n          \"y\": \"abc\"\n}",
146                                                      EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
147 }
148 
149 /**
150  * @tc.name: Stringify_003
151  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
152  *           the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
153  *           is String,This situation will stringize parameters through "SerializeJSONObject" function.
154  * @tc.type: FUNC
155  * @tc.require:
156  */
HWTEST_F_L0(JsonStringifierTest,Stringify_003)157 HWTEST_F_L0(JsonStringifierTest, Stringify_003)
158 {
159     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
160     JsonStringifier stringifier(thread);
161 
162     JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
163     JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
164     JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
165 
166     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
167     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
168     JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
169 
170     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
171     EXPECT_TRUE(resultString->IsString());
172     JSHandle<EcmaString> resultStr =
173         factory->NewFromASCII("{\ntttt\"z\": 1,\ntttt\"x\": 3.6,\ntttt\"y\": \"abc\"\n}");
174     EXPECT_EQ(EcmaStringAccessor::Compare(*resultStr, reinterpret_cast<EcmaString *>(resultString->GetRawData())), 0);
175 }
176 
177 /**
178  * @tc.name: Stringify_004
179  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
180  *           the first parameter of the ECMAObject,the second parameter is JSArray,the third parameter
181  *           is String.This situation will stringize parameters through "SerializeJSONObject" function.
182  * @tc.type: FUNC
183  * @tc.require:
184  */
HWTEST_F_L0(JsonStringifierTest,Stringify_004)185 HWTEST_F_L0(JsonStringifierTest, Stringify_004)
186 {
187     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
188     JsonStringifier stringifier(thread);
189 
190     JSHandle<JSTaggedValue> handleObj1 = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
191 
192     JSArray *handleArr =
193         JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
194     JSHandle<JSObject> handleObj2(thread, handleArr);
195     JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
196     JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("z"));
197     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey0, handleValue0);
198 
199     JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
200     JSHandle<JSTaggedValue> handleValue1(factory->NewFromASCII("x"));
201     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj2), handleKey1, handleValue1);
202 
203     JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
204     JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
205 
206     JSHandle<JSTaggedValue> handleValue(thread, handleObj1.GetTaggedValue());
207     JSHandle<JSTaggedValue> handleReplacer(thread, handleObj2.GetTaggedValue());
208     JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
209 
210     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
211     EXPECT_TRUE(resultString->IsString());
212     JSHandle<EcmaString> handleEcmaStr(resultString);
213     EXPECT_STREQ("{\ntttt\"z\": 1,\ntttt\"x\": 3.6\n}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
214 }
215 
216 /**
217  * @tc.name: Stringify_005
218  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
219  *           the first parameter of the ECMAObject,the second parameter is Undefined,the third parameter
220  *           is Undefined.This situation will stringize the first parameter through "SerializeJSONObject" function.
221  * @tc.type: FUNC
222  * @tc.require:
223  */
HWTEST_F_L0(JsonStringifierTest,Stringify_005)224 HWTEST_F_L0(JsonStringifierTest, Stringify_005)
225 {
226     JsonStringifier stringifier(thread);
227     JSHandle<JSTaggedValue> handleObj = JSHandle<JSTaggedValue>(thread, CreateBaseJSObject(thread, "z"));
228 
229     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
230     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
231     JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
232 
233     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
234     EXPECT_TRUE(resultString->IsString());
235     JSHandle<EcmaString> handleEcmaStr(resultString);
236     EXPECT_STREQ("{\"z\":1,\"x\":3.6,\"y\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
237 }
238 
239 /**
240  * @tc.name: Stringify_006
241  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
242  *           the first parameter of the JSArray,the second parameter is Undefined,the third parameter
243  *           is String,This situation will stringize parameters through "SerializeJSArray" function.
244  * @tc.type: FUNC
245  * @tc.require:
246  */
HWTEST_F_L0(JsonStringifierTest,Stringify_006)247 HWTEST_F_L0(JsonStringifierTest, Stringify_006)
248 {
249     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
250     JsonStringifier stringifier(thread);
251 
252     JSArray *handleArr =
253         JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
254     JSHandle<JSObject> handleObj(thread, handleArr);
255 
256     JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
257     JSHandle<JSTaggedValue> handleValue0(factory->NewFromASCII("json"));
258     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey0, handleValue0);
259 
260     JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
261     PropertyDescriptor handleDesc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(100)), true, true, true);
262     JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc);
263 
264     JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2));
265     JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc"));
266     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2);
267 
268     JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("tttt"));
269     JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg));
270 
271     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
272     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
273     JSHandle<JSTaggedValue> handleGap(thread, handleStr.GetTaggedValue());
274 
275     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
276     EXPECT_TRUE(resultString->IsString());
277     JSHandle<EcmaString> handleEcmaStr(resultString);
278     EXPECT_STREQ("[\ntttt\"json\",\ntttt100,\ntttt\"abc\"\n]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
279 }
280 
281 /**
282  * @tc.name: Stringify_007
283  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
284  *           the first parameter of the JSObject,the second parameter is Undefined,the third parameter
285  *           is Undefined.This situation will stringize the first parameter through "SerializeJSArray" function.
286  * @tc.type: FUNC
287  * @tc.require:
288  */
HWTEST_F_L0(JsonStringifierTest,Stringify_007)289 HWTEST_F_L0(JsonStringifierTest, Stringify_007)
290 {
291     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
292     JsonStringifier stringifier(thread);
293 
294     JSArray *handleArr =
295         JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject());
296     JSHandle<JSObject> handleObj(thread, handleArr);
297 
298     JSHandle<JSTaggedValue> handleKey0(thread, JSTaggedValue(0));
299     PropertyDescriptor handleDesc0(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), true, true, true);
300     JSArray::DefineOwnProperty(thread, handleObj, handleKey0, handleDesc0);
301 
302     JSHandle<JSTaggedValue> handleKey1(thread, JSTaggedValue(1));
303     PropertyDescriptor handleDesc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(3.6)), true, true, true);
304     JSArray::DefineOwnProperty(thread, handleObj, handleKey1, handleDesc1);
305 
306     JSHandle<JSTaggedValue> handleKey2(thread, JSTaggedValue(2));
307     JSHandle<JSTaggedValue> handleValue2(factory->NewFromASCII("abc"));
308     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(handleObj), handleKey2, handleValue2);
309 
310     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
311     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
312     JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
313 
314     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
315     EXPECT_TRUE(resultString->IsString());
316     JSHandle<EcmaString> handleEcmaStr(resultString);
317     EXPECT_STREQ("[1,3.6,\"abc\"]", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
318 }
319 
320 /**
321  * @tc.name: Stringify_008
322  * @tc.desc: Check whether the result returned through "Stringify" function is within expectations
323  *           the first parameter of the JSObject,the second parameter is Undefined,the third parameter
324  *           is Undefined.This situation will stringize the first parameter through "SerializePrimitiveRef"
325  *           function.
326  * @tc.type: FUNC
327  * @tc.require:
328  */
HWTEST_F_L0(JsonStringifierTest,Stringify_008)329 HWTEST_F_L0(JsonStringifierTest, Stringify_008)
330 {
331     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
332     JsonStringifier stringifier(thread);
333 
334     JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("\"\\\b\f\n\r\t"));
335     JSHandle<JSPrimitiveRef> handlePrimitiveRef = factory->NewJSString(handleStr);
336     JSHandle<JSObject> handleObj(thread, handlePrimitiveRef.GetTaggedValue());
337 
338     JSHandle<JSTaggedValue> handleValue(thread, handleObj.GetTaggedValue());
339     JSHandle<JSTaggedValue> handleReplacer(thread, JSTaggedValue::Undefined());
340     JSHandle<JSTaggedValue> handleGap(thread, JSTaggedValue::Undefined());
341 
342     JSHandle<JSTaggedValue> resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap);
343     EXPECT_TRUE(resultString->IsString());
344     JSHandle<EcmaString> handleEcmaStr(resultString);
345     EXPECT_STREQ("\"\\\"\\\\\\b\\f\\n\\r\\t\"", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
346 }
347 }  // namespace panda::test
348