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 25 26 27 28 29 30 31 32 33 34 35
        
        type A = {
        	username:"张三",
        	age:12
        }
        type C = {
        	work:0.001
        }
        let b = UTSJSONObject.assign(new A("张三",12),{b:2},new C(0.002))
        expect(b['age']).toEqual(12);
        expect(b['username']).toEqual("张三");
        expect(b['b']).toEqual(2);
        expect(b['work']).toEqual(0.002);
36 37 38
      // #endif
    })
    
39
    test('setvalue-after-get', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
40
      // #ifdef APP-ANDROID
41 42 43 44 45 46 47 48
        let obj = {
          "cars":[
            {
              name:"car1",
              value:100
            }
          ]
        }
lizhongyi_'s avatar
lizhongyi_ 已提交
49
            
50 51 52 53
        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_ 已提交
54
        // #endif
55 56
    })
    
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    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
    })
  })
}