From 2f943e242bb8f9927bb8af0aa3d972e1429f922f Mon Sep 17 00:00:00 2001 From: Patrik Svensson Date: Wed, 29 May 2024 11:16:00 +0200 Subject: [PATCH] Implement IComparer for PathComparer --- .../Unit/PathComparerTests.cs | 34 +++++++++++++++++-- src/Spectre.IO/PathComparer.cs | 32 ++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Spectre.IO.Tests/Unit/PathComparerTests.cs b/src/Spectre.IO.Tests/Unit/PathComparerTests.cs index 5d9d8ff..77fb1c0 100644 --- a/src/Spectre.IO.Tests/Unit/PathComparerTests.cs +++ b/src/Spectre.IO.Tests/Unit/PathComparerTests.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; using Shouldly; using Spectre.IO.Internal; using Xunit; @@ -88,7 +91,8 @@ public void Different_Paths_Are_Not_Considered_Equal(bool isCaseSensitive) [Theory] [InlineData(true, false)] [InlineData(false, true)] - public void Same_Paths_But_Different_Casing_Are_Considered_Equal_Depending_On_Case_Sensitivity(bool isCaseSensitive, bool expected) + public void Same_Paths_But_Different_Casing_Are_Considered_Equal_Depending_On_Case_Sensitivity( + bool isCaseSensitive, bool expected) { // Given, When var comparer = new PathComparer(isCaseSensitive); @@ -101,6 +105,31 @@ public void Same_Paths_But_Different_Casing_Are_Considered_Equal_Depending_On_Ca } } + public sealed class TheCompareMethod + { + [Fact] + public void Should_Sort_Paths() + { + // Given + var paths = new List + { + new("foo/bar/qux"), + new("foo/bar/baz"), + new("foo/bar"), + }; + + // When + var result = paths + .Order(new PathComparer(isCaseSensitive: false)) + .ToList(); + + // Then + result[0].FullPath.ShouldBe("foo/bar"); + result[1].FullPath.ShouldBe("foo/bar/baz"); + result[2].FullPath.ShouldBe("foo/bar/qux"); + } + } + public sealed class TheGetHashCodeMethod { [Fact] @@ -156,7 +185,8 @@ public void Different_Paths_Get_Different_Hash_Codes(bool isCaseSensitive) [Theory] [InlineData(true, false)] [InlineData(false, true)] - public void Same_Paths_But_Different_Casing_Get_Same_Hash_Code_Depending_On_Case_Sensitivity(bool isCaseSensitive, bool expected) + public void Same_Paths_But_Different_Casing_Get_Same_Hash_Code_Depending_On_Case_Sensitivity( + bool isCaseSensitive, bool expected) { // Given, When var comparer = new PathComparer(isCaseSensitive); diff --git a/src/Spectre.IO/PathComparer.cs b/src/Spectre.IO/PathComparer.cs index 64b690a..ab78fc6 100644 --- a/src/Spectre.IO/PathComparer.cs +++ b/src/Spectre.IO/PathComparer.cs @@ -8,7 +8,7 @@ namespace Spectre.IO /// /// Compares instances. /// - public sealed class PathComparer : IEqualityComparer + public sealed class PathComparer : IEqualityComparer, IComparer { /// /// Gets the default path comparer. @@ -47,6 +47,36 @@ public PathComparer(IEnvironment environment) IsCaseSensitive = environment.Platform.IsUnix(); } + /// + public int Compare(Path? x, Path? y) + { + if (x == null && y == null) + { + return 0; + } + + if (x != null && y == null) + { + return -1; + } + + if (x == null && y != null) + { + return 1; + } + + if (IsCaseSensitive) + { + return StringComparer.Ordinal.Compare( + x!.FullPath, + y!.FullPath); + } + + return StringComparer.OrdinalIgnoreCase.Compare( + x!.FullPath, + y!.FullPath); + } + /// public bool Equals(Path? x, Path? y) {