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

// See also test_async_utils.dart which has some stack manipulation code.

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

9 10 11 12 13
/// Report call site for `expect()` call. Returns the number of frames that
/// should be elided if a stack were to be modified to hide the expect call, or
/// zero if no such call was found.
///
/// If the head of the stack trace consists of a failure as a result of calling
14 15 16 17
/// the test_widgets [expect] function, this will fill the given
/// FlutterErrorBuilder with the precise file and line number that called that
/// function.
int reportExpectCall(StackTrace stack, List<DiagnosticsNode> information) {
18 19 20 21 22
  final RegExp line0 = RegExp(r'^#0 +fail \(.+\)$');
  final RegExp line1 = RegExp(r'^#1 +_expect \(.+\)$');
  final RegExp line2 = RegExp(r'^#2 +expect \(.+\)$');
  final RegExp line3 = RegExp(r'^#3 +expect \(.+\)$');
  final RegExp line4 = RegExp(r'^#4 +[^(]+ \((.+?):([0-9]+)(?::[0-9]+)?\)$');
23 24 25
  final List<String> stackLines = stack.toString().split('\n');
  if (line0.firstMatch(stackLines[0]) != null &&
      line1.firstMatch(stackLines[1]) != null &&
26
      line2.firstMatch(stackLines[2]) != null &&
27
      line3.firstMatch(stackLines[3]) != null) {
28
    final Match expectMatch = line4.firstMatch(stackLines[4])!;
29 30
    assert(expectMatch != null);
    assert(expectMatch.groupCount == 2);
31 32 33 34 35
    information.add(DiagnosticsStackTrace.singleFrame(
      'This was caught by the test expectation on the following line',
      frame: '${expectMatch.group(1)} line ${expectMatch.group(2)}',
    ));

36
    return 4;
37 38
  }
  return 0;
39
}