Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Expose Path.Join and tests (#27522)
Browse files Browse the repository at this point in the history
* Expose Path.Join and test. Depends on dotnet/coreclr#16561

* Update for final API review changes

* Address feedback

* Address more feedback.
  • Loading branch information
JeremyKuhne authored Feb 28, 2018
1 parent 9af716e commit 0a092a2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,10 @@ public static partial class Path
public static bool IsPathFullyQualified(string path) { throw null; }
public static bool IsPathFullyQualified(ReadOnlySpan<char> path) { throw null; }
public static string GetRelativePath(string relativeTo, string path) { throw null; }
public static string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2) { throw null; }
public static string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3) { throw null; }
public static bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, Span<char> destination, out int charsWritten) { throw null; }
public static bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, Span<char> destination, out int charsWritten) { throw null; }
}

public partial class BinaryReader : System.IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="System\IO\PathTests_Windows.netcoreapp.cs" />
<Compile Include="System\IO\PathTests.netcoreapp.cs" />
<Compile Include="System\IO\PathTests_Unix.cs" />
<Compile Include="System\IO\PathTests_Join.netcoreapp.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)'!='netstandard'">
<Compile Include="System\BitConverterSpan.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace System.IO.Tests
{
public partial class PathTestsBase
{
protected static string Sep = Path.DirectorySeparatorChar.ToString();
protected static string AltSep = Path.AltDirectorySeparatorChar.ToString();

public static TheoryData<string> TestData_EmbeddedNull => new TheoryData<string>
{
"a\0b"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.IO.Tests
{
public class PathTests_Join : PathTestsBase
{
public static TheoryData<string, string, string> TestData_JoinTwoPaths = new TheoryData<string, string, string>
{
{ "", "", "" },
{ Sep, "", Sep },
{ AltSep, "", AltSep },
{ "", Sep, Sep },
{ "", AltSep, AltSep },
{ Sep, Sep, $"{Sep}{Sep}" },
{ AltSep, AltSep, $"{AltSep}{AltSep}" },
{ "a", "", "a" },
{ "", "a", "a" },
{ "a", "a", $"a{Sep}a" },
{ $"a{Sep}", "a", $"a{Sep}a" },
{ "a", $"{Sep}a", $"a{Sep}a" },
{ $"a{Sep}", $"{Sep}a", $"a{Sep}{Sep}a" },
{ "a", $"a{Sep}", $"a{Sep}a{Sep}" },
{ $"a{AltSep}", "a", $"a{AltSep}a" },
{ "a", $"{AltSep}a", $"a{AltSep}a" },
{ $"a{Sep}", $"{AltSep}a", $"a{Sep}{AltSep}a" },
{ $"a{AltSep}", $"{AltSep}a", $"a{AltSep}{AltSep}a" },
{ "a", $"a{AltSep}", $"a{Sep}a{AltSep}" },
};

[Theory, MemberData(nameof(TestData_JoinTwoPaths))]
public void JoinTwoPaths(string path1, string path2, string expected)
{
Assert.Equal(expected, Path.Join(path1, path2));
}

[Theory, MemberData(nameof(TestData_JoinTwoPaths))]
public void TryJoinTwoPaths(string path1, string path2, string expected)
{
char[] output = new char[expected.Length];

Assert.True(Path.TryJoin(path1, path2, output, out int written));
Assert.Equal(expected.Length, written);
Assert.Equal(expected, new string(output));

if (expected.Length > 0)
{
Assert.False(Path.TryJoin(path1, path2, Span<char>.Empty, out written));
Assert.Equal(0, written);

output = new char[expected.Length - 1];
Assert.False(Path.TryJoin(path1, path2, output, out written));
Assert.Equal(0, written);
Assert.Equal(output, new char[output.Length]);
}
}

public static TheoryData<string, string, string, string> TestData_JoinThreePaths = new TheoryData<string, string, string, string>
{
{ "", "", "", "" },
{ Sep, Sep, Sep, $"{Sep}{Sep}{Sep}" },
{ AltSep, AltSep, AltSep, $"{AltSep}{AltSep}{AltSep}" },
{ "a", "", "", "a" },
{ "", "a", "", "a" },
{ "", "", "a", "a" },
{ "a", "", "a", $"a{Sep}a" },
{ "a", "a", "", $"a{Sep}a" },
{ "", "a", "a", $"a{Sep}a" },
{ "a", "a", "a", $"a{Sep}a{Sep}a" },
{ "a", Sep, "a", $"a{Sep}a" },
{ $"a{Sep}", "", "a", $"a{Sep}a" },
{ $"a{Sep}", "a", "", $"a{Sep}a" },
{ "", $"a{Sep}", "a", $"a{Sep}a" },
{ "a", "", $"{Sep}a", $"a{Sep}a" },
{ $"a{AltSep}", "", "a", $"a{AltSep}a" },
{ $"a{AltSep}", "a", "", $"a{AltSep}a" },
{ "", $"a{AltSep}", "a", $"a{AltSep}a" },
{ "a", "", $"{AltSep}a", $"a{AltSep}a" },
};

[Theory, MemberData(nameof(TestData_JoinThreePaths))]
public void JoinThreePaths(string path1, string path2, string path3, string expected)
{
Assert.Equal(expected, Path.Join(path1, path2, path3));
}

[Theory, MemberData(nameof(TestData_JoinThreePaths))]
public void TryJoinThreePaths(string path1, string path2, string path3, string expected)
{
char[] output = new char[expected.Length];

Assert.True(Path.TryJoin(path1, path2, path3, output, out int written));
Assert.Equal(expected.Length, written);
Assert.Equal(expected, new string(output));

if (expected.Length > 0)
{
Assert.False(Path.TryJoin(path1, path2, path3, Span<char>.Empty, out written));
Assert.Equal(0, written);

output = new char[expected.Length - 1];
Assert.False(Path.TryJoin(path1, path2, path3, output, out written));
Assert.Equal(0, written);
Assert.Equal(output, new char[output.Length]);
}
}
}
}

0 comments on commit 0a092a2

Please sign in to comment.