From 070513ffe99f1ea5aa38263d58da880ad9d10455 Mon Sep 17 00:00:00 2001 From: Anton Cherkashyn Date: Wed, 20 Feb 2019 09:06:46 -0800 Subject: [PATCH 1/2] Add KtMap methods none(), all() and any() --- .../extension/map_extensions_mixin.dart | 51 +++++++++++++ lib/src/collection/kt_map.dart | 18 +++++ test/collection/map_extensions_test.dart | 74 +++++++++++++++++++ 3 files changed, 143 insertions(+) diff --git a/lib/src/collection/extension/map_extensions_mixin.dart b/lib/src/collection/extension/map_extensions_mixin.dart index 86299d95..b34a6a5f 100644 --- a/lib/src/collection/extension/map_extensions_mixin.dart +++ b/lib/src/collection/extension/map_extensions_mixin.dart @@ -178,6 +178,57 @@ abstract class KtMapExtensionsMixin entries.forEach((entry) => action(entry.key, entry.value)); } + @override + bool none(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return true; + } + for (KtMapEntry entry in entries.iter) { + if (predicate(entry.key, entry.value)) { + return false; + } + } + return true; + } + + @override + bool all(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return true; + } + for (KtMapEntry entry in entries.iter) { + if (!predicate(entry.key, entry.value)) { + return false; + } + } + return true; + } + + @override + bool any(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return false; + } + for (KtMapEntry entry in entries.iter) { + if (predicate(entry.key, entry.value)) { + return true; + } + } + return false; + } + @override String toString() { return entries.joinToString( diff --git a/lib/src/collection/kt_map.dart b/lib/src/collection/kt_map.dart index d0d3031e..82555c59 100644 --- a/lib/src/collection/kt_map.dart +++ b/lib/src/collection/kt_map.dart @@ -261,4 +261,22 @@ abstract class KtMapExtension { * [action] must not be null. */ void forEach(Function(K key, V value) action); + + /** + * Returns `true` if there is no entries in the map that match the given [predicate]. + * [predicate] must not be null. + */ + bool none(Function(K key, V value) predicate); + + /** + * Returns true if all entries match the given [predicate]. + * [predicate] must not be null. + */ + bool all(Function(K key, V value) predicate); + + /** + * Returns true if there is at least one entry that matches the given [predicate]. + * [predicate] must not be null. + */ + bool any(Function(K key, V value) predicate); } diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index d9d3d3e7..3647e95d 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -494,6 +494,80 @@ void testMap(KtMap Function() emptyMap, expect(e.message, allOf(contains("null"), contains("action"))); }); }); + + group("none", () { + test("none() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), false); + expect(map.none((_, value) => value == "Random text"), true); + }); + + test("none() empty map", () { + final map = emptyMap(); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), true); + expect(map.none((key, value) => false), true); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().none(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); + + group("all", () { + test("all() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.all((key, value) => key > 0 && value.contains("aur")), true); + expect(map.all((key, value) => value == "Bulbasaur"), false); + expect(map.all((key, value) => value.isNotEmpty), true); + }); + + test("all() empty map", () { + final map = emptyMap(); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), true); + expect(map.none((key, value) => false), true); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().none(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); + + group("any", () { + test("any() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.any((key, value) => value == "Bulbasaur"), true); + expect(map.any((key, _) => key == -35), false); + }); + + test("any() empty map", () { + final map = emptyMap(); + expect(map.any((key, value) => key == 1 && value == "Bulbasaur"), false); + expect(map.any((key, value) => true), false); + expect(map.any((key, value) => false), false); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().none(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); } class ThirdPartyMap From 90e284d3b2c60ecece6d6379d41e64955b3e883d Mon Sep 17 00:00:00 2001 From: Anton Cherkashyn Date: Wed, 20 Feb 2019 09:20:49 -0800 Subject: [PATCH 2/2] Fix missing code coverage --- test/collection/map_extensions_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index 3647e95d..ee7f852b 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -539,7 +539,7 @@ void testMap(KtMap Function() emptyMap, test("predicate must be non null", () { final e = - catchException(() => emptyMap().none(null)); + catchException(() => emptyMap().all(null)); expect(e.message, allOf(contains("null"), contains("predicate"))); }); }); @@ -564,7 +564,7 @@ void testMap(KtMap Function() emptyMap, test("predicate must be non null", () { final e = - catchException(() => emptyMap().none(null)); + catchException(() => emptyMap().any(null)); expect(e.message, allOf(contains("null"), contains("predicate"))); }); });