提交 b4a9a16b 编写于 作者: 杜庆泉's avatar 杜庆泉

增加自定制输出 JSON.stringify的示例

上级 1ec178eb
......@@ -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
......
......@@ -2,7 +2,7 @@ import { Matchers,NumberMatchers } from './Matchers.uts'
export const describes = new Map<string, Result>()
export class Result implements io.dcloud.uts.json.IJsonStringify {
export class Result implements IJsonStringify {
total = 0
passed: string[] = []
failed: string[] = []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册