Make tests xplat

This commit is contained in:
MBucari 2022-12-15 23:04:27 -07:00
parent 9cd10eca58
commit 210ab065c2
4 changed files with 242 additions and 133 deletions

View file

@ -21,51 +21,78 @@ namespace FileUtilityTests
[TestMethod]
// non-empty replacement
[DataRow("abc*abc.txt", "abc✱abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", @"a\b\c\d")]
[DataRow("abc*abc.txt", "abc✱abc.txt", PlatformID.Win32NT)]
[DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
// standardize slashes. There is no unix equivalent because there is no alt directory separator
[DataRow(@"a/b\c/d", @"a\b\c\d", PlatformID.Win32NT)]
// remove illegal chars
[DataRow("a*?:z.txt", "a✱z.txt")]
[DataRow("a*?:z.txt", "a✱z.txt", PlatformID.Win32NT)]
[DataRow("a*?:z.txt", "a*?:z.txt", PlatformID.Unix)]
// retain drive letter path colon
[DataRow(@"C:\az.txt", @"C:\az.txt")]
[DataRow(@"C:\az.txt", @"C:\az.txt", PlatformID.Win32NT)]
[DataRow(@"/:/az.txt", @"/:/az.txt", PlatformID.Unix)]
// replace all other colons
[DataRow(@"a\b:c\d.txt", @"a\bc\d.txt")]
[DataRow(@"a\b:c\d.txt", @"a\bc\d.txt", PlatformID.Win32NT)]
[DataRow(@"a/b:c/d.txt", @"a/b:c/d.txt", PlatformID.Unix)]
// 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);
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
[DataRow(@"C:\""foo\<id>", @"C:\“foo\id", PlatformID.Win32NT)]
[DataRow(@"/""foo/<id>", @"/“foo/<id>", PlatformID.Unix)]
public void DefaultTests(string inStr, string outStr, PlatformID platformID)
=> Test(inStr, outStr, Default, platformID);
[TestMethod]
// non-empty replacement
[DataRow("abc*abc.txt", "abc_abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", @"a\b\c\d")]
[DataRow("abc*abc.txt", "abc_abc.txt", PlatformID.Win32NT)]
[DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
// standardize slashes. There is no unix equivalent because there is no alt directory separator
[DataRow(@"a/b\c/d", @"a\b\c\d", PlatformID.Win32NT)]
// remove illegal chars
[DataRow("a*?:z.txt", "a__-z.txt")]
[DataRow("a*?:z.txt", "a__-z.txt", PlatformID.Win32NT)]
[DataRow("a*?:z.txt", "a*?:z.txt", PlatformID.Unix)]
// retain drive letter path colon
[DataRow(@"C:\az.txt", @"C:\az.txt")]
[DataRow(@"C:\az.txt", @"C:\az.txt", PlatformID.Win32NT)]
[DataRow(@"/:az.txt", @"/:az.txt", PlatformID.Unix)]
// replace all other colons
[DataRow(@"a\b:c\d.txt", @"a\b-c\d.txt")]
[DataRow(@"a\b:c\d.txt", @"a\b-c\d.txt", PlatformID.Win32NT)]
[DataRow(@"a/b:c/d.txt", @"a/b:c/d.txt", PlatformID.Unix)]
// 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);
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
[DataRow(@"C:\""foo\<id>", @"C:\'foo\{id}", PlatformID.Win32NT)]
[DataRow(@"/""foo/<id>", @"/""foo/<id>", PlatformID.Unix)]
public void LoFiDefaultTests(string inStr, string outStr, PlatformID platformID)
=> Test(inStr, outStr, LoFiDefault, platformID);
[TestMethod]
// empty replacement
[DataRow("abc*abc.txt", "abc_abc.txt")]
// standardize slashes
[DataRow(@"a/b\c/d", @"a\b\c\d")]
[DataRow("abc*abc.txt", "abc_abc.txt", PlatformID.Win32NT)]
[DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
// standardize slashes. There is no unix equivalent because there is no alt directory separator
[DataRow(@"a/b\c/d", @"a\b\c\d", PlatformID.Win32NT)]
// remove illegal chars
[DataRow("a*?:z.txt", "a___z.txt")]
[DataRow("a*?:z.txt", "a___z.txt", PlatformID.Win32NT)]
[DataRow("a*?:z.txt", "a*?:z.txt", PlatformID.Unix)]
// retain drive letter path colon
[DataRow(@"C:\az.txt", @"C:\az.txt")]
[DataRow(@"C:\az.txt", @"C:\az.txt", PlatformID.Win32NT)]
[DataRow(@"/:az.txt", @"/:az.txt", PlatformID.Unix)]
// replace all other colons
[DataRow(@"a\b:c\d.txt", @"a\b_c\d.txt")]
[DataRow(@"a\b:c\d.txt", @"a\b_c\d.txt", PlatformID.Win32NT)]
[DataRow(@"a/b:c/d.txt", @"a/b:c/d.txt", PlatformID.Unix)]
// 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 BarebonesDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Barebones).PathWithoutPrefix);
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
[DataRow(@"C:\""foo\<id>", @"C:\_foo\_id_", PlatformID.Win32NT)]
[DataRow(@"/""foo/<id>", @"/""foo/<id>", PlatformID.Unix)]
public void BarebonesDefaultTests(string inStr, string outStr, PlatformID platformID)
=> Test(inStr, outStr, Barebones, platformID);
private void Test(string inStr, string outStr, ReplacementCharacters replacements, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
FileUtility.GetSafePath(inStr, replacements).PathWithoutPrefix.Should().Be(outStr);
}
}
[TestClass]
@ -77,23 +104,33 @@ namespace FileUtilityTests
// 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) => DefaultReplacementTest(inStr, outStr);
[DataRow("http://test.com/a/b/c", "httptest.comabc", PlatformID.Win32NT)]
[DataRow("http://test.com/a/b/c", "http:test.comabc", PlatformID.Unix)]
public void url_null_replacement(string inStr, string outStr, PlatformID platformID) => DefaultReplacementTest(inStr, outStr, platformID);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "httptest.comabc")]
public void DefaultReplacementTest(string inStr, string outStr) => Default.ReplaceFilenameChars(inStr).Should().Be(outStr);
[DataRow("http://test.com/a/b/c", "httptest.comabc", PlatformID.Win32NT)]
[DataRow("http://test.com/a/b/c", "http:test.comabc", PlatformID.Unix)]
public void DefaultReplacementTest(string inStr, string outStr, PlatformID platformID) => Test(inStr, outStr, Default, platformID);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c")]
public void LoFiDefaultReplacementTest(string inStr, string outStr) => LoFiDefault.ReplaceFilenameChars(inStr).Should().Be(outStr);
[DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c", PlatformID.Win32NT)]
[DataRow("http://test.com/a/b/c", "http:__test.com_a_b_c", PlatformID.Unix)]
public void LoFiDefaultReplacementTest(string inStr, string outStr, PlatformID platformID) => Test(inStr, outStr, LoFiDefault, platformID);
[TestMethod]
// empty replacement
[DataRow("http://test.com/a/b/c", "http___test.com_a_b_c")]
public void BarebonesDefaultReplacementTest(string inStr, string outStr) => Barebones.ReplaceFilenameChars(inStr).Should().Be(outStr);
[DataRow("http://test.com/a/b/c", "http___test.com_a_b_c", PlatformID.Win32NT)]
[DataRow("http://test.com/a/b/c", "http:__test.com_a_b_c", PlatformID.Unix)]
public void BarebonesDefaultReplacementTest(string inStr, string outStr, PlatformID platformID) => Test(inStr, outStr, Barebones, platformID);
private void Test(string inStr, string outStr, ReplacementCharacters replacements, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
replacements.ReplaceFilenameChars(inStr).Should().Be(outStr);
}
}
[TestClass]
@ -154,32 +191,41 @@ namespace FileUtilityTests
}
[TestClass]
public class GetValidFilename
public class GetValidFilename
{
static ReplacementCharacters Replacements = ReplacementCharacters.Default;
[TestMethod]
// dot-files
[DataRow(@"C:\a bc\x y z\.f i l e.txt")]
[DataRow(@"C:\a bc\x y z\.f i l e.txt", PlatformID.Win32NT)]
[DataRow(@"/a bc/x y z/.f i l e.txt", PlatformID.Unix)]
// dot-folders
[DataRow(@"C:\a bc\.x y z\f i l e.txt")]
public void Valid(string input) => Tests(input, input);
[DataRow(@"C:\a bc\.x y z\f i l e.txt", PlatformID.Win32NT)]
[DataRow(@"/a bc/.x y z/f i l e.txt", PlatformID.Unix)]
public void Valid(string input, PlatformID platformID) => Tests(input, input, platformID);
[TestMethod]
// folder spaces
[DataRow(@"C:\ a bc \x y z ", @"C:\a bc\x y z")]
[DataRow(@"C:\ a bc \x y z ", @"C:\a bc\x y z", PlatformID.Win32NT)]
[DataRow(@"/ a bc /x y z ", @"/a bc/x y z", PlatformID.Unix)]
// file spaces
[DataRow(@"C:\a bc\x y z\ f i l e.txt ", @"C:\a bc\x y z\f i l e.txt")]
[DataRow(@"C:\a bc\x y z\ f i l e.txt ", @"C:\a bc\x y z\f i l e.txt", PlatformID.Win32NT)]
[DataRow(@"/a bc/x y z/ f i l e.txt ", @"/a bc/x y z/f i l e.txt", PlatformID.Unix)]
// eliminate beginning space and end dots and spaces
[DataRow(@"C:\a bc\ . . . x y z . . . \f i l e.txt", @"C:\a bc\. . . x y z\f i l e.txt")]
[DataRow(@"C:\a bc\ . . . x y z . . . \f i l e.txt", @"C:\a bc\. . . x y z\f i l e.txt", PlatformID.Win32NT)]
[DataRow(@"/a bc/ . . . x y z . . . /f i l e.txt", @"/a bc/. . . x y z/f i l e.txt", PlatformID.Unix)]
// 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, Replacements).PathWithoutPrefix.Should().Be(expected);
[DataRow(@"C:\a bc\x y z\f i l e.txt . . .", @"C:\a bc\x y z\f i l e.txt", PlatformID.Win32NT)]
[DataRow(@"/a bc/x y z/f i l e.txt . . .", @"/a bc/x y z/f i l e.txt", PlatformID.Unix)]
public void Tests(string input, string expected, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
FileUtility.GetValidFilename(input, Replacements).PathWithoutPrefix.Should().Be(expected);
}
}
[TestClass]
public class RemoveLastCharacter
public class RemoveLastCharacter
{
[TestMethod]
public void is_null() => Tests(null, null);