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

                    Assert.AreEqual(expected, CommonUtils.GetFileOrDirectoryName(path), "Path: " + path);

                    if (scheme.Contains("://"))
                    {
                        // Path.GetFileName will always fail on these, so don't
                        // even bother testing.
                        continue;
                    }

                    string ioPathResult;
                    try {
                        ioPathResult = CommonUtils.GetFileOrDirectoryName(path);
                    } catch (ArgumentException) {
                        continue;
                    }

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