print_test.dart 1.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth committed
2 3 4 5
// 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';
6
import 'package:fake_async/fake_async.dart';
7
import '../flutter_test_alternative.dart';
Adam Barth's avatar
Adam Barth committed
8

9
import 'capture_output.dart';
Adam Barth's avatar
Adam Barth committed
10

11 12
String? foo;

Adam Barth's avatar
Adam Barth committed
13 14 15 16
void main() {
  test('debugPrint', () {
    expect(
      captureOutput(() { debugPrintSynchronously('Hello, world'); }),
17
      equals(<String>['Hello, world']),
Adam Barth's avatar
Adam Barth committed
18 19 20 21
    );

    expect(
      captureOutput(() { debugPrintSynchronously('Hello, world', wrapWidth: 10); }),
22
      equals(<String>['Hello,\nworld']),
Adam Barth's avatar
Adam Barth committed
23 24 25 26 27
    );

    for (int i = 0; i < 14; ++i) {
      expect(
        captureOutput(() { debugPrintSynchronously('Hello,   world', wrapWidth: i); }),
28
        equals(<String>['Hello,\nworld']),
Adam Barth's avatar
Adam Barth committed
29 30 31 32 33
      );
    }

    expect(
      captureOutput(() { debugPrintThrottled('Hello, world'); }),
34
      equals(<String>['Hello, world']),
Adam Barth's avatar
Adam Barth committed
35 36 37 38
    );

    expect(
      captureOutput(() { debugPrintThrottled('Hello, world', wrapWidth: 10); }),
39
      equals(<String>['Hello,', 'world']),
Adam Barth's avatar
Adam Barth committed
40 41 42 43
    );
  });

  test('debugPrint throttling', () {
44
    FakeAsync().run((FakeAsync async) {
Adam Barth's avatar
Adam Barth committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      List<String> log = captureOutput(() {
        debugPrintThrottled('A' * (22 * 1024) + '\nB');
      });
      expect(log.length, 1);
      async.elapse(const Duration(seconds: 2));
      expect(log.length, 2);

      log = captureOutput(() {
        debugPrintThrottled('C' * (22 * 1024));
        debugPrintThrottled('D');
      });

      expect(log.length, 1);
      async.elapse(const Duration(seconds: 2));
      expect(log.length, 2);
    });
  });
62 63 64 65 66 67 68 69 70 71 72 73

  test('debugPrint can print null', () {
    expect(
      captureOutput(() { debugPrintThrottled(null); }),
      equals(<String>['null']),
    );

    expect(
      captureOutput(() { debugPrintThrottled(null, wrapWidth: 80); }),
      equals(<String>['null']),
    );
  });
Adam Barth's avatar
Adam Barth committed
74
}