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

Negate matcher #743

Closed
passsy opened this Issue Jan 2, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@passsy

passsy commented Jan 2, 2018

When writing tests I miss some negation matchers. There is a contains matcher but not a doesNotContain matcher. Same for equals which lacks a isNotEqualTo. Is this intentional?

I wrote a not(Matcher) matcher negating a matcher but it doesn't provide good error messages:

test("does not contain", () {
  expect([1, 2, 4], not(contains(3))); // success

  expect([1, 2, 4], not(contains(4))); // fail
  // Expected: NOT contains <4>
  //  Actual: [1, 2, 4]
});
Matcher not<T>(Matcher matcher) => new NotMatcher(matcher);

class NotMatcher extends Matcher {
  final Matcher matcher;

  NotMatcher(this.matcher);

  @override
  Description describe(Description description) {
    description.add("NOT ");
    matcher.describe(description);
    return description;
  }

  @override
  Description describeMismatch(
      item, Description mismatchDescription, Map matchState, bool verbose) {
    return matcher.describeMismatch(
        item, mismatchDescription, matchState, verbose);
  }

  @override
  bool matches(item, Map matchState) => !matcher.matches(item, matchState);
}

Would the negation patterns be useful for this project? I'd like to add Matchers similar to AssertJ.

@natebosch

This comment has been minimized.

Show comment
Hide comment
Member

natebosch commented Jan 2, 2018

@passsy

This comment has been minimized.

Show comment
Hide comment
@passsy

passsy Jan 2, 2018

totally missed that!

passsy commented Jan 2, 2018

totally missed that!

@nex3 nex3 closed this Jan 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment