| 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 | |
| 6 typedef void DebugCallback(String methodName, var arg1, var arg2); | 5 typedef void DebugCallback(String methodName, var arg1, var arg2); |
| 7 | 6 |
| 8 class DebugMap<K, V> implements Map<K, V> { | 7 class DebugMap<K, V> implements Map<K, V> { |
| 9 final Map<K, V> map; | 8 final Map<K, V> map; |
| 10 DebugCallback indexSetCallBack; | 9 DebugCallback indexSetCallBack; |
| 11 DebugCallback putIfAbsentCallBack; | 10 DebugCallback putIfAbsentCallBack; |
| 12 | 11 |
| 13 DebugMap(this.map, {DebugCallback addCallback}) { | 12 DebugMap(this.map, {DebugCallback addCallback}) { |
| 14 if (addCallback != null) { | 13 if (addCallback != null) { |
| 15 this.addCallback = addCallback; | 14 this.addCallback = addCallback; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 Iterable<E> where(bool test(E element)) => iterable.where(test); | 76 Iterable<E> where(bool test(E element)) => iterable.where(test); |
| 78 | 77 |
| 79 Iterable expand(Iterable f(E element)) => iterable.expand(f); | 78 Iterable expand(Iterable f(E element)) => iterable.expand(f); |
| 80 | 79 |
| 81 bool contains(Object element) => iterable.contains(element); | 80 bool contains(Object element) => iterable.contains(element); |
| 82 | 81 |
| 83 void forEach(void f(E element)) => iterable.forEach(f); | 82 void forEach(void f(E element)) => iterable.forEach(f); |
| 84 | 83 |
| 85 E reduce(E combine(E value, E element)) => iterable.reduce(combine); | 84 E reduce(E combine(E value, E element)) => iterable.reduce(combine); |
| 86 | 85 |
| 87 dynamic fold(var initialValue, | 86 dynamic fold( |
| 88 dynamic combine(var previousValue, E element)) { | 87 var initialValue, dynamic combine(var previousValue, E element)) { |
| 89 return iterable.fold(initialValue, combine); | 88 return iterable.fold(initialValue, combine); |
| 90 } | 89 } |
| 91 | 90 |
| 92 bool every(bool test(E element)) => iterable.every(test); | 91 bool every(bool test(E element)) => iterable.every(test); |
| 93 | 92 |
| 94 String join([String separator = ""]) => iterable.join(separator); | 93 String join([String separator = ""]) => iterable.join(separator); |
| 95 | 94 |
| 96 bool any(bool test(E element)) => iterable.any(test); | 95 bool any(bool test(E element)) => iterable.any(test); |
| 97 | 96 |
| 98 List<E> toList({ bool growable: true }) { | 97 List<E> toList({bool growable: true}) { |
| 99 return iterable.toList(growable: growable); | 98 return iterable.toList(growable: growable); |
| 100 } | 99 } |
| 101 | 100 |
| 102 Set<E> toSet() => iterable.toSet(); | 101 Set<E> toSet() => iterable.toSet(); |
| 103 | 102 |
| 104 int get length => iterable.length; | 103 int get length => iterable.length; |
| 105 | 104 |
| 106 bool get isEmpty => iterable.isEmpty; | 105 bool get isEmpty => iterable.isEmpty; |
| 107 | 106 |
| 108 bool get isNotEmpty => iterable.isNotEmpty; | 107 bool get isNotEmpty => iterable.isNotEmpty; |
| 109 | 108 |
| 110 Iterable<E> take(int n) => iterable.take(n); | 109 Iterable<E> take(int n) => iterable.take(n); |
| 111 | 110 |
| 112 Iterable<E> takeWhile(bool test(E value)) => iterable.takeWhile(test); | 111 Iterable<E> takeWhile(bool test(E value)) => iterable.takeWhile(test); |
| 113 | 112 |
| 114 Iterable<E> skip(int n) => iterable.skip(n); | 113 Iterable<E> skip(int n) => iterable.skip(n); |
| 115 | 114 |
| 116 Iterable<E> skipWhile(bool test(E value)) => iterable.skipWhile(test); | 115 Iterable<E> skipWhile(bool test(E value)) => iterable.skipWhile(test); |
| 117 | 116 |
| 118 E get first => iterable.first; | 117 E get first => iterable.first; |
| 119 | 118 |
| 120 E get last => iterable.last; | 119 E get last => iterable.last; |
| 121 | 120 |
| 122 E get single => iterable.single; | 121 E get single => iterable.single; |
| 123 | 122 |
| 124 E firstWhere(bool test(E element), { E orElse() }) { | 123 E firstWhere(bool test(E element), {E orElse()}) { |
| 125 return iterable.firstWhere(test, orElse: orElse); | 124 return iterable.firstWhere(test, orElse: orElse); |
| 126 } | 125 } |
| 127 | 126 |
| 128 E lastWhere(bool test(E element), {E orElse()}) { | 127 E lastWhere(bool test(E element), {E orElse()}) { |
| 129 return iterable.lastWhere(test, orElse: orElse); | 128 return iterable.lastWhere(test, orElse: orElse); |
| 130 } | 129 } |
| 131 | 130 |
| 132 E singleWhere(bool test(E element)) => iterable.singleWhere(test); | 131 E singleWhere(bool test(E element)) => iterable.singleWhere(test); |
| 133 | 132 |
| 134 E elementAt(int index) => iterable.elementAt(index); | 133 E elementAt(int index) => iterable.elementAt(index); |
| 135 | |
| 136 } | 134 } |
| 137 | 135 |
| 138 class DebugList<E> extends DebugIterable<E> implements List<E> { | 136 class DebugList<E> extends DebugIterable<E> implements List<E> { |
| 139 DebugCallback addCallback; | 137 DebugCallback addCallback; |
| 140 | 138 |
| 141 DebugList(List<E> list, {this.addCallback}) : super(list); | 139 DebugList(List<E> list, {this.addCallback}) : super(list); |
| 142 | 140 |
| 143 List<E> get list => iterable; | 141 List<E> get list => iterable; |
| 144 | 142 |
| 145 E operator [](int index) => list[index]; | 143 E operator [](int index) => list[index]; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 Set<E> toSet() => set.toSet(); | 259 Set<E> toSet() => set.toSet(); |
| 262 } | 260 } |
| 263 | 261 |
| 264 /// Throws an exception if the runtime type of [object] is not in | 262 /// Throws an exception if the runtime type of [object] is not in |
| 265 /// [runtimeTypes]. | 263 /// [runtimeTypes]. |
| 266 /// | 264 /// |
| 267 /// Use this to gradually build the set of actual runtime values of [object] | 265 /// Use this to gradually build the set of actual runtime values of [object] |
| 268 /// at the call site by running test programs and adding to [runtimeTypes] when | 266 /// at the call site by running test programs and adding to [runtimeTypes] when |
| 269 /// new type are found. | 267 /// new type are found. |
| 270 void assertType(String name, List<String> runtimeTypes, var object, | 268 void assertType(String name, List<String> runtimeTypes, var object, |
| 271 {bool showObjects: false}) { | 269 {bool showObjects: false}) { |
| 272 String runtimeType = '${object.runtimeType}'; | 270 String runtimeType = '${object.runtimeType}'; |
| 273 if (runtimeTypes != null && runtimeTypes.contains(runtimeType)) return; | 271 if (runtimeTypes != null && runtimeTypes.contains(runtimeType)) return; |
| 274 throw '$name: $runtimeType' | 272 throw '$name: $runtimeType' |
| 275 '${showObjects ? ' ($object)' : ''}'; | 273 '${showObjects ? ' ($object)' : ''}'; |
| 276 } | 274 } |
| 277 | 275 |
| 278 /// Callback for the [addCallback] of [DebugMap] that throws an exception if | 276 /// Callback for the [addCallback] of [DebugMap] that throws an exception if |
| 279 /// the runtime type of key/value pairs are not in [runtimeTypes]. | 277 /// the runtime type of key/value pairs are not in [runtimeTypes]. |
| 280 /// | 278 /// |
| 281 /// Use this to gradually build the set of actual runtime values of key/value | 279 /// Use this to gradually build the set of actual runtime values of key/value |
| 282 /// pairs of a map by running test programs and adding to [runtimeTypes] when | 280 /// pairs of a map by running test programs and adding to [runtimeTypes] when |
| 283 /// new type are found. | 281 /// new type are found. |
| 284 class MapTypeAsserter { | 282 class MapTypeAsserter { |
| 285 final String name; | 283 final String name; |
| 286 final Map<String, List<String>> runtimeTypes; | 284 final Map<String, List<String>> runtimeTypes; |
| 287 final bool showObjects; | 285 final bool showObjects; |
| 288 | 286 |
| 289 const MapTypeAsserter(this.name, this.runtimeTypes, | 287 const MapTypeAsserter(this.name, this.runtimeTypes, |
| 290 {bool this.showObjects: false}); | 288 {bool this.showObjects: false}); |
| 291 | 289 |
| 292 void call(String methodName, var key, var value) { | 290 void call(String methodName, var key, var value) { |
| 293 check(key, value, '$methodName: '); | 291 check(key, value, '$methodName: '); |
| 294 } | 292 } |
| 295 | 293 |
| 296 void check(var key, var value, [String text = '']) { | 294 void check(var key, var value, [String text = '']) { |
| 297 String keyType = '${key.runtimeType}'; | 295 String keyType = '${key.runtimeType}'; |
| 298 String valueType = '${value.runtimeType}'; | 296 String valueType = '${value.runtimeType}'; |
| 299 List<String> valuesTypes = runtimeTypes[keyType]; | 297 List<String> valuesTypes = runtimeTypes[keyType]; |
| 300 if (valuesTypes != null && valuesTypes.contains(valueType)) return; | 298 if (valuesTypes != null && valuesTypes.contains(valueType)) return; |
| 301 throw '$name: $text$keyType => $valueType' | 299 throw '$name: $text$keyType => $valueType' |
| 302 '${showObjects ? ' ($key => $value)' : ''}'; | 300 '${showObjects ? ' ($key => $value)' : ''}'; |
| 303 } | 301 } |
| 304 } | 302 } |
| 305 | 303 |
| 306 /// Callback for the [addCallback] of [DebugSet] or [DebugList] that throws an | 304 /// Callback for the [addCallback] of [DebugSet] or [DebugList] that throws an |
| 307 /// exception if the runtime type of the elements are not in [runtimeTypes]. | 305 /// exception if the runtime type of the elements are not in [runtimeTypes]. |
| 308 /// | 306 /// |
| 309 /// Use this to gradually build the set of actual runtime values of the elements | 307 /// Use this to gradually build the set of actual runtime values of the elements |
| 310 /// of a collection by running test programs and adding to [runtimeTypes] when | 308 /// of a collection by running test programs and adding to [runtimeTypes] when |
| 311 /// new type are found. | 309 /// new type are found. |
| 312 class CollectionTypeAsserter { | 310 class CollectionTypeAsserter { |
| 313 final String name; | 311 final String name; |
| 314 final List<String> runtimeTypes; | 312 final List<String> runtimeTypes; |
| 315 final bool showObjects; | 313 final bool showObjects; |
| 316 | 314 |
| 317 const CollectionTypeAsserter(this.name, this.runtimeTypes, | 315 const CollectionTypeAsserter(this.name, this.runtimeTypes, |
| 318 {bool this.showObjects: false}); | 316 {bool this.showObjects: false}); |
| 319 | 317 |
| 320 void call(String methodName, var element, _) { | 318 void call(String methodName, var element, _) { |
| 321 check(element, '$methodName: '); | 319 check(element, '$methodName: '); |
| 322 } | 320 } |
| 323 | 321 |
| 324 void check(var element, [String text = '']) { | 322 void check(var element, [String text = '']) { |
| 325 String elementType = '${element.runtimeType}'; | 323 String elementType = '${element.runtimeType}'; |
| 326 if (runtimeTypes.contains(elementType)) return; | 324 if (runtimeTypes.contains(elementType)) return; |
| 327 throw '$name: $text$elementType' | 325 throw '$name: $text$elementType' |
| 328 '${showObjects ? ' ($element)' : ''}'; | 326 '${showObjects ? ' ($element)' : ''}'; |
| 329 } | 327 } |
| 330 } | 328 } |
| OLD | NEW |