diff --git a/MoreLinq.Test/BatchTest.cs b/MoreLinq.Test/BatchTest.cs index 09cdadbae..f297b2969 100644 --- a/MoreLinq.Test/BatchTest.cs +++ b/MoreLinq.Test/BatchTest.cs @@ -117,5 +117,15 @@ public void BatchReadOnlyCollectionSmallerThanSize(int oversize) reader.Read().AssertSequenceEqual(1, 2, 3, 4, 5); reader.ReadEnd(); } + + [TestCase(SourceKind.Sequence)] + [TestCase(SourceKind.BreakingList)] + [TestCase(SourceKind.BreakingReadOnlyList)] + [TestCase(SourceKind.BreakingCollection)] + public void BatchEmptySource(SourceKind kind) + { + var batches = Enumerable.Empty().ToSourceKind(kind).Batch(100); + Assert.That(batches, Is.Empty); + } } } diff --git a/MoreLinq/Batch.cs b/MoreLinq/Batch.cs index c6db41d82..99afb6102 100644 --- a/MoreLinq/Batch.cs +++ b/MoreLinq/Batch.cs @@ -19,6 +19,7 @@ namespace MoreLinq { using System; using System.Collections.Generic; + using System.Linq; static partial class MoreEnumerable { @@ -86,6 +87,10 @@ public static IEnumerable Batch(this IEnumerable collection when collection.Count == 0: + { + return Enumerable.Empty(); + } case ICollection collection when collection.Count <= size: { return _(); IEnumerable _() @@ -95,6 +100,10 @@ public static IEnumerable Batch(this IEnumerable list when list.Count == 0: + { + return Enumerable.Empty(); + } case IReadOnlyList list when list.Count <= size: { return _(); IEnumerable _()