Example #1
0
        public void TestGetLastDirectoryName()
        {
            foreach (var testCase in Pairs(
                         @"a\b\c", "b",
                         @"a\b\", "b",
                         @"a\b\c\", "c",
                         @"a\b\.\c", "b",
                         @"a\b\.\.\.\.\.\.\c", "b"
                         ))
            {
                foreach (var scheme in new[] { "", "C:\\", "\\", ".\\", "\\\\share\\root\\", "ftp://" })
                {
                    var path = scheme + testCase.Item1;

                    Assert.AreEqual(testCase.Item2, CommonUtils.GetLastDirectoryName(path), "Path: " + path);

                    if (path.IndexOf('.') >= 0)
                    {
                        // Path.GetFileName will always fail on these, so don't
                        // even bother testing.
                        continue;
                    }

                    string ioPathResult;
                    try {
                        ioPathResult = Path.GetFileName(CommonUtils.TrimEndSeparator(Path.GetDirectoryName(path)));
                    } catch (ArgumentException) {
                        continue;
                    }

                    Assert.AreEqual(
                        CommonUtils.GetLastDirectoryName(path),
                        ioPathResult ?? string.Empty,
                        "Did not match Path.GetFileName(...) result for " + path
                        );
                }
            }
        }