debug_test.dart 1.47 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
// 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', () {
10 11
    late DebugPrintCallback originalDebugPrintCallback;
    late StringBuffer printBuffer;
12 13 14

    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
      await expectLater(
        () => debugInstrumentAction<void>('throws', () async {
42
          await Future<void>.delayed(Duration.zero);
43
          throw 'Error';
44 45 46 47
        }),
        throwsA('Error'),
      );
      expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
48 49 50
    });
  });
}