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

Consolidate error formatting between AssertCount and Fold #929

Merged
merged 1 commit into from
Jan 16, 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
14 changes: 9 additions & 5 deletions MoreLinq.Test/AssertCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ public void AssertCountLongSequence()
Throws.TypeOf<SequenceException>());
}

[TestCase(4, "Sequence contains too few elements when exactly 4 were expected.")]
[TestCase(2, "Sequence contains too many elements when exactly 2 were expected.")]
public void AssertCountDefaultExceptionMessageVariesWithCase(int count, string expectedMessage)
[TestCase("", 1, "Sequence contains too few elements when exactly 1 was expected.")]
[TestCase("foo,bar,baz", 1, "Sequence contains too many elements when exactly 1 was expected.")]
[TestCase("foo,bar,baz", 4, "Sequence contains too few elements when exactly 4 were expected.")]
[TestCase("foo,bar,baz", 2, "Sequence contains too many elements when exactly 2 were expected.")]
public void AssertCountDefaultExceptionMessageVariesWithCase(string str, int count, string expectedMessage)
{
var tokens = "foo,bar,baz".GenerateSplits(',');
var tokens = str.GenerateSplits(',')
.Where(t => t.Length > 0)
.AssertCount(count);

Assert.That(() => tokens.AssertCount(count).Consume(),
Assert.That(() => tokens.Consume(),
Throws.TypeOf<SequenceException>().With.Message.EqualTo(expectedMessage));
}

Expand Down
9 changes: 6 additions & 3 deletions MoreLinq.Test/FoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@ public class FoldTest
public void FoldWithTooFewItems()
{
Assert.That(() => Enumerable.Range(1, 3).Fold(BreakingFunc.Of<int, int, int, int, int>()),
Throws.TypeOf<InvalidOperationException>());
Throws.TypeOf<InvalidOperationException>()
.And.Message.EqualTo("Sequence contains too few elements when exactly 4 were expected."));
}

[Test]
public void FoldWithEmptySequence()
{
Assert.That(() => Enumerable.Empty<int>().Fold(BreakingFunc.Of<int, int>()),
Throws.TypeOf<InvalidOperationException>());
Throws.TypeOf<InvalidOperationException>()
.And.Message.EqualTo("Sequence contains too few elements when exactly 1 was expected."));
}

[Test]
public void FoldWithTooManyItems()
{
Assert.That(() => Enumerable.Range(1, 3).Fold(BreakingFunc.Of<int, int, int>()),
Throws.TypeOf<InvalidOperationException>());
Throws.TypeOf<InvalidOperationException>()
.And.Message.EqualTo("Sequence contains too many elements when exactly 2 were expected."));
}

[Test]
Expand Down
12 changes: 5 additions & 7 deletions MoreLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ 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)
{
var message = cmp < 0
? "Sequence contains too few elements when exactly {0} were expected."
: "Sequence contains too many elements when exactly {0} were expected.";
return new SequenceException(string.Format(message, count.ToString("N0")));
}
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

Expand Down
9 changes: 2 additions & 7 deletions MoreLinq/Fold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ static TResult FoldImpl<T, TResult>(IEnumerable<T> source, int count,

static readonly Func<int, int, Exception> OnFolderSourceSizeErrorSelector = OnFolderSourceSizeError;

static Exception OnFolderSourceSizeError(int cmp, int count)
{
var message = cmp < 0
? "Sequence contains too few elements when exactly {0} {1} expected."
: "Sequence contains too many elements when exactly {0} {1} expected.";
return new InvalidOperationException(string.Format(message, count.ToString("N0"), count == 1 ? "was" : "were"));
}
static Exception OnFolderSourceSizeError(int cmp, int count) =>
new InvalidOperationException(FormatSequenceLengthErrorMessage(cmp, count));
}
}