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

Implement Linq CountBy method for IEnumerable and IQueryable #91507

Merged
merged 8 commits into from
Sep 14, 2023
30 changes: 30 additions & 0 deletions src/libraries/System.Linq/tests/CountByTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ public void CountBy_KeySelectorNull_ThrowsArgumentNullException()
AssertExtensions.Throws<ArgumentNullException>("keySelector", () => source.CountBy(keySelector, new AnagramEqualityComparer()));
}

[Fact]
public void CountBy_SourceThrowsOnGetEnumerator()
{
IEnumerable<int> source = new ThrowsOnGetEnumerator();

var enumerator = source.CountBy(x => x).GetEnumerator();
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved

Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}

[Fact]
public void CountBy_SourceThrowsOnMoveNext()
{
IEnumerable<int> source = new ThrowsOnMoveNext();

var enumerator = source.CountBy(x => x).GetEnumerator();

Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}

[Fact]
public void CountBy_SourceThrowsOnCurrent()
{
IEnumerable<int> source = new ThrowsOnCurrentEnumerator();

var enumerator = source.CountBy(x => x).GetEnumerator();

Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}

[Theory]
[MemberData(nameof(CountBy_TestData))]
public static void CountBy_HasExpectedOutput<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, int>> expected)
Expand Down