diff --git a/Source/SuperLinq/Slice.cs b/Source/SuperLinq/Slice.cs index 5d9e49ee..7128e71c 100644 --- a/Source/SuperLinq/Slice.cs +++ b/Source/SuperLinq/Slice.cs @@ -28,7 +28,6 @@ public static partial class SuperEnumerable /// return all elements up to that point. There is no guarantee that the resulting sequence will contain the /// number of elements requested - it may have anywhere from 0 to . /// - [Obsolete("Slice has been replaced by Take(Range).")] public static IEnumerable Slice(this IEnumerable source, int startIndex, int count) { return source.Take(startIndex..(startIndex + count)); diff --git a/Tests/SuperLinq.Test/SliceTest.cs b/Tests/SuperLinq.Test/SliceTest.cs new file mode 100644 index 00000000..fffd7b3b --- /dev/null +++ b/Tests/SuperLinq.Test/SliceTest.cs @@ -0,0 +1,76 @@ +namespace Test; + +/// +/// Verify the behavior of the Slice operator +/// +public class SliceTests +{ + /// + /// Verify that Slice evaluates in a lazy manner. + /// + [Fact] + public void TestSliceIsLazy() + { + _ = new BreakingSequence().Slice(10, 10); + } + + public static IEnumerable GetSequences() => + Enumerable.Range(1, 5) + .GetCollectionSequences() + .Select(x => new object[] { x }); + + [Theory] + [MemberData(nameof(GetSequences))] + public void TestSliceIdentity(IDisposableEnumerable seq) + { + using (seq) + { + var result = seq.Slice(0, 5); + result.AssertSequenceEqual(1, 2, 3, 4, 5); + } + } + + [Theory] + [MemberData(nameof(GetSequences))] + public void TestSliceFirstItem(IDisposableEnumerable seq) + { + using (seq) + { + var result = seq.Slice(0, 1); + result.AssertSequenceEqual(1); + } + } + + [Theory] + [MemberData(nameof(GetSequences))] + public void TestSliceLastItem(IDisposableEnumerable seq) + { + using (seq) + { + var result = seq.Slice(4, 1); + result.AssertSequenceEqual(5); + } + } + + [Theory] + [MemberData(nameof(GetSequences))] + public void TestSliceSmallerThanSequence(IDisposableEnumerable seq) + { + using (seq) + { + var result = seq.Slice(1, 2); + result.AssertSequenceEqual(2, 3); + } + } + + [Theory] + [MemberData(nameof(GetSequences))] + public void TestSliceLongerThanSequence(IDisposableEnumerable seq) + { + using (seq) + { + var result = seq.Slice(3, 5); + result.AssertSequenceEqual(4, 5); + } + } +}