print_test.dart 1.89 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:fake_async/fake_async.dart';
6
import 'package:flutter/foundation.dart';
7
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
8

9
import 'capture_output.dart';
Adam Barth's avatar
Adam Barth committed
10 11 12 13 14

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

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

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

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

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

  test('debugPrint throttling', () {
42
    FakeAsync().run((FakeAsync async) {
Adam Barth's avatar
Adam Barth committed
43
      List<String> log = captureOutput(() {
44
        debugPrintThrottled('${'A' * (22 * 1024)}\nB');
Adam Barth's avatar
Adam Barth committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      });
      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);
    });
  });
60 61 62 63 64 65 66 67 68 69 70 71

  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
72
}