From b4a9a16b63709599cabe64a6bdd4dd0c7d6fda79 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Sat, 21 Sep 2024 13:00:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E5=88=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=20JSON.stringify=E7=9A=84=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/JSON.uts | 54 +++++++++++++++++++++++++- uni_modules/uts-tests/utssdk/tests.uts | 2 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/JSON.uts b/uni_modules/uts-tests/utssdk/JSON.uts index 32cb2da..cdf8917 100644 --- a/uni_modules/uts-tests/utssdk/JSON.uts +++ b/uni_modules/uts-tests/utssdk/JSON.uts @@ -38,9 +38,59 @@ function countOccurrences(str : string, substr : string) : number { return count; } +class A1 implements IJsonStringify{ + override toJSON():any|null{ + let jsonRet = { + 'name': "zhangsan", + 'age': 12, + } + return jsonRet + } +} + +class A2 implements IJsonStringify{ + override toJSON():any|null{ + return 2 + } +} + +class A3 implements IJsonStringify{ + override toJSON():any|null{ + return "json" + } +} + +class A4 implements IJsonStringify{ + override toJSON():any|null{ + return null + } +} + +class A5 implements IJsonStringify{ + override toJSON():any|null{ + return new A1() + } +} + +class A6 implements IJsonStringify{ + override toJSON():any|null{ + return new A5() + } +} + export function testJSON() : Result { return describe("JSON", () => { + + test('custom-stringify', () => { + expect(JSON.stringify(new A1()).length).toEqual(28) + expect(JSON.stringify(new A2())).toEqual("2") + expect(JSON.stringify(new A3())).toEqual('"json"') + expect(JSON.stringify(new A4())).toEqual('null') + expect(JSON.stringify(new A5()).length).toEqual(28) + expect(JSON.stringify(new A6()).length).toEqual(28) + }) + test('parse', () => { // #TEST JSON.parse_tip,JSON.parse const json = `{"result":true, "count":42}`; @@ -264,9 +314,11 @@ export function testJSON() : Result { expect(a8.length).toEqual(73); expect(countOccurrences(a8, "99999")).toEqual(6); - // #endif }) + + + test('stringify', () => { // #ifdef APP-ANDROID diff --git a/uni_modules/uts-tests/utssdk/tests.uts b/uni_modules/uts-tests/utssdk/tests.uts index 27b1136..fc6457d 100644 --- a/uni_modules/uts-tests/utssdk/tests.uts +++ b/uni_modules/uts-tests/utssdk/tests.uts @@ -2,7 +2,7 @@ import { Matchers,NumberMatchers } from './Matchers.uts' export const describes = new Map() -export class Result implements io.dcloud.uts.json.IJsonStringify { +export class Result implements IJsonStringify { total = 0 passed: string[] = [] failed: string[] = [] -- GitLab