From 56d04668c34820135dc5b3cb9a9998d11b9a13f0 Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Fri, 14 Dec 2018 21:15:30 +0100 Subject: [PATCH] New KMutableList indexed set operator []= --- lib/src/collection/list_mutable.dart | 3 +++ lib/src/k_list_mutable.dart | 5 +++++ test/collection/iterable_extensions_test.dart | 2 +- test/collection/list_mutable_test.dart | 20 +++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/src/collection/list_mutable.dart b/lib/src/collection/list_mutable.dart index 599e7828..c46e0bee 100644 --- a/lib/src/collection/list_mutable.dart +++ b/lib/src/collection/list_mutable.dart @@ -112,6 +112,9 @@ class DartMutableList return old; } + @override + void operator []=(int index, T element) => set(index, element); + @override bool removeAll(KIterable elements) { var changed = false; diff --git a/lib/src/k_list_mutable.dart b/lib/src/k_list_mutable.dart index 796e1e94..a5b5a828 100644 --- a/lib/src/k_list_mutable.dart +++ b/lib/src/k_list_mutable.dart @@ -54,6 +54,11 @@ abstract class KMutableList @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]. */ diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 293306f4..16d18f56 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -32,7 +32,7 @@ void main() { void testIterable(KIterable Function() emptyIterable, KIterable Function(Iterable iterable) iterableOf, - {ordered = true}) { + {bool ordered = true}) { group('any', () { test("matches single", () { final iterable = iterableOf(["abc", "bcd", "cde"]); diff --git a/test/collection/list_mutable_test.dart b/test/collection/list_mutable_test.dart index 9da6d2f6..54e83c1d 100644 --- a/test/collection/list_mutable_test.dart +++ b/test/collection/list_mutable_test.dart @@ -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);