error_reporting_test.dart 13.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
@TestOn('!chrome')
6

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter_test/flutter_test.dart';
9

10
Object getAssertionErrorWithMessage() {
11 12 13 14 15 16 17 18
  try {
    assert(false, 'Message goes here.');
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

19
Object getAssertionErrorWithoutMessage() {
20 21 22 23 24 25 26 27
  try {
    assert(false);
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

28
Object getAssertionErrorWithLongMessage() {
29 30 31 32 33 34 35 36 37
  try {
    assert(false, 'word ' * 100);
  } catch (e) {
    return e;
  }
  throw 'assert failed';
}

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

41
Future<void> main() async {
42
  final List<String?> console = <String?>[];
43 44 45

  final StackTrace sampleStack = await getSampleStack();

46
  setUp(() async {
47
    expect(debugPrint, equals(debugPrintThrottled));
48
    debugPrint = (String? message, { int? wrapWidth }) {
49 50 51 52
      console.add(message);
    };
  });

53 54 55 56 57
  tearDown(() async {
    expect(console, isEmpty);
    debugPrint = debugPrintThrottled;
  });

58 59
  test('Error reporting - assert with message', () async {
    expect(console, isEmpty);
60
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
61 62 63
      exception: getAssertionErrorWithMessage(),
      stack: sampleStack,
      library: 'error handling test',
64 65
      context: ErrorDescription('testing the error handling logic'),
      informationCollector: () sync* {
66 67
        yield ErrorDescription('line 1 of extra information');
        yield ErrorHint('line 2 of extra information\n');
68 69
      },
    ));
70
    expect(console.join('\n'), matches(
71 72 73 74 75 76 77 78 79 80
      r'^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
      r'The following assertion was thrown testing the error handling logic:\n'
      r'Message goes here\.\n'
      r"'[^']+flutter/test/foundation/error_reporting_test\.dart':\n"
      r"Failed assertion: line [0-9]+ pos [0-9]+: 'false'\n"
      r'\n'
      r'When the exception was thrown, this was the stack:\n'
      r'#0      getSampleStack\.<anonymous closure> \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
      r'#2      getSampleStack \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
      r'#3      main \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
81
      r'(.+\n)+', // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
82
    ));
83
    console.clear();
84
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
85 86 87 88 89 90 91 92 93
      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);
94
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
95 96
      exception: getAssertionErrorWithLongMessage(),
    ));
97
    expect(console.join('\n'), matches(
98 99 100 101 102 103 104 105 106 107
      r'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      r'The following assertion was thrown:\n'
      r'word word word word word word word word word word word word word word word word word word word word\n'
      r'word word word word word word word word word word word word word word word word word word word word\n'
      r'word word word word word word word word word word word word word word word word word word word word\n'
      r'word word word word word word word word word word word word word word word word word word word word\n'
      r'word word word word word word word word word word word word word word word word word word word word\n'
      r"'[^']+flutter/test/foundation/error_reporting_test\.dart':\n"
      r"Failed assertion: line [0-9]+ pos [0-9]+: 'false'\n"
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
108
    ));
109
    console.clear();
110
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
111 112 113 114 115 116 117 118 119
      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 '
120
      'word word word word word word word word word word word word word word word word word word word word',
121 122 123 124 125 126 127
    );
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - assert with no message', () async {
    expect(console, isEmpty);
128
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
129 130 131
      exception: getAssertionErrorWithoutMessage(),
      stack: sampleStack,
      library: 'error handling test',
132 133 134 135
      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
136
      },
137
    ));
138
    expect(console.join('\n'), matches(
139 140 141 142 143 144 145 146 147
      r'^══╡ EXCEPTION CAUGHT BY ERROR HANDLING TEST ╞═══════════════════════════════════════════════════════\n'
      r'The following assertion was thrown testing the error handling logic:\n'
      r"'[^']+flutter/test/foundation/error_reporting_test\.dart':[\n ]"
      r"Failed[\n ]assertion:[\n ]line[\n ][0-9]+[\n ]pos[\n ][0-9]+:[\n ]'false':[\n ]is[\n ]not[\n ]true\.\n"
      r'\n'
      r'When the exception was thrown, this was the stack:\n'
      r'#0      getSampleStack\.<anonymous closure> \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
      r'#2      getSampleStack \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
      r'#3      main \([^)]+flutter/test/foundation/error_reporting_test\.dart:[0-9]+:[0-9]+\)\n'
148
      r'(.+\n)+', // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
149
    ));
150
    console.clear();
151
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
152 153
      exception: getAssertionErrorWithoutMessage(),
    ));
154
    expect(console.join('\n'), matches(r"Another exception was thrown: '[^']+flutter/test/foundation/error_reporting_test\.dart': Failed assertion: line [0-9]+ pos [0-9]+: 'false': is not true\."));
155 156 157 158 159 160
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - NoSuchMethodError', () async {
    expect(console, isEmpty);
161
    final Object exception = NoSuchMethodError.withInvocation(5, Invocation.method(#foo, <dynamic>[2, 4]));
162

163
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
164 165
      exception: exception,
    ));
166
    expect(console.join('\n'), matches(
167 168
      r'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      r'The following NoSuchMethodError was thrown:\n'
169
      r'int has no foo method accepting arguments \(_, _\)\n'
170
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
171
    ));
172
    console.clear();
173
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
174 175
      exception: exception,
    ));
176 177 178 179
    expect(
      console.join('\n'),
      'Another exception was thrown: NoSuchMethodError: int has no foo method accepting arguments (_, _)',
    );
180 181 182 183 184 185
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - NoSuchMethodError', () async {
    expect(console, isEmpty);
186
    FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
187 188
      exception: 'hello',
    ));
189
    expect(console.join('\n'), matches(
190 191 192 193
      r'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      r'The following message was thrown:\n'
      r'hello\n'
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
194
    ));
195
    console.clear();
196
    FlutterError.dumpErrorToConsole(const FlutterErrorDetails(
197 198 199 200 201 202
      exception: 'hello again',
    ));
    expect(console.join('\n'), 'Another exception was thrown: hello again');
    console.clear();
    FlutterError.resetErrorCount();
  });
203 204 205 206 207 208 209 210 211 212 213 214 215

  // Regression test for https://github.com/flutter/flutter/issues/62223
  test('Error reporting - empty stack', () async {
    expect(console, isEmpty);
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
      exception: 'exception - empty stack',
      stack: StackTrace.fromString(''),
    ));
    expect(console.join('\n'), matches(
      r'^══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════\n'
      r'The following message was thrown:\n'
      r'exception - empty stack\n'
      r'\n'
216
      r'When the exception was thrown, this was the stack\n'
217 218 219
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
    ));
    console.clear();
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    FlutterError.resetErrorCount();
  });

  test('Stack traces are not truncated', () async {
    const String stackString = '''
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
#2      new Text (package:flutter/src/widgets/text.dart:287:10)
#3      _MyHomePageState.build (package:hello_flutter/main.dart:72:16)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4414:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4303:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4027:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4286:5)
#8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4461:11)
#9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4281:5)
#10      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3276:14)''';

    expect(console, isEmpty);
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
      exception: AssertionError('Test assertion'),
      stack: StackTrace.fromString(stackString),
    ));
    final String x = console.join('\n');
    expect(x, startsWith('''
══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
The following assertion was thrown:
Assertion failed: "Test assertion"

When the exception was thrown, this was the stack:
#2      new Text (package:flutter/src/widgets/text.dart:287:10)
#3      _MyHomePageState.build (package:hello_flutter/main.dart:72:16)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4414:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4303:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4027:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4286:5)
#8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4461:11)
#9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4281:5)''',
    ));
    console.clear();
    FlutterError.resetErrorCount();
260
  });
261
}