OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.matcher.util; | 5 library unittest.matcher.util; |
6 | 6 |
7 import 'core_matchers.dart'; | 7 import 'core_matchers.dart'; |
8 import 'interfaces.dart'; | 8 import 'interfaces.dart'; |
9 | 9 |
| 10 typedef bool _Predicate(value); |
| 11 |
10 /// A [Map] between whitespace characters and their escape sequences. | 12 /// A [Map] between whitespace characters and their escape sequences. |
11 const _escapeMap = const { | 13 const _escapeMap = const { |
12 '\n': r'\n', | 14 '\n': r'\n', |
13 '\r': r'\r', | 15 '\r': r'\r', |
14 '\f': r'\f', | 16 '\f': r'\f', |
15 '\b': r'\b', | 17 '\b': r'\b', |
16 '\t': r'\t', | 18 '\t': r'\t', |
17 '\v': r'\v', | 19 '\v': r'\v', |
18 '\x7F': r'\x7F', // delete | 20 '\x7F': r'\x7F', // delete |
19 }; | 21 }; |
(...skipping 11 matching lines...) Expand all Loading... |
31 } | 33 } |
32 | 34 |
33 /// Takes an argument and returns an equivalent [Matcher]. | 35 /// Takes an argument and returns an equivalent [Matcher]. |
34 /// | 36 /// |
35 /// If the argument is already a matcher this does nothing, | 37 /// If the argument is already a matcher this does nothing, |
36 /// else if the argument is a function, it generates a predicate | 38 /// else if the argument is a function, it generates a predicate |
37 /// function matcher, else it generates an equals matcher. | 39 /// function matcher, else it generates an equals matcher. |
38 Matcher wrapMatcher(x) { | 40 Matcher wrapMatcher(x) { |
39 if (x is Matcher) { | 41 if (x is Matcher) { |
40 return x; | 42 return x; |
41 } else if (x is Function) { | 43 } else if (x is _Predicate) { |
42 return predicate(x); | 44 return predicate(x); |
43 } else { | 45 } else { |
44 return equals(x); | 46 return equals(x); |
45 } | 47 } |
46 } | 48 } |
47 | 49 |
48 /// Returns [str] with all whitespace characters represented as their escape | 50 /// Returns [str] with all whitespace characters represented as their escape |
49 /// sequences. | 51 /// sequences. |
50 /// | 52 /// |
51 /// Backslash characters are escaped as `\\` | 53 /// Backslash characters are escaped as `\\` |
52 String escape(String str) { | 54 String escape(String str) { |
53 str = str.replaceAll('\\', r'\\'); | 55 str = str.replaceAll('\\', r'\\'); |
54 return str.replaceAllMapped(_escapeRegExp, (match) { | 56 return str.replaceAllMapped(_escapeRegExp, (match) { |
55 var mapped = _escapeMap[match[0]]; | 57 var mapped = _escapeMap[match[0]]; |
56 if (mapped != null) return mapped; | 58 if (mapped != null) return mapped; |
57 return _getHexLiteral(match[0]); | 59 return _getHexLiteral(match[0]); |
58 }); | 60 }); |
59 } | 61 } |
60 | 62 |
61 /// Given single-character string, return the hex-escaped equivalent. | 63 /// Given single-character string, return the hex-escaped equivalent. |
62 String _getHexLiteral(String input) { | 64 String _getHexLiteral(String input) { |
63 int rune = input.runes.single; | 65 int rune = input.runes.single; |
64 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0'); | 66 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0'); |
65 } | 67 } |
OLD | NEW |