From 15318deec6e39ac79618051018781683da1a4c21 Mon Sep 17 00:00:00 2001 From: Anton Cherkashyn Date: Tue, 19 Feb 2019 17:33:21 -0800 Subject: [PATCH 1/3] Add KtMap.forEach() method. --- .../extension/map_extensions_mixin.dart | 9 +++++++ lib/src/collection/kt_map.dart | 7 ++++++ test/collection/map_extensions_test.dart | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/lib/src/collection/extension/map_extensions_mixin.dart b/lib/src/collection/extension/map_extensions_mixin.dart index 723559cf..bac0f91a 100644 --- a/lib/src/collection/extension/map_extensions_mixin.dart +++ b/lib/src/collection/extension/map_extensions_mixin.dart @@ -169,6 +169,15 @@ abstract class KtMapExtensionsMixin return mutableMapFrom(map); } + @override + void forEach( Function(K key, V value) action) { + assert(() { + if (action == null) throw ArgumentError("action can't be null"); + return true; + }()); + entries.forEach( (entry) => action(entry.key, entry.value) ); + } + @override String toString() { return entries.joinToString( diff --git a/lib/src/collection/kt_map.dart b/lib/src/collection/kt_map.dart index 280411aa..daf9668e 100644 --- a/lib/src/collection/kt_map.dart +++ b/lib/src/collection/kt_map.dart @@ -254,4 +254,11 @@ abstract class KtMapExtension { * The returned map preserves the entry iteration order of the original map. */ KtMutableMap toMutableMap(); + + /** + * Performs given [action] on each key/value pair from this map. + * + * [action] must not be null. + */ + void forEach( Function(K key, V value) action ); } diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index a804e050..6bcf3e08 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -467,6 +467,31 @@ void testMap(KtMap Function() emptyMap, expect(map.size, 2); }); }); + + group("forEach", () { + test("forEach", () { + final result = mutableListOf(); + var map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + map.forEach( (number, value) => result.add('$number-$value') ); + if (ordered) { + expect(result.size, 3); + expect(result, listOf("1-Bulbasaur", "2-Ivysaur", "3-Stegosaur")); + } else { + expect(result.size, 3); + expect(result, containsAll(["1-Bulbasaur", "2-Ivysaur", "3-Stegosaur"])); + } + }); + + test("action must be non null", () { + final e = + catchException(() => emptyMap().forEach(null)); + expect(e.message, allOf(contains("null"), contains("action"))); + }); + }); } class ThirdPartyMap From 134ced6685fa780859dc79f60ea7869ed03a0937 Mon Sep 17 00:00:00 2001 From: Anton Cherkashyn Date: Tue, 19 Feb 2019 20:17:20 -0800 Subject: [PATCH 2/3] Fix test failure --- test/collection/map_extensions_test.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index 6bcf3e08..9aab991d 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -471,7 +471,7 @@ void testMap(KtMap Function() emptyMap, group("forEach", () { test("forEach", () { final result = mutableListOf(); - var map = mapFrom({ + final map = mapFrom({ 1: "Bulbasaur", 2: "Ivysaur", 3: "Stegosaur", @@ -479,10 +479,12 @@ void testMap(KtMap Function() emptyMap, map.forEach( (number, value) => result.add('$number-$value') ); if (ordered) { expect(result.size, 3); - expect(result, listOf("1-Bulbasaur", "2-Ivysaur", "3-Stegosaur")); + expect(result[0], "1-Bulbasaur"); + expect(result[1], "2-Ivysaur"); + expect(result[2], "3-Stegosaur"); } else { expect(result.size, 3); - expect(result, containsAll(["1-Bulbasaur", "2-Ivysaur", "3-Stegosaur"])); + expect(result.toSet(), listOf("1-Bulbasaur", "2-Ivysaur", "3-Stegosaur").toSet()); } }); From 34edd0c2021b79fb011628b8989b082a25db6dba Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Wed, 20 Feb 2019 15:15:08 +0100 Subject: [PATCH 3/3] Reformat code --- lib/src/collection/extension/map_extensions_mixin.dart | 4 ++-- lib/src/collection/kt_map.dart | 4 ++-- test/collection/list_extensions_test.dart | 2 +- test/collection/map_extensions_test.dart | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/src/collection/extension/map_extensions_mixin.dart b/lib/src/collection/extension/map_extensions_mixin.dart index bac0f91a..86299d95 100644 --- a/lib/src/collection/extension/map_extensions_mixin.dart +++ b/lib/src/collection/extension/map_extensions_mixin.dart @@ -170,12 +170,12 @@ abstract class KtMapExtensionsMixin } @override - void forEach( Function(K key, V value) action) { + void forEach(Function(K key, V value) action) { assert(() { if (action == null) throw ArgumentError("action can't be null"); return true; }()); - entries.forEach( (entry) => action(entry.key, entry.value) ); + entries.forEach((entry) => action(entry.key, entry.value)); } @override diff --git a/lib/src/collection/kt_map.dart b/lib/src/collection/kt_map.dart index daf9668e..d0d3031e 100644 --- a/lib/src/collection/kt_map.dart +++ b/lib/src/collection/kt_map.dart @@ -257,8 +257,8 @@ abstract class KtMapExtension { /** * Performs given [action] on each key/value pair from this map. - * + * * [action] must not be null. */ - void forEach( Function(K key, V value) action ); + void forEach(Function(K key, V value) action); } diff --git a/test/collection/list_extensions_test.dart b/test/collection/list_extensions_test.dart index e838fd4e..87d28a9c 100644 --- a/test/collection/list_extensions_test.dart +++ b/test/collection/list_extensions_test.dart @@ -302,7 +302,7 @@ void testList( expect(list.takeLast(2).toList(), listFrom([1, null])); }); }); - + group("takeLastWhile", () { test("take no elements returns empty", () { final list = listOf(1, 2, 3, 4); diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index 9aab991d..d9d3d3e7 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -476,7 +476,7 @@ void testMap(KtMap Function() emptyMap, 2: "Ivysaur", 3: "Stegosaur", }); - map.forEach( (number, value) => result.add('$number-$value') ); + map.forEach((number, value) => result.add('$number-$value')); if (ordered) { expect(result.size, 3); expect(result[0], "1-Bulbasaur"); @@ -484,13 +484,13 @@ void testMap(KtMap Function() emptyMap, expect(result[2], "3-Stegosaur"); } else { expect(result.size, 3); - expect(result.toSet(), listOf("1-Bulbasaur", "2-Ivysaur", "3-Stegosaur").toSet()); + expect(result.toSet(), + listOf("1-Bulbasaur", "2-Ivysaur", "3-Stegosaur").toSet()); } }); test("action must be non null", () { - final e = - catchException(() => emptyMap().forEach(null)); + final e = catchException(() => emptyMap().forEach(null)); expect(e.message, allOf(contains("null"), contains("action"))); }); });