export class Matchers { private actual: T constructor(actual: T) { this.actual = actual } toEqual(expected: T) { if (JSON.stringify(expected) == JSON.stringify(this.actual)) { return } // #ifndef APP-IOS throw new Error(format(expected, this.actual)) // #endif // #ifdef APP-IOS NSException(name = NSExceptionName.internalInconsistencyException, reason = format(expected, this.actual)).raise() // #endif } toEqualNumber(expected: T) { if (expected == this.actual) { return } } } /** * 数值对比器 */ 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 } } function format(expected: any | null, actual: any | null): string { return `expected:<${expected}> but was:<${actual}>` }