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
library;
7

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

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

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

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

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

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

  final StackTrace sampleStack = await getSampleStack();

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

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

59 60
  test('Error reporting - assert with message', () async {
    expect(console, isEmpty);
61
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
62 63 64
      exception: getAssertionErrorWithMessage(),
      stack: sampleStack,
      library: 'error handling test',
65 66
      context: ErrorDescription('testing the error handling logic'),
      informationCollector: () sync* {
67 68
        yield ErrorDescription('line 1 of extra information');
        yield ErrorHint('line 2 of extra information\n');
69 70
      },
    ));
71
    expect(console.join('\n'), matches(
72 73 74 75 76 77 78 79 80 81
      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'
82
      r'(.+\n)+', // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
83
    ));
84
    console.clear();
85
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
86 87 88 89 90 91 92 93 94
      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);
95
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
96 97
      exception: getAssertionErrorWithLongMessage(),
    ));
98
    expect(console.join('\n'), matches(
99 100 101 102 103 104 105 106 107 108
      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'════════════════════════════════════════════════════════════════════════════════════════════════════$',
109
    ));
110
    console.clear();
111
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
112 113 114 115 116 117 118 119 120
      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 '
121
      'word word word word word word word word word word word word word word word word word word word word',
122 123 124 125 126 127 128
    );
    console.clear();
    FlutterError.resetErrorCount();
  });

  test('Error reporting - assert with no message', () async {
    expect(console, isEmpty);
129
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
130 131 132
      exception: getAssertionErrorWithoutMessage(),
      stack: sampleStack,
      library: 'error handling test',
133 134 135 136
      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
137
      },
138
    ));
139
    expect(console.join('\n'), matches(
140 141 142 143 144 145 146 147 148
      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'
149
      r'(.+\n)+', // TODO(ianh): when fixing #4021, also filter out frames from the test infrastructure below the first call to our main()
150
    ));
151
    console.clear();
152
    FlutterError.dumpErrorToConsole(FlutterErrorDetails(
153 154
      exception: getAssertionErrorWithoutMessage(),
    ));
155
    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\."));
156 157 158 159 160 161
    console.clear();
    FlutterError.resetErrorCount();
  });

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

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

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

  // 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'
217
      r'When the exception was thrown, this was the stack\n'
218 219 220
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
    ));
    console.clear();
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 260
    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();
261
  });
262
}