OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Some common utilities used by other libraries in this package. |
| 6 library smoke.src.common; |
| 7 |
| 8 /// Returns [input] adjusted to be within [min] and [max] length. Truncating it |
| 9 /// if it's longer, or padding it with nulls if it's shorter. The returned list |
| 10 /// is a new copy if any modification is needed, otherwise [input] is returned. |
| 11 List adjustList(List input, int min, int max) { |
| 12 if (input.length < min) { |
| 13 return new List(min)..setRange(0, input.length, input); |
| 14 } |
| 15 |
| 16 if (input.length > max) { |
| 17 return new List(max)..setRange(0, max, input); |
| 18 } |
| 19 return input; |
| 20 } |
| 21 |
| 22 /// Returns whether [metadata] contains any annotation that is either equal to |
| 23 /// an annotation in [queryAnnotations] or whose type is listed in |
| 24 /// [queryAnnotations]. |
| 25 bool matchesAnnotation(Iterable metadata, Iterable queryAnnotations) { |
| 26 return metadata.any((m) => queryAnnotations.contains(m) || |
| 27 queryAnnotations.contains(m.runtimeType)); |
| 28 } |
| 29 |
| 30 /// Number of arguments supported by [minArgs] and [maxArgs]. |
| 31 const SUPPORTED_ARGS = 3; |
| 32 |
| 33 typedef _Func0(); |
| 34 typedef _Func1(a); |
| 35 typedef _Func2(a, b); |
| 36 typedef _Func3(a, b, c); |
| 37 |
| 38 /// Returns the minimum number of arguments that [f] takes as input, in other |
| 39 /// words, the total number of required arguments of [f]. If [f] expects more |
| 40 /// than [SUPPORTED_ARGS], this function returns `SUPPORTED_ARGS + 1`. |
| 41 /// |
| 42 /// For instance, the current implementation only supports calculating the |
| 43 /// number of arguments between `0` and `3`. If the function takes `4` or more, |
| 44 /// this function automatically returns `4`. |
| 45 int minArgs(Function f) { |
| 46 if (f is _Func0) return 0; |
| 47 if (f is _Func1) return 1; |
| 48 if (f is _Func2) return 2; |
| 49 if (f is _Func3) return 3; |
| 50 return SUPPORTED_ARGS + 1; |
| 51 } |
| 52 |
| 53 /// Returns the maximum number of arguments that [f] takes as input, which is |
| 54 /// the total number of required and optional arguments of [f]. If |
| 55 /// [f] may take more than [SUPPORTED_ARGS] required arguments, this function |
| 56 /// returns `-1`. However, if it takes less required arguments, but more than |
| 57 /// [SUPPORTED_ARGS] arguments including optional arguments, the result will be |
| 58 /// [SUPPORTED_ARGS]. |
| 59 /// |
| 60 /// For instance, the current implementation only supports calculating the |
| 61 /// number of arguments between `0` and `3`. If the function takes `4` |
| 62 /// mandatory arguments, this function returns `-1`, but if the funtion takes |
| 63 /// `2` mandatory arguments and 10 optional arguments, this function returns |
| 64 /// `3`. |
| 65 int maxArgs(Function f) { |
| 66 if (f is _Func3) return 3; |
| 67 if (f is _Func2) return 2; |
| 68 if (f is _Func1) return 1; |
| 69 if (f is _Func0) return 0; |
| 70 return -1; |
| 71 } |
OLD | NEW |