Unit tests for Path.Join overloads (dotnet/corefx#37285)
authorBlake Hensley <jbhensley@users.noreply.github.com>
Fri, 17 May 2019 19:29:18 +0000 (12:29 -0700)
committerAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Fri, 17 May 2019 19:29:18 +0000 (12:29 -0700)
* 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

src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs
src/libraries/System.Runtime.Extensions/tests/System/IO/PathTests_Join.netcoreapp.cs

index 5ee78a2..cf87b98 100644 (file)
@@ -1424,8 +1424,11 @@ namespace System.IO
         public static bool IsPathRooted(string path) { throw null; }
         public static string Join(System.ReadOnlySpan<char> path1, System.ReadOnlySpan<char> path2) { throw null; }
         public static string Join(System.ReadOnlySpan<char> path1, System.ReadOnlySpan<char> path2, System.ReadOnlySpan<char> path3) { throw null; }
+        public static string Join(System.ReadOnlySpan<char> path1, System.ReadOnlySpan<char> path2, System.ReadOnlySpan<char> path3, System.ReadOnlySpan<char> 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<char> path1, System.ReadOnlySpan<char> path2, System.ReadOnlySpan<char> path3, System.Span<char> destination, out int charsWritten) { throw null; }
         public static bool TryJoin(System.ReadOnlySpan<char> path1, System.ReadOnlySpan<char> path2, System.Span<char> destination, out int charsWritten) { throw null; }
     }
index 53f4b91..88146a8 100644 (file)
@@ -8,6 +8,15 @@ namespace System.IO.Tests
 {
     public class PathTests_Join : PathTestsBase
     {
+        public static TheoryData<string, string> TestData_JoinOnePath = new TheoryData<string, string>
+        {
+            { "", "" },
+            { Sep, Sep },
+            { AltSep, AltSep },
+            { "a", "a" },
+            { null, "" }
+        };
+
         public static TheoryData<string, string, string> TestData_JoinTwoPaths = new TheoryData<string, string, string>
         {
             { "", "", "" },
@@ -117,5 +126,111 @@ namespace System.IO.Tests
                 Assert.Equal(output, new char[output.Length]);
             }
         }
+
+        public static TheoryData<string, string, string, string, string> TestData_JoinFourPaths = new TheoryData<string, string, string, string, string>
+        {
+            { "", "", "", "", "" },
+            { 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<ArgumentNullException>(() => 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 }));
+        }
     }
 }