dart-sdk/tests/corelib/set_unmodifiable_view_test.dart
Lasse R.H. Nielsen b464fc9037 Reland "Tweak expect.dart library."
This reverts commit cd2c566bcf.

Reason for revert: Updating to not remove field used by Flutter engine.

Original change's description:
> Revert "Tweak `expect.dart` library."
>
> This reverts commit ff5f391c0a.
>
> Reason for revert: The expect library is used by Flutter engine, and some of its tests use assertStatementsEnabled. There should be a migration path that doesn't require an atomic change, like adding the replacement api before removing the old one.
>
> Original change's description:
> > Tweak `expect.dart` library.
> >
> > Make API more consistent for a few methods.
> > Reduce the number of language features used in tests:
> > * Never iterating an iterable, always converting it
> >   using `.toList()` first and iterating using indices
> >   (fx `setEquals`).
> >   Also require a `List` in places where an `Iterable`
> >   wasn't necessary.
> > * Avoid doing complicated computations that are also
> >   used for the error message. Do simple check first,
> >   then recompute to get better error messages
> >   (fx `allDistinct`).
> >
> > Renamed some rarely used members for consistency
> > (`stringContainsInOrder`->`containsInOrder`,
> > where other string-contains functions just start
> > with `contains`, and `containsOneOf` -> `containsAny`
> > to match `Iterable.any` phrasing, and also it accepts
> > if containing at least one, not precisely one.)
> >
> > Removed a function that wasn't used anywhere.
> >
> > Moved `assertStatementsEnabled` to `variations.dart` as `asserts`.
> > Removed `typeAssertionsEnabled` and `checkedModeEnabled`. The former used in one place, where it was replaced with `checkedImplicitDowncasts` from `variations.dart`, the latter wasn't used anywhere.
> >
> > Deprecates `package:expect/minitest.dart`. It was never intended
> > to be used for new tests, only as a help to convert existing tests
> > written against `package:unit_test`.
> > All existing imports marked as `// ignore: deprecated_member_use`.
> >
> > Change-Id: I07e21d4c0f3ccf11b82ee34af2668fdbb22264d2
> > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352360
> > Reviewed-by: Slava Egorov <vegorov@google.com>
> > Reviewed-by: Ömer Ağacan <omersa@google.com>
> > Reviewed-by: Nate Bosch <nbosch@google.com>
> > Reviewed-by: Stephen Adams <sra@google.com>
> > Commit-Queue: Lasse Nielsen <lrn@google.com>
>
> Change-Id: I360b4347470a0bb2b63c3108e2b83ee2a771bf3f
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362020
> Reviewed-by: Nate Bosch <nbosch@google.com>
> Reviewed-by: Ömer Ağacan <omersa@google.com>
> Reviewed-by: Stephen Adams <sra@google.com>
> Reviewed-by: Leaf Petersen <leafp@google.com>
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Commit-Queue: William Hesse <whesse@google.com>

CoreLibraryReviewExempt: Reland
Change-Id: I53db40edc0733842a008839c3913d51c885e39ab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362502
Reviewed-by: Alexander Thomas <athom@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2024-04-26 15:28:26 +00:00

224 lines
6.3 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "dart:collection";
main() {
testIterableApi();
testUnmodifiableSetApi();
testMutatingApisThrow();
testChangesInOriginalSetAreObservedInUnmodifiableView();
}
void testIterableApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.equals(wrapped.any((_) => true), original.any((_) => true));
Expect.equals(wrapped.any((_) => false), original.any((_) => false));
Expect.equals(wrapped.contains(0), original.contains(0));
Expect.equals(wrapped.elementAt(0), original.elementAt(0));
Expect.equals(wrapped.every((_) => true), original.every((_) => true));
Expect.equals(wrapped.every((_) => false), original.every((_) => false));
Expect.listEquals(wrapped.expand((x) => [x, x]).toList(),
original.expand((x) => [x, x]).toList());
Expect.equals(wrapped.first, original.first);
Expect.equals(
wrapped.firstWhere((_) => true), original.firstWhere((_) => true));
Expect.throwsStateError(() {
wrapped.firstWhere((_) => false);
}, "firstWhere");
Expect.equals(wrapped.fold<int>(0, (x, y) => x + y),
original.fold<int>(0, (x, y) => x + y));
testForeach(wrapped, original);
Expect.equals(wrapped.isEmpty, original.isEmpty);
Expect.equals(wrapped.isNotEmpty, original.isNotEmpty);
testIterator(wrapped, original);
Expect.equals(wrapped.join(""), original.join(""));
Expect.equals(wrapped.join("-"), original.join("-"));
Expect.equals(wrapped.last, original.last);
Expect.equals(
wrapped.lastWhere((_) => true), original.lastWhere((_) => true));
Expect.throwsStateError(() {
wrapped.lastWhere((_) => false);
}, "lastWhere");
Expect.equals(wrapped.length, original.length);
Expect.setEquals(
wrapped.map((x) => "[$x]").toSet(), original.map((x) => "[$x]").toSet());
Expect.equals(
wrapped.reduce((x, y) => x + y), original.reduce((x, y) => x + y));
Expect.throwsStateError(() {
wrapped.single;
}, "single");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => true);
}, "singleWhere true");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => false);
}, "singleWhere false");
Expect.setEquals(wrapped.skip(0).toSet(), original.skip(0).toSet());
Expect.setEquals(wrapped.skip(1).toSet(), original.skip(1).toSet());
Expect.setEquals(wrapped.skipWhile((_) => true).toSet(),
original.skipWhile((_) => true).toSet());
Expect.setEquals(wrapped.skipWhile((_) => false).toSet(),
original.skipWhile((_) => false).toSet());
Expect.setEquals(wrapped.take(0).toSet(), original.take(0).toSet());
Expect.setEquals(wrapped.take(1).toSet(), original.take(1).toSet());
Expect.setEquals(wrapped.takeWhile((_) => true).toSet(),
original.takeWhile((_) => true).toSet());
Expect.setEquals(wrapped.takeWhile((_) => false).toSet(),
original.takeWhile((_) => false).toSet());
var toListResult = wrapped.toList();
Expect.listEquals(original.toList(), toListResult);
toListResult.add(4);
Expect.listEquals([1, 2, 3, 4], toListResult);
toListResult[3] = 5;
Expect.listEquals([1, 2, 3, 5], toListResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
var toSetResult = wrapped.toSet();
Expect.setEquals(original.toSet(), toSetResult);
toSetResult.add(4);
Expect.setEquals({1, 2, 3, 4}, toSetResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
Expect.setEquals(
wrapped.where((_) => true).toSet(), original.where((_) => true).toSet());
Expect.setEquals(wrapped.where((_) => false).toSet(),
original.where((_) => false).toSet());
}
void testUnmodifiableSetApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.isTrue(wrapped.containsAll(copy));
Expect.isTrue(wrapped.containsAll(copy.toList()));
Expect.isTrue(wrapped.containsAll([]));
Expect.isTrue(wrapped.intersection({}).isEmpty);
Expect.setEquals(wrapped.intersection(copy), original);
Expect.setEquals(wrapped.union({}), original);
Expect.setEquals(wrapped.union(copy), original);
Expect.setEquals(wrapped.difference({}), original);
Expect.isTrue(wrapped.difference(copy).isEmpty);
}
void testMutatingApisThrow() {
UnmodifiableSetView<int> s = new UnmodifiableSetView({1, 2, 3});
Expect.throwsUnsupportedError(() {
s.add(3);
}, "add");
Expect.throwsUnsupportedError(() {
s.addAll({1, 2, 3});
}, "addAll");
Expect.throwsUnsupportedError(() {
s.addAll(<int>{});
}, "addAll empty");
Expect.throwsUnsupportedError(() {
s.remove(3);
}, "remove");
Expect.throwsUnsupportedError(() {
s.removeAll({1, 2, 3});
}, "removeAll");
Expect.throwsUnsupportedError(() {
s.removeAll(<int>{});
}, "removeAll empty");
Expect.throwsUnsupportedError(() {
s.retainAll({1, 2, 3});
}, "retainAll");
Expect.throwsUnsupportedError(() {
s.retainAll(<int>{});
}, "retainAll empty");
Expect.throwsUnsupportedError(() {
s.removeWhere((_) => true);
}, "removeWhere");
Expect.throwsUnsupportedError(() {
s.retainWhere((_) => false);
}, "retainWhere");
Expect.throwsUnsupportedError(() {
s.clear();
}, "clear");
}
void testChangesInOriginalSetAreObservedInUnmodifiableView() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
original.add(4);
Expect.setEquals(original, wrapped);
Expect.setEquals({4}, wrapped.difference(copy));
}
void testForeach(Set<int> wrapped, Set<int> original) {
var wrapCtr = 0;
var origCtr = 0;
wrapped.forEach((x) {
wrapCtr += x;
});
original.forEach((x) {
origCtr += x;
});
Expect.equals(wrapCtr, origCtr);
}
void testIterator(Set<int> wrapped, Set<int> original) {
Iterator wrapIter = wrapped.iterator;
Iterator origIter = original.iterator;
while (origIter.moveNext()) {
Expect.isTrue(wrapIter.moveNext());
Expect.equals(wrapIter.current, origIter.current);
}
Expect.isFalse(wrapIter.moveNext());
}