Skip to content

Commit eba7b4e

Browse files
sovaForNeVeR
authored andcommitted
(#43) moved IsPrefixOf to IPath and added StartsWith to IPath
1 parent b82fb17 commit eba7b4e

5 files changed

Lines changed: 67 additions & 4 deletions

File tree

TruePath.Tests/AbsolutePathTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ namespace TruePath.Tests;
66

77
public class AbsolutePathTests
88
{
9+
[Theory]
10+
[InlineData("/home/user", "/home/user/documents")]
11+
[InlineData("/home/usEr", "/home/User/documents")]
12+
[InlineData("/home/user/documents", "/home/user/documents")]
13+
[InlineData("/home/user/documents", "/home/user")]
14+
public void IsPrefixOfShouldBeEquivalentToStartsWith(string pathA, string pathB)
15+
{
16+
if (OperatingSystem.IsWindows()) return;
17+
18+
// Arrange
19+
var a = new AbsolutePath(pathA);
20+
var b = new AbsolutePath(pathB);
21+
22+
// Act
23+
var isPrefix = a.IsPrefixOf(b);
24+
var startsWith = b.Value.StartsWith(a.Value);
25+
26+
// Assert
27+
Assert.Equal(isPrefix, startsWith);
28+
}
29+
930
[Fact]
1031
public void CurrentWorkingDirectoryShouldReturnCorrectAbsolutePath()
1132
{

TruePath.Tests/LocalPathTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ namespace TruePath.Tests;
66

77
public class LocalPathTests
88
{
9+
[Theory]
10+
[InlineData("user", "user/documents")]
11+
[InlineData("usEr", "User/documents")]
12+
[InlineData("user/documents", "user/documents")]
13+
[InlineData("user/documents", "user")]
14+
public void IsPrefixOfShouldBeEquivalentToStartsWith(string pathA, string pathB)
15+
{
16+
// Arrange
17+
var a = new LocalPath(pathA);
18+
var b = new LocalPath(pathB);
19+
20+
// Act
21+
var isPrefix = a.IsPrefixOf(b);
22+
var startsWith = b.Value.StartsWith(a.Value);
23+
24+
// Assert
25+
Assert.Equal(isPrefix, startsWith);
26+
}
27+
928
[Fact]
1029
public void AbsolutePathIsNormalizedOnCreation()
1130
{

TruePath/AbsolutePath.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public AbsolutePath(LocalPath localPath) : this(localPath.Value, checkAbsolutene
5454
/// <inheritdoc cref="IPath.FileName"/>
5555
public string FileName => Underlying.FileName;
5656

57+
/// <inheritdoc cref="IPath.StartsWith"/>
58+
public bool StartsWith(AbsolutePath other) => Value.StartsWith(other.Value);
59+
60+
/// <inheritdoc cref="IPath.IsPrefixOf"/>
61+
public bool IsPrefixOf(AbsolutePath other)
62+
{
63+
if (!(Value.Length <= other.Value.Length && other.Value.StartsWith(Value))) return false;
64+
return other.Value.Length == Value.Length || other.Value[Value.Length] == Path.DirectorySeparatorChar;
65+
}
66+
5767
/// <summary>
5868
/// Gets the current working directory as an AbsolutePath instance.
5969
/// </summary>

TruePath/IPath.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ public interface IPath<TPath> where TPath : IPath<TPath>
3434

3535
/// <inheritdoc cref="op_Division(TPath,LocalPath)"/>
3636
static abstract TPath operator /(TPath basePath, string appended);
37+
38+
/// <remarks>
39+
/// Checks for a non-strict prefix: if the paths are equal then they are still considered prefixes of each other.
40+
/// </remarks>
41+
/// <remarks>Note that currently this comparison is case-sensitive.</remarks>
42+
bool IsPrefixOf(TPath other);
43+
44+
/// <summary>
45+
/// Determines whether the current path starts with the specified path.
46+
/// </summary>
47+
/// <param name="other">The path to compare to the current path.</param>
48+
/// <remarks>Note that currently this comparison is case-sensitive.</remarks>
49+
bool StartsWith(TPath other);
3750
}

TruePath/LocalPath.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public override int GetHashCode()
6969
return !left.Equals(right);
7070
}
7171

72-
/// <remarks>
73-
/// Checks for a non-strict prefix: if the paths are equal then they are still considered prefixes of each other.
74-
/// </remarks>
75-
/// <remarks>Note that currently this comparison is case-sensitive.</remarks>
72+
/// <inheritdoc cref="IPath.StartsWith"/>
73+
public bool StartsWith(LocalPath other) => Value.StartsWith(other.Value);
74+
75+
/// <inheritdoc cref="IPath.IsPrefixOf"/>
7676
public bool IsPrefixOf(LocalPath other)
7777
{
7878
if (!(Value.Length <= other.Value.Length && other.Value.StartsWith(Value))) return false;

0 commit comments

Comments
 (0)