From 553631c1b5c444083cbb04ece15c6b6dbe89a404 Mon Sep 17 00:00:00 2001 From: Blake Hensley Date: Fri, 17 May 2019 12:29:18 -0700 Subject: [PATCH] Unit tests for Path.Join overloads (dotnet/corefx#37285) * Unit tests for Path.Join overloads * Add more unit tests * Minor mod to JoinStringArray_8 test * Add unit test for zero-length array * Use proper const for zero-len string * Added unit test for array of one element * Format fix: missing/extra space Commit migrated from https://github.com/dotnet/corefx/commit/a4da51f02f194bdedc9320a50aa63b72411488b6 --- .../ref/System.Runtime.Extensions.cs | 3 + .../tests/System/IO/PathTests_Join.netcoreapp.cs | 115 +++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs index 5ee78a2..cf87b98 100644 --- a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs +++ b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs @@ -1424,8 +1424,11 @@ namespace System.IO public static bool IsPathRooted(string path) { throw null; } public static string Join(System.ReadOnlySpan path1, System.ReadOnlySpan path2) { throw null; } public static string Join(System.ReadOnlySpan path1, System.ReadOnlySpan path2, System.ReadOnlySpan path3) { throw null; } + public static string Join(System.ReadOnlySpan path1, System.ReadOnlySpan path2, System.ReadOnlySpan path3, System.ReadOnlySpan path4) { throw null; } public static string Join(string path1, string path2) { throw null; } public static string Join(string path1, string path2, string path3) { throw null; } + public static string Join(string path1, string path2, string path3, string path4) { throw null; } + public static string Join(params string[] paths) { throw null; } public static bool TryJoin(System.ReadOnlySpan path1, System.ReadOnlySpan path2, System.ReadOnlySpan path3, System.Span destination, out int charsWritten) { throw null; } public static bool TryJoin(System.ReadOnlySpan path1, System.ReadOnlySpan path2, System.Span destination, out int charsWritten) { throw null; } } diff --git a/src/libraries/System.Runtime.Extensions/tests/System/IO/PathTests_Join.netcoreapp.cs b/src/libraries/System.Runtime.Extensions/tests/System/IO/PathTests_Join.netcoreapp.cs index 53f4b91..88146a8 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/IO/PathTests_Join.netcoreapp.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/IO/PathTests_Join.netcoreapp.cs @@ -8,6 +8,15 @@ namespace System.IO.Tests { public class PathTests_Join : PathTestsBase { + public static TheoryData TestData_JoinOnePath = new TheoryData + { + { "", "" }, + { Sep, Sep }, + { AltSep, AltSep }, + { "a", "a" }, + { null, "" } + }; + public static TheoryData TestData_JoinTwoPaths = new TheoryData { { "", "", "" }, @@ -117,5 +126,111 @@ namespace System.IO.Tests Assert.Equal(output, new char[output.Length]); } } + + public static TheoryData TestData_JoinFourPaths = new TheoryData + { + { "", "", "", "", "" }, + { Sep, Sep, Sep, Sep, $"{Sep}{Sep}{Sep}{Sep}" }, + { AltSep, AltSep, AltSep, AltSep, $"{AltSep}{AltSep}{AltSep}{AltSep}" }, + { "a", "", "", "", "a" }, + { "", "a", "", "", "a" }, + { "", "", "a", "", "a" }, + { "", "", "", "a", "a" }, + { "a", "b", "", "", $"a{Sep}b" }, + { "a", "", "b", "", $"a{Sep}b" }, + { "a", "", "", "b", $"a{Sep}b" }, + { "a", "b", "c", "", $"a{Sep}b{Sep}c" }, + { "a", "b", "", "c", $"a{Sep}b{Sep}c" }, + { "a", "", "b", "c", $"a{Sep}b{Sep}c" }, + { "", "a", "b", "c", $"a{Sep}b{Sep}c" }, + { "a", "b", "c", "d", $"a{Sep}b{Sep}c{Sep}d" }, + { "a", Sep, "b", "", $"a{Sep}b" }, + { "a", Sep, "", "b", $"a{Sep}b" }, + { "a", "", Sep, "b", $"a{Sep}b" }, + { $"a{Sep}", "b", "", "", $"a{Sep}b" }, + { $"a{Sep}", "", "b", "", $"a{Sep}b" }, + { $"a{Sep}", "", "", "b", $"a{Sep}b" }, + { "", $"a{Sep}", "b", "", $"a{Sep}b" }, + { "", $"a{Sep}", "", "b", $"a{Sep}b" }, + { "", "", $"a{Sep}", "b", $"a{Sep}b" }, + { "a", $"{Sep}b", "", "", $"a{Sep}b" }, + { "a", "", $"{Sep}b", "", $"a{Sep}b" }, + { "a", "", "", $"{Sep}b", $"a{Sep}b" }, + { $"{Sep}a", "", "", "", $"{Sep}a" }, + { "", $"{Sep}a", "", "", $"{Sep}a" }, + { "", "", $"{Sep}a", "", $"{Sep}a" }, + { "", "", "", $"{Sep}a", $"{Sep}a" }, + { $"{Sep}a", "b", "", "", $"{Sep}a{Sep}b" }, + { "", $"{Sep}a", "b", "", $"{Sep}a{Sep}b" }, + { "", "", $"{Sep}a", "b", $"{Sep}a{Sep}b" }, + { $"a{Sep}", $"{Sep}b", "", "", $"a{Sep}{Sep}b" }, + { $"a{Sep}", "", $"{Sep}b", "", $"a{Sep}{Sep}b" }, + { $"a{Sep}", "", "", $"{Sep}b", $"a{Sep}{Sep}b" }, + { $"a{AltSep}", "b", "", "", $"a{AltSep}b" }, + { $"a{AltSep}", "", "b", "", $"a{AltSep}b" }, + { $"a{AltSep}", "", "", "b", $"a{AltSep}b" }, + { "", $"a{AltSep}", "b", "", $"a{AltSep}b" }, + { "", $"a{AltSep}", "", "b", $"a{AltSep}b" }, + { "", "", $"a{AltSep}", "b", $"a{AltSep}b" }, + { "a", $"{AltSep}b", "", "", $"a{AltSep}b" }, + { "a", "", $"{AltSep}b", "", $"a{AltSep}b" }, + { "a", "", "", $"{AltSep}b", $"a{AltSep}b" }, + { null, null, null, null, "" }, + { "a", null, null, null, "a" }, + { null, "a", null, null, "a" }, + { null, null, "a", null, "a" }, + { null, null, null, "a", "a" }, + { "a", null, "b", null, $"a{Sep}b" }, + { "a", null, null, "b", $"a{Sep}b" } + }; + + [Theory, MemberData(nameof(TestData_JoinFourPaths))] + public void JoinFourPaths(string path1, string path2, string path3, string path4, string expected) + { + Assert.Equal(expected, Path.Join(path1.AsSpan(), path2.AsSpan(), path3.AsSpan(), path4.AsSpan())); + Assert.Equal(expected, Path.Join(path1, path2, path3, path4)); + } + + [Fact] + public void JoinStringArray_ThrowsArugmentNullException() + { + Assert.Throws(() => Path.Join(null)); + } + + [Fact] + public void JoinStringArray_ZeroLengthArray() + { + Assert.Equal(string.Empty, Path.Join(new string[0])); + } + + [Theory, MemberData(nameof(TestData_JoinOnePath))] + public void JoinStringArray_1(string path1, string expected) + { + Assert.Equal(expected, Path.Join(new string[] { path1 })); + } + + [Theory, MemberData(nameof(TestData_JoinTwoPaths))] + public void JoinStringArray_2(string path1, string path2, string expected) + { + Assert.Equal(expected, Path.Join(new string[] { path1, path2 })); + } + + [Theory, MemberData(nameof(TestData_JoinThreePaths))] + public void JoinStringArray_3(string path1, string path2, string path3, string expected) + { + Assert.Equal(expected, Path.Join(new string[] { path1, path2, path3 })); + } + + [Theory, MemberData(nameof(TestData_JoinFourPaths))] + public void JoinStringArray_4(string path1, string path2, string path3, string path4, string expected) + { + Assert.Equal(expected, Path.Join(new string[] { path1, path2, path3, path4 })); + } + + [Theory, MemberData(nameof(TestData_JoinFourPaths))] + public void JoinStringArray_8(string path1, string path2, string path3, string path4, string fourJoined) + { + Assert.Equal(Path.Join(fourJoined, fourJoined), Path.Join(new string[] { path1, path2, path3, path4, path1, path2, path3, path4 })); + } } } -- 2.7.4