Skip to content

Commit f980d66

Browse files
Add a matcher for Matrix4 that includes epsilon (#107326)
1 parent acb0a47 commit f980d66

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

packages/flutter_test/lib/src/matchers.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ Matcher rectMoreOrLessEquals(Rect value, { double epsilon = precisionErrorTolera
266266
return _IsWithinDistance<Rect>(_rectDistance, value, epsilon);
267267
}
268268

269+
/// Asserts that two [Matrix4]s are equal, within some tolerated error.
270+
///
271+
/// {@macro flutter.flutter_test.moreOrLessEquals}
272+
///
273+
/// See also:
274+
///
275+
/// * [moreOrLessEquals], which is for [double]s.
276+
/// * [offsetMoreOrLessEquals], which is for [Offset]s.
277+
Matcher matrixMoreOrLessEquals(Matrix4 value, { double epsilon = precisionErrorTolerance }) {
278+
return _IsWithinDistance<Matrix4>(_matrixDistance, value, epsilon);
279+
}
280+
269281
/// Asserts that two [Offset]s are equal, within some tolerated error.
270282
///
271283
/// {@macro flutter.flutter_test.moreOrLessEquals}
@@ -1144,6 +1156,14 @@ double _rectDistance(Rect a, Rect b) {
11441156
return delta;
11451157
}
11461158

1159+
double _matrixDistance(Matrix4 a, Matrix4 b) {
1160+
double delta = 0.0;
1161+
for (int i = 0; i < 16; i += 1) {
1162+
delta = math.max<double>((a[i] - b[i]).abs(), delta);
1163+
}
1164+
return delta;
1165+
}
1166+
11471167
double _sizeDistance(Size a, Size b) {
11481168
// TODO(a14n): remove ignore when lint is updated, https://github.com/dart-lang/linter/issues/1843
11491169
// ignore: unnecessary_parenthesis

packages/flutter_test/test/matchers_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// flutter_ignore_for_file: golden_tag (see analyze.dart)
66

7+
import 'dart:math' as math;
78
import 'dart:typed_data';
89

910
import 'package:flutter/rendering.dart';
@@ -197,6 +198,38 @@ void main() {
197198
expect(-11.0, moreOrLessEquals(11.0, epsilon: 100.0));
198199
});
199200

201+
test('matrixMoreOrLessEquals', () {
202+
expect(
203+
Matrix4.rotationZ(math.pi),
204+
matrixMoreOrLessEquals(Matrix4.fromList(<double>[
205+
-1, 0, 0, 0,
206+
0, -1, 0, 0,
207+
0, 0, 1, 0,
208+
0, 0, 0, 1,
209+
]))
210+
);
211+
212+
expect(
213+
Matrix4.rotationZ(math.pi),
214+
matrixMoreOrLessEquals(Matrix4.fromList(<double>[
215+
-2, 0, 0, 0,
216+
0, -2, 0, 0,
217+
0, 0, 1, 0,
218+
0, 0, 0, 1,
219+
]), epsilon: 2)
220+
);
221+
222+
expect(
223+
Matrix4.rotationZ(math.pi),
224+
isNot(matrixMoreOrLessEquals(Matrix4.fromList(<double>[
225+
-2, 0, 0, 0,
226+
0, -2, 0, 0,
227+
0, 0, 1, 0,
228+
0, 0, 0, 1,
229+
])))
230+
);
231+
});
232+
200233
test('rectMoreOrLessEquals', () {
201234
expect(
202235
const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0),

0 commit comments

Comments
 (0)