Skip to content

Commit

Permalink
Consolidate error formatting between "AssertCount" & "Fold"
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Jan 16, 2023
1 parent 15f35f0 commit 4315fda
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
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));
}
}

0 comments on commit 4315fda

Please sign in to comment.