diff --git a/uni_modules/uts-tests/utssdk/Matchers.uts b/uni_modules/uts-tests/utssdk/Matchers.uts index 5818ccd9389fa5ba6b4104b2ed62c793e6795680..d510892492f959680299ef58898c07262cf2509c 100644 --- a/uni_modules/uts-tests/utssdk/Matchers.uts +++ b/uni_modules/uts-tests/utssdk/Matchers.uts @@ -16,6 +16,32 @@ export class Matchers { // #endif } } +/** + * 数值对比器 + */ +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}>` diff --git a/uni_modules/uts-tests/utssdk/Math.uts b/uni_modules/uts-tests/utssdk/Math.uts index e6c889ec763fdbced7b31475451b8955a7a9f3d7..bcfd8b7dccf237bdc0dd03e1268c4b8e1900d8c3 100644 --- a/uni_modules/uts-tests/utssdk/Math.uts +++ b/uni_modules/uts-tests/utssdk/Math.uts @@ -1,4 +1,4 @@ -import { describe, test, expect, Result } from './tests.uts' +import { describe, test, expect,expectNumber, Result } from './tests.uts' export function testMath(): Result { return describe("Math", () => { @@ -68,8 +68,8 @@ export function testMath(): Result { test('acosh', () => { // 解决精度问题 expect(Math.acosh(1)).toEqual(0.0); - expect(Math.acosh(2)).toEqual(1.3169578969248166); - expect(Math.acosh(2.5)).toEqual(1.566799236972411); + expectNumber(Math.acosh(2)).toEqualDouble(1.3169578969248166); + expectNumber(Math.acosh(2.5)).toEqualDouble(1.566799236972411); }) test('asin', () => { expect(Math.asin(-1)).toEqual(-1.5707963267948966); @@ -79,10 +79,10 @@ export function testMath(): Result { expect(Math.asin(1)).toEqual(1.5707963267948966); }) test('asinh', () => { - expect(Math.asinh(1)).toEqual(0.881373587019543); - expect(Math.asinh(0)).toEqual(0); - expect(Math.asinh(-1)).toEqual(-0.881373587019543); - expect(Math.asinh(2)).toEqual(1.4436354751788103); + expectNumber(Math.asinh(1)).toEqualDouble(0.881373587019543); + expectNumber(Math.asinh(0)).toEqualDouble(0); + expectNumber(Math.asinh(-1)).toEqualDouble(-0.881373587019543); + expectNumber(Math.asinh(2)).toEqualDouble(1.4436354751788103); }) test('atan', () => { expect(Math.atan(1)).toEqual(0.7853981633974483); @@ -133,8 +133,8 @@ export function testMath(): Result { expect(Math.exp(1)).toEqual(2.718281828459045); }) test('expm1', () => { - expect(Math.expm1(1)).toEqual(1.718281828459045); - expect(Math.expm1(-38)).toEqual(-1); + expectNumber(Math.expm1(1)).toEqualDouble(1.718281828459045); + expectNumber(Math.expm1(-38)).toEqualDouble(-1); }) test('floor', () => { expect(Math.floor(5.95)).toEqual(5); @@ -144,7 +144,7 @@ export function testMath(): Result { }) test('fround', () => { expect(Math.fround(1.5)).toEqual(1.5); - expect(Math.fround(1.337)).toEqual(1.3370000123977661); + expectNumber(Math.fround(1.337)).toEqualDouble(1.3370000123977661); }) // test('hypot', () => { // expect(Math.hypot(3, 4)).toEqual(5); @@ -187,8 +187,8 @@ export function testMath(): Result { expect(Math.min(-2, -3, -1)).toEqual(-3); }) test('pow', () => { - expect(Math.pow(7, 3)).toEqual(343); - expect(Math.pow(4, 0.5)).toEqual(2); + expectNumber(Math.pow(7, 3)).toEqualDouble(343); + expectNumber(Math.pow(4, 0.5)).toEqualDouble(2); }) test('random', () => { function getRandomInt(max:number):number { diff --git a/uni_modules/uts-tests/utssdk/tests.uts b/uni_modules/uts-tests/utssdk/tests.uts index d647677dccd4b2ad8d59c0d102ac96e44d8b6df6..953d7217774a4b5c108e8ee75b36265409580737 100644 --- a/uni_modules/uts-tests/utssdk/tests.uts +++ b/uni_modules/uts-tests/utssdk/tests.uts @@ -1,4 +1,4 @@ -import { Matchers } from './Matchers.uts' +import { Matchers,NumberMatchers } from './Matchers.uts' export const describes = new Map() @@ -32,3 +32,7 @@ export function test(name: string, fn: () => void) { export function expect(value: T): Matchers { return new Matchers(value); } + +export function expectNumber(value: number): NumberMatchers { + return new NumberMatchers(value); +}