提交 77ecebe3 编写于 作者: 杜庆泉's avatar 杜庆泉

增加JSON parseObject测试示例

上级 e4aa0b50
...@@ -484,7 +484,7 @@ export function logDateCombineTest(){ ...@@ -484,7 +484,7 @@ export function logDateCombineTest(){
* *
*/ */
export function quitApp(){ export function quitApp(){
UTSAndroid.quitApp() UTSAndroid.exit()
} }
export class User { export class User {
......
import { describe, test, expect, Result } from './tests.uts'
export function testExtapi() : Result {
return describe("Storage", () => {
test('setStorage', () => {
uni.setStorage({
key: "dddddd",
data: 1,
complete: (res) => {
let ret = uni.getStorageSync("dddddd")
expect(ret).toEqual(1)
uni.setStorageSync("dddddd",3.14159265358)
ret = uni.getStorageSync("dddddd")
expect(ret).toEqual(3.14159265358)
}
})
uni.setStorage({
key: "dddddd",
data: true,
complete: (res) => {
console.log(res)
let ret = uni.getStorageSync("dddddd")
expect(ret).toEqual(true)
uni.setStorageSync("dddddd",false)
ret = uni.getStorageSync("dddddd")
expect(ret).toEqual(false)
}
})
uni.setStorage({
key: "dddddd",
data: "海上生明月,天涯共此时,情人怨遥夜,竟夕起相思。",
complete: (res) => {
console.log(res)
let ret = uni.getStorageSync("dddddd")
expect(ret).toEqual("海上生明月,天涯共此时,情人怨遥夜,竟夕起相思。")
uni.setStorageSync("dddddd","床前明月光,疑似地上霜")
ret = uni.getStorageSync("dddddd")
expect(ret).toEqual("床前明月光,疑似地上霜")
}
})
// IOS 打印对象乱序,暂时注释
// uni.setStorage({
// key: "dddddd",
// data: {
// "a": 1,
// "b": "2",
// "c": {
// "x": "1",
// "y": true
// }
// },
// complete: (res) => {
// let ret = uni.getStorageSync("dddddd")
// console.log(ret)
// expect(JSON.stringify(ret)).toEqual('{"a":1,"b":"2","c":{"x":"1","y":true}}')
// }
// })
})
})
}
\ No newline at end of file
...@@ -2,9 +2,9 @@ import { describe, test, expect, Result } from './tests.uts' ...@@ -2,9 +2,9 @@ import { describe, test, expect, Result } from './tests.uts'
class User{ type UserJSON={
name:string = ""; id:string;
age:number = 12; name:string|null;
} }
export function testJSON() : Result { export function testJSON() : Result {
...@@ -73,6 +73,32 @@ export function testJSON() : Result { ...@@ -73,6 +73,32 @@ export function testJSON() : Result {
// expect(JSON.parse('"foo"')!).toEqual("foo"); // expect(JSON.parse('"foo"')!).toEqual("foo");
// expect(JSON.parse('null')!).toEqual(null); // expect(JSON.parse('null')!).toEqual(null);
}) })
test('parseObject', () => {
const json = `{"result":true, "count":42}`;
const obj = JSON.parseObject(json);
expect(obj!["count"]).toEqual(42);
expect(obj!["result"] as boolean).toEqual(true);
expect(JSON.parseObject('{}')!).toEqual({});
const json1 = `{
"name": "John",
"id": "30"
}`;
let obj2 = JSON.parseObject<UserJSON>(json1);
expect(obj2!!.id).toEqual("30");
const json2 = `{
"id": "30"
}`;
let obj3 = JSON.parseObject<UserJSON>(json2);
expect(obj3!!.id).toEqual("30");
const json3 = `{
"name": "John"
}`;
let obj4 = JSON.parseObject<UserJSON>(json3);
expect(obj4).toEqual(null);
})
test('stringify', () => { test('stringify', () => {
const obj = { name: 'John', age: 30 }; const obj = { name: 'John', age: 30 };
const json = JSON.stringify(obj); const json = JSON.stringify(obj);
...@@ -111,5 +137,10 @@ export function testJSON() : Result { ...@@ -111,5 +137,10 @@ export function testJSON() : Result {
expect(JSON.stringify(Math.PI)).toEqual('3.141592653589793'); expect(JSON.stringify(Math.PI)).toEqual('3.141592653589793');
expect(JSON.stringify(Math.E)).toEqual('2.718281828459045'); expect(JSON.stringify(Math.E)).toEqual('2.718281828459045');
}) })
}) })
} }
\ No newline at end of file
...@@ -3,6 +3,7 @@ import { testDate } from './Date.uts' ...@@ -3,6 +3,7 @@ import { testDate } from './Date.uts'
import { testString } from './String.uts' import { testString } from './String.uts'
import { testError } from './Error.uts' import { testError } from './Error.uts'
import { testKeyWord } from './KeyWord.uts' import { testKeyWord } from './KeyWord.uts'
import { testExtapi } from './Extapi.uts'
import { testJSON } from './JSON.uts' import { testJSON } from './JSON.uts'
import { testNumber } from './Number.uts' import { testNumber } from './Number.uts'
import { testMap } from './Map.uts' import { testMap } from './Map.uts'
...@@ -30,6 +31,8 @@ export function runTests() : UTSJSONObject { ...@@ -30,6 +31,8 @@ export function runTests() : UTSJSONObject {
const MathRes = testMath(); const MathRes = testMath();
const RegExpRes = testRegExp(); const RegExpRes = testRegExp();
const KeyWordRes = testKeyWord(); const KeyWordRes = testKeyWord();
const extapiRes = testExtapi();
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
// const ReactiveArrayRes = testReactiveArray(); // const ReactiveArrayRes = testReactiveArray();
// const ReactiveMapRes = testReactiveMap(); // const ReactiveMapRes = testReactiveMap();
...@@ -53,6 +56,7 @@ export function runTests() : UTSJSONObject { ...@@ -53,6 +56,7 @@ export function runTests() : UTSJSONObject {
Operators: OperatorsRes, Operators: OperatorsRes,
Math: MathRes, Math: MathRes,
RegExp: RegExpRes, RegExp: RegExpRes,
KeyWord:KeyWordRes KeyWord:KeyWordRes,
extapi:extapiRes
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册