JSON.uts 4.8 KB
Newer Older
Y
yurj26 已提交
1 2
import { describe, test, expect, Result } from './tests.uts'

杜庆泉's avatar
杜庆泉 已提交
3 4 5 6 7 8 9


class User{
	name:string = "";
	age:number = 12;
}

Y
yurj26 已提交
10 11 12 13
export function testJSON() : Result {
    return describe("JSON", () => {
        test('parse', () => {
            const json = `{"result":true, "count":42}`;
14
            const obj = JSON.parse(json) as UTSJSONObject;
Y
yurj26 已提交
15
            expect(obj["count"]).toEqual(42);
lizhongyi_'s avatar
lizhongyi_ 已提交
16
            expect(obj["result"] as boolean).toEqual(true);
Y
yurj26 已提交
17 18 19 20 21 22 23

            const json1 = `{
                "name": "John",
                "age": 30,
                "city": "New York"
              }`;
            const obj1 = JSON.parse(json1);
lizhongyi_'s avatar
lizhongyi_ 已提交
24 25 26 27 28 29
            // expect(obj1).toEqual({
            //     name: 'John',
            //     age: 30,
            //     city: 'New York',
            // });
			expect((obj1! as UTSJSONObject).getString('name')).toEqual("John")
杜庆泉's avatar
杜庆泉 已提交
30 31
			
			
Y
yurj26 已提交
32
            const json2 = '{"string":"Hello","number":42,"boolean":true,"nullValue":null,"array":[1,2,3],"object":{"nestedKey":"nestedValue"}}';
33
            const obj2 = JSON.parse<UTSJSONObject>(json2)!;
lizhongyi_'s avatar
lizhongyi_ 已提交
34 35 36 37 38 39 40 41 42 43
            // expect(obj2).toEqual({
            //     string: 'Hello',
            //     number: 42,
            //     boolean: true,
            //     nullValue: null,
            //     array: [1, 2, 3],
            //     object: {
            //         nestedKey: 'nestedValue',
            //     },
            // });
Y
yurj26 已提交
44 45
            expect(obj2['object']).toEqual({
                nestedKey: 'nestedValue',
lizhongyi_'s avatar
lizhongyi_ 已提交
46 47 48
            })
			expect(obj2.getString("object.nestedKey")).toEqual('nestedValue')
	
杜庆泉's avatar
杜庆泉 已提交
49 50 51 52 53
			
			// 目前仅android 支持,暂不放开
			// let obj3 = JSON.parse<User>(json1);
			// console.log(obj3)
			
Y
yurj26 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            // const json3 = '["apple","banana","cherry"]';
            // const obj3 = JSON.parse(json3)!;
            // TODO JSON.parse 后数组的类型 无法强转
            // expect(obj3).toEqual(['apple', 'banana', 'cherry']);

            // const json4 = '[1, "two", true, null, {"key": "value"}, ["nested"]]';
            // const obj4 = JSON.parse(json4)!;
            // expect(obj4).toEqual([1, 'two', true, null, { key: 'value' }, ['nested']]);

            // TODO 暂不支持多个参数
            // const json5 = '{"p": 5}';
            // const obj5 = JSON.parse(json5, function (k : string, v : number) : number {
            //     if (k === '') return v;
            //     return v * 2;
            // })!;
            // expect(obj5).toEqual({
            //     p: 10,
            // });

            expect(JSON.parse('{}')!).toEqual({});
            // TODO 不支持boolean、string,js端需抹平
            // expect(JSON.parse('true')!).toEqual(true);
            // expect(JSON.parse('"foo"')!).toEqual("foo");
            // expect(JSON.parse('null')!).toEqual(null);
        })
        test('stringify', () => {
            const obj = { name: 'John', age: 30 };
            const json = JSON.stringify(obj);
lizhongyi_'s avatar
lizhongyi_ 已提交
82 83
            // expect(json).toEqual('{"name":"John","age":30}');
		    console.log(json)
Y
yurj26 已提交
84 85

            const obj1 = { name: 'John', age: 30, address: { city: 'New York', country: 'USA' } };
lizhongyi_'s avatar
lizhongyi_ 已提交
86 87 88
            const json1 = JSON.stringify(obj1);
			console.log(json1)
            // expect(json1).toEqual('{"address":{"country":"USA","city":"New York"},"name":"John","age":30}');
Y
yurj26 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

            const obj2 = ['apple', 'banana', 'cherry'];
            const json2 = JSON.stringify(obj2);
            expect(json2).toEqual('["apple","banana","cherry"]');

            // TODO 暂不支持多个参数
            // const obj3 = { name: 'John', age: '30' };
            // const replacer = (key : string, value : string) : string => (key === 'name' ? value.toUpperCase() : value);
            // const json3 = JSON.stringify(obj3, replacer);
            // expect(json3).toEqual('{"name":"JOHN","age":"30"}');

            // const obj4 = { name: 'John', age: 30 };
            // const json4 = JSON.stringify(obj4, null, 4);
//             expect(json4).toEqual(`{
//     "name": "John",
//     "age": 30
// }`);
lizhongyi_'s avatar
lizhongyi_ 已提交
106 107
            // expect(JSON.stringify({ x: 5, y: 6 })).toEqual(`{"x":5,"y":6}`);
			console.log(JSON.stringify({ x: 5, y: 6 }))
Y
yurj26 已提交
108 109
            expect(JSON.stringify([3, 'false', false])).toEqual(`[3,"false",false]`);
            expect(JSON.stringify({})).toEqual('{}');
杜庆泉's avatar
杜庆泉 已提交
110 111 112
			expect(JSON.stringify(1002)).toEqual('1002');
			expect(JSON.stringify(1002.202)).toEqual('1002.202');
			expect(JSON.stringify(null)).toEqual('null');
lizhongyi_'s avatar
lizhongyi_ 已提交
113
			// expect(JSON.stringify(100/0.0)).toEqual('null');
Y
yurj26 已提交
114
            expect(JSON.stringify(true)).toEqual('true');
杜庆泉's avatar
杜庆泉 已提交
115
			expect(JSON.stringify(false)).toEqual('false');
lizhongyi_'s avatar
lizhongyi_ 已提交
116
            expect(JSON.stringify('foo')).toEqual('foo');
杜庆泉's avatar
杜庆泉 已提交
117 118
			expect(JSON.stringify(Math.PI)).toEqual('3.141592653589793');
			expect(JSON.stringify(Math.E)).toEqual('2.718281828459045');
Y
yurj26 已提交
119 120 121
        })
    })
}