Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Matcher generic #136

Closed
DartBot opened this issue Jun 5, 2015 · 7 comments
Closed

make Matcher generic #136

DartBot opened this issue Jun 5, 2015 · 7 comments
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="96" height="96"hspace="10"> Issue by seaneagan
Originally opened as dart-lang/sdk#3652


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 dart-lang/sdk#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);

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="48" height="48"hspace="10"> Comment by seaneagan


If going the Matcher#test route, it would be nice to have it be a "final method" as in Java, either in BaseMatcher, or just rename BaseMatcher to Matcher and get rid of the explicit interface.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/3276024?v=3" align="left" width="48" height="48"hspace="10"> Comment by anders-sandholm


Added Area-Test, Triaged labels.

@DartBot DartBot added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) Priority-Medium labels Jun 5, 2015
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/3276024?v=3" align="left" width="48" height="48"hspace="10"> Comment by anders-sandholm


Removed Area-Test label.
Added Area-UnitTest label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/2762632?v=3" align="left" width="48" height="48"hspace="10"> Comment by gramster


In many cases, the type of the argument is tested anyway and the matcher will fail with an appropriate error message if it is the wrong type. This could be done at compile time rather than run time using generics, but it adds developer pain to have to type every call to expect.


Added NotPlanned label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="48" height="48"hspace="10"> Comment by seaneagan


Presumably if Dart added generic methods (issue dart-lang/sdk#254) the type argument to the method call would be optional just like all other type annotations are. Also, type arguments could likely be inferred in many cases.

Even if generic methods aren't added this would still make it much easier to tell which types each matcher matches against, since you can tell merely by looking at the method signature or field definitition, as opposed to having to read the doc comments.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/17034?v=3" align="left" width="48" height="48"hspace="10"> Comment by kevmoo


Added Pkg-Unittest label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/17034?v=3" align="left" width="48" height="48"hspace="10"> Comment by kevmoo


Removed Area-UnitTest label.
Added Area-Pkg label.

@DartBot DartBot closed this as completed Jun 5, 2015
@DartBot DartBot added closed-not-planned Closed as we don't intend to take action on the reported issue and removed NotPlanned labels Jun 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

1 participant