提交 7c66a5cc 编写于 作者: lizhongyi_'s avatar lizhongyi_

新增uts内置对象混编测试例

上级 b8186934
......@@ -29,7 +29,8 @@ import { testTypeFromAppJs, Options } from '@/uni_modules/uts-ios-tests'
// 目前android已经不再启用移除type类型的transformer
import type { TestType } from '@/uni_modules/uts-tests'
// #endif
import { testSyntaxUnion } from '@/uni_modules/uts-test-syntax-union'
import { testSyntaxUnion } from '@/uni_modules/uts-test-syntax-union'
import { testBuildinNative } from '@/uni_modules/uts-tests-hybrid'
export default {
data() {
return {
......@@ -40,7 +41,8 @@ export default {
}
},
onReady() {
testSyntaxUnion()
testSyntaxUnion()
testBuildinNative()
this.test()
},
methods: {
......
{
"id": "uts-tests-hybrid",
"displayName": "uts-tests-hybrid",
"version": "1.0.0",
"description": "uts-tests-hybrid",
"keywords": [
"uts-tests-hybrid"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u",
"alipay": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u",
"app-harmony": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uts-tests-hybrid
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[UTS uni-app兼容模式组件](https://uniapp.dcloud.net.cn/plugin/uts-component.html)
[UTS 标准模式组件](https://doc.dcloud.net.cn/uni-app-x/plugin/uts-vue-component.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts)
\ No newline at end of file
{
"minSdkVersion": "21"
}
\ No newline at end of file
import Foundation
import DCloudUTSFoundation
class TestNumber {
static func testAll() {
testToFixed()
testIsFinite()
testIsInteger()
testIsNaN()
testToPrecision()
testToString()
testValueOf()
testToInt()
testFrom()
}
static func testToFixed() {
// #TEST Number.toFixed
let financial = { (x: NSNumber) in
return x.toFixed(2)
}
console.log(financial(NSNumber(value: 123.456)))
// expected output: "123.46"
console.log(financial(NSNumber(value: 0.004)));
// expected output: "0.00"
let num: NSNumber = 3.1415926
console.log(num.toFixed(2))
//expected output: "3.14"
// #END
}
static func testIsFinite() {
// #TEST Number.isFinite
console.log(NSNumber.isFinite(1000 / 1))
//expected output: true
console.log(NSNumber.isFinite(910))
//expected output: true
console.log(NSNumber.isFinite(Double.infinity))
//expected output: false
// #END
}
static func testIsInteger() {
// #TEST Number.isInteger
console.log(NSNumber.isInteger(12))
//expected output: true
console.log(NSNumber.isInteger(12.01))
//expected output: false
console.log(NSNumber.isInteger(-213123112.01))
//expected output: false
console.log(NSNumber.isInteger(-213123112))
//expected output: true
// #END
}
static func testIsNaN() {
// #TEST Number.isInteger
console.log(isNaN(0))
//expected output: false
console.log(NSNumber.isNaN(0))
// expected output: false
let obj: UTSJSONObject? = JSON.parseObject("{\"a\":1}")
let aNumber = obj?.getNumber("a") ?? 0
console.log(NSNumber.isNaN(aNumber))
//expected output: false
console.log(NSNumber.isNaN(11))
//expected output: false
console.log(NSNumber.isNaN( 1 / 0))
//expected output: false
// #END
}
static func testToPrecision() {
// #TEST Number.toPrecision
console.log(123.456.toPrecision(4))
//expected output: "123.5"
console.log((0.004).toPrecision(4))
//expected output: "0.004000"
// #END
}
static func testToString() {
// #TEST Number.toString
console.log(10.toString())
//expected output: "10"
console.log(17.2.toString())
//expected output: "17.2"
console.log(6.toString(2))
//expected output: "110"
console.log(254.toString(16))
//expected output: "fe"
console.log((-10).toString(2))
//expected output: "-1010"
console.log(10.22.toString(8))
//expected output: "12.16050753412172704"
console.log((-10.22).toString(8))
//expected output: "-12.16050753412172704"
console.log(123456789987654.toString(16))
//expected output: "7048861cc146"
console.log((-0xff).toString(2))
//expected output: "-11111111"
// #END
}
static func testValueOf() {
// #TEST Number.valueOf
console.log(10.valueOf())
//expected output: 10
console.log((-10.2).valueOf())
//expected output: -10.2
console.log(0xf.valueOf())
//expected output: 15
// #END
}
static func testToInt() {
// #TEST Number.toInt
let a: NSNumber = 12
console.log(a.toInt())
//expected output: 12
let b: NSNumber = 2147483648
console.log(b.toInt())
//expected output: 2147483648
// #END
}
static func testFrom() {
// #TEST Number.toInt
let a: Int = 12
let b = NSNumber.from(a)
console.log(b)
//expected output: 12
// #END
}
}
import Foundation
import DCloudUTSFoundation
class TestUTSJSONObject {
static func testKeys() {
// #TEST UTSJSONObject.keys
let obj = UTSJSONObject(dictionary: [
"name": "zhangsan",
"age": 11
])
let ret1 = UTSJSONObject.keys(obj).length
console.log(ret1) //2
// #END
}
static func testAssign() {
// #TEST UTSJSONObject.assign
let target = UTSJSONObject(dictionary: [
"a": 1,
"b": 2
]);
let source = UTSJSONObject(dictionary: [
"b": 4,
"c": 5
])
// 得到一个UTSJSONObject对象
let returnedTarget = UTSJSONObject.assign(target, source);
console.log(returnedTarget.toMap().count) //3
// #END
// #TEST UTSJSONObject.assign
let target1 = UTSJSONObject(dictionary: [
"a": 1,
"b": 2
]);
let source1 = UTSJSONObject(dictionary: [
"b": 4,
"c": 5
])
// 得到一个UTSJSONObject对象
let ret = UTSJSONObject.assign(target1, source1, type: UTSJSONObject.self);
console.log(ret) // {"a": 1, "b": 4, "c": 5}
// #END
}
static func testGetNumber() {
// #TEST UTSJSONObject.getNumber
let obj = UTSJSONObject(dictionary: [
"qq": [11, 22]
])
console.log(obj.getNumber("obj[2]")) // null
console.log(obj.getNumber("obj[2]", 999)) // 999
// #END
}
static func testGetJSON() {
// #TEST UTSJSONObject.getJSON
let obj = UTSJSONObject(dictionary: [
"cars": [
UTSJSONObject(dictionary: [
"name": "car1",
"value": 100
])
]
])
let firstCar = obj.getJSON("cars[0]")
console.log(firstCar!["value"]) // 100
// #END
}
static func testGetArray() {
// #TEST UTSJSONObject.getArray
let obj = UTSJSONObject(dictionary: [
"cars": [
UTSJSONObject(dictionary: [
"name": "car1",
"value": 100
])
]
])
let cars: [UTSJSONObject]? = obj.getArray("cars")
cars![0].set("value", 20)
console.log(cars![0]["value"]) // 20
// #END
// #TEST UTSJSONObject.getArray_1
//这个方法用来获取指定元素类型的数组
let obj1 = JSON.parseObject("{\"name\":\"tom\",\"tag\":[\"student\",\"user\"]}")
// 这里得到是 Array<*>
let noGenericArray = obj1!.getArray("tag")
console.log(noGenericArray)
// 这里得到是 Array<string>, 注意:要手动指定返回值类型,以便Swift进行泛型推断
let genericArray: [String]? = obj1!.getArray("tag")
console.log(genericArray) //["student", "user"]
// #END
}
static func testGetString() {
// #TEST UTSJSONObject.getString
let obj = UTSJSONObject()
var i = 0
while i < 100 {
obj.set("\(i)", "\(i)")
i++
}
let startTime = Date.now()
var j = 0
while j < 10000 {
obj.getString("0")
j++
}
let spendTime = Date.now() - startTime
console.log(spendTime < 800) // true
// #END
}
static func testSample() {
// #TEST UTSJSONObject.sample_create,UTSJSONObject.get,UTSJSONObject.set
var person: UTSJSONObject = UTSJSONObject([
"name": "Tom",
"printName": {() -> Void in}
])
//返回指定键对应的值,如果对象中不存在此键则返回 null。
var name: String? = person["name"] as? String
//get 方法可以简化为使用下标运算符 `[]` 访问
name = person["name"] as? String
//增加或更新指定键对应的值。
person.set("name", "Tom1")
//set 方法可以简化为使用下标运算符 `[]` 赋值
person["name"] = "Tom2"
// #END
// #TEST UTSJSONObject.sample_create1
// 写法1 推荐
var person1: UTSJSONObject = JSON.parseObject("{\"name\":\"Tom\"}")!
// 写法2 推荐
let person2: UTSJSONObject = JSON.parse("{\"name\":\"Tom\"}", UTSJSONObject.self)!
// 写法3 如果 as 转换的实际类型不匹配 会导致 crash,建议先通过 `instanceof` 判断类型再进行as转换。
let parseRet3 = JSON.parse("{\"name\":\"Tom\"}")
if parseRet3 is UTSJSONObject {
person = parseRet3 as! UTSJSONObject
}
// #END
// #TEST UTSJSONObject.toMap
person1 = JSON.parseObject("{\"name\":\"Tom\"}")!
person1.toMap().forEach { value, key in
console.log(key, value)
}
// #END
}
static func testConvert() {
// #TEST UTSJSONObject.convert
//自定义class 如果需要使用JSON.stringify 或者 JSON.parse处理,则需要实现 Codable 协议
//通常 uts 代码中 Class 的Codable 协议的实现由编译器自动实现,因此,这类代码不建议在混编代码中使用,除非你能很熟练的使用Codable协议
class User : Codable {
var name: String
var age: NSNumber
public init(_ obj: UTSJSONObject) {
self.name = obj["name"] as! String
self.age = obj["age"] as! NSNumber
}
enum CodingKeys: String, CodingKey { case name; case age }
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name, decoder)
self.age = try container.decode(NSNumber.self, forKey: .age, decoder)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name, encoder)
try container.encode(age, forKey: .age, encoder)
}
}
let jsonObj = UTSJSONObject([
"name": "张三",
"age": 12 as NSNumber
])
let userA = JSON.parse(JSON.stringify(jsonObj)!, User.self)
console.log(userA?.name)
let utsJsonA = JSON.parseObject(JSON.stringify(userA)!)
console.log(utsJsonA)
// #END
}
static func testAll() {
testKeys()
testAssign()
testGetNumber()
testGetJSON()
testGetArray()
testGetString()
testSample()
testConvert()
}
}
export function testBuildinNative() {
TestNumber.testAll()
TestUTSJSONObject.testAll()
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册