From 1df6e1a1fa500a4df7f79ec6a5bca75345ee053f Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Mon, 24 May 2021 04:40:34 +0200 Subject: [PATCH] To use dart property, T doesn't have to be Comparable --- lib/src/collection/kt_iterable.dart | 12 ++++---- test/collection/iterable_extensions_test.dart | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/src/collection/kt_iterable.dart b/lib/src/collection/kt_iterable.dart index 2cc5eda..825f029 100644 --- a/lib/src/collection/kt_iterable.dart +++ b/lib/src/collection/kt_iterable.dart @@ -16,12 +16,6 @@ abstract class KtIterable { extension KtComparableIterableExtension> on KtIterable { - /// Returns a dart:core [Iterable] - /// - /// This method can be used to interop between the dart:collection and the - /// kt.dart world. - Iterable get dart => iter; - /// Returns the largest element or `null` if there are no elements. T? max() { final i = iterator(); @@ -125,6 +119,12 @@ extension KtDoubleIterableExtension on KtIterable { } extension KtIterableExtensions on KtIterable { + /// Returns a dart:core [Iterable] + /// + /// This method can be used to interop between the dart:collection and the + /// kt.dart world. + Iterable get dart => iter; + /// Returns `true` if all elements match the given [predicate]. bool all(bool Function(T element) predicate) { if (this is KtCollection && (this as KtCollection).isEmpty()) return true; diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 211c606..1fe4ed6 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -314,6 +314,15 @@ void testIterable(KtIterable Function() emptyIterable, final Iterable iterable = emptyIterable().dart; expect(iterable.length, 0); }); + + test('dart work on all objects', () { + // there was once a bug where it only worked for Comparable + iterableOf([]).dart; + iterableOf([]).dart; + iterableOf([]).dart; + iterableOf([]).dart; + iterableOf([]).dart; + }); }); group("drop", () { @@ -1025,6 +1034,27 @@ void testIterable(KtIterable Function() emptyIterable, }); }); + group("iter", () { + test("iterate using a for loop", () { + final items = KtMutableList.empty(); + for (final String s in iterableOf(["a", "b", "c"]).iter) { + items.add(s); + } + expect(items.size, 3); + if (ordered) { + expect(items, listOf("a", "b", "c")); + } + }); + + test('iter work on all objects', () { + iterableOf([]).iter; + iterableOf([]).iter; + iterableOf([]).iter; + iterableOf([]).iter; + iterableOf([]).iter; + }); + }); + group("joinToString", () { if (ordered) { test("joinToString", () {