| 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 import 'package:smoke/smoke.dart' as smoke show isSubclassOf; | |
| 9 | |
| 10 /// Returns [input] adjusted to be within [min] and [max] length. Truncating it | |
| 11 /// if it's longer, or padding it with nulls if it's shorter. The returned list | |
| 12 /// is a new copy if any modification is needed, otherwise [input] is returned. | |
| 13 List adjustList(List input, int min, int max) { | |
| 14 if (input.length < min) { | |
| 15 return new List(min)..setRange(0, input.length, input); | |
| 16 } | |
| 17 | |
| 18 if (input.length > max) { | |
| 19 return new List(max)..setRange(0, max, input); | |
| 20 } | |
| 21 return input; | |
| 22 } | |
| 23 | |
| 24 /// Returns whether [metadata] contains any annotation that is either equal to | |
| 25 /// an annotation in [queryAnnotations] or whose type is listed in | |
| 26 /// [queryAnnotations]. | |
| 27 bool matchesAnnotation(Iterable metadata, Iterable queryAnnotations) { | |
| 28 for (var meta in metadata) { | |
| 29 for (var queryMeta in queryAnnotations) { | |
| 30 if (meta == queryMeta) return true; | |
| 31 if (queryMeta is Type && | |
| 32 smoke.isSubclassOf(meta.runtimeType, queryMeta)) return true; | |
| 33 } | |
| 34 } | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 /// Number of arguments supported by [minArgs] and [maxArgs]. | |
| 39 const SUPPORTED_ARGS = 3; | |
| 40 | |
| 41 typedef _Func0(); | |
| 42 typedef _Func1(a); | |
| 43 typedef _Func2(a, b); | |
| 44 typedef _Func3(a, b, c); | |
| 45 | |
| 46 /// Returns the minimum number of arguments that [f] takes as input, in other | |
| 47 /// words, the total number of required arguments of [f]. If [f] expects more | |
| 48 /// than [SUPPORTED_ARGS], this function returns `SUPPORTED_ARGS + 1`. | |
| 49 /// | |
| 50 /// For instance, the current implementation only supports calculating the | |
| 51 /// number of arguments between `0` and `3`. If the function takes `4` or more, | |
| 52 /// this function automatically returns `4`. | |
| 53 int minArgs(Function f) { | |
| 54 if (f is _Func0) return 0; | |
| 55 if (f is _Func1) return 1; | |
| 56 if (f is _Func2) return 2; | |
| 57 if (f is _Func3) return 3; | |
| 58 return SUPPORTED_ARGS + 1; | |
| 59 } | |
| 60 | |
| 61 /// Returns the maximum number of arguments that [f] takes as input, which is | |
| 62 /// the total number of required and optional arguments of [f]. If | |
| 63 /// [f] may take more than [SUPPORTED_ARGS] required arguments, this function | |
| 64 /// returns `-1`. However, if it takes less required arguments, but more than | |
| 65 /// [SUPPORTED_ARGS] arguments including optional arguments, the result will be | |
| 66 /// [SUPPORTED_ARGS]. | |
| 67 /// | |
| 68 /// For instance, the current implementation only supports calculating the | |
| 69 /// number of arguments between `0` and `3`. If the function takes `4` | |
| 70 /// mandatory arguments, this function returns `-1`, but if the funtion takes | |
| 71 /// `2` mandatory arguments and 10 optional arguments, this function returns | |
| 72 /// `3`. | |
| 73 int maxArgs(Function f) { | |
| 74 if (f is _Func3) return 3; | |
| 75 if (f is _Func2) return 2; | |
| 76 if (f is _Func1) return 1; | |
| 77 if (f is _Func0) return 0; | |
| 78 return -1; | |
| 79 } | |
| 80 | |
| 81 /// Shallow comparison of two lists. | |
| 82 bool compareLists(List a, List b, {bool unordered: false}) { | |
| 83 if (a == null && b != null) return false; | |
| 84 if (a != null && b == null) return false; | |
| 85 if (a.length != b.length) return false; | |
| 86 if (unordered) { | |
| 87 var countMap = {}; | |
| 88 for (var x in b) { | |
| 89 var count = countMap[x]; | |
| 90 if (count == null) count = 0; | |
| 91 countMap[x] = count + 1; | |
| 92 } | |
| 93 for (var x in a) { | |
| 94 var count = countMap[x]; | |
| 95 if (count == null) return false; | |
| 96 if (count == 1) { | |
| 97 countMap.remove(x); | |
| 98 } else { | |
| 99 countMap[x] = count - 1; | |
| 100 } | |
| 101 } | |
| 102 return countMap.isEmpty; | |
| 103 } else { | |
| 104 for (int i = 0; i < a.length; i++) { | |
| 105 if (a[i] != b[i]) return false; | |
| 106 } | |
| 107 } | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 /// Shallow comparison of two maps. | |
| 112 bool compareMaps(Map a, Map b) { | |
| 113 if (a == null && b != null) return false; | |
| 114 if (a != null && b == null) return false; | |
| 115 if (a.length != b.length) return false; | |
| 116 for (var k in a.keys) { | |
| 117 if (!b.containsKey(k) || a[k] != b[k]) return false; | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| OLD | NEW |