Skip to content

Commit

Permalink
Restore missing TrySingle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Jan 3, 2025
1 parent 303374d commit 10a24b0
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions Tests/SuperLinq.Tests/TrySingleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public void TrySingleWithEmptySource(IDisposableEnumerable<int?> seq)
{
using (seq)
{
var (cardinality, value) = seq.TrySingle("zero", "one", "many");
var value = seq.TrySingle();

Assert.Equal("zero", cardinality);
Assert.Null(value);
}
}
Expand All @@ -26,9 +25,8 @@ public void TrySingleWithSingleton(IDisposableEnumerable<int?> seq)
{
using (seq)
{
var (cardinality, value) = seq.TrySingle("zero", "one", "many");
var value = seq.TrySingle();

Assert.Equal("one", cardinality);
Assert.Equal(1, value);
}
}
Expand All @@ -37,37 +35,62 @@ public void TrySingleWithSingleton(IDisposableEnumerable<int?> seq)
public void TrySingleWithSingletonCollection()
{
var source = new BreakingSingleElementCollection<int>(10);
var (cardinality, value) = source.TrySingle("zero", "one", "many");
var value = source.TrySingle();

Assert.Equal("one", cardinality);
Assert.Equal(10, value);
}

private sealed class BreakingSingleElementCollection<T>(
T element
) : ICollection<T>
[Test]
[MethodDataSource(nameof(GetAllSequences), Arguments = [2])]
public void TrySingleWithMoreThanOne(IDisposableEnumerable<int?> seq)
{
public int Count { get; } = 1;
using (seq)
{
var value = seq.TrySingle();

Assert.Null(value);
}
}

public IEnumerator<T> GetEnumerator()
[Test]
[MethodDataSource(nameof(GetAllSequences), Arguments = [0])]
public void TrySingleCardinalityWithEmptySource(IDisposableEnumerable<int?> seq)
{
using (seq)
{
yield return element;
throw new InvalidOperationException($"{nameof(SuperEnumerable.TrySingle)} should not have attempted to consume a second element.");
var (cardinality, value) = seq.TrySingle("zero", "one", "many");

Assert.Equal("zero", cardinality);
Assert.Null(value);
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
[Test]
[MethodDataSource(nameof(GetAllSequences), Arguments = [1])]
public void TrySingleCardinalityWithSingleton(IDisposableEnumerable<int?> seq)
{
using (seq)
{
var (cardinality, value) = seq.TrySingle("zero", "one", "many");

public void Add(T item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(T item) => throw new NotSupportedException();
public void CopyTo(T[] array, int arrayIndex) => throw new NotSupportedException();
public bool Remove(T item) => throw new NotSupportedException();
public bool IsReadOnly => true;
Assert.Equal("one", cardinality);
Assert.Equal(1, value);
}
}

[Test]
public void TrySingleCardinalityWithSingletonCollection()
{
var source = new BreakingSingleElementCollection<int>(10);
var (cardinality, value) = source.TrySingle("zero", "one", "many");

Assert.Equal("one", cardinality);
Assert.Equal(10, value);
}

[Test]
[MethodDataSource(nameof(GetAllSequences), Arguments = [2])]
public void TrySingleWithMoreThanOne(IDisposableEnumerable<int?> seq)
public void TrySingleCardinalityWithMoreThanOne(IDisposableEnumerable<int?> seq)
{
using (seq)
{
Expand All @@ -78,3 +101,25 @@ public void TrySingleWithMoreThanOne(IDisposableEnumerable<int?> seq)
}
}
}

file sealed class BreakingSingleElementCollection<T>(
T element
) : ICollection<T>
{
public int Count { get; } = 1;

public IEnumerator<T> GetEnumerator()
{
yield return element;
throw new InvalidOperationException($"{nameof(SuperEnumerable.TrySingle)} should not have attempted to consume a second element.");
}

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

public void Add(T item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(T item) => throw new NotSupportedException();
public void CopyTo(T[] array, int arrayIndex) => throw new NotSupportedException();
public bool Remove(T item) => throw new NotSupportedException();
public bool IsReadOnly => true;
}

0 comments on commit 10a24b0

Please sign in to comment.