Skip to content

Commit

Permalink
Implement IComparer<Path?> for PathComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed May 29, 2024
1 parent e304a37 commit 2f943e2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/Spectre.IO.Tests/Unit/PathComparerTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<FilePath>
{
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]
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion src/Spectre.IO/PathComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Spectre.IO
/// <summary>
/// Compares <see cref="Path"/> instances.
/// </summary>
public sealed class PathComparer : IEqualityComparer<Path?>
public sealed class PathComparer : IEqualityComparer<Path?>, IComparer<Path?>
{
/// <summary>
/// Gets the default path comparer.
Expand Down Expand Up @@ -47,6 +47,36 @@ public PathComparer(IEnvironment environment)
IsCaseSensitive = environment.Platform.IsUnix();
}

/// <inheritdoc/>
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);
}

/// <inheritdoc/>
public bool Equals(Path? x, Path? y)
{
Expand Down

0 comments on commit 2f943e2

Please sign in to comment.