Skip to content

Commit

Permalink
Merge pull request #23 from passsy/feature/indexed_setter_operator
Browse files Browse the repository at this point in the history
New KMutableList indexed set operator []=
  • Loading branch information
passsy authored Dec 14, 2018
2 parents c49cf2b + 56d0466 commit 9329221
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/src/collection/list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class DartMutableList<T>
return old;
}

@override
void operator []=(int index, T element) => set(index, element);

@override
bool removeAll(KIterable<T> elements) {
var changed = false;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/k_list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ abstract class KMutableList<T>
@nullable
T set(int index, T element);

/**
* Replaces the element at the specified position in this list with the specified element.
*/
void operator []=(int index, T element);

/**
* Inserts an element into the list at the specified [index].
*/
Expand Down
2 changes: 1 addition & 1 deletion test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() {

void testIterable(KIterable<T> Function<T>() emptyIterable,
KIterable<T> Function<T>(Iterable<T> iterable) iterableOf,
{ordered = true}) {
{bool ordered = true}) {
group('any', () {
test("matches single", () {
final iterable = iterableOf(["abc", "bcd", "cde"]);
Expand Down
20 changes: 20 additions & 0 deletions test/collection/list_mutable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ void main() {
expect(list0.hashCode, isNot(equals(list2.hashCode)));
});

test("set", () {
final list = mutableListOf([1, 2, 3, 4, 5]);
list.set(2, 10);
expect(list, listOf([1, 2, 10, 4, 5]));
list.set(0, 4);
expect(list, listOf([4, 2, 10, 4, 5]));
list.set(4, 1);
expect(list, listOf([4, 2, 10, 4, 1]));
});

test("set operator", () {
final list = mutableListOf([1, 2, 3, 4, 5]);
list[2] = 10;
expect(list, listOf([1, 2, 10, 4, 5]));
list[0] = 4;
expect(list, listOf([4, 2, 10, 4, 5]));
list[4] = 1;
expect(list, listOf([4, 2, 10, 4, 1]));
});

test("sublist works ", () {
final list = mutableListOf(["a", "b", "c"]);
final subList = list.subList(1, 3);
Expand Down

0 comments on commit 9329221

Please sign in to comment.