From 48973dc0bf1e4bde3449369da05ea55450a5913e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 20 Apr 2024 23:22:58 +0200 Subject: [PATCH 1/2] Add HasExtension --- .../Helpers/Execute.SimulatedPath.cs | 10 +++++++++- .../ClassGenerators/FileSystemClassGenerator.cs | 1 + .../FileSystem/Path/HasExtensionTests.cs | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs index a21c282b3..726a88959 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs @@ -342,7 +342,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 /// diff --git a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs index 13d82cabd..cd00b8d53 100644 --- a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs +++ b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs @@ -241,6 +241,7 @@ private bool IncludeSimulatedTests(ClassModel @class) "GetPathRootTests", "GetRandomFileNameTests", "GetTempPathTests", + "HasExtensionTests", "IsPathRootedTests", "JoinTests", "Tests", 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)] From 90a8b217a12f5385fa9eacb388ae733d765d2566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 20 Apr 2024 23:26:36 +0200 Subject: [PATCH 2/2] Add TrimEndingDirectorySeparator --- .../Helpers/Execute.SimulatedPath.cs | 6 +++++- .../ClassGenerators/FileSystemClassGenerator.cs | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs index 726a88959..d6a1e041d 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs @@ -430,7 +430,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 cd00b8d53..95777c8ce 100644 --- a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs +++ b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs @@ -245,6 +245,7 @@ private bool IncludeSimulatedTests(ClassModel @class) "IsPathRootedTests", "JoinTests", "Tests", + "TrimEndingDirectorySeparatorTests", "TryJoinTests" ]; return @class.Namespace