pairwiseComparesTo<S> method

void pairwiseComparesTo<S>(
  1. List<S> expected,
  2. Condition<T> elementCondition(
    1. S
    ),
  3. String description
)

Expects that the iterable contains elements that correspond by the elementCondition exactly to each element in expected.

Fails if the iterable has a different length than expected.

For each element in the iterable, calls elementCondition with the corresponding element from expected to get the specific condition for that index.

description is used in the Expected clause. It should be a predicate without the object, for example with the description 'is less than' the full expectation will be: "pairwise is less than $expected"

Implementation

void pairwiseComparesTo<S>(List<S> expected,
    Condition<T> Function(S) elementCondition, String description) {
  context.expect(() {
    return prefixFirst('pairwise $description ', literal(expected));
  }, (actual) {
    final iterator = actual.iterator;
    for (var i = 0; i < expected.length; i++) {
      final expectedValue = expected[i];
      if (!iterator.moveNext()) {
        return Rejection(which: [
          'has too few elements, there is no element to match at index $i'
        ]);
      }
      final actualValue = iterator.current;
      final failure = softCheck(actualValue, elementCondition(expectedValue));
      if (failure == null) continue;
      final innerDescription = describe<T>(elementCondition(expectedValue));
      final which = failure.rejection.which;
      return Rejection(which: [
        'does not have an element at index $i that:',
        ...innerDescription,
        ...prefixFirst(
            'Actual element at index $i: ', failure.rejection.actual),
        if (which != null) ...prefixFirst('Which: ', which),
      ]);
    }
    if (!iterator.moveNext()) return null;
    return Rejection(which: [
      'has too many elements, expected exactly ${expected.length}'
    ]);
  });
}