Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "dart:collection"; | 6 import "dart:collection"; |
| 7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
| 8 | 8 |
| 9 main() { | 9 main() { |
| 10 var intTest = new Test<int>(); | 10 var intTest = new Test<int>(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 throws("removeRange", () { | 132 throws("removeRange", () { |
| 133 list.removeRange(0, 1); | 133 list.removeRange(0, 1); |
| 134 }); | 134 }); |
| 135 throws("replaceRange", () { | 135 throws("replaceRange", () { |
| 136 list.replaceRange(0, 1, []); | 136 list.replaceRange(0, 1, []); |
| 137 }); | 137 }); |
| 138 throws("fillRange", () { | 138 throws("fillRange", () { |
| 139 list.fillRange(0, 1, null); | 139 list.fillRange(0, 1, null); |
| 140 }); | 140 }); |
| 141 | 141 |
| 142 success(opName, op(list)) { | 142 success(opName, op(List list), [bool throws = false]) { |
| 143 var expect; | 143 if (throws) { |
| 144 try { | 144 var e1, e2; |
| 145 expect = op(elements); | 145 Expect.throws(() => op(elements), (e) { |
| 146 } catch (e) { | 146 e1 = e; |
| 147 try { | 147 return true; |
| 148 op(list); | 148 }, '$name :: $opName should throw for $elements'); |
| 149 } catch (e2) { | 149 Expect.throws(() => op(list), (e) { |
| 150 Expect.equals( | 150 e2 = e; |
| 151 e.runtimeType, | 151 return true; |
| 152 e2.runtimeType, | 152 }, '$name :: $opName should throw for $list'); |
| 153 "$name :: $opName threw different exceptions: " | 153 Expect.equals( |
| 154 "${e.runtimeType} vs ${e2.runtimeType}"); | 154 e1.runtimeType, |
| 155 return; | 155 e2.runtimeType, |
| 156 } | 156 "$name :: $opName threw different errors for $elements and $list: " |
| 157 Expect.fail("$name-$opName didn't throw, expected: $e"); | 157 "${e1.runtimeType} vs ${e2.runtimeType}"); |
| 158 return; | |
| 158 } | 159 } |
| 160 var expect = op(elements); | |
| 159 var actual = op(list); | 161 var actual = op(list); |
| 160 checkElements(); | 162 checkElements(); |
| 161 if (expect is List) { | 163 if (expect is List) { |
| 162 Expect.listEquals(expect, actual, "$name-$opName"); | 164 Expect.listEquals(expect, actual, "$name-$opName"); |
| 163 } else if (expect is Iterable) { | 165 } else if (expect is Iterable) { |
| 164 Expect.isTrue(actual is Iterable); | 166 Expect.isTrue(actual is Iterable); |
| 165 Expect.listEquals(expect.toList(), actual.toList(), "$name-$opName"); | 167 Expect.listEquals(expect.toList(), actual.toList(), "$name-$opName"); |
| 166 } else { | 168 } else { |
| 167 Expect.equals(expect, actual, "$name-$opName"); | 169 Expect.equals(expect, actual, "$name-$opName"); |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 | 172 |
| 171 success("indexOf", (l) => l.indexOf(null)); | 173 success("indexOf", (l) => l.indexOf(null)); |
| 172 success("lastIndexOf", (l) => l.lastIndexOf(null)); | 174 success("lastIndexOf", (l) => l.lastIndexOf(null)); |
| 173 success("contains", (l) => l.contains(2)); | 175 success("contains", (l) => l.contains(2)); |
| 174 success("elementAt", (l) => l.elementAt[1]); | 176 success("elementAt", (l) => l.elementAt(1), list.length < 2); |
|
Jennifer Messerly
2017/08/09 18:47:25
note the square brackets ... oops!
| |
| 175 success("reversed", (l) => l.reversed); | 177 success("reversed", (l) => l.reversed); |
| 176 success("sublist0-1", (l) => l.sublist(0, 1)); | 178 success("sublist0-1", (l) => l.sublist(0, 1), list.isEmpty); |
| 177 success("getRange0-1", (l) => l.getRange(0, 1)); | 179 success("getRange0-1", (l) => l.getRange(0, 1), list.isEmpty); |
| 178 success("asMap-keys", (l) => l.asMap().keys); | 180 success("asMap-keys", (l) => l.asMap().keys); |
| 179 success("asMap-values", (l) => l.asMap().values); | 181 success("asMap-values", (l) => l.asMap().values); |
| 180 success("where", (l) => l.where((x) => true)); | 182 success("where", (l) => l.where((x) => true)); |
| 181 success("map", (l) => l.map((x) => x)); | 183 success("map", (l) => l.map((x) => x)); |
| 182 success("expand", (l) => l.expand((x) => [x, x])); | 184 success("expand", (l) => l.expand((x) => [x, x])); |
| 183 success("skip", (l) => l.skip(1)); | 185 success("skip", (l) => l.skip(1)); |
| 184 success("take", (l) => l.take(1)); | 186 success("take", (l) => l.take(1)); |
| 185 success("skipWhile", (l) => l.skipWhile((x) => false)); | 187 success("skipWhile", (l) => l.skipWhile((x) => false)); |
| 186 success("takeWhile", (l) => l.takeWhile((x) => true)); | 188 success("takeWhile", (l) => l.takeWhile((x) => true)); |
| 187 success("first", (l) => l.first); | 189 success("first", (l) => l.first, list.isEmpty); |
| 188 success("last", (l) => l.last); | 190 success("last", (l) => l.last, list.isEmpty); |
| 189 success("single", (l) => l.single); | 191 success("single", (l) => l.single, list.length != 1); |
| 190 success("firstWhere", (l) => l.firstWhere((x) => true)); | 192 success("firstWhere", (l) => l.firstWhere((x) => true), list.isEmpty); |
| 191 success("lastWhere", (l) => l.lastWhere((x) => true)); | 193 success("lastWhere", (l) => l.lastWhere((x) => true), list.isEmpty); |
| 192 success("singleWhere", (l) => l.singleWhere((x) => true)); | 194 success("singleWhere", (l) => l.singleWhere((x) => true), list.length != 1); |
| 193 success("isEmpty", (l) => l.isEmpty); | 195 success("isEmpty", (l) => l.isEmpty); |
| 194 success("isNotEmpty", (l) => l.isNotEmpty); | 196 success("isNotEmpty", (l) => l.isNotEmpty); |
| 195 success("join", (l) => l.join("/")); | 197 success("join", (l) => l.join("/")); |
| 196 success("fold", (l) => l.fold("--", (a, b) => "$a/$b")); | 198 success("fold", (l) => l.fold("--", (a, b) => "$a/$b")); |
| 197 success("reduce", (l) => l.reduce((a, b) => a + b)); | 199 if (elements is List<num> && list is List<num>) { |
| 200 success( | |
| 201 "reduce", | |
| 202 (l) => (l as List<num>).reduce((a, b) => (a + b).floor()), | |
| 203 list.isEmpty); | |
| 204 } | |
| 198 success("every", (l) => l.every((x) => x == 0)); | 205 success("every", (l) => l.every((x) => x == 0)); |
| 199 success("any", (l) => l.any((x) => x == 2)); | 206 success("any", (l) => l.any((x) => x == 2)); |
| 200 success("toList", (l) => l.toList()); | 207 success("toList", (l) => l.toList()); |
| 201 success("toSet", (l) => l.toSet()); | 208 success("toSet", (l) => l.toSet()); |
| 202 success("toString", (l) => l.toString()); | 209 success("toString", (l) => l.toString()); |
| 203 | 210 |
| 204 var it = elements.iterator; | 211 var it = elements.iterator; |
| 205 list.forEach((v) { | 212 list.forEach((v) { |
| 206 Expect.isTrue(it.moveNext()); | 213 Expect.isTrue(it.moveNext()); |
| 207 Expect.equals(it.current, v); | 214 Expect.equals(it.current, v); |
| 208 }); | 215 }); |
| 209 Expect.isFalse(it.moveNext()); | 216 Expect.isFalse(it.moveNext()); |
| 210 | 217 |
| 211 if (elements is List<int> && list is List<int>) { | 218 if (elements is List<int> && list is List<int>) { |
| 212 success("String.fromCharCodes", (l) => new String.fromCharCodes(l)); | 219 success("String.fromCharCodes", |
| 220 (l) => new String.fromCharCodes(l as List<int>)); | |
| 213 } | 221 } |
| 214 } | 222 } |
| 215 } | 223 } |
| 216 | 224 |
| 217 List<int> createConstList(int n) { | 225 List<int> createConstList(int n) { |
| 218 if (n == 0) return const <int>[]; | 226 if (n == 0) return const <int>[]; |
| 219 return const <int>[1, 2, 3]; | 227 return const <int>[1, 2, 3]; |
| 220 } | 228 } |
| 221 | 229 |
| 222 List<int> createFixedList(int n) { | 230 List<int> createFixedList(int n) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 Iterable createCodeUnits(int n) { | 303 Iterable createCodeUnits(int n) { |
| 296 var string = new String.fromCharCodes(new Iterable.generate(n)); | 304 var string = new String.fromCharCodes(new Iterable.generate(n)); |
| 297 return string.codeUnits; | 305 return string.codeUnits; |
| 298 } | 306 } |
| 299 | 307 |
| 300 Uint8List createTypedList(int n) { | 308 Uint8List createTypedList(int n) { |
| 301 var tl = new Uint8List(n); | 309 var tl = new Uint8List(n); |
| 302 for (int i = 0; i < n; i++) tl[i] = i; | 310 for (int i = 0; i < n; i++) tl[i] = i; |
| 303 return tl; | 311 return tl; |
| 304 } | 312 } |
| OLD | NEW |