error_reporting_test.dart 10.8 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 6
@TestOn('!chrome') // web has different stack traces

7
import 'package:flutter/foundation.dart';
8
import '../flutter_test_alternative.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 await 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 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'
      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 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'
      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("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,
162 163
        Invocation.method(#foo, <dynamic>[2, 4]));

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
    expect(console.join('\n'),
      'Another exception was thrown: NoSuchMethodError: int has no foo method accepting arguments (_, _)');
179 180 181 182 183 184
    console.clear();
    FlutterError.resetErrorCount();
  });

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

  // 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'
      r'When the exception was thrown, this was the stack:\n'
      r'...\n'
      r'════════════════════════════════════════════════════════════════════════════════════════════════════$',
    ));
    console.clear();
  });
221
}