debug_test.dart 1.53 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

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

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

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

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

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