demangle_test.dart 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 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 38
// Copyright 2014 The Flutter 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';
import 'package:flutter_test/flutter_test.dart';
import 'package:stack_trace/stack_trace.dart' as stack_trace;

Future<void> main() async {
  // We use AutomatedTestWidgetsFlutterBinding to allow the test binding to set
  // FlutterError.demangleStackTrace and FlutterError.onError without testWidgets.
  final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding();

  test('FlutterErrorDetails demangles', () async {
    await binding.runTest(() async {
      // When we call toString on a FlutterErrorDetails, it attempts to parse and
      // filter the stack trace, which fails if demangleStackTrace returns a
      // mangled stack trace.
      FlutterErrorDetails(
        exception: const CustomException(),
        stack: await getMangledStack(),
      ).toString();

      // Additional logic is used to parse assertion stack traces.
      FlutterErrorDetails(
        exception: AssertionError('Some assertion'),
        stack: await getMangledStack(),
      ).toString();
    }, () {});
    binding.postTest();
  });

  test('debugPrintStack demangles', () async {
    await binding.runTest(() async {
      final DebugPrintCallback oldDebugPrint = debugPrint;
      try {
39
        debugPrint = (String? message, {int? wrapWidth}) {};
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        debugPrintStack(
          stackTrace: await getMangledStack(),
        );
      } finally {
        debugPrint = oldDebugPrint;
      }
    }, () {});
    binding.postTest();
  });
}

Future<StackTrace> getMangledStack() {
  // package:test uses package:stack_trace to wrap tests in a Zone that overrides
  // errorCallback, the error callback transforms any StackTrace propagated
  // to futures into a Chain, which has a format different from the vm.
  final Completer<StackTrace> stackCompleter = Completer<StackTrace>();
  final Completer<void> completer = Completer<void>();
  completer.future.then(
    (void value) {
      assert(false);
    },
    onError: (Object error, StackTrace stack) {
      expect(error, isA<CustomException>());
      expect(stack, isA<stack_trace.Chain>());
      stackCompleter.complete(stack);
    },
  );
  completer.completeError(const CustomException());
  return stackCompleter.future;
}

class CustomException implements Exception {
  const CustomException();
}