Skip to content

Commit

Permalink
Merge branch 'master' into core-libs-dart-3
Browse files Browse the repository at this point in the history
* master:
  Add new Dart 2 Iterable and List methods (google#422)
  Document google#373 as breaking change (google#424)
  • Loading branch information
srawlins committed Mar 20, 2018
2 parents c5cac82 + d84e3af commit d0056af
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#### Master

* New: DelegatingIterable now includes a real implementation of
`followedBy`, and accepts the `orElse` parameter on `singleWhere`.
* New: DelegatingList now includes real implementations of `operator +`,
`indexWhere`, and `lastIndexWhere`.
* New: The map returned by `Multimap.asMap()` now includes real
implementations of `get entries` and `removeWhere`. This class also has
"real" implementations of `addEntries`, `map`, `update`, and `updateAll`,
Expand Down Expand Up @@ -53,6 +57,9 @@
`extent`, `forEachAsync`, `max`, `merge`, `min`, `reduceAsync`, and `zip`
are now type parameterized. Depending on the inferred value of each type
parameter, the return type of each function may change in existing code.
* BREAKING CHANGE: `Optional`'s `==` operator now takes into account `T`,
the type of the value. This changes, e.g. `Optional<int>.absent()` to no
longer be equal to `Optional<String>.absent()`.
* Deprecated: `reverse` in the `strings` library. No replacement is
provided.
* Deprecated: `createTimer`, `createTimerPeriodic` in the `async` library.
Expand Down
12 changes: 3 additions & 9 deletions lib/src/collection/delegates/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ abstract class DelegatingIterable<E> implements Iterable<E> {
delegate.fold(initialValue, combine);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
Iterable<E> followedBy(Iterable<E> other) {
throw new UnimplementedError("followedBy");
}
Iterable<E> followedBy(Iterable<E> other) => delegate.followedBy(other);

void forEach(void f(E element)) => delegate.forEach(f);

Expand Down Expand Up @@ -89,10 +85,8 @@ abstract class DelegatingIterable<E> implements Iterable<E> {

E get single => delegate.single;

E singleWhere(bool test(E element), {E orElse()}) {
if (orElse != null) throw new UnimplementedError("singleWhere:orElse");
return delegate.singleWhere(test);
}
E singleWhere(bool test(E element), {E orElse()}) =>
delegate.singleWhere(test, orElse: orElse);

Iterable<E> skip(int n) => delegate.skip(n);

Expand Down
22 changes: 5 additions & 17 deletions lib/src/collection/delegates/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
List<E> operator +(List<E> other) {
throw new UnimplementedError("+");
}
List<E> operator +(List<E> other) => delegate + other;

void add(E value) => delegate.add(value);

Expand Down Expand Up @@ -79,20 +75,15 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
int indexOf(E element, [int start = 0]) => delegate.indexOf(element, start);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
int indexWhere(bool test(E element), [int start = 0]) {
throw new UnimplementedError("indexWhere");
}
int indexWhere(bool test(E element), [int start = 0]) =>
delegate.indexWhere(test, start);

void insert(int index, E element) => delegate.insert(index, element);

void insertAll(int index, Iterable<E> iterable) =>
delegate.insertAll(index, iterable);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_setter
void set last(E element) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[this.length - 1] = element;
Expand All @@ -102,11 +93,8 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
delegate.lastIndexOf(element, start);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
int lastIndexWhere(bool test(E element), [int start]) {
throw new UnimplementedError("lastIndexWhere");
}
int lastIndexWhere(bool test(E element), [int start]) =>
delegate.lastIndexWhere(test, start);

void set length(int newLength) {
delegate.length = newLength;
Expand Down
1 change: 1 addition & 0 deletions lib/src/collection/multimap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ class _WrappedList<K, V> extends _WrappedIterable<K, V, List<V>>

@override
int lastIndexWhere(bool test(V element), [int start]) {
_syncDelegate();
return _delegate.lastIndexWhere(test, start);
}

Expand Down
9 changes: 9 additions & 0 deletions test/collection/delegates/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ void main() {
expect(new MyIterable([]).isNotEmpty, isFalse);
});

test('followedBy', () {
expect(delegatingIterable.followedBy(['d', 'e']),
equals(['a', 'b', 'cc', 'd', 'e']));
expect(delegatingIterable.followedBy(delegatingIterable),
equals(['a', 'b', 'cc', 'a', 'b', 'cc']));
});

test('forEach', () {
final it = delegatingIterable.iterator;
expect(it.current, isNull);
Expand Down Expand Up @@ -138,6 +145,8 @@ void main() {
expect(delegatingIterable.singleWhere((e) => e == 'b'), equals('b'));
expect(() => delegatingIterable.singleWhere((e) => e == 'd'),
throwsStateError);
expect(delegatingIterable.singleWhere((e) => e == 'd', orElse: () => 'X'),
equals('X'));
});

test('skip', () {
Expand Down
15 changes: 15 additions & 0 deletions test/collection/delegates/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void main() {
expect(delegatingList, equals(['d', 'b', 'cc']));
});

test('+', () {
var sum = delegatingList + ['d', 'e'];
expect(sum, equals(['a', 'b', 'cc', 'd', 'e']));
});

test('add', () {
delegatingList.add('d');
expect(delegatingList, equals(['a', 'b', 'cc', 'd']));
Expand Down Expand Up @@ -82,6 +87,11 @@ void main() {
expect(delegatingList.indexOf('cc', 1), equals(2));
});

test('indexWhere', () {
delegatingList.add('bb');
expect(delegatingList.indexWhere((e) => e.length > 1), equals(2));
});

test('insert', () {
delegatingList.insert(1, 'd');
expect(delegatingList, equals(['a', 'd', 'b', 'cc']));
Expand All @@ -98,6 +108,11 @@ void main() {
expect(delegatingList.lastIndexOf('cc', 1), equals(-1));
});

test('lastIndexWhere', () {
delegatingList.add('bb');
expect(delegatingList.lastIndexWhere((e) => e.length > 1), equals(3));
});

test('set length', () {
delegatingList.length = 2;
expect(delegatingList, equals(['a', 'b']));
Expand Down

0 comments on commit d0056af

Please sign in to comment.