OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library unittest.utils; | 5 library unittest.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 if (element is Iterable) { | 56 if (element is Iterable) { |
57 helper(element); | 57 helper(element); |
58 } else { | 58 } else { |
59 result.add(element); | 59 result.add(element); |
60 } | 60 } |
61 } | 61 } |
62 } | 62 } |
63 helper(nested); | 63 helper(nested); |
64 return result; | 64 return result; |
65 } | 65 } |
| 66 |
| 67 /// Truncates [text] to fit within [maxLength]. |
| 68 /// |
| 69 /// This will try to truncate along word boundaries and preserve words both at |
| 70 /// the beginning and the end of [text]. |
| 71 String truncate(String text, int maxLength) { |
| 72 // Return the full message if it fits. |
| 73 if (text.length <= maxLength) return text; |
| 74 |
| 75 // If we can fit the first and last three words, do so. |
| 76 var words = text.split(' '); |
| 77 if (words.length > 1) { |
| 78 var i = words.length; |
| 79 var length = words.first.length + 4; |
| 80 do { |
| 81 i--; |
| 82 length += 1 + words[i].length; |
| 83 } while (length <= maxLength && i > 0); |
| 84 if (length > maxLength || i == 0) i++; |
| 85 if (i < words.length - 4) { |
| 86 // Require at least 3 words at the end. |
| 87 var buffer = new StringBuffer(); |
| 88 buffer.write(words.first); |
| 89 buffer.write(' ...'); |
| 90 for ( ; i < words.length; i++) { |
| 91 buffer.write(' '); |
| 92 buffer.write(words[i]); |
| 93 } |
| 94 return buffer.toString(); |
| 95 } |
| 96 } |
| 97 |
| 98 // Otherwise truncate to return the trailing text, but attempt to start at |
| 99 // the beginning of a word. |
| 100 var result = text.substring(text.length - maxLength + 4); |
| 101 var firstSpace = result.indexOf(' '); |
| 102 if (firstSpace > 0) { |
| 103 result = result.substring(firstSpace); |
| 104 } |
| 105 return '...$result'; |
| 106 } |
OLD | NEW |