unorderedMatches method

void unorderedMatches(
  1. Iterable<Condition<T>> expected
)

Expects that the iterable contains elements which match all conditions of expected in any order.

Should not be used for very large collections, runtime is O(n^2.5) in the worst case where conditions match many elements, and O(n^2) in more typical cases.

Implementation

void unorderedMatches(Iterable<Condition<T>> expected) {
  context.expect(() => prefixFirst('unordered matches ', literal(expected)),
      (actual) {
    final which = unorderedCompare(
      actual,
      expected,
      (actual, expected) => softCheck(actual, expected) == null,
      (expected, index, count) => [
        'has no element matching the condition at index $index:',
        ...describe(expected),
        if (count > 1) 'or ${count - 1} other conditions',
      ],
      (actual, index, count) => [
        ...prefixFirst(
            'has an unmatched element at index $index: ', literal(actual)),
        if (count > 1) 'and ${count - 1} other unmatched elements',
      ],
    );
    if (which == null) return null;
    return Rejection(which: which);
  });
}