emits function

StreamMatcher emits(
  1. Object? matcher

Returns a StreamMatcher for matcher.

If matcher is already a StreamMatcher, it's returned as-is. If it's any other Matcher, this matches a single event that matches that matcher. If it's any other Object, this matches a single event that's equal to that object.

This functions like wrapMatcher for StreamMatchers: it can convert any matcher-like value into a proper StreamMatcher.


StreamMatcher emits(Object? matcher) {
  if (matcher is StreamMatcher) return matcher;
  var wrapped = wrapMatcher(matcher);

  var matcherDescription = wrapped.describe(StringDescription());

  return StreamMatcher((queue) async {
    if (!await queue.hasNext) return '';

    var matchState = {};
    var actual = await queue.next;
    if (wrapped.matches(actual, matchState)) return null;

    var mismatchDescription = StringDescription();
    wrapped.describeMismatch(actual, mismatchDescription, matchState, false);

    if (mismatchDescription.length == 0) return '';
    return 'emitted an event that $mismatchDescription';
      // TODO(nweiz): add "should" once matcher#42 is fixed.
      'emit an event that $matcherDescription');