Skip to content

Commit

Permalink
Distinguish Min/MaxBy from net6.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Nov 17, 2021
1 parent 32092f5 commit df3aeb9
Show file tree
Hide file tree
Showing 8 changed files with 641 additions and 387 deletions.
75 changes: 39 additions & 36 deletions MoreLinq.Test/MaxByTest.cs → MoreLinq.Test/MaxElementsByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,47 @@ namespace MoreLinq.Test
using System;
using NUnit.Framework;

using static MoreLinq.Extensions.MaxByExtension;
using static MoreLinq.Extensions.MaxElementsByExtension;

[TestFixture]
public class MaxByTest
public class MaxElementsByTest
{
[Test]
public void MaxByIsLazy()
public void MaxElementsByIsLazy()
{
new BreakingSequence<int>().MaxBy(BreakingFunc.Of<int, int>());
new BreakingSequence<int>().MaxElementsBy(BreakingFunc.Of<int, int>());
}

[Test]
public void MaxByReturnsMaxima()
public void MaxElementsByReturnsMaxima()
{
Assert.AreEqual(new[] { "hello", "world" },
SampleData.Strings.MaxBy(x => x.Length));
SampleData.Strings.MaxElementsBy(x => x.Length));
}

[Test]
public void MaxByNullComparer()
public void MaxElementsByNullComparer()
{
Assert.AreEqual(SampleData.Strings.MaxBy(x => x.Length),
SampleData.Strings.MaxBy(x => x.Length, null));
Assert.AreEqual(SampleData.Strings.MaxElementsBy(x => x.Length),
SampleData.Strings.MaxElementsBy(x => x.Length, null));
}

[Test]
public void MaxByEmptySequence()
public void MaxElementsByEmptySequence()
{
Assert.That(new string[0].MaxBy(x => x.Length), Is.Empty);
Assert.That(new string[0].MaxElementsBy(x => x.Length), Is.Empty);
}

[Test]
public void MaxByWithNaturalComparer()
public void MaxElementsByWithNaturalComparer()
{
Assert.AreEqual(new[] { "az" }, SampleData.Strings.MaxBy(x => x[1]));
Assert.AreEqual(new[] { "az" }, SampleData.Strings.MaxElementsBy(x => x[1]));
}

[Test]
public void MaxByWithComparer()
public void MaxElementsByWithComparer()
{
Assert.AreEqual(new[] { "aa" }, SampleData.Strings.MaxBy(x => x[1], Comparable<char>.DescendingOrderComparer));
Assert.AreEqual(new[] { "aa" }, SampleData.Strings.MaxElementsBy(x => x[1], Comparable<char>.DescendingOrderComparer));
}

public class First
Expand All @@ -69,15 +69,15 @@ public class First
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("hello"));
}

[Test]
public void WithComparerReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.First(maxima), Is.EqualTo("ax"));
}

Expand All @@ -86,15 +86,15 @@ public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
MoreEnumerable.First(strings.MaxBy(s => s.Length)));
MoreEnumerable.First(strings.MaxElementsBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
MoreEnumerable.First(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
MoreEnumerable.First(strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -104,31 +104,31 @@ public class FirstOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("hello"));
}

[Test]
public void WithComparerReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.EqualTo("ax"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.FirstOrDefault(maxima), Is.Null);
}
}
Expand All @@ -139,15 +139,15 @@ public class Last
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("world"));
}

[Test]
public void WithComparerReturnsMaximumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.Last(maxima), Is.EqualTo("az"));
}

Expand All @@ -156,15 +156,15 @@ public void WithEmptySourceThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
MoreEnumerable.Last(strings.MaxBy(s => s.Length)));
MoreEnumerable.Last(strings.MaxElementsBy(s => s.Length)));
}

[Test]
public void WithEmptySourceWithComparerThrows()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
Assert.Throws<InvalidOperationException>(() =>
MoreEnumerable.Last(strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
MoreEnumerable.Last(strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer)));
}
}

Expand All @@ -174,31 +174,31 @@ public class LastOrDefault
public void ReturnsMaximum()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("world"));
}

[Test]
public void WithComparerReturnsMaximumPerComparer()
{
using var strings = SampleData.Strings.AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.EqualTo("az"));
}

[Test]
public void WithEmptySourceReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length);
var maxima = strings.MaxElementsBy(s => s.Length);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}

[Test]
public void WithEmptySourceWithComparerReturnsDefault()
{
using var strings = Enumerable.Empty<string>().AsTestingSequence();
var maxima = strings.MaxBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
var maxima = strings.MaxElementsBy(s => s.Length, Comparable<int>.DescendingOrderComparer);
Assert.That(MoreEnumerable.LastOrDefault(maxima), Is.Null);
}
}
Expand All @@ -212,7 +212,7 @@ public class Take
public string[] ReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s.Length).Take(count).ToArray();
return strings.MaxElementsBy(s => s.Length).Take(count).ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -226,7 +226,7 @@ public string[] ReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s[index], Comparable<char>.DescendingOrderComparer)
return strings.MaxElementsBy(s => s[index], Comparable<char>.DescendingOrderComparer)
.Take(count)
.ToArray();
}
Expand All @@ -241,7 +241,7 @@ public class TakeLast
public string[] TakeLastReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s.Length).TakeLast(count).ToArray();
return strings.MaxElementsBy(s => s.Length).TakeLast(count).ToArray();
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -255,7 +255,7 @@ public string[] TakeLastReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.MaxBy(s => s[index], Comparable<char>.DescendingOrderComparer)
return strings.MaxElementsBy(s => s[index], Comparable<char>.DescendingOrderComparer)
.TakeLast(count)
.ToArray();
}
Expand All @@ -270,9 +270,12 @@ namespace Linq

public static partial class BuildTest
{
public static void MaxByCanBuildWithSystemLinq()
public static void MaxElementsByCanBuildWithSystemLinq()
{
#pragma warning disable CS0618
new int[0].MaxBy(x => 1);
new int[0].MaxElementsBy(x => 1);
#pragma warning restore CS0618
}
}
}
Loading

0 comments on commit df3aeb9

Please sign in to comment.