Skip to content

Commit 066fb69

Browse files
authored
feat: implement HasExtension and TrimEndingDirectorySeparator for simulated Path (#574)
Implement the `HasExtension` and `TrimEndingDirectorySeparator` methods for `Path`.
1 parent c9d4256 commit 066fb69

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,15 @@ public bool HasExtension(ReadOnlySpan<char> path)
319319

320320
/// <inheritdoc cref="IPath.HasExtension(string)" />
321321
public bool HasExtension([NotNullWhen(true)] string? path)
322-
=> System.IO.Path.HasExtension(path);
322+
{
323+
if (path == null)
324+
{
325+
return false;
326+
}
327+
328+
return TryGetExtensionIndex(path, out var dotIndex)
329+
&& dotIndex < path.Length - 1;
330+
}
323331

324332
#if FEATURE_SPAN
325333
/// <inheritdoc cref="IPath.IsPathFullyQualified(ReadOnlySpan{char})" />
@@ -406,7 +414,11 @@ public ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path)
406414
#if FEATURE_PATH_ADVANCED
407415
/// <inheritdoc cref="IPath.TrimEndingDirectorySeparator(string)" />
408416
public string TrimEndingDirectorySeparator(string path)
409-
=> System.IO.Path.TrimEndingDirectorySeparator(path);
417+
{
418+
return EndsInDirectorySeparator(path) && path.Length != GetRootLength(path)
419+
? path.Substring(0, path.Length - 1)
420+
: path;
421+
}
410422
#endif
411423

412424
#if FEATURE_PATH_JOIN

Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs

+2
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ private bool IncludeSimulatedTests(ClassModel @class)
273273
"GetPathRootTests",
274274
"GetRandomFileNameTests",
275275
"GetTempPathTests",
276+
"HasExtensionTests",
276277
"IsPathRootedTests",
277278
"JoinTests",
278279
"Tests",
280+
"TrimEndingDirectorySeparatorTests",
279281
"TryJoinTests"
280282
];
281283
return @class.Namespace

Tests/Testably.Abstractions.Tests/FileSystem/Path/HasExtensionTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public void HasExtension_Null_ShouldReturnFalse()
1414
}
1515

1616
[SkippableTheory]
17+
[InlineAutoData("abc.", false)]
1718
[InlineAutoData(".foo", true)]
1819
[InlineAutoData(".abc.xyz", true)]
1920
[InlineAutoData("foo", false)]
@@ -30,6 +31,7 @@ public void HasExtension_ShouldReturnExpectedResult(
3031

3132
#if FEATURE_SPAN
3233
[SkippableTheory]
34+
[InlineAutoData("abc.", false)]
3335
[InlineAutoData(".foo", true)]
3436
[InlineAutoData(".abc.xyz", true)]
3537
[InlineAutoData("foo", false)]

0 commit comments

Comments
 (0)