assertions_test.dart 2.61 KB
Newer Older
1 2 3 4 5
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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 '../flutter_test_alternative.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20

import 'capture_output.dart';

void main() {
  test('debugPrintStack', () {
    final List<String> log = captureOutput(() {
      debugPrintStack(label: 'Example label', maxFrames: 7);
    });
    expect(log[0], contains('Example label'));
    expect(log[1], contains('debugPrintStack'));
  });

  test('debugPrintStack', () {
    final List<String> log = captureOutput(() {
21
      final FlutterErrorDetails details = FlutterErrorDetails(
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        exception: 'Example exception',
        stack: StackTrace.current,
        library: 'Example library',
        context: 'Example context',
        informationCollector: (StringBuffer information) {
          information.writeln('Example information');
        },
      );

      FlutterError.dumpErrorToConsole(details);
    });

    expect(log[0], contains('EXAMPLE LIBRARY'));
    expect(log[1], contains('Example context'));
    expect(log[2], contains('Example exception'));

    final String joined = log.join('\n');

    expect(joined, contains('captureOutput'));
    expect(joined, contains('\nExample information\n'));
  });

44 45
  test('FlutterErrorDetails.toString', () {
    expect(
46
      FlutterErrorDetails(
47 48 49 50 51 52 53 54 55 56 57 58
        exception: 'MESSAGE',
        library: 'LIBRARY',
        context: 'CONTEXTING',
        informationCollector: (StringBuffer information) {
          information.writeln('INFO');
        },
      ).toString(),
      'Error caught by LIBRARY, thrown CONTEXTING.\n'
      'MESSAGE\n'
      'INFO',
    );
    expect(
59
      FlutterErrorDetails(
60 61 62 63 64 65 66 67 68 69 70
        library: 'LIBRARY',
        context: 'CONTEXTING',
        informationCollector: (StringBuffer information) {
          information.writeln('INFO');
        },
      ).toString(),
      'Error caught by LIBRARY, thrown CONTEXTING.\n'
      '  null\n'
      'INFO',
    );
    expect(
71
      FlutterErrorDetails(
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        exception: 'MESSAGE',
        context: 'CONTEXTING',
        informationCollector: (StringBuffer information) {
          information.writeln('INFO');
        },
      ).toString(),
      'Error caught by Flutter framework, thrown CONTEXTING.\n'
      'MESSAGE\n'
      'INFO',
    );
    expect(
      const FlutterErrorDetails(
        exception: 'MESSAGE',
      ).toString(),
      'Error caught by Flutter framework.\n'
87
      'MESSAGE',
88 89 90 91
    );
    expect(
      const FlutterErrorDetails().toString(),
      'Error caught by Flutter framework.\n'
92
      '  null',
93 94
    );
  });
95
}