OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import "package:collection/collection.dart"; |
| 6 import "package:test/test.dart"; |
| 7 |
| 8 import '../utils.dart'; |
| 9 |
| 10 void main() { |
| 11 group("with valid types, forwards", () { |
| 12 var wrapper; |
| 13 var emptyWrapper; |
| 14 var singleWrapper; |
| 15 setUp(() { |
| 16 wrapper = |
| 17 DelegatingIterable.typed<int>(<Object>[1, 2, 3, 4, 5].map((i) => i)); |
| 18 emptyWrapper = DelegatingIterable.typed<int>(<Object>[].map((i) => i)); |
| 19 singleWrapper = DelegatingIterable.typed<int>(<Object>[1].map((i) => i)); |
| 20 }); |
| 21 |
| 22 test("any()", () { |
| 23 expect(wrapper.any((i) => i > 3), isTrue); |
| 24 expect(wrapper.any((i) => i > 5), isFalse); |
| 25 }); |
| 26 |
| 27 test("contains()", () { |
| 28 expect(wrapper.contains(2), isTrue); |
| 29 expect(wrapper.contains(6), isFalse); |
| 30 expect(wrapper.contains("foo"), isFalse); |
| 31 }); |
| 32 |
| 33 test("elementAt()", () { |
| 34 expect(wrapper.elementAt(1), equals(2)); |
| 35 expect(wrapper.elementAt(4), equals(5)); |
| 36 expect(() => wrapper.elementAt(5), throwsRangeError); |
| 37 expect(() => wrapper.elementAt(-1), throwsRangeError); |
| 38 }); |
| 39 |
| 40 test("every()", () { |
| 41 expect(wrapper.every((i) => i < 6), isTrue); |
| 42 expect(wrapper.every((i) => i > 3), isFalse); |
| 43 }); |
| 44 |
| 45 test("expand()", () { |
| 46 expect(wrapper.expand((i) => [i]), equals([1, 2, 3, 4, 5])); |
| 47 expect(wrapper.expand((i) => [i, i]), |
| 48 equals([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])); |
| 49 }); |
| 50 |
| 51 test("first", () { |
| 52 expect(wrapper.first, equals(1)); |
| 53 expect(() => emptyWrapper.first, throwsStateError); |
| 54 }); |
| 55 |
| 56 test("firstWhere()", () { |
| 57 expect(wrapper.firstWhere((i) => i > 3), equals(4)); |
| 58 expect(() => wrapper.firstWhere((i) => i > 5), throwsStateError); |
| 59 expect(wrapper.firstWhere((i) => i > 5, orElse: () => -1), equals(-1)); |
| 60 }); |
| 61 |
| 62 test("fold()", () { |
| 63 expect(wrapper.fold("", (previous, i) => previous + i.toString()), |
| 64 equals("12345")); |
| 65 expect(emptyWrapper.fold(null, (previous, i) => previous + i), isNull); |
| 66 }); |
| 67 |
| 68 test("forEach()", () { |
| 69 var results = []; |
| 70 wrapper.forEach(results.add); |
| 71 expect(results, equals([1, 2, 3, 4, 5])); |
| 72 |
| 73 emptyWrapper.forEach(expectAsync1((_) {}, count: 0)); |
| 74 }); |
| 75 |
| 76 test("isEmpty", () { |
| 77 expect(wrapper.isEmpty, isFalse); |
| 78 expect(emptyWrapper.isEmpty, isTrue); |
| 79 }); |
| 80 |
| 81 test("isNotEmpty", () { |
| 82 expect(wrapper.isNotEmpty, isTrue); |
| 83 expect(emptyWrapper.isNotEmpty, isFalse); |
| 84 }); |
| 85 |
| 86 test("iterator", () { |
| 87 var iterator = wrapper.iterator; |
| 88 expect(iterator.current, isNull); |
| 89 expect(iterator.moveNext(), isTrue); |
| 90 expect(iterator.current, equals(1)); |
| 91 expect(iterator.moveNext(), isTrue); |
| 92 expect(iterator.current, equals(2)); |
| 93 expect(iterator.moveNext(), isTrue); |
| 94 expect(iterator.current, equals(3)); |
| 95 expect(iterator.moveNext(), isTrue); |
| 96 expect(iterator.current, equals(4)); |
| 97 expect(iterator.moveNext(), isTrue); |
| 98 expect(iterator.current, equals(5)); |
| 99 expect(iterator.moveNext(), isFalse); |
| 100 expect(iterator.current, isNull); |
| 101 }); |
| 102 |
| 103 test("join()", () { |
| 104 expect(wrapper.join(), "12345"); |
| 105 expect(wrapper.join("-"), "1-2-3-4-5"); |
| 106 }); |
| 107 |
| 108 test("last", () { |
| 109 expect(wrapper.last, equals(5)); |
| 110 expect(() => emptyWrapper.last, throwsStateError); |
| 111 }); |
| 112 |
| 113 test("lastWhere()", () { |
| 114 expect(wrapper.lastWhere((i) => i > 3), equals(5)); |
| 115 expect(() => wrapper.lastWhere((i) => i > 5), throwsStateError); |
| 116 expect(wrapper.lastWhere((i) => i > 5, orElse: () => -1), equals(-1)); |
| 117 }); |
| 118 |
| 119 test("length", () { |
| 120 expect(wrapper.length, equals(5)); |
| 121 expect(emptyWrapper.length, equals(0)); |
| 122 }); |
| 123 |
| 124 test("map()", () { |
| 125 expect(wrapper.map((i) => i + 1), equals([2, 3, 4, 5, 6])); |
| 126 expect(wrapper.map((i) => i / 2), equals([0.5, 1.0, 1.5, 2.0, 2.5])); |
| 127 }); |
| 128 |
| 129 test("reduce()", () { |
| 130 expect(wrapper.reduce((value, i) => value + i), equals(15)); |
| 131 expect( |
| 132 () => emptyWrapper.reduce((value, i) => value + i), throwsStateError); |
| 133 }); |
| 134 |
| 135 test("single", () { |
| 136 expect(() => wrapper.single, throwsStateError); |
| 137 expect(singleWrapper.single, equals(1)); |
| 138 }); |
| 139 |
| 140 test("singleWhere()", () { |
| 141 expect(() => wrapper.singleWhere((i) => i.isOdd), throwsStateError); |
| 142 expect(singleWrapper.singleWhere((i) => i.isOdd), equals(1)); |
| 143 expect( |
| 144 () => singleWrapper.singleWhere((i) => i.isEven), throwsStateError); |
| 145 }); |
| 146 |
| 147 test("skip()", () { |
| 148 expect(wrapper.skip(3), equals([4, 5])); |
| 149 expect(wrapper.skip(10), isEmpty); |
| 150 expect(() => wrapper.skip(-1), throwsRangeError); |
| 151 }); |
| 152 |
| 153 test("skipWhile()", () { |
| 154 expect(wrapper.skipWhile((i) => i < 3), equals([3, 4, 5])); |
| 155 expect(wrapper.skipWhile((i) => i < 10), isEmpty); |
| 156 }); |
| 157 |
| 158 test("take()", () { |
| 159 expect(wrapper.take(3), equals([1, 2, 3])); |
| 160 expect(wrapper.take(10), equals([1, 2, 3, 4, 5])); |
| 161 expect(() => wrapper.take(-1), throwsRangeError); |
| 162 }); |
| 163 |
| 164 test("takeWhile()", () { |
| 165 expect(wrapper.takeWhile((i) => i < 3), equals([1, 2])); |
| 166 expect(wrapper.takeWhile((i) => i < 10), equals([1, 2, 3, 4, 5])); |
| 167 }); |
| 168 |
| 169 test("toList()", () { |
| 170 expect(wrapper.toList(), equals([1, 2, 3, 4, 5])); |
| 171 expect(wrapper.toList(growable: false), equals([1, 2, 3, 4, 5])); |
| 172 expect( |
| 173 () => wrapper.toList(growable: false).add(6), throwsUnsupportedError); |
| 174 }); |
| 175 |
| 176 test("toSet()", () { |
| 177 expect(wrapper.toSet(), unorderedEquals([1, 2, 3, 4, 5])); |
| 178 expect(DelegatingIterable.typed<int>(<Object>[1, 1, 2, 2]).toSet(), |
| 179 unorderedEquals([1, 2])); |
| 180 }); |
| 181 |
| 182 test("where()", () { |
| 183 expect(wrapper.where((i) => i.isOdd), equals([1, 3, 5])); |
| 184 expect(wrapper.where((i) => i.isEven), equals([2, 4])); |
| 185 }); |
| 186 |
| 187 test("toString()", () { |
| 188 expect(wrapper.toString(), equals("(1, 2, 3, 4, 5)")); |
| 189 expect(emptyWrapper.toString(), equals("()")); |
| 190 }); |
| 191 }); |
| 192 |
| 193 group("with invalid types", () { |
| 194 var wrapper; |
| 195 var singleWrapper; |
| 196 setUp(() { |
| 197 wrapper = DelegatingIterable |
| 198 .typed<int>(<Object>["foo", "bar", "baz"].map((element) => element)); |
| 199 singleWrapper = DelegatingIterable |
| 200 .typed<int>(<Object>["foo"].map((element) => element)); |
| 201 }); |
| 202 |
| 203 group("throws a CastError for", () { |
| 204 test("any()", () { |
| 205 expect(() => wrapper.any(expectAsync1((_) => false, count: 0)), |
| 206 throwsCastError); |
| 207 }); |
| 208 |
| 209 test("elementAt()", () { |
| 210 expect(() => wrapper.elementAt(1), throwsCastError); |
| 211 }); |
| 212 |
| 213 test("every()", () { |
| 214 expect(() => wrapper.every(expectAsync1((_) => false, count: 0)), |
| 215 throwsCastError); |
| 216 }); |
| 217 |
| 218 test("expand()", () { |
| 219 var lazy = wrapper.expand(expectAsync1((_) => [], count: 0)); |
| 220 expect(() => lazy.first, throwsCastError); |
| 221 }); |
| 222 |
| 223 test("first", () { |
| 224 expect(() => wrapper.first, throwsCastError); |
| 225 }); |
| 226 |
| 227 test("firstWhere()", () { |
| 228 expect(() => wrapper.firstWhere(expectAsync1((_) => false, count: 0)), |
| 229 throwsCastError); |
| 230 }); |
| 231 |
| 232 test("fold()", () { |
| 233 expect( |
| 234 () => wrapper.fold(null, expectAsync2((_, __) => null, count: 0)), |
| 235 throwsCastError); |
| 236 }); |
| 237 |
| 238 test("forEach()", () { |
| 239 expect(() => wrapper.forEach(expectAsync1((_) {}, count: 0)), |
| 240 throwsCastError); |
| 241 }); |
| 242 |
| 243 test("iterator", () { |
| 244 var iterator = wrapper.iterator; |
| 245 expect(iterator.current, isNull); |
| 246 expect(() => iterator.moveNext(), throwsCastError); |
| 247 }); |
| 248 |
| 249 test("last", () { |
| 250 expect(() => wrapper.last, throwsCastError); |
| 251 }); |
| 252 |
| 253 test("lastWhere()", () { |
| 254 expect(() => wrapper.lastWhere(expectAsync1((_) => false, count: 0)), |
| 255 throwsCastError); |
| 256 }); |
| 257 |
| 258 test("map()", () { |
| 259 var lazy = wrapper.map(expectAsync1((_) => null, count: 0)); |
| 260 expect(() => lazy.first, throwsCastError); |
| 261 }); |
| 262 |
| 263 test("reduce()", () { |
| 264 expect(() => wrapper.reduce(expectAsync2((_, __) => null, count: 0)), |
| 265 throwsCastError); |
| 266 }); |
| 267 |
| 268 test("single", () { |
| 269 expect(() => singleWrapper.single, throwsCastError); |
| 270 }); |
| 271 |
| 272 test("singleWhere()", () { |
| 273 expect(() { |
| 274 singleWrapper.singleWhere(expectAsync1((_) => false, count: 0)); |
| 275 }, throwsCastError); |
| 276 }); |
| 277 |
| 278 test("skip()", () { |
| 279 var lazy = wrapper.skip(1); |
| 280 expect(() => lazy.first, throwsCastError); |
| 281 }); |
| 282 |
| 283 test("skipWhile()", () { |
| 284 var lazy = wrapper.skipWhile(expectAsync1((_) => false, count: 0)); |
| 285 expect(() => lazy.first, throwsCastError); |
| 286 }); |
| 287 |
| 288 test("take()", () { |
| 289 var lazy = wrapper.take(1); |
| 290 expect(() => lazy.first, throwsCastError); |
| 291 }); |
| 292 |
| 293 test("takeWhile()", () { |
| 294 var lazy = wrapper.takeWhile(expectAsync1((_) => false, count: 0)); |
| 295 expect(() => lazy.first, throwsCastError); |
| 296 }); |
| 297 |
| 298 test("toList()", () { |
| 299 var list = wrapper.toList(); |
| 300 expect(() => list.first, throwsCastError); |
| 301 }); |
| 302 |
| 303 test("toSet()", () { |
| 304 var asSet = wrapper.toSet(); |
| 305 expect(() => asSet.first, throwsCastError); |
| 306 }); |
| 307 |
| 308 test("where()", () { |
| 309 var lazy = wrapper.where(expectAsync1((_) => false, count: 0)); |
| 310 expect(() => lazy.first, throwsCastError); |
| 311 }); |
| 312 }); |
| 313 |
| 314 group("doesn't throw a CastError for", () { |
| 315 test("contains()", () { |
| 316 expect(wrapper.contains(1), isFalse); |
| 317 expect(wrapper.contains("foo"), isTrue); |
| 318 }); |
| 319 |
| 320 test("elementAt()", () { |
| 321 expect(() => wrapper.elementAt(-1), throwsRangeError); |
| 322 expect(() => wrapper.elementAt(10), throwsRangeError); |
| 323 }); |
| 324 |
| 325 test("isEmpty", () { |
| 326 expect(wrapper.isEmpty, isFalse); |
| 327 }); |
| 328 |
| 329 test("isNotEmpty", () { |
| 330 expect(wrapper.isNotEmpty, isTrue); |
| 331 }); |
| 332 |
| 333 test("join()", () { |
| 334 expect(wrapper.join(), "foobarbaz"); |
| 335 }); |
| 336 |
| 337 test("length", () { |
| 338 expect(wrapper.length, equals(3)); |
| 339 }); |
| 340 |
| 341 test("single", () { |
| 342 expect(() => wrapper.single, throwsStateError); |
| 343 }); |
| 344 |
| 345 test("skip()", () { |
| 346 expect(() => wrapper.skip(-1), throwsRangeError); |
| 347 }); |
| 348 |
| 349 test("toString()", () { |
| 350 expect(wrapper.toString(), equals("(foo, bar, baz)")); |
| 351 }); |
| 352 }); |
| 353 }, skip: "Re-enable this when test can run DDC (test#414)."); |
| 354 } |
OLD | NEW |