debug_test.dart 1.55 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10 11 12 13 14 15 16
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('debugInstrumentAction', () {
    DebugPrintCallback originalDebugPrintCallback;
    StringBuffer printBuffer;

    setUp(() {
      debugInstrumentationEnabled = true;
17
      printBuffer = StringBuffer();
18
      originalDebugPrintCallback = debugPrint;
19
      debugPrint = (String message, { int wrapWidth }) {
20 21 22 23 24 25 26 27 28
        printBuffer.writeln(message);
      };
    });

    tearDown(() {
      debugInstrumentationEnabled = false;
      debugPrint = originalDebugPrintCallback;
    });

29
    test('works with non-failing actions', () async {
30 31 32 33 34 35 36
      final int result = await debugInstrumentAction<int>('no-op', () async {
        debugPrint('action()');
        return 1;
      });
      expect(result, 1);
      expect(
        printBuffer.toString(),
37
        matches(RegExp('^action\\(\\)\nAction "no-op" took .+\$', multiLine: true)),
38 39 40
      );
    });

41
    test('returns failing future if action throws', () async {
42 43
      try {
        await debugInstrumentAction<void>('throws', () async {
44
          await Future<void>.delayed(Duration.zero);
45 46 47 48 49 50 51 52 53 54
          throw 'Error';
        });
        fail('Error expected but not thrown');
      } on String catch (error) {
        expect(error, 'Error');
        expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
      }
    });
  });
}