Skip to content

Commit

Permalink
Use "EqualityComparer<T>.Create" from .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Nov 20, 2023
1 parent c349f1d commit a263661
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions MoreLinq.Test/EndsWithTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public void EndsWithUsesSpecifiedEqualityComparerOrDefault()

Assert.That(first.EndsWith(second), Is.False);
Assert.That(first.EndsWith(second, null), Is.False);
Assert.That(first.EndsWith(second, EqualityComparer.Create<int>(delegate { return false; })), Is.False);
Assert.That(first.EndsWith(second, EqualityComparer.Create<int>(delegate { return true; })), Is.True);
Assert.That(first.EndsWith(second, EqualityComparer<int>.Create(delegate { return false; })), Is.False);
Assert.That(first.EndsWith(second, EqualityComparer<int>.Create(delegate { return true; })), Is.True);
}

[TestCase(SourceKind.BreakingCollection)]
Expand Down
15 changes: 11 additions & 4 deletions MoreLinq.Test/EqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
// limitations under the License.
#endregion

#if !NET8_0_OR_GREATER

namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;

static class EqualityComparer
static class EqualityComparer<T>
{
public static System.Collections.Generic.EqualityComparer<T>
Default => System.Collections.Generic.EqualityComparer<T>.Default;

/// <summary>
/// Creates an <see cref="IEqualityComparer{T}"/> given a
/// <see cref="Func{T,T,Boolean}"/>.
/// </summary>

public static IEqualityComparer<T> Create<T>(Func<T?, T?, bool> comparer) =>
new DelegatingComparer<T>(comparer);
public static IEqualityComparer<T> Create(Func<T?, T?, bool> comparer) =>
new DelegatingComparer(comparer);

sealed class DelegatingComparer<T> : IEqualityComparer<T>
sealed class DelegatingComparer : IEqualityComparer<T>
{
readonly Func<T?, T?, bool> comparer;
readonly Func<T, int> hasher;
Expand All @@ -49,3 +54,5 @@ public DelegatingComparer(Func<T?, T?, bool> comparer)
}
}
}

#endif
2 changes: 1 addition & 1 deletion MoreLinq.Test/PermutationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void TestPermutationsAreIndependent()
static class SequenceEqualityComparer<T>
{
public static readonly IEqualityComparer<IEnumerable<T>> Instance =
EqualityComparer.Create<IEnumerable<T>>((x, y) => x is { } sx && y is { } sy && sx.SequenceEqual(sy));
EqualityComparer<IEnumerable<T>>.Create((x, y) => x is { } sx && y is { } sy && sx.SequenceEqual(sy));
}
}
}
4 changes: 2 additions & 2 deletions MoreLinq.Test/StartsWithTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public void StartsWithUsesSpecifiedEqualityComparerOrDefault()

Assert.That(first.StartsWith(second), Is.False);
Assert.That(first.StartsWith(second, null), Is.False);
Assert.That(first.StartsWith(second, EqualityComparer.Create<int>(delegate { return false; })), Is.False);
Assert.That(first.StartsWith(second, EqualityComparer.Create<int>(delegate { return true; })), Is.True);
Assert.That(first.StartsWith(second, EqualityComparer<int>.Create(delegate { return false; })), Is.False);
Assert.That(first.StartsWith(second, EqualityComparer<int>.Create(delegate { return true; })), Is.True);
}

[TestCase(SourceKind.BreakingCollection)]
Expand Down

0 comments on commit a263661

Please sign in to comment.