UTSJSONObject.uts 3.2 KB
Newer Older
1
import { describe, test, expect, expectNumber, Result } from './tests.uts'
2
import {mockData} from './data.uts'
3 4 5 6 7 8 9 10 11


export function testUTSJSONObject() : Result {
  return describe("utsjsonobject", () => {
    test('keys', () => {
        let obj = {
          name:"zhangsan",
          age:11
        }
雪洛's avatar
雪洛 已提交
12
        expect(UTSJSONObject.keys(obj).length).toEqual(2);
13 14
        expect(UTSJSONObject.keys(mockData).length).toEqual(2);
        
15 16
    })
    
杜庆泉's avatar
杜庆泉 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    test('toJSONObject', () => {
      // #ifdef APP-ANDROID
        let result = {}
        result["opt"] = "xxxx"
        
        let subArray = [] as Array<UTSJSONObject>
        let subModel = {}
        subModel["name"] = "xxx"
        subModel["type"] = 0
        subArray.push(subModel)
        
        let service = {}
        service["name"] = "0xxxb34fb"
        service["type"] = 0
        
        //这个数据返回为空对象数组
        service["array"] = subArray
        
        result["service"] = service
        
        expect(result.toJSONObject().toJSONString()).toEqual('{"opt":"xxxx","service":{"array":[{"name":"xxx","type":0}],"name":"0xxxb34fb","type":0}}');
        
      // #endif
    })
    
42 43 44 45 46 47
    test('assign-notype', () => {
      // #ifdef APP-ANDROID
        const target = { a: 1, b: 2 };
        const source = { b: 4, c: 5 };
        const returnedTarget = UTSJSONObject.assign(target, source);
        
杜庆泉's avatar
杜庆泉 已提交
48
        expect(returnedTarget.toMap().size).toEqual(3);
杜庆泉's avatar
杜庆泉 已提交
49 50
        
        type A = {
51 52
        	username:string
        	age:number
杜庆泉's avatar
杜庆泉 已提交
53 54
        }
        type C = {
55
        	work:number
杜庆泉's avatar
杜庆泉 已提交
56
        }
57 58 59 60 61 62 63 64 65
       
        let b = UTSJSONObject.assign( {
          username:"张三",
          age:12
        } as A,
        {b:2},
        {
          work:0.002
        } as C)
杜庆泉's avatar
杜庆泉 已提交
66 67 68 69
        expect(b['age']).toEqual(12);
        expect(b['username']).toEqual("张三");
        expect(b['b']).toEqual(2);
        expect(b['work']).toEqual(0.002);
70 71 72
      // #endif
    })
    
73
    test('setvalue-after-get', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
74
      // #ifdef APP-ANDROID
75 76 77 78 79 80 81 82
        let obj = {
          "cars":[
            {
              name:"car1",
              value:100
            }
          ]
        }
lizhongyi_'s avatar
lizhongyi_ 已提交
83
            
84 85 86 87
        let cars = obj.getArray<UTSJSONObject>("cars")
        cars![0].set("value",20)
        let firstCar = obj.getJSON("cars[0]")
        expect(firstCar!['value']).toEqual(20);
lizhongyi_'s avatar
lizhongyi_ 已提交
88
        // #endif
89 90
    })
    
杜庆泉's avatar
杜庆泉 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    
    test('get-speed', () => {
      // #ifdef APP-ANDROID
        const utsObj: UTSJSONObject = {} as any as UTSJSONObject
        for (let i = 0; i < 100; i++) {
          utsObj.set('' + i, '' + i)
        }
        
        console.log('--start--')
        let startTime = Date.now()
        for (let i = 0; i < 10000; i++) {
          utsObj.getString('0')
        }
        let spendTime = Date.now() - startTime
        expect(spendTime < 300).toEqual(true);
        // #endif
    })
    
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    test('assign-withtype', () => {
      // #ifdef APP-ANDROID
        type User = {
          a:number
          b:number
        }
        const target = { a: 1, b: 2 };
        const source = { b: 4, c: 5 };
        const returnedTarget = UTSJSONObject.assign<User>(target, source);
        
        expect(returnedTarget!.a).toEqual(1);
        console.log(returnedTarget)
      // #endif
    })
  })
}