Custom illegal character replacement

This commit is contained in:
Michael Bucari-Tovo 2022-06-23 13:01:24 -06:00
parent 184ba84600
commit 2ab466c570
24 changed files with 838 additions and 218 deletions

View file

@ -12,49 +12,88 @@ namespace FileUtilityTests
[TestClass]
public class GetSafePath
{
[TestMethod]
public void null_path_throws() => Assert.ThrowsException<ArgumentNullException>(() => FileUtility.GetSafePath(null));
static readonly ReplacementCharacters Default = ReplacementCharacters.Default;
static readonly ReplacementCharacters LoFiDefault = ReplacementCharacters.LoFiDefault;
static readonly ReplacementCharacters Barebones = ReplacementCharacters.Minimum;
// needs separate method. middle null param not running correctly in TestExplorer when used in DataRow()
[TestMethod]
[DataRow("http://test.com/a/b/c", @"http\test.com\a\b\c")]
public void null_replacement(string inStr, string outStr) => Tests(inStr, null, outStr);
public void null_path_throws() => Assert.ThrowsException<ArgumentNullException>(() => FileUtility.GetSafePath(null, Default));
[TestMethod]
// non-empty replacement
[DataRow("abc*abc.txt", "abc✱abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", @"a\b\c\d")]
// remove illegal chars
[DataRow("a*?:z.txt", "a✱z.txt")]
// retain drive letter path colon
[DataRow(@"C:\az.txt", @"C:\az.txt")]
// replace all other colons
[DataRow(@"a\b:c\d.txt", @"a\bc\d.txt")]
// remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
[DataRow(@"C:\""foo\<id>", @"C:\“foo\id")]
public void DefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Default).PathWithoutPrefix);
[TestMethod]
// non-empty replacement
[DataRow("abc*abc.txt", "abc_abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", @"a\b\c\d")]
// remove illegal chars
[DataRow("a*?:z.txt", "a__-z.txt")]
// retain drive letter path colon
[DataRow(@"C:\az.txt", @"C:\az.txt")]
// replace all other colons
[DataRow(@"a\b:c\d.txt", @"a\b-c\d.txt")]
// remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
[DataRow(@"C:\""foo\<id>", @"C:\'foo\{id}")]
public void LoFiDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, LoFiDefault).PathWithoutPrefix);
[TestMethod]
// empty replacement
[DataRow("abc*abc.txt", "", "abc✱abc.txt")]
// non-empty replacement
[DataRow("abc*abc.txt", "ZZZ", "abc✱abc.txt")]
[DataRow("abc*abc.txt", "abc_abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", "Z", @"a\b\c\d")]
[DataRow(@"a/b\c/d", @"a\b\c\d")]
// remove illegal chars
[DataRow("a*?:z.txt", "Z", "a✱z.txt")]
[DataRow("a*?:z.txt", "a___z.txt")]
// retain drive letter path colon
[DataRow(@"C:\az.txt", "Z", @"C:\az.txt")]
[DataRow(@"C:\az.txt", @"C:\az.txt")]
// replace all other colons
[DataRow(@"a\b:c\d.txt", "ZZZ", @"a\bc\d.txt")]
[DataRow(@"a\b:c\d.txt", @"a\b_c\d.txt")]
// remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", "ZZZ", @"C:\a\b\c\d.txt")]
[DataRow(@"C:\""foo\<id>", "ZZZ", @"C:\“foo\id")]
public void Tests(string inStr, string replacement, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, replacement).PathWithoutPrefix);
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")]
[DataRow(@"C:\""foo\<id>", @"C:\_foo\_id_")]
public void BarebonesDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Barebones).PathWithoutPrefix);
}
[TestClass]
public class GetSafeFileName
{
static readonly ReplacementCharacters Default = ReplacementCharacters.Default;
static readonly ReplacementCharacters LoFiDefault = ReplacementCharacters.LoFiDefault;
static readonly ReplacementCharacters Barebones = ReplacementCharacters.Minimum;
// needs separate method. middle null param not running correctly in TestExplorer when used in DataRow()
[TestMethod]
[DataRow("http://test.com/a/b/c", "httptest.comabc")]
public void url_null_replacement(string inStr, string outStr) => ReplacementTests(inStr, null, outStr);
[DataRow("http://test.com/a/b/c", "httptest.comabc")]
public void url_null_replacement(string inStr, string outStr) => DefaultReplacementTest(inStr, outStr);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "", "httptest.comabc")]
// single char replace
[DataRow("http://test.com/a/b/c", "_", "http___test.com_a_b_c")]
// multi char replace
[DataRow("http://test.com/a/b/c", "!!!", "http!!!!!!!!!test.com!!!a!!!b!!!c")]
public void ReplacementTests(string inStr, string replacement, string outStr) => FileUtility.GetSafeFileName(inStr, replacement).Should().Be(outStr);
[DataRow("http://test.com/a/b/c", "httptest.comabc")]
public void DefaultReplacementTest(string inStr, string outStr) => Default.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c")]
public void LoFiDefaultReplacementTest(string inStr, string outStr) => LoFiDefault.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "http___test.com_a_b_c")]
public void BarebonesDefaultReplacementTest(string inStr, string outStr) => Barebones.ReplaceInvalidFilenameChars(inStr).Should().Be(outStr);
}
[TestClass]
@ -117,6 +156,8 @@ namespace FileUtilityTests
[TestClass]
public class GetValidFilename
{
static ReplacementCharacters Replacements = ReplacementCharacters.Default;
[TestMethod]
// dot-files
[DataRow(@"C:\a bc\x y z\.f i l e.txt")]
@ -134,7 +175,7 @@ namespace FileUtilityTests
// file end dots
[DataRow(@"C:\a bc\x y z\f i l e.txt . . .", @"C:\a bc\x y z\f i l e.txt")]
public void Tests(string input, string expected)
=> FileUtility.GetValidFilename(input).PathWithoutPrefix.Should().Be(expected);
=> FileUtility.GetValidFilename(input, Replacements).PathWithoutPrefix.Should().Be(expected);
}
[TestClass]