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

throw for KMutableEntry.setValue #56

Merged
merged 3 commits into from
Jan 3, 2019
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
15 changes: 15 additions & 0 deletions lib/src/collection/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/collection/dart_iterable.dart';
import 'package:dart_kollection/src/collection/iterator.dart';
import 'package:dart_kollection/src/extension/iterable_extension_mixin.dart';
import 'package:dart_kollection/src/extension/iterable_mutable_extension_mixin.dart';

class EmptyIterable<T> extends KIterable<T> with KIterableExtensionsMixin<T> {
@override
Expand All @@ -22,3 +23,17 @@ class DartIterable<T> extends KIterable<T> with KIterableExtensionsMixin<T> {
@override
KIterator<T> iterator() => InterOpKIterator(_iterable.iterator);
}

class DartMutableIterable<T> extends KMutableIterable<T>
with KIterableExtensionsMixin<T>, KMutableIterableExtensionsMixin<T> {
DartMutableIterable(this._iterable);

// only allow lists for now, because the mutable iterator only supports lists
List<T> _iterable;

@override
Iterable<T> get iter => _iterable;

@override
KMutableIterator<T> iterator() => InterOpKListIterator(_iterable, 0);
}
9 changes: 6 additions & 3 deletions lib/src/collection/map_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ class _MutableEntry<K, V> implements KMutableMapEntry<K, V> {

@override
V setValue(V newValue) {
final old = _value;
_value = value;
return old;
// 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");
}

@override
Expand Down
66 changes: 66 additions & 0 deletions test/collection/iterable_mutable_extensions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/collection/iterable.dart';
import 'package:test/test.dart';

import '../test/assert_dart.dart';

void main() {
group("mutableIterable", () {
testIterable(<T>() => DartMutableIterable<T>([]),
<T>(Iterable<T> iterable) => DartMutableIterable(iterable));
});
group("mutableList", () {
testIterable(<T>() => mutableListOf<T>(),
<T>(Iterable<T> iterable) => mutableListOf(iterable));
});

group("hashset", () {
testIterable(<T>() => hashSetOf<T>(),
<T>(Iterable<T> iterable) => hashSetOf(iterable),
ordered: false);
});
group("linkedSet", () {
testIterable(<T>() => linkedSetOf<T>(),
<T>(Iterable<T> iterable) => linkedSetOf(iterable));
});
}

void testIterable(KMutableIterable<T> Function<T>() emptyIterable,
KMutableIterable<T> Function<T>(Iterable<T> iterable) mutableIterableOf,
{bool ordered = true}) {
group("removal functions throw", () {});

group("removeAllWhere", () {
test("removeAllWhere", () {
final iterable = mutableIterableOf(["paul", "john", "max", "lisa"]);
final e = catchException(
() => iterable.removeAllWhere((it) => it.endsWith("x")));
// TODO remove error assertion once implemented
expect(e, const TypeMatcher<UnimplementedError>());
//expect(iterable.toList(), listOf(["paul", "john", "lisa"]));
});

test("removeAllWhere requires predicate to be non null", () {
final e = catchException<ArgumentError>(
() => emptyIterable().removeAllWhere(null));
expect(e.message, allOf(contains("null"), contains("predicate")));
});
});

group("retainAllWhere", () {
test("retainAllWhere", () {
final iterable = mutableIterableOf(["paul", "john", "max", "lisa"]);
final e = catchException(
() => iterable.retainAllWhere((it) => it.endsWith("x")));
// TODO remove error assertion once implemented
expect(e, const TypeMatcher<UnimplementedError>());
//expect(iterable.toList(), listOf(["max"]));
});

test("retainAllWhere requires predicate to be non null", () {
final e = catchException<ArgumentError>(
() => emptyIterable().retainAllWhere(null));
expect(e.message, allOf(contains("null"), contains("predicate")));
});
});
}
39 changes: 39 additions & 0 deletions test/collection/map_mutable_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:test/test.dart';

import '../test/assert_dart.dart';

void main() {
group("_MutableEntry", () {
test("entries can be converted to pairs", () {
final pokemon = mutableMapOf({
1: "Bulbasaur",
2: "Ivysaur",
});
expect(pokemon.entries.map((it) => it.toPair()),
listOf([KPair(1, "Bulbasaur"), KPair(2, "Ivysaur")]));
});

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

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

// TODO exchange error check with assertion once https://github.com/passsy/dart_kollection/issues/55 has been fixed
expect(e, TypeMatcher<UnimplementedError>());
// expect(
// pokemon,
// mapOf({
// 1: "BULBASAUR",
// 2: "IVYSAUR",
// }));
});
});
}
5 changes: 5 additions & 0 deletions test/dart_kollection_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'collection/collection_mutable_test.dart' as collection_mutable_test;
import 'collection/collection_test.dart' as collection_test;
import 'collection/iterable_extensions_test.dart' as iterable_extensions_test;
import 'collection/iterable_mutable_extensions_test.dart'
as iterable_mutable_extensions_test;
import 'collection/iterator_test.dart' as iterator_test;
import 'collection/list_empty_test.dart' as list_empty_test;
import 'collection/list_extensions_test.dart' as list_extensions_test;
Expand All @@ -12,6 +14,7 @@ import 'collection/map_empty_test.dart' as map_empty_test;
import 'collection/map_extensions_test.dart' as map_extensions_test;
import 'collection/map_mutable_extensions_test.dart'
as map_mutable_extensions_test;
import 'collection/map_mutable_test.dart' as map_mutable_test;
import 'collection/map_test.dart' as map_test;
import 'collection/set_test.dart' as set_test;
import 'collections_test.dart' as collections_test;
Expand All @@ -25,6 +28,7 @@ void main() {
collection_mutable_test.main();
collection_test.main();
iterable_extensions_test.main();
iterable_mutable_extensions_test.main();
iterator_test.main();
list_empty_test.main();
list_extensions_test.main();
Expand All @@ -34,6 +38,7 @@ void main() {
map_empty_test.main();
map_extensions_test.main();
map_mutable_extensions_test.main();
map_mutable_test.main();
map_test.main();
set_test.main();
collections_test.main();
Expand Down