From 706d8ced2e7d5c8c558ddbce981b54ab643be831 Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Wed, 15 Jan 2025 03:18:04 +0530 Subject: [PATCH 1/8] [CoreLib] Iterable.fromIteratorFactory Introduced a new factory constructor fromIteratorFactory to create an iterable from iterator --- sdk/lib/core/iterable.dart | 32 ++++ .../iterable_from_iterator_factory_test.dart | 143 ++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 tests/corelib/iterable_from_iterator_factory_test.dart diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart index aee4c73279bc..843d11b67bde 100644 --- a/sdk/lib/core/iterable.dart +++ b/sdk/lib/core/iterable.dart @@ -127,6 +127,23 @@ abstract mixin class Iterable { return _GeneratorIterable(count, generator); } + /// Creates an [Iterable] from an [Iterator] factory function. + /// + /// The returned iterable creates a new iterator each time [iterator] is accessed, + /// by calling the provided [iteratorFactory] function. The [iteratorFactory] + /// function must return a new instance of [Iterator] on each call. + /// + /// Example: + /// ```dart + /// final numbers = Iterable.fromIteratorFactory(() => [1, 2, 3].iterator); + /// print(numbers.toList()); // [1, 2, 3] + /// ``` + /// + /// This is useful when you need to create an iterable from a custom iterator, + /// or when you want to ensure a fresh iteration state on each use. + factory Iterable.fromIteratorFactory(Iterator Function() iteratorFactory) = + _FromIteratorIterable; + /// Creates an empty iterable. /// /// The empty iterable has no elements, and iterating it always stops @@ -915,6 +932,21 @@ class _GeneratorIterable extends ListIterable { static int _id(int n) => n; } +class _FromIteratorIterable extends Iterable { + final Iterator Function() _iteratorFactory; + + /// Creates an iterable that gets its elements from iterators created by + /// the provided factory function. + _FromIteratorIterable(Iterator Function() iteratorFactory) + : _iteratorFactory = iteratorFactory; + + @override + Iterator get iterator => _iteratorFactory(); + + @override + String toString() => Iterable.iterableToShortString(this, '(', ')'); +} + /// Convert elements of [iterable] to strings and store them in [parts]. void _iterablePartsToStrings(Iterable iterable, List parts) { // This is the complicated part of [iterableToShortString]. diff --git a/tests/corelib/iterable_from_iterator_factory_test.dart b/tests/corelib/iterable_from_iterator_factory_test.dart new file mode 100644 index 000000000000..b031618e28db --- /dev/null +++ b/tests/corelib/iterable_from_iterator_factory_test.dart @@ -0,0 +1,143 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; +import 'dart:collection'; + +void main() { + group('Iterable.fromIteratorFactory tests', () { + testEmptyIterator(); + testSingleElementIterator(); + testMultipleElementIterator(); + testMultipleIterations(); + testLazyInitialization(); + testAfterMoveNext(); + testErrorHandling(); + }); +} + +void group(String description, void Function() body) { + print('Testing: $description'); + body(); +} + +void testEmptyIterator() { + test('empty iterator behaves correctly', () { + var emptyIterable = Iterable.fromIteratorFactory(() => [].iterator); + Expect.isTrue(emptyIterable.isEmpty); + Expect.equals(0, emptyIterable.length); + Expect.isTrue(emptyIterable.every((element) => false)); + Expect.isFalse(emptyIterable.any((element) => true)); + Expect.equals('[]', emptyIterable.toList().toString()); + }); +} + +void testSingleElementIterator() { + test('single element iterator behaves correctly', () { + var singleIterable = Iterable.fromIteratorFactory(() => [1].iterator); + Expect.equals(1, singleIterable.length); + Expect.equals(1, singleIterable.first); + Expect.equals(1, singleIterable.last); + Expect.equals(1, singleIterable.single); + Expect.isFalse(singleIterable.isEmpty); + Expect.isTrue(singleIterable.contains(1)); + Expect.isFalse(singleIterable.contains(2)); + }); +} + +void testMultipleElementIterator() { + test('multiple element iterator behaves correctly', () { + var multiIterable = Iterable.fromIteratorFactory(() => [1, 2, 3].iterator); + Expect.equals(3, multiIterable.length); + Expect.equals(1, multiIterable.first); + Expect.equals(3, multiIterable.last); + Expect.equals('[1, 2, 3]', multiIterable.toList().toString()); + Expect.equals(6, multiIterable.reduce((a, b) => a + b)); + Expect.isTrue(multiIterable.contains(2)); + Expect.isFalse(multiIterable.contains(4)); + + // Test transformation methods + Expect.equals( + '[2, 4, 6]', multiIterable.map((e) => e * 2).toList().toString()); + Expect.equals( + '[1, 2]', multiIterable.where((e) => e < 3).toList().toString()); + }); +} + +void testMultipleIterations() { + test('multiple iterations create fresh iterators', () { + var count = 0; + var countingIterable = Iterable.fromIteratorFactory(() { + count++; + return [1, 2].iterator; + }); + + // Test multiple iterations + for (var i = 1; i <= 3; i++) { + for (var x in countingIterable) {} + Expect.equals(i, count); + } + + // Test concurrent iterations + var iterators = List.generate(2, (_) => countingIterable.iterator); + Expect.equals(5, count); + }); +} + +void testLazyInitialization() { + test('iterator factory is called lazily', () { + var factoryCalled = false; + var lazyIterable = Iterable.fromIteratorFactory(() { + factoryCalled = true; + return [1].iterator; + }); + + Expect.isFalse(factoryCalled); // Not called until iteration + var iterator = lazyIterable.iterator; + Expect.isTrue(factoryCalled); + + // Test that accessing length doesn't trigger another factory call + factoryCalled = false; + lazyIterable.length; + Expect.isTrue(factoryCalled); + }); +} + +void testAfterMoveNext() { + test('iterator factory is called after moveNext', () { + var items = [1, 2, 3]; + var iterator = items.iterator; + iterator.moveNext(); + var lazyIterable = Iterable.fromIteratorFactory(() => iterator); + + // Test that the first element is already consumed + var elements = lazyIterable.toList(); + Expect.isNotEmpty(elements); + Expect.equals(2, elements.first); + }); +} + +void testErrorHandling() { + test('handles null and error cases', () { + // Test with null value + var nullIterable = Iterable.fromIteratorFactory(() => [null].iterator); + Expect.equals(1, nullIterable.length); + Expect.isNull(nullIterable.first); + + // Test with throwing iterator + bool throwingCalled = false; + var throwingIterable = Iterable.fromIteratorFactory(() { + throwingCalled = true; + throw StateError('Test error'); + }); + + Expect.throws(() => throwingIterable.iterator); + Expect.isTrue(throwingCalled); + }); +} + +void test(String description, void Function() body) { + print(' $description'); + body(); +} From fa07cf80a24a8bc2d59024d3521f157b33260ba4 Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Wed, 15 Jan 2025 14:08:09 +0530 Subject: [PATCH 2/8] chore: rename to withIterable --- sdk/lib/core/iterable.dart | 4 ++-- .../iterable_from_iterator_factory_test.dart | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart index 843d11b67bde..236ed1435584 100644 --- a/sdk/lib/core/iterable.dart +++ b/sdk/lib/core/iterable.dart @@ -135,13 +135,13 @@ abstract mixin class Iterable { /// /// Example: /// ```dart - /// final numbers = Iterable.fromIteratorFactory(() => [1, 2, 3].iterator); + /// final numbers = Iterable.withIterator(() => [1, 2, 3].iterator); /// print(numbers.toList()); // [1, 2, 3] /// ``` /// /// This is useful when you need to create an iterable from a custom iterator, /// or when you want to ensure a fresh iteration state on each use. - factory Iterable.fromIteratorFactory(Iterator Function() iteratorFactory) = + factory Iterable.withIterator(Iterator Function() iteratorFactory) = _FromIteratorIterable; /// Creates an empty iterable. diff --git a/tests/corelib/iterable_from_iterator_factory_test.dart b/tests/corelib/iterable_from_iterator_factory_test.dart index b031618e28db..0285e7c56440 100644 --- a/tests/corelib/iterable_from_iterator_factory_test.dart +++ b/tests/corelib/iterable_from_iterator_factory_test.dart @@ -6,7 +6,7 @@ import "package:expect/expect.dart"; import 'dart:collection'; void main() { - group('Iterable.fromIteratorFactory tests', () { + group('Iterable.withIterator tests', () { testEmptyIterator(); testSingleElementIterator(); testMultipleElementIterator(); @@ -24,7 +24,7 @@ void group(String description, void Function() body) { void testEmptyIterator() { test('empty iterator behaves correctly', () { - var emptyIterable = Iterable.fromIteratorFactory(() => [].iterator); + var emptyIterable = Iterable.withIterator(() => [].iterator); Expect.isTrue(emptyIterable.isEmpty); Expect.equals(0, emptyIterable.length); Expect.isTrue(emptyIterable.every((element) => false)); @@ -35,7 +35,7 @@ void testEmptyIterator() { void testSingleElementIterator() { test('single element iterator behaves correctly', () { - var singleIterable = Iterable.fromIteratorFactory(() => [1].iterator); + var singleIterable = Iterable.withIterator(() => [1].iterator); Expect.equals(1, singleIterable.length); Expect.equals(1, singleIterable.first); Expect.equals(1, singleIterable.last); @@ -48,7 +48,7 @@ void testSingleElementIterator() { void testMultipleElementIterator() { test('multiple element iterator behaves correctly', () { - var multiIterable = Iterable.fromIteratorFactory(() => [1, 2, 3].iterator); + var multiIterable = Iterable.withIterator(() => [1, 2, 3].iterator); Expect.equals(3, multiIterable.length); Expect.equals(1, multiIterable.first); Expect.equals(3, multiIterable.last); @@ -68,7 +68,7 @@ void testMultipleElementIterator() { void testMultipleIterations() { test('multiple iterations create fresh iterators', () { var count = 0; - var countingIterable = Iterable.fromIteratorFactory(() { + var countingIterable = Iterable.withIterator(() { count++; return [1, 2].iterator; }); @@ -88,7 +88,7 @@ void testMultipleIterations() { void testLazyInitialization() { test('iterator factory is called lazily', () { var factoryCalled = false; - var lazyIterable = Iterable.fromIteratorFactory(() { + var lazyIterable = Iterable.withIterator(() { factoryCalled = true; return [1].iterator; }); @@ -109,7 +109,7 @@ void testAfterMoveNext() { var items = [1, 2, 3]; var iterator = items.iterator; iterator.moveNext(); - var lazyIterable = Iterable.fromIteratorFactory(() => iterator); + var lazyIterable = Iterable.withIterator(() => iterator); // Test that the first element is already consumed var elements = lazyIterable.toList(); @@ -121,13 +121,13 @@ void testAfterMoveNext() { void testErrorHandling() { test('handles null and error cases', () { // Test with null value - var nullIterable = Iterable.fromIteratorFactory(() => [null].iterator); + var nullIterable = Iterable.withIterator(() => [null].iterator); Expect.equals(1, nullIterable.length); Expect.isNull(nullIterable.first); // Test with throwing iterator bool throwingCalled = false; - var throwingIterable = Iterable.fromIteratorFactory(() { + var throwingIterable = Iterable.withIterator(() { throwingCalled = true; throw StateError('Test error'); }); From c3624e9b65e234c1e4053e6d7d909f915b5d32fa Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Thu, 16 Jan 2025 12:54:58 +0530 Subject: [PATCH 3/8] fix: Iterable.withIterator docs and consolidated tests --- sdk/lib/core/iterable.dart | 19 +- .../iterable_from_iterator_factory_test.dart | 194 ++++++------------ 2 files changed, 67 insertions(+), 146 deletions(-) diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart index 236ed1435584..0467eee0c745 100644 --- a/sdk/lib/core/iterable.dart +++ b/sdk/lib/core/iterable.dart @@ -127,20 +127,21 @@ abstract mixin class Iterable { return _GeneratorIterable(count, generator); } - /// Creates an [Iterable] from an [Iterator] factory function. + /// Creates an [Iterable] from the [Iterator] factory. /// - /// The returned iterable creates a new iterator each time [iterator] is accessed, + /// The returned iterable creates a new iterator each time [iterator] is read, /// by calling the provided [iteratorFactory] function. The [iteratorFactory] - /// function must return a new instance of [Iterator] on each call. - /// + /// function must return a new instance of `Iterator` on each call. + /// + /// This factory is useful when you need to create an iterable from a custom + /// iterator, or when you want to ensure a fresh iteration state on each use. + /// /// Example: /// ```dart /// final numbers = Iterable.withIterator(() => [1, 2, 3].iterator); /// print(numbers.toList()); // [1, 2, 3] /// ``` - /// - /// This is useful when you need to create an iterable from a custom iterator, - /// or when you want to ensure a fresh iteration state on each use. + @Since("3.8") factory Iterable.withIterator(Iterator Function() iteratorFactory) = _FromIteratorIterable; @@ -940,11 +941,7 @@ class _FromIteratorIterable extends Iterable { _FromIteratorIterable(Iterator Function() iteratorFactory) : _iteratorFactory = iteratorFactory; - @override Iterator get iterator => _iteratorFactory(); - - @override - String toString() => Iterable.iterableToShortString(this, '(', ')'); } /// Convert elements of [iterable] to strings and store them in [parts]. diff --git a/tests/corelib/iterable_from_iterator_factory_test.dart b/tests/corelib/iterable_from_iterator_factory_test.dart index 0285e7c56440..9323c3546f56 100644 --- a/tests/corelib/iterable_from_iterator_factory_test.dart +++ b/tests/corelib/iterable_from_iterator_factory_test.dart @@ -1,143 +1,67 @@ -// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - import "package:expect/expect.dart"; -import 'dart:collection'; void main() { - group('Iterable.withIterator tests', () { - testEmptyIterator(); - testSingleElementIterator(); - testMultipleElementIterator(); - testMultipleIterations(); - testLazyInitialization(); - testAfterMoveNext(); - testErrorHandling(); - }); -} - -void group(String description, void Function() body) { - print('Testing: $description'); - body(); -} - -void testEmptyIterator() { - test('empty iterator behaves correctly', () { - var emptyIterable = Iterable.withIterator(() => [].iterator); - Expect.isTrue(emptyIterable.isEmpty); - Expect.equals(0, emptyIterable.length); - Expect.isTrue(emptyIterable.every((element) => false)); - Expect.isFalse(emptyIterable.any((element) => true)); - Expect.equals('[]', emptyIterable.toList().toString()); - }); -} - -void testSingleElementIterator() { - test('single element iterator behaves correctly', () { - var singleIterable = Iterable.withIterator(() => [1].iterator); - Expect.equals(1, singleIterable.length); - Expect.equals(1, singleIterable.first); - Expect.equals(1, singleIterable.last); - Expect.equals(1, singleIterable.single); - Expect.isFalse(singleIterable.isEmpty); - Expect.isTrue(singleIterable.contains(1)); - Expect.isFalse(singleIterable.contains(2)); - }); -} - -void testMultipleElementIterator() { - test('multiple element iterator behaves correctly', () { - var multiIterable = Iterable.withIterator(() => [1, 2, 3].iterator); - Expect.equals(3, multiIterable.length); - Expect.equals(1, multiIterable.first); - Expect.equals(3, multiIterable.last); - Expect.equals('[1, 2, 3]', multiIterable.toList().toString()); - Expect.equals(6, multiIterable.reduce((a, b) => a + b)); - Expect.isTrue(multiIterable.contains(2)); - Expect.isFalse(multiIterable.contains(4)); - - // Test transformation methods - Expect.equals( - '[2, 4, 6]', multiIterable.map((e) => e * 2).toList().toString()); - Expect.equals( - '[1, 2]', multiIterable.where((e) => e < 3).toList().toString()); - }); + testIteratorFactory(); + testIteratorEquality(); } -void testMultipleIterations() { - test('multiple iterations create fresh iterators', () { - var count = 0; - var countingIterable = Iterable.withIterator(() { - count++; - return [1, 2].iterator; - }); - - // Test multiple iterations - for (var i = 1; i <= 3; i++) { - for (var x in countingIterable) {} - Expect.equals(i, count); - } - - // Test concurrent iterations - var iterators = List.generate(2, (_) => countingIterable.iterator); - Expect.equals(5, count); - }); -} - -void testLazyInitialization() { - test('iterator factory is called lazily', () { - var factoryCalled = false; - var lazyIterable = Iterable.withIterator(() { - factoryCalled = true; - return [1].iterator; - }); - - Expect.isFalse(factoryCalled); // Not called until iteration - var iterator = lazyIterable.iterator; - Expect.isTrue(factoryCalled); - - // Test that accessing length doesn't trigger another factory call - factoryCalled = false; - lazyIterable.length; - Expect.isTrue(factoryCalled); - }); -} - -void testAfterMoveNext() { - test('iterator factory is called after moveNext', () { - var items = [1, 2, 3]; - var iterator = items.iterator; - iterator.moveNext(); - var lazyIterable = Iterable.withIterator(() => iterator); - - // Test that the first element is already consumed - var elements = lazyIterable.toList(); - Expect.isNotEmpty(elements); - Expect.equals(2, elements.first); - }); -} - -void testErrorHandling() { - test('handles null and error cases', () { - // Test with null value - var nullIterable = Iterable.withIterator(() => [null].iterator); - Expect.equals(1, nullIterable.length); - Expect.isNull(nullIterable.first); - - // Test with throwing iterator - bool throwingCalled = false; - var throwingIterable = Iterable.withIterator(() { - throwingCalled = true; - throw StateError('Test error'); - }); - - Expect.throws(() => throwingIterable.iterator); - Expect.isTrue(throwingCalled); - }); +void testIteratorFactory() { + Iterator? returnedIterator; + Object? thrownError; + int calls = 0; + Iterator f() { + calls++; + if (thrownError case var error?) throw error; + return returnedIterator as Iterator; + } + + var ints = Iterable.withIterator(f); + var intIterator = [].iterator; + returnedIterator = intIterator; + Expect.identical(intIterator, ints.iterator, "First iterator should match"); + Expect.equals(1, calls, "First call count"); + Expect.identical(intIterator, ints.iterator, "Second iterator should match"); + Expect.equals(2, calls, "Second call count"); + + var intIterator2 = [].iterator; + Expect.notIdentical(intIterator, intIterator2, "Different iterators should not be identical"); + returnedIterator = intIterator2; + Expect.identical(intIterator2, ints.iterator, "Third iterator should match"); + Expect.equals(3, calls, "Third call count"); + Expect.identical(intIterator2, ints.iterator, "Fourth iterator should match"); + Expect.equals(4, calls, "Fourth call count"); + + var error = StateError("quo"); + thrownError = error; + Expect.identical(error, Expect.throws(() => ints.iterator), "Should throw first error"); + Expect.equals(5, calls, "Fifth call count"); + Expect.identical(error, Expect.throws(() => ints.iterator), "Should throw second error"); + Expect.equals(6, calls, "Sixth call count"); + + thrownError = null; + Expect.identical(intIterator2, ints.iterator, "Fifth iterator should match"); + Expect.equals(7, calls, "Seventh call count"); + + var objectqs = Iterable.withIterator(f); + Expect.identical(intIterator2, objectqs.iterator, "Object iterator should match"); + Expect.equals(8, calls, "Eighth call count"); + + var nulls = Iterable.withIterator(f); + var nullIterator = [].iterator; + returnedIterator = nullIterator; + Expect.identical(nullIterator, nulls.iterator, "Null iterator should match"); + Expect.equals(9, calls, "Ninth call count"); + + var nevers = Iterable.withIterator(f); + thrownError = error; + Expect.identical(error, Expect.throws(() => nevers.iterator), "Should throw third error"); + Expect.equals(10, calls, "Tenth call count"); + Expect.identical(error, Expect.throws(() => nevers.iterator), "Should throw fourth error"); + Expect.equals(11, calls, "Eleventh call count"); } -void test(String description, void Function() body) { - print(' $description'); - body(); +void testIteratorEquality() { + var iterable1 = Iterable.withIterator(() => [1, 2, 3].iterator); + var iterable2 = Iterable.withIterator(() => [1, 2, 3].iterator); + Expect.listEquals(iterable1.toList(), iterable2.toList(), "Iterator contents should be equal"); } From 4cd11d4589ff88dfbb067498ad9aa7a8d56b1b53 Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Thu, 16 Jan 2025 14:06:26 +0530 Subject: [PATCH 4/8] Updated CHANGELOG.md: Added Iterable.withIterator --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01e0d697c74c..e2b05fd2cdf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 3.8.0 + +### Libraries + +#### `dart:core` + +- Added `Iterable.withIterator` constructor. + ## 3.7.0 ### Language From 1db030778177368fcd168ec6730f0d4747985704 Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Fri, 17 Jan 2025 02:18:19 +0530 Subject: [PATCH 5/8] fix: test file rename to iterable_with_iterator_test --- ...terator_factory_test.dart => iterable_with_iterator_test.dart} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/corelib/{iterable_from_iterator_factory_test.dart => iterable_with_iterator_test.dart} (100%) diff --git a/tests/corelib/iterable_from_iterator_factory_test.dart b/tests/corelib/iterable_with_iterator_test.dart similarity index 100% rename from tests/corelib/iterable_from_iterator_factory_test.dart rename to tests/corelib/iterable_with_iterator_test.dart From 458904f45aa9a3d5c3c944a2c849d690096013b9 Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Fri, 17 Jan 2025 13:13:06 +0530 Subject: [PATCH 6/8] refactor: renaming private class and added doc comments --- sdk/lib/core/iterable.dart | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart index 0467eee0c745..edccd388a589 100644 --- a/sdk/lib/core/iterable.dart +++ b/sdk/lib/core/iterable.dart @@ -143,7 +143,7 @@ abstract mixin class Iterable { /// ``` @Since("3.8") factory Iterable.withIterator(Iterator Function() iteratorFactory) = - _FromIteratorIterable; + _WithIteratorIterable; /// Creates an empty iterable. /// @@ -933,13 +933,16 @@ class _GeneratorIterable extends ListIterable { static int _id(int n) => n; } -class _FromIteratorIterable extends Iterable { +/// Used internally by [Iterable.withIterator] to provide abstraction. +/// +/// This class implements [Iterable] by delegating the iterator creation +/// to the provided function. +class _WithIteratorIterable extends Iterable { final Iterator Function() _iteratorFactory; /// Creates an iterable that gets its elements from iterators created by /// the provided factory function. - _FromIteratorIterable(Iterator Function() iteratorFactory) - : _iteratorFactory = iteratorFactory; + _WithIteratorIterable(this._iteratorFactory); Iterator get iterator => _iteratorFactory(); } From dccd61730055c8a05411eb966aa7363b8758826d Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Fri, 17 Jan 2025 20:37:45 +0530 Subject: [PATCH 7/8] fix: docs white space and shorten description --- sdk/lib/core/iterable.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart index edccd388a589..8c354d0826fb 100644 --- a/sdk/lib/core/iterable.dart +++ b/sdk/lib/core/iterable.dart @@ -132,10 +132,10 @@ abstract mixin class Iterable { /// The returned iterable creates a new iterator each time [iterator] is read, /// by calling the provided [iteratorFactory] function. The [iteratorFactory] /// function must return a new instance of `Iterator` on each call. - /// + /// /// This factory is useful when you need to create an iterable from a custom /// iterator, or when you want to ensure a fresh iteration state on each use. - /// + /// /// Example: /// ```dart /// final numbers = Iterable.withIterator(() => [1, 2, 3].iterator); @@ -933,7 +933,7 @@ class _GeneratorIterable extends ListIterable { static int _id(int n) => n; } -/// Used internally by [Iterable.withIterator] to provide abstraction. +/// Used internally by [Iterable.withIterator]. /// /// This class implements [Iterable] by delegating the iterator creation /// to the provided function. From a6596bda27ee9dd4ce86da04cc6b3ff075dd783b Mon Sep 17 00:00:00 2001 From: Dhikshith Reddy Date: Fri, 17 Jan 2025 21:15:17 +0530 Subject: [PATCH 8/8] fmt: dart format iterable_with_iterator_test.dart --- .../corelib/iterable_with_iterator_test.dart | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/tests/corelib/iterable_with_iterator_test.dart b/tests/corelib/iterable_with_iterator_test.dart index 9323c3546f56..b1833f3e23de 100644 --- a/tests/corelib/iterable_with_iterator_test.dart +++ b/tests/corelib/iterable_with_iterator_test.dart @@ -24,7 +24,11 @@ void testIteratorFactory() { Expect.equals(2, calls, "Second call count"); var intIterator2 = [].iterator; - Expect.notIdentical(intIterator, intIterator2, "Different iterators should not be identical"); + Expect.notIdentical( + intIterator, + intIterator2, + "Different iterators should not be identical", + ); returnedIterator = intIterator2; Expect.identical(intIterator2, ints.iterator, "Third iterator should match"); Expect.equals(3, calls, "Third call count"); @@ -33,9 +37,17 @@ void testIteratorFactory() { var error = StateError("quo"); thrownError = error; - Expect.identical(error, Expect.throws(() => ints.iterator), "Should throw first error"); + Expect.identical( + error, + Expect.throws(() => ints.iterator), + "Should throw first error", + ); Expect.equals(5, calls, "Fifth call count"); - Expect.identical(error, Expect.throws(() => ints.iterator), "Should throw second error"); + Expect.identical( + error, + Expect.throws(() => ints.iterator), + "Should throw second error", + ); Expect.equals(6, calls, "Sixth call count"); thrownError = null; @@ -43,7 +55,11 @@ void testIteratorFactory() { Expect.equals(7, calls, "Seventh call count"); var objectqs = Iterable.withIterator(f); - Expect.identical(intIterator2, objectqs.iterator, "Object iterator should match"); + Expect.identical( + intIterator2, + objectqs.iterator, + "Object iterator should match", + ); Expect.equals(8, calls, "Eighth call count"); var nulls = Iterable.withIterator(f); @@ -54,14 +70,26 @@ void testIteratorFactory() { var nevers = Iterable.withIterator(f); thrownError = error; - Expect.identical(error, Expect.throws(() => nevers.iterator), "Should throw third error"); + Expect.identical( + error, + Expect.throws(() => nevers.iterator), + "Should throw third error", + ); Expect.equals(10, calls, "Tenth call count"); - Expect.identical(error, Expect.throws(() => nevers.iterator), "Should throw fourth error"); + Expect.identical( + error, + Expect.throws(() => nevers.iterator), + "Should throw fourth error", + ); Expect.equals(11, calls, "Eleventh call count"); } void testIteratorEquality() { var iterable1 = Iterable.withIterator(() => [1, 2, 3].iterator); var iterable2 = Iterable.withIterator(() => [1, 2, 3].iterator); - Expect.listEquals(iterable1.toList(), iterable2.toList(), "Iterator contents should be equal"); + Expect.listEquals( + iterable1.toList(), + iterable2.toList(), + "Iterator contents should be equal", + ); }