Skip to content

Commit cb46a69

Browse files
authored
Start implementing Dart 2.0 collection methods. (flutter#77)
* Start implementing Dart 2.0 collection methods. * Travis and tests.
1 parent fac9e50 commit cb46a69

File tree

7 files changed

+47
-46
lines changed

7 files changed

+47
-46
lines changed

.travis.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: dart
22

33
dart:
44
- dev
5-
- stable
65

76
dart_task:
87
- test: --platform vm
@@ -13,12 +12,6 @@ dart_task:
1312
branches:
1413
only: [master]
1514

16-
matrix:
17-
# Only run dartfmt checks with stable.
18-
exclude:
19-
- dart: dev
20-
dart_task: dartfmt
21-
2215
cache:
2316
directories:
2417
- $HOME/.pub-cache

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.14.7-dev
2+
3+
* Only the Dart 2 dev SDK (`>=2.0.0-dev.22.0`) is now supported.
4+
* Add support for `Map.{addEntries|entries}` for Dart 2.0.
5+
16
## 1.14.6
27

38
* Make `DefaultEquality`'s `equals()` and `hash()` methods take any `Object`

lib/src/canonicalized_map.dart

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,10 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
6969
other.forEach((key, value) => this[key] = value);
7070
}
7171

72-
// TODO: Dart 2.0 requires this method to be implemented.
73-
void addEntries(Iterable<Object> entries) {
74-
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
75-
// the MapEntry class has been added.
76-
throw new UnimplementedError('addEntries');
77-
}
72+
void addEntries(Iterable<MapEntry<K, V>> entries) =>
73+
_base.addEntries(entries.map(
74+
(e) => new MapEntry(_canonicalize(e.key), new Pair(e.key, e.value))));
7875

79-
// TODO: Dart 2.0 requires this method to be implemented.
8076
Map<K2, V2> cast<K2, V2>() {
8177
throw new UnimplementedError('cast');
8278
}
@@ -93,12 +89,8 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
9389
bool containsValue(Object value) =>
9490
_base.values.any((pair) => pair.last == value);
9591

96-
// TODO: Dart 2.0 requires this method to be implemented.
97-
Iterable<Null> get entries {
98-
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
99-
// the MapEntry class has been added.
100-
throw new UnimplementedError('entries');
101-
}
92+
Iterable<MapEntry<K, V>> get entries =>
93+
_base.entries.map((e) => new MapEntry(e.value.first, e.value.last));
10294

10395
void forEach(void f(K key, V value)) {
10496
_base.forEach((key, pair) => f(pair.first, pair.last));

lib/src/typed_wrappers.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,8 @@ class TypeSafeMap<K, V> implements DelegatingMap<K, V> {
395395
_base.addAll(other);
396396
}
397397

398-
// TODO: Dart 2.0 requires this method to be implemented.
399-
void addEntries(Iterable<Object> entries) {
400-
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
401-
// the MapEntry class has been added.
402-
throw new UnimplementedError('addEntries');
398+
void addEntries(Iterable<MapEntry<K, V>> entries) {
399+
_base.addEntries(entries);
403400
}
404401

405402
// TODO: Dart 2.0 requires this method to be implemented.
@@ -415,12 +412,7 @@ class TypeSafeMap<K, V> implements DelegatingMap<K, V> {
415412

416413
bool containsValue(Object value) => _base.containsValue(value);
417414

418-
// TODO: Dart 2.0 requires this method to be implemented.
419-
Iterable<Null> get entries {
420-
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
421-
// the MapEntry class has been added.
422-
throw new UnimplementedError('entries');
423-
}
415+
Iterable<MapEntry<K, V>> get entries => _base.entries;
424416

425417
void forEach(void f(K key, V value)) {
426418
_base.forEach((key, value) => f(key as K, value as V));

lib/src/wrappers.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,8 @@ class DelegatingMap<K, V> implements Map<K, V> {
450450
_base.addAll(other);
451451
}
452452

453-
// TODO: Dart 2.0 requires this method to be implemented.
454-
void addEntries(Iterable<Object> entries) {
455-
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
456-
// the MapEntry class has been added.
457-
throw new UnimplementedError('addEntries');
453+
void addEntries(Iterable<MapEntry<K, V>> entries) {
454+
_base.addEntries(entries);
458455
}
459456

460457
void clear() {
@@ -470,12 +467,7 @@ class DelegatingMap<K, V> implements Map<K, V> {
470467

471468
bool containsValue(Object value) => _base.containsValue(value);
472469

473-
// TODO: Dart 2.0 requires this method to be implemented.
474-
Iterable<Null> get entries {
475-
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
476-
// the MapEntry class has been added.
477-
throw new UnimplementedError('entries');
478-
}
470+
Iterable<MapEntry<K, V>> get entries => _base.entries;
479471

480472
void forEach(void f(K key, V value)) {
481473
_base.forEach(f);

pubspec.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
name: collection
2-
version: 1.14.6
2+
version: 1.14.7-dev
33
author: Dart Team <[email protected]>
44
description: Collections and utilities functions and classes related to collections.
55
homepage: https://www.github.com/dart-lang/collection
6+
67
environment:
7-
sdk: '>=1.21.0 <2.0.0'
8+
# Required for Dart 2.0 collection changes.
9+
sdk: '>=2.0.0-dev.22.0 <2.0.0'
10+
811
dev_dependencies:
912
test: '^0.12.0'

test/canonicalized_map_test.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import "package:test/test.dart";
77

88
void main() {
99
group("with an empty canonicalized map", () {
10-
var map;
10+
CanonicalizedMap<int, String, String> map;
11+
1112
setUp(() {
12-
map = new CanonicalizedMap<int, String, String>(int.parse,
13+
map = new CanonicalizedMap(int.parse,
1314
isValidKey: (s) => new RegExp(r"^\d+$").hasMatch(s as String));
1415
});
1516

@@ -130,6 +131,29 @@ void main() {
130131

131132
expect(map.values, equals(["value 01", "value 2", "value 03"]));
132133
});
134+
135+
test("entries returns all key-value pairs in the map", () {
136+
map.addAll({
137+
"1": "value 1",
138+
"01": "value 01",
139+
"2": "value 2",
140+
});
141+
142+
var entries = map.entries.toList();
143+
expect(entries[0].key, "01");
144+
expect(entries[0].value, "value 01");
145+
expect(entries[1].key, "2");
146+
expect(entries[1].value, "value 2");
147+
});
148+
149+
test("addEntries adds key-value pairs to the map", () {
150+
map.addEntries([
151+
new MapEntry("1", "value 1"),
152+
new MapEntry("01", "value 01"),
153+
new MapEntry("2", "value 2"),
154+
]);
155+
expect(map, {"01": "value 01", "2": "value 2"});
156+
});
133157
});
134158

135159
group("CanonicalizedMap builds an informative string representation", () {

0 commit comments

Comments
 (0)