From 4315fdae045b456e08f9dcbcaf56f06389986378 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Mon, 16 Jan 2023 20:05:04 +0100 Subject: [PATCH] Consolidate error formatting between "AssertCount" & "Fold" --- MoreLinq.Test/AssertCountTest.cs | 14 +++++++++----- MoreLinq.Test/FoldTest.cs | 9 ++++++--- MoreLinq/AssertCount.cs | 12 +++++------- MoreLinq/Fold.cs | 9 ++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/MoreLinq.Test/AssertCountTest.cs b/MoreLinq.Test/AssertCountTest.cs index b2b165564..2a6e5ddf3 100644 --- a/MoreLinq.Test/AssertCountTest.cs +++ b/MoreLinq.Test/AssertCountTest.cs @@ -53,13 +53,17 @@ public void AssertCountLongSequence() Throws.TypeOf()); } - [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().With.Message.EqualTo(expectedMessage)); } diff --git a/MoreLinq.Test/FoldTest.cs b/MoreLinq.Test/FoldTest.cs index 25b2d8101..b6c2972f6 100644 --- a/MoreLinq.Test/FoldTest.cs +++ b/MoreLinq.Test/FoldTest.cs @@ -27,21 +27,24 @@ public class FoldTest public void FoldWithTooFewItems() { Assert.That(() => Enumerable.Range(1, 3).Fold(BreakingFunc.Of()), - Throws.TypeOf()); + Throws.TypeOf() + .And.Message.EqualTo("Sequence contains too few elements when exactly 4 were expected.")); } [Test] public void FoldWithEmptySequence() { Assert.That(() => Enumerable.Empty().Fold(BreakingFunc.Of()), - Throws.TypeOf()); + Throws.TypeOf() + .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()), - Throws.TypeOf()); + Throws.TypeOf() + .And.Message.EqualTo("Sequence contains too many elements when exactly 2 were expected.")); } [Test] diff --git a/MoreLinq/AssertCount.cs b/MoreLinq/AssertCount.cs index db4ff8e4f..f9467c7c1 100644 --- a/MoreLinq/AssertCount.cs +++ b/MoreLinq/AssertCount.cs @@ -71,13 +71,11 @@ public static IEnumerable AssertCount(this IEnumerable 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 diff --git a/MoreLinq/Fold.cs b/MoreLinq/Fold.cs index 31fa1bbef..4b77b4856 100644 --- a/MoreLinq/Fold.cs +++ b/MoreLinq/Fold.cs @@ -91,12 +91,7 @@ static TResult FoldImpl(IEnumerable source, int count, static readonly Func 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)); } }