diff --git a/MoreLinq.Test/AcquireTest.cs b/MoreLinq.Test/AcquireTest.cs index 70c247e15..deb7141db 100644 --- a/MoreLinq.Test/AcquireTest.cs +++ b/MoreLinq.Test/AcquireTest.cs @@ -30,9 +30,10 @@ public void AcquireAll() Disposable? b = null; Disposable? c = null; - var allocators = MoreEnumerable.From(() => a = new Disposable(), - () => b = new Disposable(), - () => c = new Disposable()); + using var allocators = MoreEnumerable.From(() => a = new Disposable(), + () => b = new Disposable(), + () => c = new Disposable()) + .AsTestingSequence(); var disposables = allocators.Acquire(); @@ -52,10 +53,11 @@ public void AcquireSome() Disposable? b = null; Disposable? c = null; - var allocators = MoreEnumerable.From(() => a = new Disposable(), - () => b = new Disposable(), - () => throw new TestException(), - () => c = new Disposable()); + using var allocators = MoreEnumerable.From(() => a = new Disposable(), + () => b = new Disposable(), + () => throw new TestException(), + () => c = new Disposable()) + .AsTestingSequence(); Assert.That(allocators.Acquire, Throws.TypeOf()); diff --git a/MoreLinq.Test/AggregateRightTest.cs b/MoreLinq.Test/AggregateRightTest.cs index 7ecd4825e..11057edf3 100644 --- a/MoreLinq.Test/AggregateRightTest.cs +++ b/MoreLinq.Test/AggregateRightTest.cs @@ -17,6 +17,7 @@ namespace MoreLinq.Test { + using System; using NUnit.Framework; [TestFixture] @@ -36,7 +37,8 @@ public void AggregateRightFuncIsNotInvokedOnSingleElementSequence() { const int value = 1; - var result = new[] { value }.AggregateRight(BreakingFunc.Of()); + using var source = TestingSequence.Of(value); + var result = source.AggregateRight(BreakingFunc.Of()); Assert.That(result, Is.EqualTo(value)); } @@ -46,9 +48,13 @@ public void AggregateRightFuncIsNotInvokedOnSingleElementSequence() [TestCase(SourceKind.Sequence)] public void AggregateRight(SourceKind sourceKind) { - var enumerable = Enumerable.Range(1, 5).Select(x => x.ToInvariantString()).ToSourceKind(sourceKind); + var source = Enumerable.Range(1, 5) + .Select(x => x.ToInvariantString()) + .ToSourceKind(sourceKind); - var result = enumerable.AggregateRight((a, b) => $"({a}+{b})"); + string result; + using (source as IDisposable) // primarily for `TestingSequence<>` + result = source.AggregateRight((a, b) => $"({a}+{b})"); Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))")); } @@ -60,7 +66,8 @@ public void AggregateRight(SourceKind sourceKind) [TestCase(true)] public void AggregateRightSeedWithEmptySequence(object defaultValue) { - Assert.That(new int[0].AggregateRight(defaultValue, (_, b) => b), Is.EqualTo(defaultValue)); + using var source = TestingSequence.Of(); + Assert.That(source.AggregateRight(defaultValue, (_, b) => b), Is.EqualTo(defaultValue)); } [Test] @@ -68,7 +75,8 @@ public void AggregateRightSeedFuncIsNotInvokedOnEmptySequence() { const int value = 1; - var result = new int[0].AggregateRight(value, BreakingFunc.Of()); + using var source = TestingSequence.Of(); + var result = source.AggregateRight(value, BreakingFunc.Of()); Assert.That(result, Is.EqualTo(value)); } @@ -76,9 +84,8 @@ public void AggregateRightSeedFuncIsNotInvokedOnEmptySequence() [Test] public void AggregateRightSeed() { - var result = Enumerable.Range(1, 4) - .AggregateRight("5", (a, b) => $"({a}+{b})"); - + using var source = TestingSequence.Of("1", "2", "3", "4"); + var result = source.AggregateRight("5", (a, b) => $"({a}+{b})"); Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))")); } @@ -89,15 +96,15 @@ public void AggregateRightSeed() [TestCase(true)] public void AggregateRightResultorWithEmptySequence(object defaultValue) { - Assert.That(new int[0].AggregateRight(defaultValue, (_, b) => b, a => a == defaultValue), Is.EqualTo(true)); + using var source = TestingSequence.Of(); + Assert.That(source.AggregateRight(defaultValue, (_, b) => b, a => a == defaultValue), Is.EqualTo(true)); } [Test] public void AggregateRightResultor() { - var result = Enumerable.Range(1, 4) - .AggregateRight("5", (a, b) => $"({a}+{b})", a => a.Length); - + using var source = TestingSequence.Of("1", "2", "3", "4"); + var result = source.AggregateRight("5", (a, b) => $"({a}+{b})", a => a.Length); Assert.That(result, Is.EqualTo("(1+(2+(3+(4+5))))".Length)); } } diff --git a/MoreLinq.Test/AggregateTest.cs b/MoreLinq.Test/AggregateTest.cs index a1bbec1b8..8ea6baf39 100644 --- a/MoreLinq.Test/AggregateTest.cs +++ b/MoreLinq.Test/AggregateTest.cs @@ -105,31 +105,29 @@ into t [Test] public void SevenUniqueAccumulators() { + using var source = Enumerable.Range(1, 10).AsTestingSequence(); + var result = - Enumerable - .Range(1, 10) - .Shuffle() - .Select(n => new { Num = n, Str = n.ToString(CultureInfo.InvariantCulture) }) - .Aggregate( - 0, (s, e) => s + e.Num, - 0, (s, e) => e.Num % 2 == 0 ? s + e.Num : s, - 0, (s, _) => s + 1, - (int?)null, (s, e) => s is { } n ? Math.Min(n, e.Num) : e.Num, - (int?)null, (s, e) => s is { } n ? Math.Max(n, e.Num) : e.Num, - new HashSet(), (s, e) => { _ = s.Add(e.Str.Length); return s; }, - new List<(int Num, string Str)>(), (s, e) => { s.Add((e.Num, e.Str)); return s; }, - (sum, esum, count, min, max, lengths, items) => new - { - Sum = sum, - EvenSum = esum, - Count = count, - Average = (double)sum / count, - Min = min ?? throw new InvalidOperationException(), - Max = max ?? throw new InvalidOperationException(), - UniqueLengths = lengths, - Items = items, - } - ); + source.Shuffle() + .Select(n => new { Num = n, Str = n.ToString(CultureInfo.InvariantCulture) }) + .Aggregate(0, (s, e) => s + e.Num, + 0, (s, e) => e.Num % 2 == 0 ? s + e.Num : s, + 0, (s, _) => s + 1, + (int?)null, (s, e) => s is { } n ? Math.Min(n, e.Num) : e.Num, + (int?)null, (s, e) => s is { } n ? Math.Max(n, e.Num) : e.Num, + new HashSet(), (s, e) => { _ = s.Add(e.Str.Length); return s; }, + new List<(int Num, string Str)>(), (s, e) => { s.Add((e.Num, e.Str)); return s; }, + (sum, esum, count, min, max, lengths, items) => new + { + Sum = sum, + EvenSum = esum, + Count = count, + Average = (double)sum / count, + Min = min ?? throw new InvalidOperationException(), + Max = max ?? throw new InvalidOperationException(), + UniqueLengths = lengths, + Items = items, + }); Assert.That(result.Sum , Is.EqualTo(55)); Assert.That(result.EvenSum, Is.EqualTo(30)); @@ -156,31 +154,29 @@ public void SevenUniqueAccumulators() [Test] public void SevenUniqueAccumulatorComprehensions() { + using var source = Enumerable.Range(1, 10).AsTestingSequence(); + var result = - Enumerable - .Range(1, 10) - .Shuffle() - .Select(n => new { Num = n, Str = n.ToString(CultureInfo.InvariantCulture) }) - .Aggregate( - s => s.Sum(e => e.Num), - s => s.Select(e => e.Num).Where(n => n % 2 == 0).Sum(), - s => s.Count(), - s => s.Min(e => e.Num), - s => s.Max(e => e.Num), - s => s.Select(e => e.Str.Length).Distinct().ToArray(), - s => s.ToArray(), - (sum, esum, count, min, max, lengths, items) => new - { - Sum = sum, - EvenSum = esum, - Count = count, - Average = (double)sum / count, - Min = min, - Max = max, - UniqueLengths = lengths, - Items = items, - } - ); + source.Shuffle() + .Select(n => new { Num = n, Str = n.ToString(CultureInfo.InvariantCulture) }) + .Aggregate(s => s.Sum(e => e.Num), + s => s.Select(e => e.Num).Where(n => n % 2 == 0).Sum(), + s => s.Count(), + s => s.Min(e => e.Num), + s => s.Max(e => e.Num), + s => s.Select(e => e.Str.Length).Distinct().ToArray(), + s => s.ToArray(), + (sum, esum, count, min, max, lengths, items) => new + { + Sum = sum, + EvenSum = esum, + Count = count, + Average = (double)sum / count, + Min = min, + Max = max, + UniqueLengths = lengths, + Items = items, + }); Assert.That(result.Sum , Is.EqualTo(55)); Assert.That(result.EvenSum, Is.EqualTo(30)); @@ -206,11 +202,11 @@ public void SevenUniqueAccumulatorComprehensions() [Test(Description = "https://github.com/morelinq/MoreLINQ/issues/616")] public void Issue616() { - var (first, last) = - Enumerable.Range(1, 10) - .Aggregate(ds => ds.FirstAsync(), - ds => ds.LastAsync(), - ValueTuple.Create); + using var source = Enumerable.Range(1, 10).AsTestingSequence(); + + var (first, last) = source.Aggregate(ds => ds.FirstAsync(), + ds => ds.LastAsync(), + ValueTuple.Create); Assert.That(first, Is.EqualTo(1)); Assert.That(last, Is.EqualTo(10)); diff --git a/MoreLinq.Test/AppendTest.cs b/MoreLinq.Test/AppendTest.cs index 0398a5658..6dcd18254 100644 --- a/MoreLinq.Test/AppendTest.cs +++ b/MoreLinq.Test/AppendTest.cs @@ -28,7 +28,7 @@ public class AppendTest [Test] public void AppendWithNonEmptyHeadSequence() { - var head = new[] { "first", "second" }; + using var head = TestingSequence.Of("first", "second"); var tail = "third"; var whole = head.Append(tail); whole.AssertSequenceEqual("first", "second", "third"); @@ -37,7 +37,7 @@ public void AppendWithNonEmptyHeadSequence() [Test] public void AppendWithEmptyHeadSequence() { - string[] head = []; + using var head = TestingSequence.Of(); var tail = "first"; var whole = head.Append(tail); whole.AssertSequenceEqual("first"); @@ -46,7 +46,7 @@ public void AppendWithEmptyHeadSequence() [Test] public void AppendWithNullTail() { - var head = new[] { "first", "second" }; + using var head = TestingSequence.Of("first", "second"); string? tail = null; var whole = head.Append(tail); whole.AssertSequenceEqual("first", "second", null); @@ -82,12 +82,15 @@ into e [Test] public void AppendWithSharedSource() { - var first = new[] { 1 }.Append(2); - var second = first.Append(3).Append(4); - var third = first.Append(4).Append(8); + using var a = new[] { 1 }.AsTestingSequence(maxEnumerations: 2); + using var b = a.Append(2).AsTestingSequence(maxEnumerations: 2); + using var c = b.Append(3).AsTestingSequence(); + using var d = c.Append(4).AsTestingSequence(); + using var e = b.Append(4).AsTestingSequence(); + using var f = e.Append(8).AsTestingSequence(); - second.AssertSequenceEqual(1, 2, 3, 4); - third.AssertSequenceEqual(1, 2, 4, 8); + d.AssertSequenceEqual(1, 2, 3, 4); + f.AssertSequenceEqual(1, 2, 4, 8); } } } diff --git a/MoreLinq.Test/AssertTest.cs b/MoreLinq.Test/AssertTest.cs index ad037a030..fe20f4a63 100644 --- a/MoreLinq.Test/AssertTest.cs +++ b/MoreLinq.Test/AssertTest.cs @@ -33,14 +33,15 @@ public void AssertIsLazy() [Test] public void AssertSequenceWithValidAllElements() { - var source = new[] { 2, 4, 6, 8 }; - source.Assert(n => n % 2 == 0).AssertSequenceEqual(source); + var xs = new[] { 2, 4, 6, 8 }; + using var source = TestingSequence.Of(xs); + source.Assert(n => n % 2 == 0).AssertSequenceEqual(xs); } [Test] public void AssertSequenceWithValidSomeInvalidElements() { - var source = new[] { 2, 4, 6, 7, 8, 9 }; + using var source = TestingSequence.Of(2, 4, 6, 7, 8, 9); Assert.That(() => source.Assert(n => n % 2 == 0).Consume(), Throws.InvalidOperationException); } @@ -48,7 +49,7 @@ public void AssertSequenceWithValidSomeInvalidElements() [Test] public void AssertSequenceWithInvalidElementsAndCustomErrorReturningNull() { - var source = new[] { 2, 4, 6, 7, 8, 9 }; + using var source = TestingSequence.Of(2, 4, 6, 7, 8, 9); Assert.That(() => source.Assert(n => n % 2 == 0, _ => null!).Consume(), Throws.InvalidOperationException); } @@ -56,7 +57,7 @@ public void AssertSequenceWithInvalidElementsAndCustomErrorReturningNull() [Test] public void AssertSequenceWithInvalidElementsAndCustomError() { - var source = new[] { 2, 4, 6, 7, 8, 9 }; + using var source = TestingSequence.Of(2, 4, 6, 7, 8, 9); Assert.That(() => source.Assert(n => n % 2 == 0, n => new ValueException(n)).Consume(), Throws.TypeOf() diff --git a/MoreLinq.Test/AtLeastTest.cs b/MoreLinq.Test/AtLeastTest.cs index 83f6093ad..cb92815ce 100644 --- a/MoreLinq.Test/AtLeastTest.cs +++ b/MoreLinq.Test/AtLeastTest.cs @@ -17,6 +17,7 @@ namespace MoreLinq.Test { + using System; using NUnit.Framework; using System.Collections.Generic; @@ -49,8 +50,13 @@ from e in new[] .SetName($"{{m}}({k}[{e.Size}], {e.Count})"); [TestCaseSource(nameof(AtLeastSource))] - public bool AtLeast(SourceKind sourceKind, int sequenceSize, int atLeastAssertCount) => - Enumerable.Range(0, sequenceSize).ToSourceKind(sourceKind).AtLeast(atLeastAssertCount); + public bool AtLeast(SourceKind sourceKind, int sequenceSize, int atLeastAssertCount) + { + var xs = Enumerable.Range(0, sequenceSize); + var source = xs.ToSourceKind(sourceKind); + using (source as IDisposable) // primarily for `TestingSequence<>` + return source.AtLeast(atLeastAssertCount); + } [Test] public void AtLeastDoesNotIterateUnnecessaryElements() diff --git a/MoreLinq.Test/AtMostTest.cs b/MoreLinq.Test/AtMostTest.cs index 74e3add7a..00f50a402 100644 --- a/MoreLinq.Test/AtMostTest.cs +++ b/MoreLinq.Test/AtMostTest.cs @@ -17,6 +17,7 @@ namespace MoreLinq.Test { + using System; using NUnit.Framework; using System.Collections.Generic; @@ -46,16 +47,21 @@ from e in new[] .SetName($"{{m}}({k}[{e.Size}], {e.Count})"); [TestCaseSource(nameof(AtMostSource))] - public bool AtMost(SourceKind sourceKind, int sequenceSize, int atMostAssertCount) => - Enumerable.Range(0, sequenceSize).ToSourceKind(sourceKind).AtMost(atMostAssertCount); + public bool AtMost(SourceKind sourceKind, int sequenceSize, int atMostAssertCount) + { + var source = Enumerable.Range(0, sequenceSize).ToSourceKind(sourceKind); + using (source as IDisposable) // primarily for `TestingSequence<>` + return source.AtMost(atMostAssertCount); + } [Test] public void AtMostDoesNotIterateUnnecessaryElements() { - var source = MoreEnumerable.From(() => 1, - () => 2, - () => 3, - () => throw new TestException()); + using var source = MoreEnumerable.From(() => 1, + () => 2, + () => 3, + () => throw new TestException()) + .AsTestingSequence(); Assert.That(source.AtMost(2), Is.False); } } diff --git a/MoreLinq.Test/ConsumeTest.cs b/MoreLinq.Test/ConsumeTest.cs index 5d802b344..e98fb5e57 100644 --- a/MoreLinq.Test/ConsumeTest.cs +++ b/MoreLinq.Test/ConsumeTest.cs @@ -26,7 +26,9 @@ public class ConsumeTest public void ConsumeReallyConsumes() { var counter = 0; - var sequence = Enumerable.Range(0, 10).Pipe(_ => counter++); + using var sequence = Enumerable.Range(0, 10) + .Pipe(_ => counter++) + .AsTestingSequence(); sequence.Consume(); Assert.That(counter, Is.EqualTo(10)); } diff --git a/MoreLinq.Test/CountByTest.cs b/MoreLinq.Test/CountByTest.cs index 027c2979e..64d2227c4 100644 --- a/MoreLinq.Test/CountByTest.cs +++ b/MoreLinq.Test/CountByTest.cs @@ -26,7 +26,9 @@ public class CountByTest [Test] public void CountBySimpleTest() { - var result = MoreEnumerable.CountBy([1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2 ], c => c); + using var source = TestingSequence.Of(1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2); + + var result = MoreEnumerable.CountBy(source, c => c); result.AssertSequenceEqual( KeyValuePair.Create(1, 4), @@ -40,7 +42,9 @@ public void CountBySimpleTest() [Test] public void CountByWithSecondOccurenceImmediatelyAfterFirst() { - var result = MoreEnumerable.CountBy("jaffer", c => c); + using var source = "jaffer".ToCharArray().AsTestingSequence(); + + var result = MoreEnumerable.CountBy(source, c => c); result.AssertSequenceEqual( KeyValuePair.Create('j', 1), @@ -53,7 +57,9 @@ public void CountByWithSecondOccurenceImmediatelyAfterFirst() [Test] public void CountByEvenOddTest() { - var result = MoreEnumerable.CountBy(Enumerable.Range(1, 100), c => c % 2); + using var source = Enumerable.Range(1, 100).AsTestingSequence(); + + var result = MoreEnumerable.CountBy(source, c => c % 2); result.AssertSequenceEqual( KeyValuePair.Create(1, 50), @@ -63,7 +69,9 @@ public void CountByEvenOddTest() [Test] public void CountByWithEqualityComparer() { - var result = MoreEnumerable.CountBy(["a", "B", "c", "A", "b", "A"], c => c, StringComparer.OrdinalIgnoreCase); + using var source = TestingSequence.Of("a", "B", "c", "A", "b", "A"); + + var result = MoreEnumerable.CountBy(source, c => c, StringComparer.OrdinalIgnoreCase); result.AssertSequenceEqual( KeyValuePair.Create("a", 3), @@ -76,7 +84,8 @@ public void CountByHasKeysOrderedLikeGroupBy() { var randomSequence = MoreEnumerable.Random(0, 100).Take(100).ToArray(); - var countByKeys = MoreEnumerable.CountBy(randomSequence, x => x).Select(x => x.Key); + using var source = randomSequence.AsTestingSequence(); + var countByKeys = MoreEnumerable.CountBy(source, x => x).Select(x => x.Key); var groupByKeys = randomSequence.GroupBy(x => x).Select(x => x.Key); countByKeys.AssertSequenceEqual(groupByKeys); @@ -91,11 +100,10 @@ public void CountByIsLazy() [Test] public void CountByWithSomeNullKeys() { - var ss = new[] - { - "foo", null, "bar", "baz", null, null, "baz", "bar", null, "foo" - }; - var result = MoreEnumerable.CountBy(ss, s => s); + using var source = TestingSequence.Of("foo", null, "bar", "baz", null, null, "baz", + "bar", null, "foo"); + + var result = MoreEnumerable.CountBy(source, s => s); result.AssertSequenceEqual( KeyValuePair.Create((string?)"foo", 2), @@ -107,7 +115,8 @@ public void CountByWithSomeNullKeys() [Test] public void CountByWithSomeNullKeysAndEqualityComparer() { - string?[] source = ["a", "B", null, "c", "A", null, "b", "A"]; + using var source = TestingSequence.Of("a", "B", null, "c", "A", null, "b", "A"); + var result = MoreEnumerable.CountBy(source, c => c, StringComparer.OrdinalIgnoreCase); result.AssertSequenceEqual( diff --git a/MoreLinq.Test/EvaluateTest.cs b/MoreLinq.Test/EvaluateTest.cs index 2e02e932e..a1d61ef82 100644 --- a/MoreLinq.Test/EvaluateTest.cs +++ b/MoreLinq.Test/EvaluateTest.cs @@ -31,13 +31,10 @@ public void TestEvaluateIsLazy() [Test] public void TestEvaluateInvokesMethods() { - var factories = new Func[] - { - () => -2, - () => 4, - () => int.MaxValue, - () => int.MinValue, - }; + using var factories = TestingSequence.Of(() => -2, + () => 4, + () => int.MaxValue, + () => int.MinValue); var results = factories.Evaluate(); results.AssertSequenceEqual(-2, 4, int.MaxValue, int.MinValue); @@ -47,11 +44,9 @@ public void TestEvaluateInvokesMethods() public void TestEvaluateInvokesMethodsMultipleTimes() { var evals = 0; - var factories = new Func[] - { - () => { evals++; return -2; }, - }; - var results = factories.Evaluate(); + var factories = new[] { () => { evals++; return -2; } }; + using var source = factories.AsTestingSequence(maxEnumerations: 3); + var results = source.Evaluate(); results.Consume(); results.Consume(); diff --git a/MoreLinq.Test/ExactlyTest.cs b/MoreLinq.Test/ExactlyTest.cs index f1858d34d..90ff0ae3a 100644 --- a/MoreLinq.Test/ExactlyTest.cs +++ b/MoreLinq.Test/ExactlyTest.cs @@ -17,6 +17,7 @@ namespace MoreLinq.Test { + using System; using NUnit.Framework; using System.Collections.Generic; @@ -44,17 +45,22 @@ from e in new[] .SetName($"{{m}}({k}[{e.Size}], {e.Count})"); [TestCaseSource(nameof(ExactlySource))] - public bool Exactly(SourceKind sourceKind, int sequenceSize, int exactlyAssertCount) => - Enumerable.Range(0, sequenceSize).ToSourceKind(sourceKind).Exactly(exactlyAssertCount); + public bool Exactly(SourceKind sourceKind, int sequenceSize, int exactlyAssertCount) + { + var source = Enumerable.Range(0, sequenceSize).ToSourceKind(sourceKind); + using (source as IDisposable) // primarily for `TestingSequence<>` + return source.Exactly(exactlyAssertCount); + } [Test] public void ExactlyDoesNotIterateUnnecessaryElements() { - var source = MoreEnumerable.From(() => 1, - () => 2, - () => 3, - () => throw new TestException()); + using var source = MoreEnumerable.From(() => 1, + () => 2, + () => 3, + () => throw new TestException()) + .AsTestingSequence(); Assert.That(source.Exactly(2), Is.False); } } diff --git a/MoreLinq.Test/ForEachTest.cs b/MoreLinq.Test/ForEachTest.cs index ddfd5f1e7..a010cb65e 100644 --- a/MoreLinq.Test/ForEachTest.cs +++ b/MoreLinq.Test/ForEachTest.cs @@ -27,7 +27,8 @@ public class ForEachTest public void ForEachWithSequence() { var results = new List(); - new[] { 1, 2, 3 }.ForEach(results.Add); + using var source = TestingSequence.Of(1, 2, 3); + source.ForEach(results.Add); results.AssertSequenceEqual(1, 2, 3); } @@ -36,7 +37,8 @@ public void ForEachIndexedWithSequence() { var valueResults = new List(); var indexResults = new List(); - new[] { 9, 7, 8 }.ForEach((x, index) => { valueResults.Add(x); indexResults.Add(index); }); + using var source = TestingSequence.Of(9, 7, 8); + source.ForEach((x, index) => { valueResults.Add(x); indexResults.Add(index); }); valueResults.AssertSequenceEqual(9, 7, 8); indexResults.AssertSequenceEqual(0, 1, 2); } diff --git a/MoreLinq.Test/SkipLastTest.cs b/MoreLinq.Test/SkipLastTest.cs index 94187ef75..f2252edce 100644 --- a/MoreLinq.Test/SkipLastTest.cs +++ b/MoreLinq.Test/SkipLastTest.cs @@ -28,9 +28,9 @@ public class SkipLastTest [TestCase(-1)] public void SkipLastWithCountLesserThanOne(int skip) { - var numbers = Enumerable.Range(1, 5); - - Assert.That(numbers.SkipLast(skip), Is.EqualTo(numbers)); + using var numbers = Enumerable.Range(1, 5).AsTestingSequence(); + var result = numbers.SkipLast(skip); + Assert.That(result, Is.EqualTo(numbers)); } [Test] @@ -40,17 +40,20 @@ public void SkipLast() const int skip = 20; var sequence = Enumerable.Range(1, take); - var expectations = sequence.Take(take - skip); - Assert.That(expectations, Is.EqualTo(sequence.SkipLast(skip))); + using var source = sequence.AsTestingSequence(); + var result = source.SkipLast(skip); + Assert.That(expectations, Is.EqualTo(result)); } [TestCase(5)] [TestCase(6)] public void SkipLastWithSequenceShorterThanCount(int skip) { - Assert.That(Enumerable.Range(1, 5).SkipLast(skip), Is.Empty); + using var source = Enumerable.Range(1, 5).AsTestingSequence(); + var result = source.SkipLast(skip); + Assert.That(result, Is.Empty); } [Test] diff --git a/MoreLinq.Test/TestExtensions.cs b/MoreLinq.Test/TestExtensions.cs index 76d6869c6..a3b22fc6c 100644 --- a/MoreLinq.Test/TestExtensions.cs +++ b/MoreLinq.Test/TestExtensions.cs @@ -89,14 +89,14 @@ internal static IEnumerable ToSourceKind(this IEnumerable input, Source sourceKind switch #pragma warning restore IDE0072 // Add missing cases { - SourceKind.Sequence => input.Select(x => x), + SourceKind.Sequence => input.AsTestingSequence(), var kind => input.ToList().AsSourceKind(kind) }; internal static IEnumerable AsSourceKind(this List input, SourceKind sourceKind) => sourceKind switch { - SourceKind.Sequence => input.Select(x => x), + SourceKind.Sequence => input.AsTestingSequence(), SourceKind.BreakingList => new BreakingList(input), SourceKind.BreakingReadOnlyList => new BreakingReadOnlyList(input), SourceKind.BreakingCollection => new BreakingCollection(input), diff --git a/MoreLinq.Test/ToDictionaryTest.cs b/MoreLinq.Test/ToDictionaryTest.cs index 5bd5f87b9..8275855fb 100644 --- a/MoreLinq.Test/ToDictionaryTest.cs +++ b/MoreLinq.Test/ToDictionaryTest.cs @@ -18,22 +18,28 @@ namespace MoreLinq.Test { using System; + using System.Collections.Generic; using NUnit.Framework; [TestFixture] public class ToDictionaryTest { + static TestingSequence> Pairs() => + TestingSequence.Of(KeyValuePair.Create("foo", 123), + KeyValuePair.Create("bar", 456), + KeyValuePair.Create("baz", 789)); + + static TestingSequence<(string, int)> Couples() => + TestingSequence.Of(("foo", 123), + ("bar", 456), + ("baz", 789)); + [Test] public void ToDictionaryWithKeyValuePairs() { - var pairs = new[] - { - KeyValuePair.Create("foo", 123), - KeyValuePair.Create("bar", 456), - KeyValuePair.Create("baz", 789), - }; + using var source = Pairs(); - var dict = MoreEnumerable.ToDictionary(pairs); + var dict = MoreEnumerable.ToDictionary(source); Assert.That(dict["foo"], Is.EqualTo(123)); Assert.That(dict["bar"], Is.EqualTo(456)); @@ -43,14 +49,9 @@ public void ToDictionaryWithKeyValuePairs() [Test] public void ToDictionaryWithCouples() { - var pairs = new[] - { - ("foo", 123), - ("bar", 456), - ("baz", 789), - }; + using var source = Couples(); - var dict = MoreEnumerable.ToDictionary(pairs); + var dict = MoreEnumerable.ToDictionary(source); Assert.That(dict["foo"], Is.EqualTo(123)); Assert.That(dict["bar"], Is.EqualTo(456)); @@ -60,14 +61,9 @@ public void ToDictionaryWithCouples() [Test] public void ToDictionaryWithKeyValuePairsWithComparer() { - var pairs = new[] - { - KeyValuePair.Create("foo", 123), - KeyValuePair.Create("bar", 456), - KeyValuePair.Create("baz", 789), - }; + using var source = Pairs(); - var dict = MoreEnumerable.ToDictionary(pairs, StringComparer.OrdinalIgnoreCase); + var dict = MoreEnumerable.ToDictionary(source, StringComparer.OrdinalIgnoreCase); Assert.That(dict["FOO"], Is.EqualTo(123)); Assert.That(dict["BAR"], Is.EqualTo(456)); @@ -77,14 +73,9 @@ public void ToDictionaryWithKeyValuePairsWithComparer() [Test] public void ToDictionaryWithCouplesWithComparer() { - var pairs = new[] - { - ("foo", 123), - ("bar", 456), - ("baz", 789), - }; + using var source = Couples(); - var dict = MoreEnumerable.ToDictionary(pairs, StringComparer.OrdinalIgnoreCase); + var dict = MoreEnumerable.ToDictionary(source, StringComparer.OrdinalIgnoreCase); Assert.That(dict["FOO"], Is.EqualTo(123)); Assert.That(dict["BAR"], Is.EqualTo(456));