1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// See also test_async_utils.dart which has some stack manipulation code. 6 7import 'package:flutter/foundation.dart'; 8 9/// Report call site for `expect()` call. Returns the number of frames that 10/// should be elided if a stack were to be modified to hide the expect call, or 11/// zero if no such call was found. 12/// 13/// If the head of the stack trace consists of a failure as a result of calling 14/// the test_widgets [expect] function, this will fill the given 15/// FlutterErrorBuilder with the precise file and line number that called that 16/// function. 17int reportExpectCall(StackTrace stack, List<DiagnosticsNode> information) { 18 final RegExp line0 = RegExp(r'^#0 +fail \(.+\)$'); 19 final RegExp line1 = RegExp(r'^#1 +_expect \(.+\)$'); 20 final RegExp line2 = RegExp(r'^#2 +expect \(.+\)$'); 21 final RegExp line3 = RegExp(r'^#3 +expect \(.+\)$'); 22 final RegExp line4 = RegExp(r'^#4 +[^(]+ \((.+?):([0-9]+)(?::[0-9]+)?\)$'); 23 final List<String> stackLines = stack.toString().split('\n'); 24 if (line0.firstMatch(stackLines[0]) != null && 25 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 assert(expectMatch != null); 30 assert(expectMatch.groupCount == 2); 31 information.add(DiagnosticsStackTrace.singleFrame( 32 'This was caught by the test expectation on the following line', 33 frame: '${expectMatch.group(1)} line ${expectMatch.group(2)}', 34 )); 35 36 return 4; 37 } 38 return 0; 39} 40