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
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 {
杜庆泉's avatar
杜庆泉 已提交
51 52 53 54 55
	
	constructor(weight: number) {
		super(weight)
	}
	
lizhongyi_'s avatar
lizhongyi_ 已提交
56 57 58 59 60 61 62 63 64 65 66
	override eat(): string {
		super.eat()
		return "children instance method"
	}
	
	// swift 中不能重写父类的存储属性,只能重写父类的计算的属性(本质上是重写计算属性的get set方法)
	// #ifndef APP-IOS
		override weight = 8
	// #endif 
}

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

杜庆泉's avatar
杜庆泉 已提交
89 90 91 92
			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_ 已提交
93
			// expect(typeof(new Map<any,any>())).toEqual('object')
杜庆泉's avatar
杜庆泉 已提交
94 95
			expect(typeof(new Date())).toEqual('object')
			expect(typeof("hello world")).toEqual('string')
lizhongyi_'s avatar
lizhongyi_ 已提交
96 97
			// 原生对象  
			// #ifndef APP-IOS
杜庆泉's avatar
杜庆泉 已提交
98
			expect(typeof(UTSAndroid.getUniActivity())).toEqual('object')
lizhongyi_'s avatar
lizhongyi_ 已提交
99 100
			// #endif
			   
杜庆泉's avatar
杜庆泉 已提交
101
		   
杜庆泉's avatar
杜庆泉 已提交
102 103 104 105
		})
        
		
		test('instanceof', () => {
杜庆泉's avatar
杜庆泉 已提交
106
			let user1:any = new User()
杜庆泉's avatar
杜庆泉 已提交
107 108
			let instanceRet1 = user1 instanceof User
			expect(instanceRet1).toEqual(true)
109
			let instanceRet2 = (typeof user1) == "string"
杜庆泉's avatar
杜庆泉 已提交
110
			expect(instanceRet2).toEqual(false)
杜庆泉's avatar
杜庆泉 已提交
111
			let num1:any = 3.1415926 
112
			let instanceRet3 = (typeof num1) == "string" 
杜庆泉's avatar
杜庆泉 已提交
113
			expect(instanceRet3).toEqual(false)
114
			let instanceRet4 = (typeof num1) == "number"
lizhongyi_'s avatar
lizhongyi_ 已提交
115
			expect(instanceRet4).toEqual(false)
杜庆泉's avatar
杜庆泉 已提交
116 117
		})
		
杜庆泉's avatar
杜庆泉 已提交
118 119 120 121 122 123
		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_ 已提交
124 125 126 127 128 129 130 131 132 133
		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
杜庆泉 已提交
134 135 136
		
    })
}