Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests that assert disposal & single iteration of enumerators #759

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
16 changes: 9 additions & 7 deletions MoreLinq.Test/AcquireTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<TestException>());

Expand Down
31 changes: 19 additions & 12 deletions MoreLinq.Test/AggregateRightTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using System;
using NUnit.Framework;

[TestFixture]
Expand All @@ -36,7 +37,8 @@ public void AggregateRightFuncIsNotInvokedOnSingleElementSequence()
{
const int value = 1;

var result = new[] { value }.AggregateRight(BreakingFunc.Of<int, int, int>());
using var source = TestingSequence.Of(value);
var result = source.AggregateRight(BreakingFunc.Of<int, int, int>());

Assert.That(result, Is.EqualTo(value));
}
Expand All @@ -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))))"));
}
Expand All @@ -60,25 +66,26 @@ 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<int>();
Assert.That(source.AggregateRight(defaultValue, (_, b) => b), Is.EqualTo(defaultValue));
}

[Test]
public void AggregateRightSeedFuncIsNotInvokedOnEmptySequence()
{
const int value = 1;

var result = new int[0].AggregateRight(value, BreakingFunc.Of<int, int, int>());
using var source = TestingSequence.Of<int>();
var result = source.AggregateRight(value, BreakingFunc.Of<int, int, int>());

Assert.That(result, Is.EqualTo(value));
}

[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))))"));
}

Expand All @@ -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<int>();
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));
}
}
Expand Down
102 changes: 49 additions & 53 deletions MoreLinq.Test/AggregateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(), (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<int>(), (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));
Expand All @@ -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));
Expand All @@ -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));
Expand Down
19 changes: 11 additions & 8 deletions MoreLinq.Test/AppendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -37,7 +37,7 @@ public void AppendWithNonEmptyHeadSequence()
[Test]
public void AppendWithEmptyHeadSequence()
{
string[] head = [];
using var head = TestingSequence.Of<string>();
var tail = "first";
var whole = head.Append(tail);
whole.AssertSequenceEqual("first");
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
11 changes: 6 additions & 5 deletions MoreLinq.Test/AssertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,31 @@ 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);
}

[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);
}

[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<ValueException>()
Expand Down
10 changes: 8 additions & 2 deletions MoreLinq.Test/AtLeastTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using System;
using NUnit.Framework;
using System.Collections.Generic;

Expand Down Expand Up @@ -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()
Expand Down
18 changes: 12 additions & 6 deletions MoreLinq.Test/AtMostTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using System;
using NUnit.Framework;
using System.Collections.Generic;

Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion MoreLinq.Test/ConsumeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Loading
Loading