forked from ForNeVeR/TruePath
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ForNeVeR#20 Improve case-sensitive path comparison
- Loading branch information
Showing
10 changed files
with
335 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
namespace TruePath.Tests; | ||
|
||
public partial class AbsolutePathTests | ||
{ | ||
[Fact] | ||
public void EqualsUseStrictStringPathComparer_SamePaths_True() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = currentDirectory; | ||
|
||
var path1 = new AbsolutePath(currentDirectory); | ||
var path2 = new AbsolutePath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer); | ||
|
||
// Assert | ||
Assert.True(equals); | ||
} | ||
|
||
[Fact] | ||
public void EqualsUseStrictStringPathComparer_NotSamePaths_False() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new AbsolutePath(currentDirectory); | ||
var path2 = new AbsolutePath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void OnLinux_EqualsDefault_CaseSensitive_False() | ||
{ | ||
// Arrange | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return; | ||
} | ||
|
||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new AbsolutePath(currentDirectory); | ||
var path2 = new AbsolutePath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void EqualsNull_False() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = currentDirectory; | ||
|
||
var path1 = new AbsolutePath(currentDirectory); | ||
var path2 = new AbsolutePath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, null); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void OnWindowsOrOsx_EqualsDefault_CaseInsensitive_True() | ||
{ | ||
// Arrange | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return; | ||
} | ||
|
||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new AbsolutePath(currentDirectory); | ||
var path2 = new AbsolutePath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2); | ||
|
||
// Assert | ||
Assert.True(equals); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
namespace TruePath.Tests; | ||
|
||
public partial class LocalPathTests | ||
{ | ||
[Fact] | ||
public void EqualsUseStrictStringPathComparer_SamePaths_True() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = currentDirectory; | ||
|
||
var path1 = new LocalPath(currentDirectory); | ||
var path2 = new LocalPath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer); | ||
|
||
// Assert | ||
Assert.True(equals); | ||
} | ||
|
||
[Fact] | ||
public void EqualsUseStrictStringPathComparer_NotSamePaths_False() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new LocalPath(currentDirectory); | ||
var path2 = new LocalPath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void OnLinux_EqualsDefault_CaseSensitive_False() | ||
{ | ||
// Arrange | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return; | ||
} | ||
|
||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new LocalPath(currentDirectory); | ||
var path2 = new LocalPath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void EqualsNull_False() | ||
{ | ||
// Arrange | ||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = currentDirectory; | ||
|
||
var path1 = new LocalPath(currentDirectory); | ||
var path2 = new LocalPath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2, null); | ||
|
||
// Assert | ||
Assert.False(equals); | ||
} | ||
|
||
[Fact] | ||
public void OnWindowsOrOsx_EqualsDefault_CaseInsensitive_True() | ||
{ | ||
// Arrange | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return; | ||
} | ||
|
||
var currentDirectory = Environment.CurrentDirectory; | ||
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray()); | ||
|
||
var path1 = new LocalPath(currentDirectory); | ||
var path2 = new LocalPath(nonCanonicalPath); | ||
|
||
// Act | ||
var equals = path1.Equals(path2); | ||
|
||
// Assert | ||
Assert.True(equals); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace TruePath.Tests; | ||
|
||
public static class Utils | ||
{ | ||
internal static IEnumerable<char> MakeNonCanonicalPath(this string path) | ||
{ | ||
foreach (var @char in path) | ||
{ | ||
if (char.IsLetter(@char) && Random.Shared.NextSingle() >= 0.5) | ||
{ | ||
yield return char.ToUpper(@char); | ||
continue; | ||
} | ||
|
||
yield return @char; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace TruePath; | ||
|
||
/// <summary> | ||
/// Provides a platform-specific string comparer for comparing file paths. | ||
/// </summary> | ||
public class PlatformDefaultPathComparer : IComparer<string> | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of the <see cref="PlatformDefaultPathComparer"/> class. | ||
/// </summary> | ||
public static readonly PlatformDefaultPathComparer Comparer = new(); | ||
|
||
private readonly StringComparer comparisonType; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlatformDefaultPathComparer"/> class. | ||
/// </summary> | ||
public PlatformDefaultPathComparer() | ||
{ | ||
// Определяем тип сравнения в зависимости от платформы | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
comparisonType = StringComparer.OrdinalIgnoreCase; | ||
} | ||
else | ||
{ | ||
comparisonType = StringComparer.Ordinal; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Compares two strings and returns an integer that indicates their relative position in the sort order. | ||
/// </summary> | ||
/// <param name="x">The first string to compare.</param> | ||
/// <param name="y">The second string to compare.</param> | ||
/// <returns> | ||
/// A value less than zero if <paramref name="x"/> is less than <paramref name="y"/>; zero if <paramref name="x"/> equals <paramref name="y"/>; a value greater than zero if <paramref name="x"/> is greater than <paramref name="y"/>. | ||
/// </returns> | ||
public int Compare(string? x, string? y) | ||
{ | ||
return comparisonType.Compare(x, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace TruePath; | ||
|
||
/// <summary> | ||
/// Provides a strict string comparer for comparing file paths using ordinal comparison. | ||
/// </summary> | ||
public class StrictStringPathComparer : IComparer<string> | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of the <see cref="StrictStringPathComparer"/> class. | ||
/// </summary> | ||
public static readonly StrictStringPathComparer Comparer = new(); | ||
|
||
/// <summary> | ||
/// Compares two strings and returns an integer that indicates their relative position in the sort order using ordinal comparison. | ||
/// </summary> | ||
/// <param name="x">The first string to compare.</param> | ||
/// <param name="y">The second string to compare.</param> | ||
/// <returns> | ||
/// A value less than zero if <paramref name="x"/> is less than <paramref name="y"/>; zero if <paramref name="x"/> equals <paramref name="y"/>; a value greater than zero if <paramref name="x"/> is greater than <paramref name="y"/>. | ||
/// </returns> | ||
public int Compare(string? x, string? y) | ||
{ | ||
return StringComparer.Ordinal.Compare(x, y); | ||
} | ||
} |