From a2441a53b73e5f1eadc68cea0568e06526608296 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 8 Dec 2022 11:44:30 -0500 Subject: [PATCH 1/4] [web] Add the moreOrLessEquals matcher --- lib/web_ui/test/engine/canvas_test.dart | 4 +- lib/web_ui/test/geometry_test.dart | 37 +++++++--------- lib/web_ui/test/lerp_test.dart | 43 ++++++++---------- lib/web_ui/test/matchers.dart | 58 +++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 47 deletions(-) diff --git a/lib/web_ui/test/engine/canvas_test.dart b/lib/web_ui/test/engine/canvas_test.dart index 053cd9c155b6b..c98ccb5d65959 100644 --- a/lib/web_ui/test/engine/canvas_test.dart +++ b/lib/web_ui/test/engine/canvas_test.dart @@ -11,6 +11,8 @@ import 'package:ui/src/engine.dart'; import 'package:ui/ui.dart' as ui; +import '../matchers.dart'; + void main() { internalBootstrapBrowserTest(() => testMain); } @@ -31,7 +33,7 @@ void runCanvasTests({required bool deviceClipRoundsOut}) { expect(value.length, equals(16)); for (int r = 0; r < 4; r++) { for (int c = 0; c < 4; c++) { - expect(value[r*4 + c], closeTo(expected[r*4 + c], 1e-10)); + expect(value[r*4 + c], moreOrLessEquals(expected[r*4 + c])); } } } diff --git a/lib/web_ui/test/geometry_test.dart b/lib/web_ui/test/geometry_test.dart index 24150d801cdf4..eb8d79b432526 100644 --- a/lib/web_ui/test/geometry_test.dart +++ b/lib/web_ui/test/geometry_test.dart @@ -13,6 +13,8 @@ import 'package:test/test.dart'; import 'package:ui/ui.dart'; +import 'matchers.dart'; + void main() { internalBootstrapBrowserTest(() => testMain); } @@ -32,33 +34,24 @@ void testMain() { test('Offset.fromDirection', () { expect(Offset.fromDirection(0.0, 0.0), const Offset(0.0, 0.0)); expect(Offset.fromDirection(pi / 2.0).dx, - closeTo(0.0, 1e-12)); // aah, floating point math. i love you so. + moreOrLessEquals(0.0)); // aah, floating point math. i love you so. expect(Offset.fromDirection(pi / 2.0).dy, 1.0); - expect(Offset.fromDirection(-pi / 2.0).dx, closeTo(0.0, 1e-12)); + expect(Offset.fromDirection(-pi / 2.0).dx, moreOrLessEquals(0.0)); expect(Offset.fromDirection(-pi / 2.0).dy, -1.0); expect(Offset.fromDirection(0.0), const Offset(1.0, 0.0)); - expect(Offset.fromDirection(pi / 4.0).dx, - closeTo(1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(pi / 4.0).dy, - closeTo(1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(-pi / 4.0).dx, - closeTo(1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(-pi / 4.0).dy, - closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(pi / 4.0).dx, moreOrLessEquals(1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(pi / 4.0).dy, moreOrLessEquals(1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(-pi / 4.0).dx, moreOrLessEquals(1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(-pi / 4.0).dy, moreOrLessEquals(-1.0 / math.sqrt(2.0))); expect(Offset.fromDirection(pi).dx, -1.0); - expect(Offset.fromDirection(pi).dy, closeTo(0.0, 1e-12)); - expect(Offset.fromDirection(pi * 3.0 / 4.0).dx, - closeTo(-1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(pi * 3.0 / 4.0).dy, - closeTo(1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(-pi * 3.0 / 4.0).dx, - closeTo(-1.0 / math.sqrt(2.0), 1e-12)); - expect(Offset.fromDirection(-pi * 3.0 / 4.0).dy, - closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(pi).dy, moreOrLessEquals(0.0)); + expect(Offset.fromDirection(pi * 3.0 / 4.0).dx, moreOrLessEquals(-1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(pi * 3.0 / 4.0).dy, moreOrLessEquals(1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(-pi * 3.0 / 4.0).dx, moreOrLessEquals(-1.0 / math.sqrt(2.0))); + expect(Offset.fromDirection(-pi * 3.0 / 4.0).dy, moreOrLessEquals(-1.0 / math.sqrt(2.0))); expect(Offset.fromDirection(0.0, 2.0), const Offset(2.0, 0.0)); - expect( - Offset.fromDirection(pi / 6, 2.0).dx, closeTo(math.sqrt(3.0), 1e-12)); - expect(Offset.fromDirection(pi / 6, 2.0).dy, closeTo(1.0, 1e-12)); + expect(Offset.fromDirection(pi / 6, 2.0).dx, moreOrLessEquals(math.sqrt(3.0))); + expect(Offset.fromDirection(pi / 6, 2.0).dy, moreOrLessEquals(1.0)); }); test('Size.aspectRatio', () { expect(const Size(0.0, 0.0).aspectRatio, 0.0); diff --git a/lib/web_ui/test/lerp_test.dart b/lib/web_ui/test/lerp_test.dart index 2c9d1fc0574c0..a3007bee93052 100644 --- a/lib/web_ui/test/lerp_test.dart +++ b/lib/web_ui/test/lerp_test.dart @@ -7,12 +7,7 @@ import 'package:test/test.dart'; import 'package:ui/ui.dart'; -/// The epsilon of tolerable double precision error. -/// -/// This is used in various places in the framework to allow for floating point -/// precision loss in calculations. Differences below this threshold are safe -/// to disregard. -const double precisionErrorTolerance = 1e-10; +import 'matchers.dart'; void main() { internalBootstrapBrowserTest(() => testMain); @@ -31,19 +26,19 @@ void testMain() { }); test('lerpDouble should treat a null input as 0 if the other input is non-null', () { - expect(lerpDouble(null, 10.0, 0.25), closeTo(2.5, precisionErrorTolerance)); - expect(lerpDouble(10.0, null, 0.25), closeTo(7.5, precisionErrorTolerance)); + expect(lerpDouble(null, 10.0, 0.25), moreOrLessEquals(2.5)); + expect(lerpDouble(10.0, null, 0.25), moreOrLessEquals(7.5)); - expect(lerpDouble(null, 10, 0.25), closeTo(2.5, precisionErrorTolerance)); - expect(lerpDouble(10, null, 0.25), closeTo(7.5, precisionErrorTolerance)); + expect(lerpDouble(null, 10, 0.25), moreOrLessEquals(2.5)); + expect(lerpDouble(10, null, 0.25), moreOrLessEquals(7.5)); }); test('lerpDouble should handle interpolation values < 0.0', () { - expect(lerpDouble(0.0, 10.0, -5.0), closeTo(-50.0, precisionErrorTolerance)); - expect(lerpDouble(10.0, 0.0, -5.0), closeTo(60.0, precisionErrorTolerance)); + expect(lerpDouble(0.0, 10.0, -5.0), moreOrLessEquals(-50.0)); + expect(lerpDouble(10.0, 0.0, -5.0), moreOrLessEquals(60.0)); - expect(lerpDouble(0, 10, -5), closeTo(-50, precisionErrorTolerance)); - expect(lerpDouble(10, 0, -5), closeTo(60, precisionErrorTolerance)); + expect(lerpDouble(0, 10, -5), moreOrLessEquals(-50)); + expect(lerpDouble(10, 0, -5), moreOrLessEquals(60)); }); test('lerpDouble should return the start value at 0.0', () { @@ -55,17 +50,17 @@ void testMain() { }); test('lerpDouble should interpolate between two values', () { - expect(lerpDouble(0.0, 10.0, 0.25), closeTo(2.5, precisionErrorTolerance)); - expect(lerpDouble(10.0, 0.0, 0.25), closeTo(7.5, precisionErrorTolerance)); + expect(lerpDouble(0.0, 10.0, 0.25), moreOrLessEquals(2.5)); + expect(lerpDouble(10.0, 0.0, 0.25), moreOrLessEquals(7.5)); - expect(lerpDouble(0, 10, 0.25), closeTo(2.5, precisionErrorTolerance)); - expect(lerpDouble(10, 0, 0.25), closeTo(7.5, precisionErrorTolerance)); + expect(lerpDouble(0, 10, 0.25), moreOrLessEquals(2.5)); + expect(lerpDouble(10, 0, 0.25), moreOrLessEquals(7.5)); // Exact answer: 20.0 - 1.0e-29 - expect(lerpDouble(10.0, 1.0e30, 1.0e-29), closeTo(20.0, precisionErrorTolerance)); + expect(lerpDouble(10.0, 1.0e30, 1.0e-29), moreOrLessEquals(20.0)); // Exact answer: 5.0 + 5.0e29 - expect(lerpDouble(10.0, 1.0e30, 0.5), closeTo(5.0e29, precisionErrorTolerance)); + expect(lerpDouble(10.0, 1.0e30, 0.5), moreOrLessEquals(5.0e29)); }); test('lerpDouble should return the end value at 1.0', () { @@ -80,11 +75,11 @@ void testMain() { }); test('lerpDouble should handle interpolation values > 1.0', () { - expect(lerpDouble(0.0, 10.0, 5.0), closeTo(50.0, precisionErrorTolerance)); - expect(lerpDouble(10.0, 0.0, 5.0), closeTo(-40.0, precisionErrorTolerance)); + expect(lerpDouble(0.0, 10.0, 5.0), moreOrLessEquals(50.0)); + expect(lerpDouble(10.0, 0.0, 5.0), moreOrLessEquals(-40.0)); - expect(lerpDouble(0, 10, 5), closeTo(50, precisionErrorTolerance)); - expect(lerpDouble(10, 0, 5), closeTo(-40, precisionErrorTolerance)); + expect(lerpDouble(0, 10, 5), moreOrLessEquals(50)); + expect(lerpDouble(10, 0, 5), moreOrLessEquals(-40)); }); test('lerpDouble should return input value in all cases if begin/end are equal', () { diff --git a/lib/web_ui/test/matchers.dart b/lib/web_ui/test/matchers.dart index b754d4b2387e0..3a11c11eab906 100644 --- a/lib/web_ui/test/matchers.dart +++ b/lib/web_ui/test/matchers.dart @@ -15,6 +15,13 @@ import 'package:test/test.dart'; import 'package:ui/src/engine.dart'; import 'package:ui/ui.dart'; +/// The epsilon of tolerable double precision error. +/// +/// This is used in various places in the framework to allow for floating point +/// precision loss in calculations. Differences below this threshold are safe +/// to disregard. +const double precisionErrorTolerance = 1e-10; + /// Enumerates all persisted surfaces in the tree rooted at [root]. /// /// If [root] is `null` returns all surfaces from the last rendered scene. @@ -496,3 +503,54 @@ final Matcher throwsAssertionError = throwsA(isAssertionError); /// * [throwsAssertionError], to test if a function throws any [AssertionError]. /// * [isFlutterError], to test if any object is a [FlutterError]. const Matcher isAssertionError = TypeMatcher(); + +/// Asserts that two [double]s are equal, within some tolerated error. +/// +/// {@template flutter.flutter_test.moreOrLessEquals} +/// Two values are considered equal if the difference between them is within +/// [precisionErrorTolerance] of the larger one. This is an arbitrary value +/// which can be adjusted using the `epsilon` argument. This matcher is intended +/// to compare floating point numbers that are the result of different sequences +/// of operations, such that they may have accumulated slightly different +/// errors. +/// {@endtemplate} +/// +/// See also: +/// +/// * [closeTo], which is identical except that the epsilon argument is +/// required and not named. +/// * [inInclusiveRange], which matches if the argument is in a specified +/// range. +/// * [rectMoreOrLessEquals] and [offsetMoreOrLessEquals], which do something +/// similar but for [Rect]s and [Offset]s respectively. +Matcher moreOrLessEquals(double value, { double epsilon = precisionErrorTolerance }) { + return _MoreOrLessEquals(value, epsilon); +} + +class _MoreOrLessEquals extends Matcher { + const _MoreOrLessEquals(this.value, this.epsilon) + : assert(epsilon >= 0); + + final double value; + final double epsilon; + + @override + bool matches(dynamic object, Map matchState) { + if (object is! double) { + return false; + } + if (object == value) { + return true; + } + return (object - value).abs() <= epsilon; + } + + @override + Description describe(Description description) => description.add('$value (±$epsilon)'); + + @override + Description describeMismatch(dynamic item, Description mismatchDescription, Map matchState, bool verbose) { + return super.describeMismatch(item, mismatchDescription, matchState, verbose) + ..add('$item is not in the range of $value (±$epsilon).'); + } +} From f26bc2a6eed769ff83cc0fb49a7c9658b63db57a Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 2 Feb 2023 10:58:20 -0500 Subject: [PATCH 2/4] address review comments --- lib/web_ui/test/geometry_test.dart | 41 +++++++++++++++++------------- lib/web_ui/test/matchers.dart | 10 ++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/web_ui/test/geometry_test.dart b/lib/web_ui/test/geometry_test.dart index eb8d79b432526..80df3611c3af8 100644 --- a/lib/web_ui/test/geometry_test.dart +++ b/lib/web_ui/test/geometry_test.dart @@ -33,25 +33,32 @@ void testMain() { }); test('Offset.fromDirection', () { expect(Offset.fromDirection(0.0, 0.0), const Offset(0.0, 0.0)); - expect(Offset.fromDirection(pi / 2.0).dx, - moreOrLessEquals(0.0)); // aah, floating point math. i love you so. - expect(Offset.fromDirection(pi / 2.0).dy, 1.0); - expect(Offset.fromDirection(-pi / 2.0).dx, moreOrLessEquals(0.0)); - expect(Offset.fromDirection(-pi / 2.0).dy, -1.0); + // aah, floating point math. i love you so. + expect(Offset.fromDirection(pi / 2.0), within(from: const Offset(0.0, 1.0))); + expect(Offset.fromDirection(-pi / 2.0), within(from: const Offset(0.0, -1.0))); expect(Offset.fromDirection(0.0), const Offset(1.0, 0.0)); - expect(Offset.fromDirection(pi / 4.0).dx, moreOrLessEquals(1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(pi / 4.0).dy, moreOrLessEquals(1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(-pi / 4.0).dx, moreOrLessEquals(1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(-pi / 4.0).dy, moreOrLessEquals(-1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(pi).dx, -1.0); - expect(Offset.fromDirection(pi).dy, moreOrLessEquals(0.0)); - expect(Offset.fromDirection(pi * 3.0 / 4.0).dx, moreOrLessEquals(-1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(pi * 3.0 / 4.0).dy, moreOrLessEquals(1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(-pi * 3.0 / 4.0).dx, moreOrLessEquals(-1.0 / math.sqrt(2.0))); - expect(Offset.fromDirection(-pi * 3.0 / 4.0).dy, moreOrLessEquals(-1.0 / math.sqrt(2.0))); + expect( + Offset.fromDirection(pi / 4.0), + within(from: Offset(1.0 / math.sqrt(2.0), 1.0 / math.sqrt(2.0))), + ); + expect( + Offset.fromDirection(-pi / 4.0), + within(from: Offset(1.0 / math.sqrt(2.0), -1.0 / math.sqrt(2.0))), + ); + expect(Offset.fromDirection(pi), within(from: const Offset(-1.0, 0.0))); + expect( + Offset.fromDirection(pi * 3.0 / 4.0), + within(from: Offset(-1.0 / math.sqrt(2.0), 1.0 / math.sqrt(2.0))), + ); + expect( + Offset.fromDirection(-pi * 3.0 / 4.0), + within(from: Offset(-1.0 / math.sqrt(2.0), -1.0 / math.sqrt(2.0))), + ); expect(Offset.fromDirection(0.0, 2.0), const Offset(2.0, 0.0)); - expect(Offset.fromDirection(pi / 6, 2.0).dx, moreOrLessEquals(math.sqrt(3.0))); - expect(Offset.fromDirection(pi / 6, 2.0).dy, moreOrLessEquals(1.0)); + expect( + Offset.fromDirection(pi / 6, 2.0), + within(from: Offset(math.sqrt(3.0), 1.0)), + ); }); test('Size.aspectRatio', () { expect(const Size(0.0, 0.0).aspectRatio, 0.0); diff --git a/lib/web_ui/test/matchers.dart b/lib/web_ui/test/matchers.dart index 3a11c11eab906..20ddaa79e00f8 100644 --- a/lib/web_ui/test/matchers.dart +++ b/lib/web_ui/test/matchers.dart @@ -67,7 +67,7 @@ Iterable enumerateOffsets([PersistedSurface? root]) { /// /// This makes it useful for comparing numbers, [Color]s, [Offset]s and other /// sets of value for which a metric space is defined. -typedef DistanceFunction = num Function(T a, T b); +typedef DistanceFunction = double Function(T a, T b); /// The type of a union of instances of [DistanceFunction] for various types /// T. @@ -92,7 +92,7 @@ const Map _kStandardDistanceFunctions = Size: _sizeDistance, }; -int _intDistance(int a, int b) => (b - a).abs(); +double _intDistance(int a, int b) => (b - a).abs().toDouble(); double _doubleDistance(double a, double b) => (b - a).abs(); double _offsetDistance(Offset a, Offset b) => (b - a).distance; @@ -138,8 +138,8 @@ double _sizeDistance(Size a, Size b) { /// [double]s and has an optional `epsilon` parameter. /// * [closeTo], which specializes in numbers only. Matcher within({ - required num distance, required T from, + double distance = precisionErrorTolerance, DistanceFunction? distanceFunction, }) { distanceFunction ??= _kStandardDistanceFunctions[T] as DistanceFunction?; @@ -159,7 +159,7 @@ class _IsWithinDistance extends Matcher { final DistanceFunction distanceFunction; final T value; - final num epsilon; + final double epsilon; @override bool matches(Object? object, Map matchState) { @@ -170,7 +170,7 @@ class _IsWithinDistance extends Matcher { return true; } final T test = object; - final num distance = distanceFunction(test, value); + final double distance = distanceFunction(test, value); if (distance < 0) { throw ArgumentError( 'Invalid distance function was used to compare a ${value.runtimeType} ' From d6dd6369fa55321b90c9430b11eb47c874058488 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 2 Feb 2023 13:17:47 -0500 Subject: [PATCH 3/4] use within() instead --- lib/web_ui/test/engine/canvas_test.dart | 2 +- lib/web_ui/test/lerp_test.dart | 36 ++++++++--------- lib/web_ui/test/matchers.dart | 51 ------------------------- 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/lib/web_ui/test/engine/canvas_test.dart b/lib/web_ui/test/engine/canvas_test.dart index c98ccb5d65959..926ac1b3fe339 100644 --- a/lib/web_ui/test/engine/canvas_test.dart +++ b/lib/web_ui/test/engine/canvas_test.dart @@ -33,7 +33,7 @@ void runCanvasTests({required bool deviceClipRoundsOut}) { expect(value.length, equals(16)); for (int r = 0; r < 4; r++) { for (int c = 0; c < 4; c++) { - expect(value[r*4 + c], moreOrLessEquals(expected[r*4 + c])); + expect(value[r*4 + c], within(from: expected[r*4 + c])); } } } diff --git a/lib/web_ui/test/lerp_test.dart b/lib/web_ui/test/lerp_test.dart index a3007bee93052..bc56301b3247e 100644 --- a/lib/web_ui/test/lerp_test.dart +++ b/lib/web_ui/test/lerp_test.dart @@ -26,19 +26,19 @@ void testMain() { }); test('lerpDouble should treat a null input as 0 if the other input is non-null', () { - expect(lerpDouble(null, 10.0, 0.25), moreOrLessEquals(2.5)); - expect(lerpDouble(10.0, null, 0.25), moreOrLessEquals(7.5)); + expect(lerpDouble(null, 10.0, 0.25), within(from: 2.5)); + expect(lerpDouble(10.0, null, 0.25), within(from: 7.5)); - expect(lerpDouble(null, 10, 0.25), moreOrLessEquals(2.5)); - expect(lerpDouble(10, null, 0.25), moreOrLessEquals(7.5)); + expect(lerpDouble(null, 10, 0.25), within(from: 2.5)); + expect(lerpDouble(10, null, 0.25), within(from: 7.5)); }); test('lerpDouble should handle interpolation values < 0.0', () { - expect(lerpDouble(0.0, 10.0, -5.0), moreOrLessEquals(-50.0)); - expect(lerpDouble(10.0, 0.0, -5.0), moreOrLessEquals(60.0)); + expect(lerpDouble(0.0, 10.0, -5.0), within(from: -50.0)); + expect(lerpDouble(10.0, 0.0, -5.0), within(from: 60.0)); - expect(lerpDouble(0, 10, -5), moreOrLessEquals(-50)); - expect(lerpDouble(10, 0, -5), moreOrLessEquals(60)); + expect(lerpDouble(0, 10, -5), within(from: -50)); + expect(lerpDouble(10, 0, -5), within(from: 60)); }); test('lerpDouble should return the start value at 0.0', () { @@ -50,17 +50,17 @@ void testMain() { }); test('lerpDouble should interpolate between two values', () { - expect(lerpDouble(0.0, 10.0, 0.25), moreOrLessEquals(2.5)); - expect(lerpDouble(10.0, 0.0, 0.25), moreOrLessEquals(7.5)); + expect(lerpDouble(0.0, 10.0, 0.25), within(from: 2.5)); + expect(lerpDouble(10.0, 0.0, 0.25), within(from: 7.5)); - expect(lerpDouble(0, 10, 0.25), moreOrLessEquals(2.5)); - expect(lerpDouble(10, 0, 0.25), moreOrLessEquals(7.5)); + expect(lerpDouble(0, 10, 0.25), within(from: 2.5)); + expect(lerpDouble(10, 0, 0.25), within(from: 7.5)); // Exact answer: 20.0 - 1.0e-29 - expect(lerpDouble(10.0, 1.0e30, 1.0e-29), moreOrLessEquals(20.0)); + expect(lerpDouble(10.0, 1.0e30, 1.0e-29), within(from: 20.0)); // Exact answer: 5.0 + 5.0e29 - expect(lerpDouble(10.0, 1.0e30, 0.5), moreOrLessEquals(5.0e29)); + expect(lerpDouble(10.0, 1.0e30, 0.5), within(from: 5.0e29)); }); test('lerpDouble should return the end value at 1.0', () { @@ -75,11 +75,11 @@ void testMain() { }); test('lerpDouble should handle interpolation values > 1.0', () { - expect(lerpDouble(0.0, 10.0, 5.0), moreOrLessEquals(50.0)); - expect(lerpDouble(10.0, 0.0, 5.0), moreOrLessEquals(-40.0)); + expect(lerpDouble(0.0, 10.0, 5.0), within(from: 50.0)); + expect(lerpDouble(10.0, 0.0, 5.0), within(from: -40.0)); - expect(lerpDouble(0, 10, 5), moreOrLessEquals(50)); - expect(lerpDouble(10, 0, 5), moreOrLessEquals(-40)); + expect(lerpDouble(0, 10, 5), within(from: 50)); + expect(lerpDouble(10, 0, 5), within(from: -40)); }); test('lerpDouble should return input value in all cases if begin/end are equal', () { diff --git a/lib/web_ui/test/matchers.dart b/lib/web_ui/test/matchers.dart index 20ddaa79e00f8..7b352979a5941 100644 --- a/lib/web_ui/test/matchers.dart +++ b/lib/web_ui/test/matchers.dart @@ -503,54 +503,3 @@ final Matcher throwsAssertionError = throwsA(isAssertionError); /// * [throwsAssertionError], to test if a function throws any [AssertionError]. /// * [isFlutterError], to test if any object is a [FlutterError]. const Matcher isAssertionError = TypeMatcher(); - -/// Asserts that two [double]s are equal, within some tolerated error. -/// -/// {@template flutter.flutter_test.moreOrLessEquals} -/// Two values are considered equal if the difference between them is within -/// [precisionErrorTolerance] of the larger one. This is an arbitrary value -/// which can be adjusted using the `epsilon` argument. This matcher is intended -/// to compare floating point numbers that are the result of different sequences -/// of operations, such that they may have accumulated slightly different -/// errors. -/// {@endtemplate} -/// -/// See also: -/// -/// * [closeTo], which is identical except that the epsilon argument is -/// required and not named. -/// * [inInclusiveRange], which matches if the argument is in a specified -/// range. -/// * [rectMoreOrLessEquals] and [offsetMoreOrLessEquals], which do something -/// similar but for [Rect]s and [Offset]s respectively. -Matcher moreOrLessEquals(double value, { double epsilon = precisionErrorTolerance }) { - return _MoreOrLessEquals(value, epsilon); -} - -class _MoreOrLessEquals extends Matcher { - const _MoreOrLessEquals(this.value, this.epsilon) - : assert(epsilon >= 0); - - final double value; - final double epsilon; - - @override - bool matches(dynamic object, Map matchState) { - if (object is! double) { - return false; - } - if (object == value) { - return true; - } - return (object - value).abs() <= epsilon; - } - - @override - Description describe(Description description) => description.add('$value (±$epsilon)'); - - @override - Description describeMismatch(dynamic item, Description mismatchDescription, Map matchState, bool verbose) { - return super.describeMismatch(item, mismatchDescription, matchState, verbose) - ..add('$item is not in the range of $value (±$epsilon).'); - } -} From 4dfd3ebb5d0fff5c7673d3664a768f824f2bb115 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 2 Feb 2023 16:07:21 -0500 Subject: [PATCH 4/4] fix wasm tests --- lib/web_ui/test/lerp_test.dart | 8 ++++---- lib/web_ui/test/matchers.dart | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/web_ui/test/lerp_test.dart b/lib/web_ui/test/lerp_test.dart index bc56301b3247e..65dde6513fbd7 100644 --- a/lib/web_ui/test/lerp_test.dart +++ b/lib/web_ui/test/lerp_test.dart @@ -37,8 +37,8 @@ void testMain() { expect(lerpDouble(0.0, 10.0, -5.0), within(from: -50.0)); expect(lerpDouble(10.0, 0.0, -5.0), within(from: 60.0)); - expect(lerpDouble(0, 10, -5), within(from: -50)); - expect(lerpDouble(10, 0, -5), within(from: 60)); + expect(lerpDouble(0, 10, -5), within(from: -50.0)); + expect(lerpDouble(10, 0, -5), within(from: 60.0)); }); test('lerpDouble should return the start value at 0.0', () { @@ -78,8 +78,8 @@ void testMain() { expect(lerpDouble(0.0, 10.0, 5.0), within(from: 50.0)); expect(lerpDouble(10.0, 0.0, 5.0), within(from: -40.0)); - expect(lerpDouble(0, 10, 5), within(from: 50)); - expect(lerpDouble(10, 0, 5), within(from: -40)); + expect(lerpDouble(0, 10, 5), within(from: 50.0)); + expect(lerpDouble(10, 0, 5), within(from: -40.0)); }); test('lerpDouble should return input value in all cases if begin/end are equal', () { diff --git a/lib/web_ui/test/matchers.dart b/lib/web_ui/test/matchers.dart index 73e21e6cea2ff..df6124d0790d5 100644 --- a/lib/web_ui/test/matchers.dart +++ b/lib/web_ui/test/matchers.dart @@ -80,7 +80,7 @@ typedef DistanceFunction = double Function(T a, T b); /// /// Calling an instance of this type must either be done dynamically, or by /// first casting it to a [DistanceFunction] for some concrete T. -typedef AnyDistanceFunction = num Function(Never a, Never b); +typedef AnyDistanceFunction = double Function(Never a, Never b); const Map _kStandardDistanceFunctions = {