Add a matcher for Matrix4 that includes epsilon (#107326)

This commit is contained in:
Jonah Williams 2022-07-11 12:23:05 -07:00 committed by GitHub
parent acb0a47616
commit f980d6654e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View file

@ -266,6 +266,18 @@ Matcher rectMoreOrLessEquals(Rect value, { double epsilon = precisionErrorTolera
return _IsWithinDistance<Rect>(_rectDistance, value, epsilon); return _IsWithinDistance<Rect>(_rectDistance, value, epsilon);
} }
/// Asserts that two [Matrix4]s are equal, within some tolerated error.
///
/// {@macro flutter.flutter_test.moreOrLessEquals}
///
/// See also:
///
/// * [moreOrLessEquals], which is for [double]s.
/// * [offsetMoreOrLessEquals], which is for [Offset]s.
Matcher matrixMoreOrLessEquals(Matrix4 value, { double epsilon = precisionErrorTolerance }) {
return _IsWithinDistance<Matrix4>(_matrixDistance, value, epsilon);
}
/// Asserts that two [Offset]s are equal, within some tolerated error. /// Asserts that two [Offset]s are equal, within some tolerated error.
/// ///
/// {@macro flutter.flutter_test.moreOrLessEquals} /// {@macro flutter.flutter_test.moreOrLessEquals}
@ -1144,6 +1156,14 @@ double _rectDistance(Rect a, Rect b) {
return delta; return delta;
} }
double _matrixDistance(Matrix4 a, Matrix4 b) {
double delta = 0.0;
for (int i = 0; i < 16; i += 1) {
delta = math.max<double>((a[i] - b[i]).abs(), delta);
}
return delta;
}
double _sizeDistance(Size a, Size b) { double _sizeDistance(Size a, Size b) {
// TODO(a14n): remove ignore when lint is updated, https://github.com/dart-lang/linter/issues/1843 // TODO(a14n): remove ignore when lint is updated, https://github.com/dart-lang/linter/issues/1843
// ignore: unnecessary_parenthesis // ignore: unnecessary_parenthesis

View file

@ -4,6 +4,7 @@
// flutter_ignore_for_file: golden_tag (see analyze.dart) // flutter_ignore_for_file: golden_tag (see analyze.dart)
import 'dart:math' as math;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
@ -197,6 +198,38 @@ void main() {
expect(-11.0, moreOrLessEquals(11.0, epsilon: 100.0)); expect(-11.0, moreOrLessEquals(11.0, epsilon: 100.0));
}); });
test('matrixMoreOrLessEquals', () {
expect(
Matrix4.rotationZ(math.pi),
matrixMoreOrLessEquals(Matrix4.fromList(<double>[
-1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]))
);
expect(
Matrix4.rotationZ(math.pi),
matrixMoreOrLessEquals(Matrix4.fromList(<double>[
-2, 0, 0, 0,
0, -2, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]), epsilon: 2)
);
expect(
Matrix4.rotationZ(math.pi),
isNot(matrixMoreOrLessEquals(Matrix4.fromList(<double>[
-2, 0, 0, 0,
0, -2, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
])))
);
});
test('rectMoreOrLessEquals', () { test('rectMoreOrLessEquals', () {
expect( expect(
const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0), const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0),