Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit fd84382

Browse files
committed
feature(Matcher): add type param to Matcher for item in functions
Eliminates 4 errors when `implicit-casts: false` is set
1 parent f089461 commit fd84382

7 files changed

+17
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.12.3
2+
3+
- Add type parameters to `Matcher` class to specify the type of `item` in
4+
`matches` and `describeMismatch` functions.
5+
16
## 0.12.2+1
27

38
- Updated SDK version to 2.0.0-dev.17.0

lib/src/core_matchers.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ const Matcher isNaN = const _IsNaN();
6969
/// A matcher that matches any non-NaN value.
7070
const Matcher isNotNaN = const _IsNotNaN();
7171

72-
class _IsNaN extends Matcher {
72+
class _IsNaN extends Matcher<num> {
7373
const _IsNaN();
7474
bool matches(item, Map matchState) => double.nan.compareTo(item) == 0;
7575
Description describe(Description description) => description.add('NaN');
7676
}
7777

78-
class _IsNotNaN extends Matcher {
78+
class _IsNotNaN extends Matcher<num> {
7979
const _IsNotNaN();
8080
bool matches(item, Map matchState) => double.nan.compareTo(item) != 0;
8181
Description describe(Description description) => description.add('not NaN');

lib/src/custom_matcher.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import 'util.dart';
3030
/// ```dart
3131
/// expect(inventoryItem, new HasPrice(greaterThan(0)));
3232
/// ```
33-
class CustomMatcher extends Matcher {
33+
class CustomMatcher<T> extends Matcher<T> {
3434
final String _featureDescription;
3535
final String _featureName;
3636
final Matcher _matcher;
@@ -39,9 +39,9 @@ class CustomMatcher extends Matcher {
3939
: this._matcher = wrapMatcher(matcher);
4040

4141
/// Override this to extract the interesting feature.
42-
Object featureValueOf(actual) => actual;
42+
Object featureValueOf(T actual) => actual;
4343

44-
bool matches(item, Map matchState) {
44+
bool matches(T item, Map matchState) {
4545
try {
4646
var f = featureValueOf(item);
4747
if (_matcher.matches(f, matchState)) return true;

lib/src/equals_matcher.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef _RecursiveMatcher = List<String> Function(
1919
dynamic, dynamic, String, int);
2020

2121
/// A special equality matcher for strings.
22-
class _StringEqualsMatcher extends Matcher {
22+
class _StringEqualsMatcher extends Matcher<String> {
2323
final String _value;
2424

2525
_StringEqualsMatcher(this._value);

lib/src/interfaces.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ abstract class Description {
3131
/// so this does not need to be provided unless a more clear description is
3232
/// required. The other two methods ([matches] and [describe])
3333
/// must always be provided as they are highly matcher-specific.
34-
abstract class Matcher {
34+
abstract class Matcher<T> {
3535
const Matcher();
3636

3737
/// This does the matching of the actual vs expected values.
3838
/// [item] is the actual value. [matchState] can be supplied
3939
/// and may be used to add details about the mismatch that are too
4040
/// costly to determine in [describeMismatch].
41-
bool matches(item, Map matchState);
41+
bool matches(T item, Map matchState);
4242

4343
/// This builds a textual description of the matcher.
4444
Description describe(Description description);
@@ -51,7 +51,7 @@ abstract class Matcher {
5151
/// A few matchers make use of the [verbose] flag to provide detailed
5252
/// information that is not typically included but can be of help in
5353
/// diagnosing failures, such as stack traces.
54-
Description describeMismatch(item, Description mismatchDescription,
54+
Description describeMismatch(T item, Description mismatchDescription,
5555
Map matchState, bool verbose) =>
5656
mismatchDescription;
5757
}

lib/src/iterable_matchers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class _UnorderedEquals extends _UnorderedMatches {
130130

131131
/// Iterable matchers match against [Iterable]s. We add this intermediate
132132
/// class to give better mismatch error messages than the base Matcher class.
133-
abstract class _IterableMatcher extends Matcher {
133+
abstract class _IterableMatcher extends Matcher<Iterable> {
134134
const _IterableMatcher();
135135
Description describeMismatch(
136136
item, Description mismatchDescription, Map matchState, bool verbose) {

test/custom_matcher_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class _BadCustomMatcher extends CustomMatcher {
1212
Object featureValueOf(actual) => throw new Exception("bang");
1313
}
1414

15-
class _HasPrice extends CustomMatcher {
15+
class _HasPrice extends CustomMatcher<Widget> {
1616
_HasPrice(matcher) : super("Widget with a price that is", "price", matcher);
17-
Object featureValueOf(actual) => actual.price;
17+
Object featureValueOf(Widget actual) => actual.price;
1818
}
1919

2020
void main() {

0 commit comments

Comments
 (0)