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