Skip to content

make Matcher generic #3652

Closed
Closed
@DartBot

Description

@DartBot

This issue was originally filed by @seaneagan


It would be nice if dart:unittest's Matcher was generic:

interface Matcher<T> {
  bool matches(T item);
  //...
}

//...

Matcher<num> closeTo(num value, num delta);
Matcher<Map<K, V>> containsPair<K, V>(K key, V value);
Matcher<Map<Dynamic, V>> containsValue<V>(V value);
Matcher<Map<K, Dynamic>> containsKey<K>(K key);
Matcher<String> startsWith(String str);
Matcher<String> endsWith(String str);
Matcher<String> matches(Pattern pattern);
Matcher<String> equalsIgnoringCase(String str);
Matcher<String> equalsIgnoringWhiteSpace(String str);
Matcher<T> lessThan<T extends Comparable<T>>(Comparable c);
Matcher<T> greaterThan<T extends Comparable<T>>(T c);
Matcher<T> lessThanOrEqualTo<T extends Comparable<T>>(T c);
Matcher<T> greaterThanOrEqualTo<T extends Comparable<T>>(T c);
Matcher<Collection<E>> every(Matcher<E>);
Matcher<Collection<E>> some(Matcher<E>);
Matcher<bool> get isTrue();
Matcher<bool> get isFalse();
Matcher<num> get isPositive();
Matcher<num> get isNegative();
Matcher<num> get isNonPositive();
Matcher<num> get isNonNegative();
Matcher<num> get isZero();
Matcher<num> get isNonZero();
Matcher<Function> get throws();
Matcher<Function> get returnsNormally();
Matcher<Future> get completes();
//etc.

If issue #4 were fixed, then "expect" could change to:

void expect<T>(T item, Matcher<T> matcher);
//...
expect<bool>(b, isTrue);
expect<num>(i, lessThan<num>(6));
expect<String>(s, matches(new Regexp(@­"\d+"));

a better option though might be to change expect to an instance method of Matcher, and rename it to "test":

isTrue.test(b);
isPositive.test(x);
matches(new Regexp(@­"\d+")).test(s);

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-pkgUsed for miscellaneous pkg/ packages not associated with specific area- teams.closed-not-plannedClosed as we don't intend to take action on the reported issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions