From 66edb501b1472de90c39518f10940f09dfb8d199 Mon Sep 17 00:00:00 2001 From: mahaifeng Date: Fri, 6 Sep 2024 16:44:03 +0800 Subject: [PATCH] =?UTF-8?q?[json]=E5=8E=BB=E9=99=A4=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E4=B8=AD=E6=89=8B=E5=8A=A8=E7=94=9F=E6=88=90=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/JSON.uts | 177 +++++++++++++++----------- 1 file changed, 105 insertions(+), 72 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/JSON.uts b/uni_modules/uts-tests/utssdk/JSON.uts index ec183d7..68f2153 100644 --- a/uni_modules/uts-tests/utssdk/JSON.uts +++ b/uni_modules/uts-tests/utssdk/JSON.uts @@ -23,16 +23,16 @@ type PersonJSON = { } -function countOccurrences(str:string, substr:string):number { - +function countOccurrences(str : string, substr : string) : number { + let count = 0 let position = 0 while (true) { - position = str.indexOf(substr, position) - if (position == -1) break - count++ - position += substr.length + position = str.indexOf(substr, position) + if (position == -1) break + count++ + position += substr.length } return count; @@ -42,8 +42,17 @@ function countOccurrences(str:string, substr:string):number { export function testJSON() : Result { return describe("JSON", () => { test('parse', () => { + // #TEST JSON.parse_tip,JSON.parse const json = `{"result":true, "count":42}`; const obj = JSON.parse(json) as UTSJSONObject; + console.log(obj["count"]); + // expected output: 42 + + console.log(obj["result"]); + // expected output: true + // #END + + expect(obj["count"]).toEqual(42); expect(obj["result"] as boolean).toEqual(true); @@ -60,9 +69,10 @@ export function testJSON() : Result { // }); expect((obj1! as UTSJSONObject).getString('name')).toEqual("John") - + // #TEST JSON.parse_1 const json2 = '{"string":"Hello","number":42,"boolean":true,"nullValue":null,"array":[1,2,3],"object":{"nestedKey":"nestedValue"}}'; const obj2 = JSON.parse(json2)!; + // #END // expect(obj2).toEqual({ // string: 'Hello', // number: 42, @@ -131,18 +141,24 @@ export function testJSON() : Result { }) test('parseObject', () => { + // #TEST JSON.parseObject const json = `{"result":true, "count":42}`; const obj = JSON.parseObject(json); + console.log(obj?.["count"])//42 + // #END + expect(obj?.["count"]).toEqual(42); expect(obj?.["result"] as boolean).toEqual(true); expect(JSON.parseObject('{}')!).toEqual({}); - + // #TEST JSON.parseObject_1 const json1 = `{ "name": "John", "id": "30" }`; let obj2 = JSON.parseObject(json1); + console.log(obj2!.id) //30 + // #END expect(obj2!.id).toEqual("30"); const json2 = `{ @@ -157,8 +173,11 @@ export function testJSON() : Result { expect(obj4).toEqual(null); }) test('parseArray', () => { + // #TEST JSON.parseArray const json1 = `[1,2,3]`; const array1 = JSON.parseArray(json1); + console.log(array1)//[1, 2, 3] + // #END expect(array1).toEqual([1, 2, 3]); const json2 = `[1,"hello world",3]`; @@ -167,82 +186,85 @@ export function testJSON() : Result { // #ifdef APP-ANDROID + // #TEST JSON.parseArray_1 const json3 = `[{"name":"John","id":"30"},{"name":"jack","id":"21"}]`; const array3 = JSON.parseArray(json3); + console.log((array3![0])["name"])//"John" + // #END expect((array3![0])["name"]).toEqual("John"); // #endif }) - + test('merge-test-1', () => { // #ifdef APP-ANDROID - const data1 = { - name: 'Tom1', - age: 21 - }; - - const data2 = { - aa: { - name: 'Tom2', - age: 22, - bb: { - name: 'Tom3', - age: 23 - } - } + const data1 = { + name: 'Tom1', + age: 21 + }; + + const data2 = { + aa: { + name: 'Tom2', + age: 22, + bb: { + name: 'Tom3', + age: 23 + } } - const obj = Object.assign(JSON.parse(JSON.stringify(data2))!, JSON.parse(JSON.stringify(data1))!) as UTSJSONObject; - const innerObj = obj.getJSON("aa.bb") - expect(innerObj instanceof UTSJSONObject).toEqual(true); + } + const obj = Object.assign(JSON.parse(JSON.stringify(data2))!, JSON.parse(JSON.stringify(data1))!) as UTSJSONObject; + const innerObj = obj.getJSON("aa.bb") + expect(innerObj instanceof UTSJSONObject).toEqual(true); // #endif }) test('stringify-with-replacer', () => { // #ifdef APP-ANDROID - let a = JSON.stringify({"x":111,"y":"aaa"}) + let a = JSON.stringify({ "x": 111, "y": "aaa" }) expect(a).toEqual('{"x":111,"y":"aaa"}'); - let a1 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ - if(key == "x"){ - return "x" - } - return value + let a1 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null { + if (key == "x") { + return "x" + } + return value }) expect(a1).toEqual('{"x":"x","y":"aaa"}'); - let a2 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ - if(key == "x"){ - return "x" - } - return value - },6) + let a2 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null { + if (key == "x") { + return "x" + } + return value + }, 6) expect(a2.length).toEqual(36); - let a3 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ - if(key == "x"){ - return "x" - } - return value - },11) + let a3 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null { + if (key == "x") { + return "x" + } + return value + }, 11) expect(a3.length).toEqual(44); - - let a4 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ - if(key == "x"){ - return "x" - } - return value - },-11) + + let a4 = JSON.stringify({ "x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null { + if (key == "x") { + return "x" + } + return value + }, -11) expect(a4.length).toEqual(19); - - let a5 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'z']) + + let a5 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'z']) expect(a5).toEqual('{"x":111,"z":{"x":123}}'); - - let a6 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) + + let a6 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z']) expect(a6).toEqual('{"x":111,"y":"aaa","z":{"x":123}}'); - let a7 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],8) + let a7 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z'], 8) expect(a7.length).toEqual(91); - - let a8 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],"99999") - + + let a8 = JSON.stringify({ "x": 111, "y": "aaa", "z": { "x": 123 } }, ["x", 'y', 'z'], "99999") + expect(a8.length).toEqual(73); - expect(countOccurrences(a8,"99999")).toEqual(6); - - + expect(countOccurrences(a8, "99999")).toEqual(6); + + // #endif }) test('stringify', () => { @@ -251,9 +273,20 @@ export function testJSON() : Result { // const obj1 = { name: 'John', age: 30, address: { city: 'New York', country: 'USA' } }; // const json1 = JSON.stringify(obj1); // expect(json1).toEqual('{"address":{"country":"USA","city":"New York"},"name":"John","age":30}'); - + // #endif - + + // #TEST JSON.stringify + console.log(JSON.stringify({ x: 5, y: 6 })); + // expected output: "{"x":5,"y":6}" + + console.log(JSON.stringify([3, 'false', boolean])); + // expected output: "[3,"false",false]" + + console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))); + // expected output: ""2006-01-02T15:04:05.000Z"" + // #END + const obj2 = ['apple', 'banana', 'cherry']; const json2 = JSON.stringify(obj2); expect(json2).toEqual('["apple","banana","cherry"]'); @@ -343,7 +376,7 @@ export function testJSON() : Result { expect(JSON.stringify(obj22)).toEqual('{"data":[{}]}') // #endif - + type A = { inserted : number } @@ -384,7 +417,7 @@ export function testJSON() : Result { a: 1 }.toMap() expect(map.get('a')).toEqual(1) - + const json = `{"result":true, "count":42}`; const obj = JSON.parse(json) as UTSJSONObject; let retStr = JSON.stringify(obj) @@ -395,7 +428,7 @@ export function testJSON() : Result { test('parse Map', () => { type A = { - num: number, + num : number, } const map = JSON.parse>(`{"a": {"num": 1}}`) // #ifndef APP-IOS @@ -407,18 +440,18 @@ export function testJSON() : Result { test('parse generic type', () => { // #ifndef APP-ANDROID type A = { - num: number, - } - function test(json: string): T { + num : number, + } + function test(json : string) : T { return JSON.parse(json)! } - + // #ifndef APP-IOS const a = test(`{"num": 1}`) expect(a.num).toEqual(1) // #endif // #endif - + }) }) -- GitLab