error_reporting_test.dart 11.9 KB
Newer Older
1 2 3 4 5 6 7
// 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 'dart:async';

import 'package:flutter/foundation.dart';
8
import '../flutter_test_alternative.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

dynamic getAssertionErrorWithMessage() {
  try {
    assert(false, 'Message goes here.');
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

dynamic getAssertionErrorWithoutMessage() {
  try {
    assert(false);
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

dynamic getAssertionErrorWithLongMessage() {
  try {
    assert(false, 'word ' * 100);
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

Future<StackTrace> getSampleStack() async {
38
  return await Future<StackTrace>.sync(() => StackTrace.current);
39 40
}

41
Future<void> main() async {
42 43 44 45 46 47 48 49 50 51 52 53 54
  final List<String> console = <String>[];

  final StackTrace sampleStack = await getSampleStack();

  test('Error reporting - pretest', () async {
    expect(debugPrint, equals(debugPrintThrottled));
    debugPrint = (String message, { int wrapWidth }) {
      console.add(message);
    };
  });

  test('Error reporting - assert with message', () async {
    expect(console, isEmpty);
55
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
56 57 58
      exception: getAssertionErrorWithMessage(),
      stack: sampleStack,
      library: 'error handling test',
59 60 61 62
      context: ErrorDescription('testing the error handling logic'),
      informationCollector: () sync* {
       yield ErrorDescription('line 1 of extra information');
       yield ErrorHint('line 2 of extra information\n');
63 64
      },
    ));
65
    expect(console.join('\n'), matches(
66 67 68
      '^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
      'The following assertion was thrown testing the error handling logic:\n'
      'Message goes here\\.\n'
69 70
      '\'[^\']+flutter/test/foundation/error_reporting_test\\.dart\':\n'
      'Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
71
      '\n'
72
      'Either the assertion indicates an error in the framework itself, or we should provide substantially\n'
73 74
      'more information in this error message to help you determine and fix the underlying cause\\.\n'
      'In either case, please report this assertion by filing a bug on GitHub:\n'
75
      '  https://github\\.com/flutter/flutter/issues/new\\?template=BUG\\.md\n'
76 77
      '\n'
      'When the exception was thrown, this was the stack:\n'
78 79
      '#0      getSampleStack\\.<anonymous closure> \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
      '#2      getSampleStack \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
80
      '<asynchronous suspension>\n' // TODO(ianh): https://github.com/flutter/flutter/issues/4021
81
      '#3      main \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
82 83 84 85 86 87
      '(.+\n)+' // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
      '\\(elided [0-9]+ frames from package dart:async\\)\n'
      '\n'
      'line 1 of extra information\n'
      'line 2 of extra information\n'
      '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
88
    ));
89
    console.clear();
90
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
91 92 93 94 95 96 97 98 99
      exception: getAssertionErrorWithMessage(),
    ));
    expect(console.join('\n'), 'Another exception was thrown: Message goes here.');
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - assert with long message', () async {
    expect(console, isEmpty);
100
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
101 102
      exception: getAssertionErrorWithLongMessage(),
    ));
103
    expect(console.join('\n'), matches(
104 105 106
      '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      'The following assertion was thrown:\n'
      'word word word word word word word word word word word word word word word word word word word word\n'
107 108 109 110 111 112
      'word word word word word word word word word word word word word word word word word word word word\n'
      'word word word word word word word word word word word word word word word word word word word word\n'
      'word word word word word word word word word word word word word word word word word word word word\n'
      'word word word word word word word word word word word word word word word word word word word word\n'
      '\'[^\']+flutter/test/foundation/error_reporting_test\\.dart\':\n'
      'Failed assertion: line [0-9]+ pos [0-9]+: \'false\'\n'
113
      '\n'
114
      'Either the assertion indicates an error in the framework itself, or we should provide substantially\n'
115 116
      'more information in this error message to help you determine and fix the underlying cause\\.\n'
      'In either case, please report this assertion by filing a bug on GitHub:\n'
117
      '  https://github\\.com/flutter/flutter/issues/new\\?template=BUG\\.md\n'
118
      '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
119
    ));
120
    console.clear();
121
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
122 123 124 125 126 127 128 129 130
      exception: getAssertionErrorWithLongMessage(),
    ));
    expect(
      console.join('\n'),
      'Another exception was thrown: '
      'word word word word word word word word word word word word word word word word word word word word '
      'word word word word word word word word word word word word word word word word word word word word '
      'word word word word word word word word word word word word word word word word word word word word '
      'word word word word word word word word word word word word word word word word word word word word '
131
      'word word word word word word word word word word word word word word word word word word word word',
132 133 134 135 136 137 138
    );
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - assert with no message', () async {
    expect(console, isEmpty);
139
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
140 141 142
      exception: getAssertionErrorWithoutMessage(),
      stack: sampleStack,
      library: 'error handling test',
143 144 145 146 147
      context: ErrorDescription('testing the error handling logic'),
      informationCollector: () sync* {
        yield ErrorDescription('line 1 of extra information');
        yield ErrorDescription('line 2 of extra information\n'); // the trailing newlines here are intentional
      }
148
    ));
149
    expect(console.join('\n'), matches(
150 151
      '^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
      'The following assertion was thrown testing the error handling logic:\n'
152 153
      '\'[^\']+flutter/test/foundation/error_reporting_test\\.dart\':[\n ]'
      'Failed[\n ]assertion:[\n ]line[\n ][0-9]+[\n ]pos[\n ][0-9]+:[\n ]\'false\':[\n ]is[\n ]not[\n ]true\\.\n'
154
      '\n'
155
      'Either the assertion indicates an error in the framework itself, or we should provide substantially\n'
156 157
      'more information in this error message to help you determine and fix the underlying cause\\.\n'
      'In either case, please report this assertion by filing a bug on GitHub:\n'
158
      '  https://github\\.com/flutter/flutter/issues/new\\?template=BUG\\.md\n'
159 160
      '\n'
      'When the exception was thrown, this was the stack:\n'
161 162
      '#0      getSampleStack\\.<anonymous closure> \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
      '#2      getSampleStack \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
163
      '<asynchronous suspension>\n' // TODO(ianh): https://github.com/flutter/flutter/issues/4021
164
      '#3      main \\([^)]+flutter/test/foundation/error_reporting_test\\.dart:[0-9]+:[0-9]+\\)\n'
165 166 167 168 169 170
      '(.+\n)+' // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
      '\\(elided [0-9]+ frames from package dart:async\\)\n'
      '\n'
      'line 1 of extra information\n'
      'line 2 of extra information\n'
      '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
171
    ));
172
    console.clear();
173
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
174 175
      exception: getAssertionErrorWithoutMessage(),
    ));
176
    expect(console.join('\n'), matches('Another exception was thrown: \'[^\']+flutter/test/foundation/error_reporting_test\\.dart\': Failed assertion: line [0-9]+ pos [0-9]+: \'false\': is not true\\.'));
177 178 179 180 181 182
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - NoSuchMethodError', () async {
    expect(console, isEmpty);
183 184
    final dynamic exception = NoSuchMethodError(5, #foo, <dynamic>[2, 4], null); // ignore: deprecated_member_use
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
185 186
      exception: exception,
    ));
187
    expect(console.join('\n'), matches(
188 189 190 191 192
      '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      'The following NoSuchMethodError was thrown:\n'
      'Receiver: 5\n'
      'Tried calling: foo = 2, 4\n'
      '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
193
    ));
194
    console.clear();
195
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
196 197 198 199 200 201 202 203 204
      exception: exception,
    ));
    expect(console.join('\n'), 'Another exception was thrown: NoSuchMethodError: Receiver: 5');
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - NoSuchMethodError', () async {
    expect(console, isEmpty);
205
    FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
206 207
      exception: 'hello',
    ));
208
    expect(console.join('\n'), matches(
209 210 211 212
      '^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      'The following message was thrown:\n'
      'hello\n'
      '════════════════════════════════════════════════════════════════════════════════════════════════════\$',
213
    ));
214
    console.clear();
215
    FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
216 217 218 219 220 221 222 223 224 225 226 227
      exception: 'hello again',
    ));
    expect(console.join('\n'), 'Another exception was thrown: hello again');
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - posttest', () async {
    expect(console, isEmpty);
    debugPrint = debugPrintThrottled;
  });
}