TestEventSequence<T>.fromString constructor

TestEventSequence<T>.fromString(
  1. String marbles, {
  2. Map<String, T> values = const {},
  3. Object error = 'Error',
})

Converts a string of marbles to an event sequence.

Implementation

factory TestEventSequence.fromString(String marbles,
    {Map<String, T> values = const {}, Object error = 'Error'}) {
  final sequence = <TestEvent<T>>[];
  var index = 0, withinGroup = false;
  for (var i = 0; i < marbles.length; i++) {
    if (marbles[i].trim().isEmpty) {
      continue; // ignore all whitespaces
    }
    switch (marbles[i]) {
      case advanceMarker:
        break;
      case groupStartMarker:
        if (withinGroup) {
          throw ArgumentError.value(marbles, 'marbles', 'Invalid grouping.');
        }
        withinGroup = true;
      case groupEndMarker:
        if (!withinGroup) {
          throw ArgumentError.value(marbles, 'marbles', 'Invalid grouping.');
        }
        withinGroup = false;
      case subscribeMarker:
        if (sequence.whereType<SubscribeEvent<T>>().isNotEmpty) {
          throw ArgumentError.value(
              marbles, 'marbles', 'Repeated subscription.');
        }
        sequence.add(SubscribeEvent(index));
      case unsubscribeMarker:
        if (sequence.whereType<UnsubscribeEvent<T>>().isNotEmpty) {
          throw ArgumentError.value(
              marbles, 'marbles', 'Repeated unsubscription.');
        }
        sequence.add(UnsubscribeEvent(index));
      case completeMarker:
        sequence.add(WrappedEvent<T>(index, Event<T>.complete()));
      case errorMarker:
        sequence.add(WrappedEvent<T>(
            index, Event<T>.error(error, StackTrace.current)));
      default:
        final marble = marbles[i];
        final value = values.containsKey(marble) ? values[marble] : marble;
        sequence.add(WrappedEvent<T>(index, Event<T>.next(value as T)));
    }
    if (!withinGroup) {
      index++;
    }
  }
  if (withinGroup) {
    throw ArgumentError.value(marbles, 'marbles', 'Invalid grouping.');
  }
  return TestEventSequence(sequence, values: values);
}