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

Address to-do about consolidating testing sequence implementations #923

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 6 additions & 61 deletions MoreLinq.Test/MemoizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace MoreLinq.Test
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Delegate = Delegating.Delegate;
Expand Down Expand Up @@ -245,13 +244,8 @@ public void MemoizeRethrowsErrorDuringIterationToAllIteratorsUntilDisposed()
{
var error = new Exception("This is a test exception.");

IEnumerable<int> TestSequence()
{
yield return 123;
throw error;
}

var xs = new DisposalTrackingSequence<int>(TestSequence());
var xs = MoreEnumerable.From(() => 123, () => throw error)
.AsTestingSequence(TestingSequence.Options.AllowMultipleEnumerations);
var memoized = xs.Memoize();
using ((IDisposable) memoized)
using (var r1 = memoized.Read())
Expand All @@ -274,14 +268,10 @@ public void MemoizeRethrowsErrorDuringIterationStartToAllIteratorsUntilDisposed(
var error = new Exception("This is a test exception.");

var i = 0;
IEnumerable<int> TestSequence()
{
if (0 == i++) // throw at start for first iteration only
throw error;
yield return 42;
}

var xs = new DisposalTrackingSequence<int>(TestSequence());
var xs = MoreEnumerable.From(() => 0 == i++
? throw error // throw at start for first iteration only
: 42)
.AsTestingSequence(TestingSequence.Options.AllowMultipleEnumerations);
var memoized = xs.Memoize();
using ((IDisposable) memoized)
using (var r1 = memoized.Read())
Expand Down Expand Up @@ -315,50 +305,5 @@ public void MemoizeRethrowsErrorDuringFirstIterationStartToAllIterationsUntilDis
((IDisposable) memo).Dispose();
Assert.That(memo.Single(), Is.EqualTo(obj));
}

// TODO Consolidate with MoreLinq.Test.TestingSequence<T>?

sealed class DisposalTrackingSequence<T> : IEnumerable<T>, IDisposable
{
readonly IEnumerable<T> _sequence;

internal DisposalTrackingSequence(IEnumerable<T> sequence) =>
_sequence = sequence;

public bool? IsDisposed { get; private set; }

void IDisposable.Dispose() => IsDisposed = null;

public IEnumerator<T> GetEnumerator()
{
var enumerator = new DisposeTestingSequenceEnumerator(_sequence.GetEnumerator());
IsDisposed = false;
enumerator.Disposed += delegate { IsDisposed = true; };
return enumerator;
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

sealed class DisposeTestingSequenceEnumerator : IEnumerator<T>
{
readonly IEnumerator<T> _sequence;

public event EventHandler? Disposed;

public DisposeTestingSequenceEnumerator(IEnumerator<T> sequence) =>
_sequence = sequence;

public T Current => _sequence.Current;
object? IEnumerator.Current => Current;
public void Reset() => _sequence.Reset();
public bool MoveNext() => _sequence.MoveNext();

public void Dispose()
{
_sequence.Dispose();
Disposed?.Invoke(this, EventArgs.Empty);
}
}
}
}
}
28 changes: 23 additions & 5 deletions MoreLinq.Test/TestingSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ namespace MoreLinq.Test
static class TestingSequence
{
internal static TestingSequence<T> Of<T>(params T[] elements) =>
new TestingSequence<T>(elements);
Of(Options.None, elements);

internal static TestingSequence<T> AsTestingSequence<T>(this IEnumerable<T> source) =>
internal static TestingSequence<T> Of<T>(Options options, params T[] elements) =>
elements.AsTestingSequence(options);

internal static TestingSequence<T> AsTestingSequence<T>(this IEnumerable<T> source,
Options options = Options.None) =>
source != null
? new TestingSequence<T>(source)
? new TestingSequence<T>(source) { IsReiterationAllowed = options.HasFlag(Options.AllowMultipleEnumerations) }
: throw new ArgumentNullException(nameof(source));

[Flags]
public enum Options
{
None,
AllowMultipleEnumerations
}
}

/// <summary>
Expand All @@ -46,6 +57,8 @@ sealed class TestingSequence<T> : IEnumerable<T>, IDisposable
internal TestingSequence(IEnumerable<T> sequence) =>
_sequence = sequence;

public bool IsDisposed => _disposed ?? false;
public bool IsReiterationAllowed { get; init; }
public int MoveNextCallCount { get; private set; }

void IDisposable.Dispose() =>
Expand All @@ -64,7 +77,9 @@ void AssertDisposed()

public IEnumerator<T> GetEnumerator()
{
Assert.That(_sequence, Is.Not.Null, "LINQ operators should not enumerate a sequence more than once.");
if (!IsReiterationAllowed)
Assert.That(_sequence, Is.Not.Null, "LINQ operators should not enumerate a sequence more than once.");

Debug.Assert(_sequence is not null);

var enumerator = _sequence.GetEnumerator().AsWatchable();
Expand All @@ -81,7 +96,10 @@ public IEnumerator<T> GetEnumerator()
ended = !moved;
MoveNextCallCount++;
};
_sequence = null;

if (!IsReiterationAllowed)
_sequence = null;

return enumerator;
}

Expand Down