diff --git a/MoreLinq.Test/MaxByTest.cs b/MoreLinq.Test/MaxByTest.cs index 124ea7e30..3e9a7fc0e 100644 --- a/MoreLinq.Test/MaxByTest.cs +++ b/MoreLinq.Test/MaxByTest.cs @@ -68,16 +68,16 @@ public class First public void ReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).First(), Is.EqualTo("hello")); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("hello")); } [Test] public void WithComparerReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .First(), - Is.EqualTo("ax")); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("ax")); } [Test] @@ -85,7 +85,7 @@ public void WithEmptySourceThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MaxBy(s => s.Length).First()); + MoreEnumerable.First(strings.MaxBy(s => s.Length))); } [Test] @@ -93,7 +93,7 @@ public void WithEmptySourceWithComparerThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer).First()); + MoreEnumerable.First(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer))); } } @@ -103,32 +103,32 @@ public class FirstOrDefault public void ReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), Is.EqualTo("hello")); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("hello")); } [Test] public void WithComparerReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .FirstOrDefault(), - Is.EqualTo("ax")); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("ax")); } [Test] public void WithEmptySourceReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).FirstOrDefault(), Is.Null); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null); } [Test] public void WithEmptySourceWithComparerReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .FirstOrDefault(), - Is.Null); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null); } } @@ -138,16 +138,16 @@ public class Last public void ReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).Last(), Is.EqualTo("world")); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("world")); } [Test] public void WithComparerReturnsMaximumPerComparer() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .Last(), - Is.EqualTo("az")); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("az")); } [Test] @@ -155,7 +155,7 @@ public void WithEmptySourceThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MaxBy(s => s.Length).Last()); + MoreEnumerable.Last(strings.MaxBy(s => s.Length))); } [Test] @@ -163,7 +163,7 @@ public void WithEmptySourceWithComparerThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer).Last()); + MoreEnumerable.Last(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer))); } } @@ -173,32 +173,32 @@ public class LastOrDefault public void ReturnsMaximum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), Is.EqualTo("world")); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("world")); } [Test] public void WithComparerReturnsMaximumPerComparer() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .LastOrDefault(), - Is.EqualTo("az")); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("az")); } [Test] public void WithEmptySourceReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length).LastOrDefault(), Is.Null); + var maxima = strings.MaxBy(s => s.Length); + Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null); } [Test] public void WithEmptySourceWithComparerReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer) - .LastOrDefault(), - Is.Null); + var maxima = strings.MaxBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null); } } diff --git a/MoreLinq.Test/MinByTest.cs b/MoreLinq.Test/MinByTest.cs index 09c834d72..acd90eb01 100644 --- a/MoreLinq.Test/MinByTest.cs +++ b/MoreLinq.Test/MinByTest.cs @@ -68,16 +68,16 @@ public class First public void ReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).First(), Is.EqualTo("ax")); + var minima = MoreEnumerable.First(strings.MinBy(s => s.Length)); + Assert.That(minima, Is.EqualTo("ax")); } [Test] public void WithComparerReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .First(), - Is.EqualTo("hello")); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.First(minima), Is.EqualTo("hello")); } [Test] @@ -85,7 +85,7 @@ public void WithEmptySourceThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MinBy(s => s.Length).First()); + MoreEnumerable.First(strings.MinBy(s => s.Length))); } [Test] @@ -93,7 +93,7 @@ public void WithEmptySourceWithComparerThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer).First()); + MoreEnumerable.First(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer))); } } @@ -103,32 +103,32 @@ public class FirstOrDefault public void ReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).FirstOrDefault(), Is.EqualTo("ax")); + var minima = strings.MinBy(s => s.Length); + Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.EqualTo("ax")); } [Test] public void WithComparerReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .FirstOrDefault(), - Is.EqualTo("hello")); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.EqualTo("hello")); } [Test] public void WithEmptySourceReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).FirstOrDefault(), Is.Null); + var minima = strings.MinBy(s => s.Length); + Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.Null); } [Test] public void WithEmptySourceWithComparerReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .FirstOrDefault(), - Is.Null); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.FirstOrDefault(minima), Is.Null); } } @@ -138,16 +138,16 @@ public class Last public void ReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).Last(), Is.EqualTo("az")); + var minima = strings.MinBy(s => s.Length); + Assert.That(MoreEnumerable.Last(minima), Is.EqualTo("az")); } [Test] public void WithComparerReturnsMinimumPerComparer() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .Last(), - Is.EqualTo("world")); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.Last(minima), Is.EqualTo("world")); } [Test] @@ -155,7 +155,7 @@ public void WithEmptySourceThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MinBy(s => s.Length).Last()); + MoreEnumerable.Last(strings.MinBy(s => s.Length))); } [Test] @@ -163,7 +163,7 @@ public void WithEmptySourceWithComparerThrows() { using var strings = Enumerable.Empty().AsTestingSequence(); Assert.Throws(() => - strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer).Last()); + MoreEnumerable.Last(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer))); } } @@ -173,32 +173,32 @@ public class LastOrDefault public void ReturnsMinimum() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).LastOrDefault(), Is.EqualTo("az")); + var minima = strings.MinBy(s => s.Length); + Assert.That(MoreEnumerable.LastOrDefault(minima), Is.EqualTo("az")); } [Test] public void WithComparerReturnsMinimumPerComparer() { using var strings = SampleData.Strings.AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .LastOrDefault(), - Is.EqualTo("world")); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.LastOrDefault(minima), Is.EqualTo("world")); } [Test] public void WithEmptySourceReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length).LastOrDefault(), Is.Null); + var minima = strings.MinBy(s => s.Length); + Assert.That(MoreEnumerable.LastOrDefault(minima), Is.Null); } [Test] public void WithEmptySourceWithComparerReturnsDefault() { using var strings = Enumerable.Empty().AsTestingSequence(); - Assert.That(strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer) - .LastOrDefault(), - Is.Null); + var minima = strings.MinBy(s => s.Length, Comparable.DescendingOrderComparer); + Assert.That(MoreEnumerable.LastOrDefault(minima), Is.Null); } } diff --git a/MoreLinq/MaxBy.cs b/MoreLinq/MaxBy.cs index f3b502727..ce31a9ec9 100644 --- a/MoreLinq/MaxBy.cs +++ b/MoreLinq/MaxBy.cs @@ -126,7 +126,7 @@ public static T Last(this IExtremaEnumerable source) public static T LastOrDefault(this IExtremaEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); - return source.Take(1).AsEnumerable().LastOrDefault(); + return source.TakeLast(1).AsEnumerable().LastOrDefault(); } ///