containsInOrder method

void containsInOrder(
  1. Iterable<String> expected
)

Expects that the String contains each of the sub strings in expected in the given order, with any content between them.

For example, the following will succeed:

check('abcdefg').containsInOrder(['a','e']);

Implementation

void containsInOrder(Iterable<String> expected) {
  context.expect(() => prefixFirst('contains, in order: ', literal(expected)),
      (actual) {
    var fromIndex = 0;
    for (var s in expected) {
      var index = actual.indexOf(s, fromIndex);
      if (index < 0) {
        return Rejection(which: [
          ...prefixFirst(
              'does not have a match for the substring ', literal(s)),
          if (fromIndex != 0)
            'following the other matches up to character $fromIndex'
        ]);
      }
      fromIndex = index + s.length;
    }
    return null;
  });
}