Skip to content

Commit

Permalink
Use compiler-generated delegate cache in "AssertCount"
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Feb 26, 2023
1 parent 777aa24 commit 2abc528
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions MoreLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ static partial class MoreEnumerable
{
#if MORELINQ

static readonly Func<int, int, Exception> DefaultErrorSelector = OnAssertCountFailure;
internal static string FormatSequenceLengthErrorMessage(int cmp, int count) =>
$"Sequence contains too {(cmp < 0 ? "few" : "many")} elements when exactly {count:N0} {(count == 1 ? "was" : "were")} expected.";

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
Expand All @@ -42,7 +43,7 @@ static partial class MoreEnumerable
/// </remarks>

public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource> source, int count) =>
AssertCountImpl(source, count, DefaultErrorSelector);
AssertCountImpl(source, count, static (cmp, count) => new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)));

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
Expand Down Expand Up @@ -71,12 +72,6 @@ public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource
int count, Func<int, int, Exception> errorSelector) =>
AssertCountImpl(source, count, errorSelector);

static Exception OnAssertCountFailure(int cmp, int count) =>
new SequenceException(FormatSequenceLengthErrorMessage(cmp, count));

internal static string FormatSequenceLengthErrorMessage(int cmp, int count) =>
$"Sequence contains too {(cmp < 0 ? "few" : "many")} elements when exactly {count:N0} {(count == 1 ? "was" : "were")} expected.";

#endif

static IEnumerable<TSource> AssertCountImpl<TSource>(IEnumerable<TSource> source,
Expand Down

0 comments on commit 2abc528

Please sign in to comment.