Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
6ee709d9
H
Hello UTS
项目概览
DCloud
/
Hello UTS
通知
1593
Star
27
Fork
9
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
Hello UTS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6ee709d9
编写于
9月 06, 2024
作者:
M
mahaifeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[json]去除文档中手动生成的代码,添加注释
上级
a5bd6e13
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
105 addition
and
72 deletion
+105
-72
uni_modules/uts-tests/utssdk/JSON.uts
uni_modules/uts-tests/utssdk/JSON.uts
+105
-72
未找到文件。
uni_modules/uts-tests/utssdk/JSON.uts
浏览文件 @
6ee709d9
...
...
@@ -23,7 +23,7 @@ type PersonJSON = {
}
function countOccurrences(str
:string, substr:string):
number {
function countOccurrences(str
: string, substr : string) :
number {
let count = 0
let position = 0
...
...
@@ -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<UTSJSONObject>(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<UserJSON>(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,8 +186,11 @@ 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<UTSJSONObject>(json3);
console.log((array3![0])["name"])//"John"
// #END
expect((array3![0])["name"]).toEqual("John");
// #endif
})
...
...
@@ -197,50 +219,50 @@ export function testJSON() : Result {
})
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")
{
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")
{
let a2 = JSON.stringify({
"x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null
{
if (key == "x")
{
return "x"
}
return value
},6)
},
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")
{
let a3 = JSON.stringify({
"x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null
{
if (key == "x")
{
return "x"
}
return value
},11)
},
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")
{
let a4 = JSON.stringify({
"x": 111, "y": "aaa" }, function (key : any, value : any | null) : any | null
{
if (key == "x")
{
return "x"
}
return value
},-11)
},
-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
...
...
@@ -254,6 +276,17 @@ export function testJSON() : Result {
// #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"]');
...
...
@@ -395,7 +428,7 @@ export function testJSON() : Result {
test('parse Map', () => {
type A = {
num: number,
num
: number,
}
const map = JSON.parse<Map<string, A>>(`{"a": {"num": 1}}`)
// #ifndef APP-IOS
...
...
@@ -407,9 +440,9 @@ export function testJSON() : Result {
test('parse generic type', () => {
// #ifndef APP-ANDROID
type A = {
num: number,
num
: number,
}
function test<T>(json
: string)
: T {
function test<T>(json
: string)
: T {
return JSON.parse<T>(json)!
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录