From 37bc48f26e256c3088516cc156a184214e51c380 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Mon, 22 Apr 2019 09:40:19 -0700 Subject: [PATCH] rectMoreOrLess equals, prep for 64bit rects (#30942) * rectMoreOrLess equals, prep for 64bit rects --- .../floating_action_button_location_test.dart | 8 ++++---- .../flutter/test/material/scaffold_test.dart | 8 ++++---- packages/flutter_test/lib/src/matchers.dart | 19 +++++++++++++++++++ packages/flutter_test/test/matchers_test.dart | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/flutter/test/material/floating_action_button_location_test.dart b/packages/flutter/test/material/floating_action_button_location_test.dart index 9777747c1c..e7c4ff56df 100644 --- a/packages/flutter/test/material/floating_action_button_location_test.dart +++ b/packages/flutter/test/material/floating_action_button_location_test.dart @@ -326,7 +326,7 @@ void main() { ), ), ); - expect(tester.getRect(find.byType(FloatingActionButton)), Rect.fromLTWH(16.0, 28.0, 56.0, 56.0)); + expect(tester.getRect(find.byType(FloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTWH(16.0, 28.0, 56.0, 56.0))); }); testWidgets('End-top floating action button location RTL', (WidgetTester tester) async { @@ -342,7 +342,7 @@ void main() { ), ), ); - expect(tester.getRect(find.byType(FloatingActionButton)), Rect.fromLTWH(16.0, 28.0, 56.0, 56.0)); + expect(tester.getRect(find.byType(FloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTWH(16.0, 28.0, 56.0, 56.0))); }); testWidgets('Start-top floating action button location RTL', (WidgetTester tester) async { @@ -358,7 +358,7 @@ void main() { ), ), ); - expect(tester.getRect(find.byType(FloatingActionButton)), Rect.fromLTWH(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0)); + expect(tester.getRect(find.byType(FloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTWH(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0))); }); testWidgets('End-top floating action button location LTR', (WidgetTester tester) async { @@ -371,7 +371,7 @@ void main() { ), ), ); - expect(tester.getRect(find.byType(FloatingActionButton)), Rect.fromLTWH(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0)); + expect(tester.getRect(find.byType(FloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTWH(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0))); }); } diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index 176d10d9b5..3d0598830e 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -762,13 +762,13 @@ void main() { expect(tester.getRect(find.byKey(appBar)), Rect.fromLTRB(0.0, 0.0, 800.0, 43.0)); expect(tester.getRect(find.byKey(body)), Rect.fromLTRB(0.0, 43.0, 800.0, 348.0)); - expect(tester.getRect(find.byKey(floatingActionButton)), Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)); + expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(Rect.fromLTRB(36.0, 255.0, 113.0, 332.0))); expect(tester.getRect(find.byKey(persistentFooterButton)), Rect.fromLTRB(28.0, 357.0, 128.0, 447.0)); // Note: has 8px each top/bottom padding. expect(tester.getRect(find.byKey(drawer)), Rect.fromLTRB(596.0, 0.0, 800.0, 600.0)); expect(tester.getRect(find.byKey(bottomNavigationBar)), Rect.fromLTRB(0.0, 515.0, 800.0, 600.0)); expect(tester.getRect(find.byKey(insideAppBar)), Rect.fromLTRB(20.0, 30.0, 750.0, 43.0)); expect(tester.getRect(find.byKey(insideBody)), Rect.fromLTRB(20.0, 43.0, 750.0, 348.0)); - expect(tester.getRect(find.byKey(insideFloatingActionButton)), Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)); + expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTRB(36.0, 255.0, 113.0, 332.0))); expect(tester.getRect(find.byKey(insidePersistentFooterButton)), Rect.fromLTRB(28.0, 357.0, 128.0, 447.0)); expect(tester.getRect(find.byKey(insideDrawer)), Rect.fromLTRB(596.0, 30.0, 750.0, 540.0)); expect(tester.getRect(find.byKey(insideBottomNavigationBar)), Rect.fromLTRB(20.0, 515.0, 750.0, 540.0)); @@ -857,12 +857,12 @@ void main() { expect(tester.getRect(find.byKey(appBar)), Rect.fromLTRB(0.0, 0.0, 800.0, 43.0)); expect(tester.getRect(find.byKey(body)), Rect.fromLTRB(0.0, 43.0, 800.0, 400.0)); - expect(tester.getRect(find.byKey(floatingActionButton)), Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)); + expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(Rect.fromLTRB(36.0, 307.0, 113.0, 384.0))); expect(tester.getRect(find.byKey(persistentFooterButton)), Rect.fromLTRB(28.0, 442.0, 128.0, 532.0)); // Note: has 8px each top/bottom padding. expect(tester.getRect(find.byKey(drawer)), Rect.fromLTRB(596.0, 0.0, 800.0, 600.0)); expect(tester.getRect(find.byKey(insideAppBar)), Rect.fromLTRB(20.0, 30.0, 750.0, 43.0)); expect(tester.getRect(find.byKey(insideBody)), Rect.fromLTRB(20.0, 43.0, 750.0, 400.0)); - expect(tester.getRect(find.byKey(insideFloatingActionButton)), Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)); + expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(Rect.fromLTRB(36.0, 307.0, 113.0, 384.0))); expect(tester.getRect(find.byKey(insidePersistentFooterButton)), Rect.fromLTRB(28.0, 442.0, 128.0, 532.0)); expect(tester.getRect(find.byKey(insideDrawer)), Rect.fromLTRB(596.0, 30.0, 750.0, 540.0)); }); diff --git a/packages/flutter_test/lib/src/matchers.dart b/packages/flutter_test/lib/src/matchers.dart index 816cdeced4..47aca19016 100644 --- a/packages/flutter_test/lib/src/matchers.dart +++ b/packages/flutter_test/lib/src/matchers.dart @@ -222,6 +222,23 @@ Matcher moreOrLessEquals(double value, { double epsilon = 1e-10 }) { return _MoreOrLessEquals(value, epsilon); } +/// Asserts that two [Rect]s are equal, within some tolerated error. +/// +/// Two values are considered equal if the difference between them is within +/// 1e-10 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. +/// +/// See also: +/// +/// * [moreOrLessEquals], which is for [double]s. +/// * [within], which offers a generic version of this functionality that can +/// be used to match [Rect]s as well as other types. +Matcher rectMoreOrLessEquals(Rect value, { double epsilon = 1e-10 }) { + return _IsWithinDistance(_rectDistance, value, epsilon); +} + /// Asserts that two [String]s are equal after normalizing likely hash codes. /// /// A `#` followed by 5 hexadecimal digits is assumed to be a short hash code @@ -1004,6 +1021,8 @@ double _sizeDistance(Size a, Size b) { /// /// * [moreOrLessEquals], which is similar to this function, but specializes in /// [double]s and has an optional `epsilon` parameter. +/// * [rectMoreOrLessEquals], which is similar to this function, but +/// specializes in [Rect]s and has an optional `epsilon` parameter. /// * [closeTo], which specializes in numbers only. Matcher within({ @required num distance, diff --git a/packages/flutter_test/test/matchers_test.dart b/packages/flutter_test/test/matchers_test.dart index 29f0f72684..2c4a3f5312 100644 --- a/packages/flutter_test/test/matchers_test.dart +++ b/packages/flutter_test/test/matchers_test.dart @@ -185,6 +185,23 @@ void main() { expect(-11.0, moreOrLessEquals(11.0, epsilon: 100.0)); }); + test('rectMoreOrLessEquals', () { + expect( + Rect.fromLTRB(0.0, 0.0, 10.0, 10.0), + rectMoreOrLessEquals(Rect.fromLTRB(1e-11, 0.0, 10.0, 10.0000000001)), + ); + + expect( + Rect.fromLTRB(11.0, 11.0, 20.0, 20.0), + isNot(rectMoreOrLessEquals(Rect.fromLTRB(-11.0, -11.0, 20.0, 20.0), epsilon: 1.0)), + ); + + expect( + Rect.fromLTRB(11.0, 11.0, 20.0, 20.0), + rectMoreOrLessEquals(Rect.fromLTRB(-11.0, -11.0, 20.0, 20.0), epsilon: 100.0), + ); + }); + test('within', () { expect(0.0, within(distance: 0.1, from: 0.05)); expect(0.0, isNot(within(distance: 0.1, from: 0.2)));