feat: Added closeToVector in flame_test (#1245)

Added matcher closeToVector to help test equality of vectors. This matcher

    follows standard Flutter convention for writing tests as expect(value, matcher);
    provides meaningful description of the mismatch in case the test fails.

Also: fixed the name of the fails_assert_test, which wasn't running automatically because the file name did not end with _test.
This commit is contained in:
Pasha Stetsenko
2021-12-18 10:32:16 -08:00
committed by GitHub
parent d70284033c
commit af45ea6cc4
4 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1 @@
46.0

View File

@@ -0,0 +1,43 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:vector_math/vector_math_64.dart';
/// Returns a matcher which matches if the argument vector is within distance
/// [epsilon] of point ([x], [y]). For example:
///
/// ```dart
/// expect(scale, closeToVector(2, -2));
/// expect(position, closeToVector(120, 150, epsilon: 1e-10));
/// ```
Matcher closeToVector(double x, double y, {double epsilon = 1e-15}) {
return _IsCloseTo(Vector2(x, y), epsilon);
}
class _IsCloseTo extends Matcher {
const _IsCloseTo(this._value, this._epsilon);
final Vector2 _value;
final double _epsilon;
@override
bool matches(dynamic item, Map matchState) {
return (item is Vector2) && (item - _value).length <= _epsilon;
}
@override
Description describe(Description description) => description
.add('a Vector2 object within $_epsilon of (${_value.x}, ${_value.y})');
@override
Description describeMismatch(
dynamic item,
Description mismatchDescription,
Map matchState,
bool verbose,
) {
if (item is! Vector2) {
return mismatchDescription.add('is not an instance of Vector2');
}
final distance = (item - _value).length;
return mismatchDescription.add('is at distance $distance');
}
}

View File

@@ -0,0 +1,42 @@
import 'package:flame_test/src/close_to_vector.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vector_math/vector_math_64.dart';
void main() {
group('closeToVector', () {
test('matches normally', () {
expect(Vector2.zero(), closeToVector(0, 0));
expect(Vector2(-14, 99), closeToVector(-14, 99));
expect(Vector2(1e-20, -1e-16), closeToVector(0, 0));
expect(Vector2(1.0001, 2.0), closeToVector(1, 2, epsilon: 0.01));
expect(Vector2(13, 14), closeToVector(10, 10, epsilon: 5));
});
test('fails on type mismatch', () {
try {
expect(3.14, closeToVector(0, 0));
} on TestFailure catch (e) {
expect(
e.message,
contains('Expected: a Vector2 object within 1e-15 of (0.0, 0.0)'),
);
expect(e.message, contains('Actual: <3.14>'));
expect(e.message, contains('Which: is not an instance of Vector2'));
}
});
test('fails on value mismatch', () {
try {
expect(Vector2(101, 217), closeToVector(100, 220));
} on TestFailure catch (e) {
expect(
e.message,
contains('Expected: a Vector2 object within 1e-15 of (100.0, 220.0)'),
);
expect(e.message, contains('Actual: Vector2:<[101.0,217.0]>'));
expect(e.message, contains('Which: is at distance 3.16227766'));
}
});
});
}