From 066fb698dfcd87f62a872e5931e5b8dd6085fa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 20 Apr 2024 23:43:46 +0200 Subject: [PATCH] feat: implement `HasExtension` and `TrimEndingDirectorySeparator` for simulated `Path` (#574) Implement the `HasExtension` and `TrimEndingDirectorySeparator` methods for `Path`. --- .../Helpers/Execute.SimulatedPath.cs | 16 ++++++++++++++-- .../ClassGenerators/FileSystemClassGenerator.cs | 2 ++ .../FileSystem/Path/HasExtensionTests.cs | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs index cca4ad123..3d2683235 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs @@ -319,7 +319,15 @@ public bool HasExtension(ReadOnlySpan path) /// public bool HasExtension([NotNullWhen(true)] string? path) - => System.IO.Path.HasExtension(path); + { + if (path == null) + { + return false; + } + + return TryGetExtensionIndex(path, out var dotIndex) + && dotIndex < path.Length - 1; + } #if FEATURE_SPAN /// @@ -406,7 +414,11 @@ public ReadOnlySpan TrimEndingDirectorySeparator(ReadOnlySpan path) #if FEATURE_PATH_ADVANCED /// public string TrimEndingDirectorySeparator(string path) - => System.IO.Path.TrimEndingDirectorySeparator(path); + { + return EndsInDirectorySeparator(path) && path.Length != GetRootLength(path) + ? path.Substring(0, path.Length - 1) + : path; + } #endif #if FEATURE_PATH_JOIN diff --git a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs index 56ee80963..a514bce74 100644 --- a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs +++ b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs @@ -273,9 +273,11 @@ private bool IncludeSimulatedTests(ClassModel @class) "GetPathRootTests", "GetRandomFileNameTests", "GetTempPathTests", + "HasExtensionTests", "IsPathRootedTests", "JoinTests", "Tests", + "TrimEndingDirectorySeparatorTests", "TryJoinTests" ]; return @class.Namespace diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Path/HasExtensionTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Path/HasExtensionTests.cs index a914c3a7b..d5fa9eaf2 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Path/HasExtensionTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Path/HasExtensionTests.cs @@ -14,6 +14,7 @@ public void HasExtension_Null_ShouldReturnFalse() } [SkippableTheory] + [InlineAutoData("abc.", false)] [InlineAutoData(".foo", true)] [InlineAutoData(".abc.xyz", true)] [InlineAutoData("foo", false)] @@ -30,6 +31,7 @@ public void HasExtension_ShouldReturnExpectedResult( #if FEATURE_SPAN [SkippableTheory] + [InlineAutoData("abc.", false)] [InlineAutoData(".foo", true)] [InlineAutoData(".abc.xyz", true)] [InlineAutoData("foo", false)]