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



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 15 16 17 18 19 20 21
        console.log(UTSJSONObject.keys(obj))
    })
    
    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
杜庆泉 已提交
22
        expect(returnedTarget.toMap().size).toEqual(3);
杜庆泉's avatar
杜庆泉 已提交
23 24
        
        type A = {
25 26
        	username:string
        	age:number
杜庆泉's avatar
杜庆泉 已提交
27 28
        }
        type C = {
29
        	work:number
杜庆泉's avatar
杜庆泉 已提交
30
        }
31 32 33 34 35 36 37 38 39
       
        let b = UTSJSONObject.assign( {
          username:"张三",
          age:12
        } as A,
        {b:2},
        {
          work:0.002
        } as C)
杜庆泉's avatar
杜庆泉 已提交
40 41 42 43
        expect(b['age']).toEqual(12);
        expect(b['username']).toEqual("张三");
        expect(b['b']).toEqual(2);
        expect(b['work']).toEqual(0.002);
44 45 46
      // #endif
    })
    
47
    test('setvalue-after-get', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
48
      // #ifdef APP-ANDROID
49 50 51 52 53 54 55 56
        let obj = {
          "cars":[
            {
              name:"car1",
              value:100
            }
          ]
        }
lizhongyi_'s avatar
lizhongyi_ 已提交
57
            
58 59 60 61
        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_ 已提交
62
        // #endif
63 64
    })
    
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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
    })
  })
}