anyOf method

  1. @awaitNotRequired
Future<void> anyOf(
  1. Iterable<Condition<StreamQueue<T>>> conditions
)

Expects that the stream statisfies at least one condition from conditions.

If this expectation fails, the source queue will be left in its original state. If this expectation succeeds, consumes the same events from the source queue as the satisfied condition. If multiple conditions are satisfied, chooses the condition which consumed the most events.

import 'package:checks/checks.dart';
import 'package:test/scaffolding.dart';

void main() {
  test('anyOf', () async {
    // This check succeeds, the second condition is satisfied.
    await check(Stream.fromIterable(['ready'])).withQueue.anyOf([
      .it()..emits(.it()..equals('starting')),
      .it()..emits(.it()..equals('ready')),
    ]);

    // This check fails because no condition is satisfied.
    await check(
      Stream.fromIterable(['ready']),
    ).withQueue.anyOf([.it()..emits(.it()..equals('starting')), .it()..isDone]);
    // Expected: a Stream<String> that:
    //   satisfies one of:
    //     emits 'starting'
    //   or,
    //     is done
    // Actual: a stream
    // Which: failed to satisfy any condition
    // failed the condition at index 0 because it:
    //   Actual: emits 'ready'
    //   Which: differs at offset 0:
    //     starting
    //     ready
    //     ^
    // failed the condition at index 1 because it:
    //   Actual: a stream that emitted 'ready'
    //   Which: emitted a value before closing
  });
}

Implementation

@awaitNotRequired
Future<void> anyOf(Iterable<Condition<StreamQueue<T>>> conditions) {
  conditions = conditions.toList();
  if (conditions.isEmpty) throw ArgumentError('conditions may not be empty');
  final descriptions = <Iterable<String>>[];
  return context.expectAsync(
    () => descriptions.isEmpty
        ? ['satisfies any of ${conditions.length} conditions']
        : [
            'satisfies one of:',
            for (var i = 0; i < descriptions.length; i++) ...[
              ...descriptions[i],
              if (i < descriptions.length - 1) 'or,',
            ],
          ],
    (actual) async {
      final transaction = actual.startTransaction();
      StreamQueue<T>? longestAccepted;
      final descriptionFuture = conditions
          .map((c) async => await c.describe())
          .wait;
      // soft checks are unexpected to throw, if they do the unhandled async
      // error should surface in the test runner.
      final failures = await conditions.map((condition) async {
        final copy = transaction.newQueue();
        final failure = await condition.softCheck(copy);
        if (failure == null &&
            (longestAccepted == null ||
                copy.eventsDispatched > longestAccepted!.eventsDispatched)) {
          longestAccepted = copy;
        }
        return failure;
      }).wait;

      descriptions.addAll(await descriptionFuture);
      if (longestAccepted case final accepted?) {
        transaction.commit(accepted);
        return null;
      }
      transaction.reject();
      return Rejection(
        actual: ['a stream'],
        which: [
          'failed to satisfy any condition',
          for (var i = 0; i < failures.length; i++) ...[
            'failed the condition at index $i because it:',
            ...indent(failures[i]!.describeNested()),
          ],
        ],
      );
    },
  );
}