Matchers.uts 1.1 KB
Newer Older
Y
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11

export class Matchers<T> {
    private actual: T
    constructor(actual: T) {
        this.actual = actual
    }
    toEqual(expected: T) {
        if (JSON.stringify(expected) == JSON.stringify(this.actual)) {
            return
        }
        // #ifndef APP-IOS
杜庆泉's avatar
杜庆泉 已提交
12
        throw new Error(format(expected, this.actual))
Y
yurj26 已提交
13 14
        // #endif
        // #ifdef APP-IOS
lizhongyi_'s avatar
lizhongyi_ 已提交
15
        // NSException(name = NSExceptionName.internalInconsistencyException, reason = format(expected, this.actual)).raise()
Y
yurj26 已提交
16 17 18
        // #endif
    }
}
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
/**
 * 数值对比器
 */
export class NumberMatchers {
	
    private actual: number
    constructor(actual: number) {
        this.actual = actual
    }
	/**
	 * 用于浮点数对比,只比较小数点后5位,web/app 浮点储存位数不同
	 */
	toEqualDouble(expected:number) {
		
		let absDiff = Math.abs(this.actual - expected)
	    if (absDiff < 0.00001) {
	        return
	    }
	    // #ifndef APP-IOS
	    throw new Error(format(expected, this.actual))
	    // #endif
	}
	

}

Y
yurj26 已提交
45 46 47 48

function format(expected: any | null, actual: any | null): string {
    return `expected:<${expected}> but was:<${actual}>`
}