| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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:unmodifiable_collection/unmodifiable_collection.dart"; | 5 import "package:unmodifiable_collection/unmodifiable_collection.dart"; |
| 6 import "package:unittest/unittest.dart"; | 6 import "package:unittest/unittest.dart"; |
| 7 | 7 |
| 8 // Test unmodifiable collection views. | 8 // Test unmodifiable collection views. |
| 9 // The collections should pass through the operations that are allowed, | 9 // The collections should pass through the operations that are allowed, |
| 10 // an throw on the ones that aren't without affecting the original. | 10 // an throw on the ones that aren't without affecting the original. |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 if (original.length != 1) { | 218 if (original.length != 1) { |
| 219 expect(() => wrapped.singleWhere((_) => true), throws); | 219 expect(() => wrapped.singleWhere((_) => true), throws); |
| 220 } else { | 220 } else { |
| 221 expect(wrapped.singleWhere((_) => true), | 221 expect(wrapped.singleWhere((_) => true), |
| 222 equals(original.singleWhere((_) => true))); | 222 equals(original.singleWhere((_) => true))); |
| 223 } | 223 } |
| 224 expect(() => wrapped.singleWhere((_) => false), throws); | 224 expect(() => wrapped.singleWhere((_) => false), throws); |
| 225 }); | 225 }); |
| 226 | 226 |
| 227 test("$name - skip", () { | 227 test("$name - skip", () { |
| 228 expect(wrapped.skip(0), equals(original.skip(0))); | 228 expect(wrapped.skip(0), orderedEquals(original.skip(0))); |
| 229 expect(wrapped.skip(1), equals(original.skip(1))); | 229 expect(wrapped.skip(1), orderedEquals(original.skip(1))); |
| 230 expect(wrapped.skip(5), equals(original.skip(5))); | 230 expect(wrapped.skip(5), orderedEquals(original.skip(5))); |
| 231 }); | 231 }); |
| 232 | 232 |
| 233 test("$name - skipWhile", () { | 233 test("$name - skipWhile", () { |
| 234 expect(wrapped.skipWhile((x) => true), | 234 expect(wrapped.skipWhile((x) => true), |
| 235 equals(original.skipWhile((x) => true))); | 235 orderedEquals(original.skipWhile((x) => true))); |
| 236 expect(wrapped.skipWhile((x) => false), | 236 expect(wrapped.skipWhile((x) => false), |
| 237 equals(original.skipWhile((x) => false))); | 237 orderedEquals(original.skipWhile((x) => false))); |
| 238 expect(wrapped.skipWhile((x) => x != 42), | 238 expect(wrapped.skipWhile((x) => x != 42), |
| 239 equals(original.skipWhile((x) => x != 42))); | 239 orderedEquals(original.skipWhile((x) => x != 42))); |
| 240 }); | 240 }); |
| 241 | 241 |
| 242 test("$name - take", () { | 242 test("$name - take", () { |
| 243 expect(wrapped.take(0), equals(original.take(0))); | 243 expect(wrapped.take(0), orderedEquals(original.take(0))); |
| 244 expect(wrapped.take(1), equals(original.take(1))); | 244 expect(wrapped.take(1), orderedEquals(original.take(1))); |
| 245 expect(wrapped.take(5), equals(original.take(5))); | 245 expect(wrapped.take(5), orderedEquals(original.take(5))); |
| 246 }); | 246 }); |
| 247 | 247 |
| 248 test("$name - takeWhile", () { | 248 test("$name - takeWhile", () { |
| 249 expect(wrapped.takeWhile((x) => true), | 249 expect(wrapped.takeWhile((x) => true), |
| 250 equals(original.takeWhile((x) => true))); | 250 orderedEquals(original.takeWhile((x) => true))); |
| 251 expect(wrapped.takeWhile((x) => false), | 251 expect(wrapped.takeWhile((x) => false), |
| 252 equals(original.takeWhile((x) => false))); | 252 orderedEquals(original.takeWhile((x) => false))); |
| 253 expect(wrapped.takeWhile((x) => x != 42), | 253 expect(wrapped.takeWhile((x) => x != 42), |
| 254 equals(original.takeWhile((x) => x != 42))); | 254 orderedEquals(original.takeWhile((x) => x != 42))); |
| 255 }); | 255 }); |
| 256 | 256 |
| 257 test("$name - toList", () { | 257 test("$name - toList", () { |
| 258 expect(wrapped.toList(), equals(original.toList())); | 258 expect(wrapped.toList(), orderedEquals(original.toList())); |
| 259 expect(wrapped.toList(growable: false), | 259 expect(wrapped.toList(growable: false), |
| 260 equals(original.toList(growable: false))); | 260 orderedEquals(original.toList(growable: false))); |
| 261 }); | 261 }); |
| 262 | 262 |
| 263 test("$name - toSet", () { | 263 test("$name - toSet", () { |
| 264 expect(wrapped.toSet(), equals(original.toSet())); | 264 expect(wrapped.toSet(), unorderedEquals(original.toSet())); |
| 265 }); | 265 }); |
| 266 | 266 |
| 267 test("$name - where", () { | 267 test("$name - where", () { |
| 268 expect(wrapped.where((x) => true), | 268 expect(wrapped.where((x) => true), |
| 269 equals(original.where((x) => true))); | 269 orderedEquals(original.where((x) => true))); |
| 270 expect(wrapped.where((x) => false), | 270 expect(wrapped.where((x) => false), |
| 271 equals(original.where((x) => false))); | 271 orderedEquals(original.where((x) => false))); |
| 272 expect(wrapped.where((x) => x != 42), | 272 expect(wrapped.where((x) => x != 42), |
| 273 equals(original.where((x) => x != 42))); | 273 orderedEquals(original.where((x) => x != 42))); |
| 274 }); | 274 }); |
| 275 } | 275 } |
| 276 | 276 |
| 277 void testReadList(List original, List wrapped, String name) { | 277 void testReadList(List original, List wrapped, String name) { |
| 278 test("$name - length", () { | 278 test("$name - length", () { |
| 279 expect(wrapped.length, equals(original.length)); | 279 expect(wrapped.length, equals(original.length)); |
| 280 }); | 280 }); |
| 281 | 281 |
| 282 test("$name - isEmpty", () { | 282 test("$name - isEmpty", () { |
| 283 expect(wrapped.isEmpty, equals(original.isEmpty)); | 283 expect(wrapped.isEmpty, equals(original.isEmpty)); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 original[0] = originalFirst; | 365 original[0] = originalFirst; |
| 366 } else { | 366 } else { |
| 367 expect(() { wrapped[0] = 42; }, throws); | 367 expect(() { wrapped[0] = 42; }, throws); |
| 368 } | 368 } |
| 369 }); | 369 }); |
| 370 | 370 |
| 371 test("$name - sort", () { | 371 test("$name - sort", () { |
| 372 List sortCopy = new List.from(original); | 372 List sortCopy = new List.from(original); |
| 373 sortCopy.sort(); | 373 sortCopy.sort(); |
| 374 wrapped.sort(); | 374 wrapped.sort(); |
| 375 expect(original, equals(sortCopy)); | 375 expect(original, orderedEquals(sortCopy)); |
| 376 original.setAll(0, copy); | 376 original.setAll(0, copy); |
| 377 }); | 377 }); |
| 378 | 378 |
| 379 test("$name - fillRange", () { | 379 test("$name - fillRange", () { |
| 380 wrapped.fillRange(0, wrapped.length, 37); | 380 wrapped.fillRange(0, wrapped.length, 37); |
| 381 for (int i = 0; i < original.length; i++) { | 381 for (int i = 0; i < original.length; i++) { |
| 382 expect(original[i], equals(37)); | 382 expect(original[i], equals(37)); |
| 383 } | 383 } |
| 384 original.setAll(0, copy); | 384 original.setAll(0, copy); |
| 385 }); | 385 }); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 | 468 |
| 469 test("$name - containsAll", () { | 469 test("$name - containsAll", () { |
| 470 expect(wrapped.containsAll(copy), isTrue); | 470 expect(wrapped.containsAll(copy), isTrue); |
| 471 expect(wrapped.containsAll(copy.toList()), isTrue); | 471 expect(wrapped.containsAll(copy.toList()), isTrue); |
| 472 expect(wrapped.containsAll([]), isTrue); | 472 expect(wrapped.containsAll([]), isTrue); |
| 473 expect(wrapped.containsAll([42]), equals(original.containsAll([42]))); | 473 expect(wrapped.containsAll([42]), equals(original.containsAll([42]))); |
| 474 }); | 474 }); |
| 475 | 475 |
| 476 test("$name - intersection", () { | 476 test("$name - intersection", () { |
| 477 expect(wrapped.intersection(new Set()), isEmpty); | 477 expect(wrapped.intersection(new Set()), isEmpty); |
| 478 expect(wrapped.intersection(copy), equals(original)); | 478 expect(wrapped.intersection(copy), unorderedEquals(original)); |
| 479 expect(wrapped.intersection(new Set.from([42])), | 479 expect(wrapped.intersection(new Set.from([42])), |
| 480 new Set.from(original.contains(42) ? [42] : [])); | 480 new Set.from(original.contains(42) ? [42] : [])); |
| 481 }); | 481 }); |
| 482 | 482 |
| 483 test("$name - union", () { | 483 test("$name - union", () { |
| 484 expect(wrapped.union(new Set()), equals(original)); | 484 expect(wrapped.union(new Set()), unorderedEquals(original)); |
| 485 expect(wrapped.union(copy), equals(original)); | 485 expect(wrapped.union(copy), unorderedEquals(original)); |
| 486 expect(wrapped.union(new Set.from([42])), | 486 expect(wrapped.union(new Set.from([42])), |
| 487 equals(original.union(new Set.from([42])))); | 487 equals(original.union(new Set.from([42])))); |
| 488 }); | 488 }); |
| 489 | 489 |
| 490 test("$name - difference", () { | 490 test("$name - difference", () { |
| 491 expect(wrapped.difference(new Set()), equals(original)); | 491 expect(wrapped.difference(new Set()), unorderedEquals(original)); |
| 492 expect(wrapped.difference(copy), isEmpty); | 492 expect(wrapped.difference(copy), isEmpty); |
| 493 expect(wrapped.difference(new Set.from([42])), | 493 expect(wrapped.difference(new Set.from([42])), |
| 494 equals(original.difference(new Set.from([42])))); | 494 equals(original.difference(new Set.from([42])))); |
| 495 }); | 495 }); |
| 496 } | 496 } |
| 497 | 497 |
| 498 void testNoChangeSet(Set original, Set wrapped, String name) { | 498 void testNoChangeSet(Set original, Set wrapped, String name) { |
| 499 Set copy = new Set.from(original); | 499 List originalElements = original.toList(); |
| 500 | 500 |
| 501 testThrows(name, thunk) { | 501 testThrows(name, thunk) { |
| 502 test(name, () { | 502 test(name, () { |
| 503 expect(thunk, throwsUnsupportedError); | 503 expect(thunk, throwsUnsupportedError); |
| 504 // No modifications happened. | 504 // No modifications happened. |
| 505 expect(original, equals(copy)); | 505 expect(original.toList(), equals(originalElements)); |
| 506 }); | 506 }); |
| 507 } | 507 } |
| 508 | 508 |
| 509 testThrows("$name - add throws", () { | 509 testThrows("$name - add throws", () { |
| 510 wrapped.add(42); | 510 wrapped.add(42); |
| 511 }); | 511 }); |
| 512 | 512 |
| 513 testThrows("$name - addAll throws", () { | 513 testThrows("$name - addAll throws", () { |
| 514 wrapped.addAll([42]); | 514 wrapped.addAll([42]); |
| 515 }); | 515 }); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 | 577 |
| 578 test("$name forEach", () { | 578 test("$name forEach", () { |
| 579 int origCnt = 0; | 579 int origCnt = 0; |
| 580 int wrapCnt = 0; | 580 int wrapCnt = 0; |
| 581 wrapped.forEach((k, v) { wrapCnt += 1 << k + 3 * v; }); | 581 wrapped.forEach((k, v) { wrapCnt += 1 << k + 3 * v; }); |
| 582 original.forEach((k, v) { origCnt += 1 << k + 3 * v; }); | 582 original.forEach((k, v) { origCnt += 1 << k + 3 * v; }); |
| 583 expect(wrapCnt, equals(origCnt)); | 583 expect(wrapCnt, equals(origCnt)); |
| 584 }); | 584 }); |
| 585 | 585 |
| 586 test("$name keys", () { | 586 test("$name keys", () { |
| 587 expect(wrapped.keys, equals(original.keys)); | 587 expect(wrapped.keys, orderedEquals(original.keys)); |
| 588 }); | 588 }); |
| 589 | 589 |
| 590 test("$name values", () { | 590 test("$name values", () { |
| 591 expect(wrapped.values, equals(original.values)); | 591 expect(wrapped.values, orderedEquals(original.values)); |
| 592 }); | 592 }); |
| 593 } | 593 } |
| 594 | 594 |
| 595 testNoChangeMap(Map original, Map wrapped, String name) { | 595 testNoChangeMap(Map original, Map wrapped, String name) { |
| 596 Map copy = new Map.from(original); | 596 Map copy = new Map.from(original); |
| 597 | 597 |
| 598 testThrows(name, thunk) { | 598 testThrows(name, thunk) { |
| 599 test(name, () { | 599 test(name, () { |
| 600 expect(thunk, throwsUnsupportedError); | 600 expect(thunk, throwsUnsupportedError); |
| 601 // No modifications happened. | 601 // No modifications happened. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 620 }); | 620 }); |
| 621 | 621 |
| 622 testThrows("$name remove throws", () { | 622 testThrows("$name remove throws", () { |
| 623 wrapped.remove(0); | 623 wrapped.remove(0); |
| 624 }); | 624 }); |
| 625 | 625 |
| 626 testThrows("$name clear throws", () { | 626 testThrows("$name clear throws", () { |
| 627 wrapped.clear(); | 627 wrapped.clear(); |
| 628 }); | 628 }); |
| 629 } | 629 } |
| OLD | NEW |