gwt_<T> function

Future Function(T) gwt_<T>(
  1. Future body(
    1. T
    )
)

Prepares widget testing environment for given/when/then methods

An exception will be thrown if you don't wrap a test body by this.

Example:

testWidgets('My test', gwt_((tester) async {
  given(...);
  when(...);
  then(...);
}));

Implementation

Future Function(T) gwt_<T>(Future Function(T) body) {
  var givens = <String>[];
  var whens = <String>[];
  var thens = <String>[];
  final Map<Object?, Object?> zoneValues = {
    givenKey: givens,
    whenKey: whens,
    thenKey: thens,
    stateKey: State(),
  };

  return (value) async {
    await runZoned(() async {
      await body(value);
    }, zoneValues: zoneValues);

    var descriptions = <String>[];

    try {
      for (final entry in givens.asMap().entries) {
        final description =
            entry.value.isEmpty ? '#${entry.key + 1}' : entry.value;
        descriptions
            .add(entry.key == 0 ? 'Given $description' : '      $description');
      }
      for (var entry in whens.asMap().entries) {
        final description =
            entry.value.isEmpty ? '#${entry.key + 1}' : entry.value;
        descriptions
            .add(entry.key == 0 ? 'When $description' : '     $description');
      }
      for (var entry in thens.asMap().entries) {
        final description =
            entry.value.isEmpty ? '#${entry.key + 1}' : entry.value;
        descriptions
            .add(entry.key == 0 ? 'Then $description' : '     $description');
      }
    } catch (_) {
      for (var description in descriptions) {
        print(description);
      }
      rethrow;
    }
  };
}