Skip to content

Commit 4124902

Browse files
Add case-insensitive HasName() method (#76)
* Initial plan * Add case-insensitive HasName() method with tests and documentation Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com> * Update API snapshots to approve HasName() method Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com> * Merge branch 'main' into copilot/add-case-insensitive-hasname-method Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
1 parent 0640a34 commit 4124902

File tree

7 files changed

+51
-0
lines changed

7 files changed

+51
-0
lines changed

Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Pathy
2424
public static Pathy.ChainablePath Temp { get; }
2525
public Pathy.ChainablePath FindParentWithFileMatching(params string[] wildcards) { }
2626
public bool HasExtension(string extension) { }
27+
public bool HasName(string name) { }
2728
public Pathy.ChainablePath ToAbsolute() { }
2829
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2930
public System.IO.DirectoryInfo ToDirectoryInfo() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Pathy
2525
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
2626
public Pathy.ChainablePath FindParentWithFileMatching(params string[] wildcards) { }
2727
public bool HasExtension(string extension) { }
28+
public bool HasName(string name) { }
2829
public Pathy.ChainablePath ToAbsolute() { }
2930
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
3031
public System.IO.DirectoryInfo ToDirectoryInfo() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Pathy
2424
public static Pathy.ChainablePath Temp { get; }
2525
public Pathy.ChainablePath FindParentWithFileMatching(params string[] wildcards) { }
2626
public bool HasExtension(string extension) { }
27+
public bool HasName(string name) { }
2728
public Pathy.ChainablePath ToAbsolute() { }
2829
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2930
public System.IO.DirectoryInfo ToDirectoryInfo() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Pathy
2525
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
2626
public Pathy.ChainablePath FindParentWithFileMatching(params string[] wildcards) { }
2727
public bool HasExtension(string extension) { }
28+
public bool HasName(string name) { }
2829
public Pathy.ChainablePath ToAbsolute() { }
2930
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
3031
public System.IO.DirectoryInfo ToDirectoryInfo() { }

Pathy.Specs/ChainablePathSpecs.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ public void Checking_for_an_extension_requires_a_valid_extension(string extensio
417417
act.Should().Throw<ArgumentException>("*null*empty*");
418418
}
419419

420+
[Theory]
421+
[InlineData("SomeFile.txt", true)]
422+
[InlineData("somefile.txt", true)]
423+
[InlineData("SOMEFILE.TXT", true)]
424+
[InlineData("SomeFile", false)]
425+
[InlineData("OtherFile.txt", false)]
426+
public void Can_check_for_a_name(string name, bool shouldMatch)
427+
{
428+
// Act
429+
var path = ChainablePath.Temp / "SomeFile.txt";
430+
431+
// Assert
432+
path.HasName(name).Should().Be(shouldMatch);
433+
}
434+
435+
[Theory]
436+
[InlineData(null)]
437+
[InlineData("")]
438+
public void Checking_for_a_name_requires_a_valid_name(string name)
439+
{
440+
// Arrange
441+
var path = ChainablePath.Temp / "SomeFile.txt";
442+
443+
// Act
444+
Action act = () => path.HasName(name);
445+
446+
// Assert
447+
act.Should().Throw<ArgumentException>("*null*empty*");
448+
}
449+
420450
#if NET6_0_OR_GREATER
421451
[Fact]
422452
public void Can_get_the_difference_as_a_relative_path()

Pathy/ChainablePath.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,22 @@ public bool HasExtension(string extension)
448448
return string.Equals(Extension, extension, StringComparison.OrdinalIgnoreCase);
449449
}
450450

451+
/// <summary>
452+
/// Determines if the current path has the specified file or directory name (case-insensitive).
453+
/// </summary>
454+
/// <param name="name">
455+
/// The name to check for (e.g., "MyFile.txt").
456+
/// </param>
457+
public bool HasName(string name)
458+
{
459+
if (string.IsNullOrEmpty(name))
460+
{
461+
throw new ArgumentException("Name cannot be null or empty", nameof(name));
462+
}
463+
464+
return string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
465+
}
466+
451467
/// <summary>
452468
/// Converts the current <see cref="ChainablePath"/> to an absolute path using the current working directory.
453469
/// </summary>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Given an instance of `ChainablePath`, you can get a lot of useful information:
118118
* Not sure if a path points to an actual file system entry? Use `IsFile`, `IsDirectory` or `Exists`
119119
* Want to know the delta between two paths? Use `AsRelativeTo`.
120120
* To determine if a file has a case-insensitive extension, use `HasExtension(".txt")` or `HasExtension("txt")`.
121+
* To check if a path has a specific file or directory name (case-insensitive), use `HasName("MyFile.txt")`.
121122
* Get the last write time in UTC using `LastWriteTimeUtc` for both files and directories.
122123

123124
And if the built-in functionality really isn't enough, you can always call `ToDirectoryInfo` or `ToFileInfo` to continue with an instance of `DirectoryInfo` and `FileInfo`.

0 commit comments

Comments
 (0)