KeyWord.uts 3.6 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7
import { describe, test, expect, Result } from './tests.uts'

class User{
	name:string = "";
	age:number = 0
}

lizhongyi_'s avatar
lizhongyi_ 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
const passcode = "secret passcode";
class Parent {
	private _name: string = ""; // private是私有的,外部不能访问
	get name(): string { // 读取name会触发此拦截器
		console.log("start to get parent.name");
		return this._name;
	}
	set name(newName: string) { // 给name赋值会触发此拦截器
		console.log("start to set parent.name");
		if (passcode == "secret passcode") { // 校验是否有权修改name的值,这里的条件可以修改以方便测试
			this._name = newName;
		} else {
			console.log("Error: set parent.name fail");
		}
	}  
	
	// 静态属性和方法
	static age: number = 30
	static run(): string {
		console.log("this is a static method")
		return "static method"
	}
	
	// readonly
	readonly address: string = ""
	
	weight: number = 80
	
	// constructor
	constructor(weight: number) {
		console.log("开始实例化")
		this.weight = weight
	}
	
	// 实例方法
	eat(): string {
		console.log("this is parent")
		return "parent instance method"
	}
	
}

class ChildrenTest extends Parent {
	override eat(): string {
		super.eat()
		return "children instance method"
	}
	
	// swift 中不能重写父类的存储属性,只能重写父类的计算的属性(本质上是重写计算属性的get set方法)
	// #ifndef APP-IOS
		override weight = 8
	// #endif 
}

杜庆泉's avatar
杜庆泉 已提交
62
export function testKeyWord(): Result {
lizhongyi_'s avatar
lizhongyi_ 已提交
63
    return describe("KeyWord", () => {
杜庆泉's avatar
杜庆泉 已提交
64
		
杜庆泉's avatar
杜庆泉 已提交
65 66
        test('new', () => {
           let new1 = new User()
lizhongyi_'s avatar
lizhongyi_ 已提交
67 68 69
		   // expect(JSON.stringify(new1)).toEqual('{"age":0,"name":""}')
		   console.log(JSON.stringify(new1))
		   
杜庆泉's avatar
杜庆泉 已提交
70 71
		   new1.age = 10
		   new1.name = "job"
lizhongyi_'s avatar
lizhongyi_ 已提交
72 73 74
		   console.log(JSON.stringify(new1))
		   // expect(JSON.stringify(new1)).toEqual('{"age":10,"name":"job"}')
		   
杜庆泉's avatar
杜庆泉 已提交
75 76 77
        })
		
		test('typeof', () => {
杜庆泉's avatar
杜庆泉 已提交
78 79
			let new1 = new User()
			expect(typeof(new1)).toEqual('object')
80
			expect(typeof(123456.789)).toEqual('Double')
杜庆泉's avatar
杜庆泉 已提交
81
			//expect(typeof(789778979798797987979)).toEqual('number')
82 83
			expect(typeof(0.0)).toEqual('Double')

杜庆泉's avatar
杜庆泉 已提交
84 85 86 87
			expect(typeof("hello world")).toEqual('string')
			expect(typeof([1,2,3])).toEqual('object')
			expect(typeof(new Array<any>())).toEqual('object')
			expect(typeof(new Set<any>())).toEqual('object')
lizhongyi_'s avatar
lizhongyi_ 已提交
88
			// expect(typeof(new Map<any,any>())).toEqual('object')
杜庆泉's avatar
杜庆泉 已提交
89 90
			expect(typeof(new Date())).toEqual('object')
			expect(typeof("hello world")).toEqual('string')
lizhongyi_'s avatar
lizhongyi_ 已提交
91 92
			// 原生对象  
			// #ifndef APP-IOS
杜庆泉's avatar
杜庆泉 已提交
93
			expect(typeof(UTSAndroid.getUniActivity())).toEqual('object')
lizhongyi_'s avatar
lizhongyi_ 已提交
94 95
			// #endif
			   
杜庆泉's avatar
杜庆泉 已提交
96
		   
杜庆泉's avatar
杜庆泉 已提交
97 98 99 100
		})
        
		
		test('instanceof', () => {
杜庆泉's avatar
杜庆泉 已提交
101
			let user1:any = new User()
杜庆泉's avatar
杜庆泉 已提交
102 103
			let instanceRet1 = user1 instanceof User
			expect(instanceRet1).toEqual(true)
104
			let instanceRet2 = (typeof user1) == "string"
杜庆泉's avatar
杜庆泉 已提交
105
			expect(instanceRet2).toEqual(false)
杜庆泉's avatar
杜庆泉 已提交
106
			let num1:any = 3.1415926 
107
			let instanceRet3 = (typeof num1) == "string" 
杜庆泉's avatar
杜庆泉 已提交
108
			expect(instanceRet3).toEqual(false)
109
			let instanceRet4 = (typeof num1) == "number"
lizhongyi_'s avatar
lizhongyi_ 已提交
110
			expect(instanceRet4).toEqual(false)
杜庆泉's avatar
杜庆泉 已提交
111 112
		})
		
杜庆泉's avatar
杜庆泉 已提交
113 114 115 116 117 118
		test('isArray', () => {
			let user1:any = new User()
			expect(Array.isArray(user1)).toEqual(false)
			expect(Array.isArray([1,2,3])).toEqual(true)
		})
		
lizhongyi_'s avatar
lizhongyi_ 已提交
119 120 121 122 123 124 125 126 127 128
		test('class', () => {
			let p = new Parent(20)
			p.name = "tom" // 会打印"start to set person.name"
			console.log(p.name); // 先打印"start to get person.name",然后打印"tom"
			expect(Parent.age).toEqual(30)
			expect(Parent.run()).toEqual("static method")
			expect(p.eat()).toEqual("parent instance method")
			let c = new ChildrenTest(0)
			expect(c.eat()).toEqual("children instance method")
		})
杜庆泉's avatar
杜庆泉 已提交
129 130 131
		
    })
}