Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes problem with MutableMap's setValue #190

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/src/collection/impl/map_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DartMutableMap<K, V> extends Object implements KtMutableMap<K, V> {

@override
Iterable<KtMapEntry<K, V>> get iter =>
_map.entries.map((entry) => _MutableEntry.from(entry));
_map.entries.map((entry) => _MutableEntry.from(entry, this));

@override
Map<K, V> asMap() => _map;
Expand All @@ -31,8 +31,8 @@ class DartMutableMap<K, V> extends Object implements KtMutableMap<K, V> {
bool containsValue(V value) => _map.containsValue(value);

@override
KtMutableSet<KtMutableMapEntry<K, V>> get entries =>
linkedSetFrom(_map.entries.map((entry) => _MutableEntry.from(entry)));
KtMutableSet<KtMutableMapEntry<K, V>> get entries => linkedSetFrom(
_map.entries.map((entry) => _MutableEntry.from(entry, this)));

@override
V? get(K key) => _map[key];
Expand Down Expand Up @@ -121,14 +121,21 @@ class DartMutableMap<K, V> extends Object implements KtMutableMap<K, V> {
}

class _MutableEntry<K, V> implements KtMutableMapEntry<K, V> {
_MutableEntry(this._key, this._value);
_MutableEntry(this._key, this._value, this._parent);

factory _MutableEntry.from(MapEntry<K, V> entry) =>
_MutableEntry(entry.key, entry.value);
factory _MutableEntry.from(
MapEntry<K, V> entry,
DartMutableMap<K, V> parent,
) =>
_MutableEntry(entry.key, entry.value, parent);

K _key;
V _value;

/// Object reference for the [DartMutableMap] which contains this
/// [_MutableEntry].
DartMutableMap<K, V> _parent;

@override
K get key => _key;

Expand All @@ -137,12 +144,9 @@ class _MutableEntry<K, V> implements KtMutableMapEntry<K, V> {

@override
V setValue(V newValue) {
// setting _value here is wrong because is is a copy of the original value.
// setValue should modify the underlying list, not the copy
// see how kotlin solved this:
// https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt
throw UnimplementedError(
"setValue() in not yet implemented. Please vote for https://github.com/passsy/dart_kollection/issues/55 for prioritization");
final oldValue = _value;
_parent._map.update(key, (value) => value = newValue);
return oldValue;
}

@override
Expand Down
25 changes: 7 additions & 18 deletions test/collection/map_mutable_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import "package:kt_dart/collection.dart";
import "package:test/test.dart";

import "../test/assert_dart.dart";

void main() {
group("KtMutableMap", () {
group("mutableMapFrom", () {
Expand Down Expand Up @@ -71,25 +69,16 @@ void testMutableMap(
});

test("set value for mutable entry", () {
final pokemon = mutableMapFrom({
1: "Bulbasaur",
2: "Ivysaur",
});
final pokemon = mutableMapFrom({1: "Bulbasaur", 2: "Ivysaur"});

pokemon.entries.forEach((entry) {
final oldValue = entry.value;
final newValue = entry.value.toUpperCase();

final e = catchException(() {
pokemon.entries.forEach((entry) {
entry.setValue(entry.value.toUpperCase());
});
expect(entry.setValue(newValue), oldValue);
});

// TODO exchange error check with assertion once https://github.com/passsy/dart_kollection/issues/55 has been fixed
expect(e, const TypeMatcher<UnimplementedError>());
// expect(
// pokemon,
// mapFrom({
// 1: "BULBASAUR",
// 2: "IVYSAUR",
// }));
expect(pokemon, mapFrom({1: "BULBASAUR", 2: "IVYSAUR"}));
});
});
}