Based on #2461, I'd like to request we have a way of adding an annotation to Matchers that, when used, will trigger a lint if the type being matched against is not expected.
Something like:
import 'package:test/test.dart';
void main() {
test('MyMatcher description', () {
expect(2, myMatcher);
expect('2', myMatcher); // This would trigger a lint because the type `String` is not on the list of types for the annotation
});
}
final myMatcher = MyMatcher();
@Annotation([int])
class MyMatcher extends Matcher {
@override
Description describe(Description description) =>
StringDescription('an integer');
@override
bool matches(item, Map<dynamic, dynamic> matchState) =>
item is int && item > 0;
}
/// Consider built-in
class Annotation {
const Annotation(List<Type> types);
}
@srawlins' #2461 (comment):
For first-party matchers in the matcher package, I don't think it would be worth the extra complexity. So the feature would be for custom matchers implemented outside of the matcher and test packages.