import { describe, test, expect, expectNumber, Result } from './tests.uts' export function testUTSJSONObject() : Result { return describe("utsjsonobject", () => { test('keys', () => { let obj = { name:"zhangsan", age:11 } expect(UTSJSONObject.keys(obj).length).toEqual(2); console.log(UTSJSONObject.keys(obj)) }) test('toJSONObject', () => { // #ifdef APP-ANDROID let result = {} result["opt"] = "xxxx" let subArray = [] as Array 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 }) test('assign-notype', () => { // #ifdef APP-ANDROID const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 }; const returnedTarget = UTSJSONObject.assign(target, source); expect(returnedTarget.toMap().size).toEqual(3); type A = { username:string age:number } type C = { work:number } let b = UTSJSONObject.assign( { username:"张三", age:12 } as A, {b:2}, { work:0.002 } as C) expect(b['age']).toEqual(12); expect(b['username']).toEqual("张三"); expect(b['b']).toEqual(2); expect(b['work']).toEqual(0.002); // #endif }) test('setvalue-after-get', () => { // #ifdef APP-ANDROID let obj = { "cars":[ { name:"car1", value:100 } ] } let cars = obj.getArray("cars") cars![0].set("value",20) let firstCar = obj.getJSON("cars[0]") expect(firstCar!['value']).toEqual(20); // #endif }) 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 }) 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(target, source); expect(returnedTarget!.a).toEqual(1); console.log(returnedTarget) // #endif }) }) }