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

@ -91,10 +91,10 @@ namespace FileManager
{ {
Replacement.OtherInvalid("_"), Replacement.OtherInvalid("_"),
Replacement.FilenameForwardSlash(""), Replacement.FilenameForwardSlash(""),
Replacement.FilenameBackSlash(""), Replacement.FilenameBackSlash("\\"),
Replacement.OpenQuote("“"), Replacement.OpenQuote("“"),
Replacement.CloseQuote("”"), Replacement.CloseQuote("”"),
Replacement.OtherQuote("") Replacement.OtherQuote("\"")
} }
}; };
@ -115,7 +115,18 @@ namespace FileManager
Replacement.Colon("-"), Replacement.Colon("-"),
} }
} }
: Barebones; : new ()
{
Replacements = new Replacement[]
{
Replacement.OtherInvalid("_"),
Replacement.FilenameForwardSlash("_"),
Replacement.FilenameBackSlash("\\"),
Replacement.OpenQuote("\""),
Replacement.CloseQuote("\""),
Replacement.OtherQuote("\"")
}
};
public static readonly ReplacementCharacters Barebones public static readonly ReplacementCharacters Barebones
= IsWindows = IsWindows
@ -164,13 +175,10 @@ namespace FileManager
private string GetFilenameCharReplacement(char toReplace, char preceding, char succeding) private string GetFilenameCharReplacement(char toReplace, char preceding, char succeding)
{ {
if (invalidSlashes.Contains(toReplace)) if (toReplace == ForwardSlash.CharacterToReplace)
{ return ForwardSlash.ReplacementString;
if (toReplace == ForwardSlash.CharacterToReplace) else if (toReplace == BackSlash.CharacterToReplace)
return ForwardSlash.ReplacementString; return BackSlash.ReplacementString;
else
return BackSlash.ReplacementString;
}
else return GetPathCharReplacement(toReplace, preceding, succeding); else return GetPathCharReplacement(toReplace, preceding, succeding);
} }
private string GetPathCharReplacement(char toReplace, char preceding, char succeding) private string GetPathCharReplacement(char toReplace, char preceding, char succeding)
@ -199,6 +207,9 @@ namespace FileManager
return OtherQuote; return OtherQuote;
} }
if (!IsWindows && toReplace == BackSlash.CharacterToReplace)
return BackSlash.ReplacementString;
//Replace any other non-mandatory characters //Replace any other non-mandatory characters
for (int i = Replacement.FIXED_COUNT; i < Replacements.Count; i++) for (int i = Replacement.FIXED_COUNT; i < Replacements.Count; i++)
{ {

View File

@ -15,16 +15,18 @@ namespace FileNamingTemplateTests
static ReplacementCharacters Replacements = ReplacementCharacters.Default; static ReplacementCharacters Replacements = ReplacementCharacters.Default;
[TestMethod] [TestMethod]
public void equiv_GetValidFilename() [DataRow(@"C:\foo\bar", @"C:\foo\bar\my book 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 [ID123456].txt", PlatformID.Win32NT)]
[DataRow(@"/foo/bar", @"/foo/bar/my: book 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 [ID123456].txt", PlatformID.Unix)]
public void equiv_GetValidFilename(string dirFullPath, string expected, PlatformID platformID)
{ {
if (Environment.OSVersion.Platform != platformID)
return;
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
sb.Append('0', 300); sb.Append('0', 300);
var longText = sb.ToString(); var longText = sb.ToString();
var expectedNew = @"C:\foo\bar\my book 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 [ID123456].txt"; NEW_GetValidFilename_FileNamingTemplate(dirFullPath, "my: book " + longText, "txt", "ID123456").Should().Be(expected);
var f2 = NEW_GetValidFilename_FileNamingTemplate(@"C:\foo\bar", "my: book " + longText, "txt", "ID123456");
f2.Should().Be(expectedNew);
} }
private static string NEW_GetValidFilename_FileNamingTemplate(string dirFullPath, string filename, string extension, string metadataSuffix) private static string NEW_GetValidFilename_FileNamingTemplate(string dirFullPath, string filename, string extension, string metadataSuffix)
@ -40,12 +42,12 @@ namespace FileNamingTemplateTests
} }
[TestMethod] [TestMethod]
public void equiv_GetMultipartFileName() [DataRow(@"C:\foo\bar\my file.txt", @"C:\foo\bar\my file - 002 - title.txt", PlatformID.Win32NT)]
[DataRow(@"/foo/bar/my file.txt", @"/foo/bar/my file - 002 - title.txt", PlatformID.Unix)]
public void equiv_GetMultipartFileName(string inStr, string outStr, PlatformID platformID)
{ {
var expected = @"C:\foo\bar\my file - 002 - title.txt"; if (Environment.OSVersion.Platform == platformID)
var f2 = NEW_GetMultipartFileName_FileNamingTemplate(@"C:\foo\bar\my file.txt", 2, 100, "title"); NEW_GetMultipartFileName_FileNamingTemplate(inStr, 2, 100, "title").Should().Be(outStr);
f2.Should().Be(expected);
} }
private static string NEW_GetMultipartFileName_FileNamingTemplate(string originalPath, int partsPosition, int partsTotal, string suffix) private static string NEW_GetMultipartFileName_FileNamingTemplate(string originalPath, int partsPosition, int partsTotal, string suffix)
@ -64,11 +66,16 @@ namespace FileNamingTemplateTests
} }
[TestMethod] [TestMethod]
public void remove_slashes() [DataRow(@"\foo\<title>.txt", @"\foo\slashes.txt", PlatformID.Win32NT)]
[DataRow(@"/foo/<title>.txt", @"/foo/s\la\sh\es.txt", PlatformID.Unix)]
public void remove_slashes(string inStr, string outStr, PlatformID platformID)
{ {
var fileNamingTemplate = new FileNamingTemplate(@"\foo\<title>.txt"); if (Environment.OSVersion.Platform == platformID)
fileNamingTemplate.AddParameterReplacement("title", @"s\l/a\s/h\e/s"); {
fileNamingTemplate.GetFilePath(Replacements).PathWithoutPrefix.Should().Be(@"\foo\slashes.txt"); var fileNamingTemplate = new FileNamingTemplate(inStr);
fileNamingTemplate.AddParameterReplacement("title", @"s\l/a\s/h\e/s");
fileNamingTemplate.GetFilePath(Replacements).PathWithoutPrefix.Should().Be(outStr);
}
} }
} }
} }

View File

@ -21,51 +21,78 @@ namespace FileUtilityTests
[TestMethod] [TestMethod]
// non-empty replacement // non-empty replacement
[DataRow("abc*abc.txt", "abc✱abc.txt")] [DataRow("abc*abc.txt", "abc✱abc.txt", PlatformID.Win32NT)]
// standardize slashes [DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
[DataRow(@"a/b\c/d", @"a\b\c\d")] // 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 // 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 // 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 // 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 // remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")] [DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"C:\""foo\<id>", @"C:\“foo\id")] [DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
public void DefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Default).PathWithoutPrefix); [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] [TestMethod]
// non-empty replacement // non-empty replacement
[DataRow("abc*abc.txt", "abc_abc.txt")] [DataRow("abc*abc.txt", "abc_abc.txt", PlatformID.Win32NT)]
// standardize slashes [DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
[DataRow(@"a/b\c/d", @"a\b\c\d")] // 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 // 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 // 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 // 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 // remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")] [DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"C:\""foo\<id>", @"C:\'foo\{id}")] [DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
public void LoFiDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, LoFiDefault).PathWithoutPrefix); [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] [TestMethod]
// empty replacement // empty replacement
[DataRow("abc*abc.txt", "abc_abc.txt")] [DataRow("abc*abc.txt", "abc_abc.txt", PlatformID.Win32NT)]
// standardize slashes [DataRow("abc*abc.txt", "abc*abc.txt", PlatformID.Unix)]
[DataRow(@"a/b\c/d", @"a\b\c\d")] // 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 // 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 // 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 // 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 // remove empty directories
[DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt")] [DataRow(@"C:\a\\\b\c\\\d.txt", @"C:\a\b\c\d.txt", PlatformID.Win32NT)]
[DataRow(@"C:\""foo\<id>", @"C:\_foo\_id_")] [DataRow(@"/a///b/c///d.txt", @"/a/b/c/d.txt", PlatformID.Unix)]
public void BarebonesDefaultTests(string inStr, string outStr) => Assert.AreEqual(outStr, FileUtility.GetSafePath(inStr, Barebones).PathWithoutPrefix); [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] [TestClass]
@ -77,23 +104,33 @@ namespace FileUtilityTests
// needs separate method. middle null param not running correctly in TestExplorer when used in DataRow() // needs separate method. middle null param not running correctly in TestExplorer when used in DataRow()
[TestMethod] [TestMethod]
[DataRow("http://test.com/a/b/c", "httptest.comabc")] [DataRow("http://test.com/a/b/c", "httptest.comabc", PlatformID.Win32NT)]
public void url_null_replacement(string inStr, string outStr) => DefaultReplacementTest(inStr, outStr); [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] [TestMethod]
// empty replacement // empty replacement
[DataRow("http://test.com/a/b/c", "httptest.comabc")] [DataRow("http://test.com/a/b/c", "httptest.comabc", PlatformID.Win32NT)]
public void DefaultReplacementTest(string inStr, string outStr) => Default.ReplaceFilenameChars(inStr).Should().Be(outStr); [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] [TestMethod]
// empty replacement // empty replacement
[DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c")] [DataRow("http://test.com/a/b/c", "http-__test.com_a_b_c", PlatformID.Win32NT)]
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.Unix)]
public void LoFiDefaultReplacementTest(string inStr, string outStr, PlatformID platformID) => Test(inStr, outStr, LoFiDefault, platformID);
[TestMethod] [TestMethod]
// empty replacement // empty replacement
[DataRow("http://test.com/a/b/c", "http___test.com_a_b_c")] [DataRow("http://test.com/a/b/c", "http___test.com_a_b_c", PlatformID.Win32NT)]
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.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] [TestClass]
@ -154,32 +191,41 @@ namespace FileUtilityTests
} }
[TestClass] [TestClass]
public class GetValidFilename public class GetValidFilename
{ {
static ReplacementCharacters Replacements = ReplacementCharacters.Default; static ReplacementCharacters Replacements = ReplacementCharacters.Default;
[TestMethod] [TestMethod]
// dot-files // 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 // dot-folders
[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)]
public void Valid(string input) => Tests(input, input); [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] [TestMethod]
// folder spaces // 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 // 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 // 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 // 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")] [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)]
public void Tests(string input, string expected) [DataRow(@"/a bc/x y z/f i l e.txt . . .", @"/a bc/x y z/f i l e.txt", PlatformID.Unix)]
=> FileUtility.GetValidFilename(input, Replacements).PathWithoutPrefix.Should().Be(expected); public void Tests(string input, string expected, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
FileUtility.GetValidFilename(input, Replacements).PathWithoutPrefix.Should().Be(expected);
}
} }
[TestClass] [TestClass]
public class RemoveLastCharacter public class RemoveLastCharacter
{ {
[TestMethod] [TestMethod]
public void is_null() => Tests(null, null); public void is_null() => Tests(null, null);

View File

@ -81,40 +81,62 @@ namespace TemplatesTests
=> Templates.getFileNamingTemplate(GetLibraryBook(), template, dirFullPath, extension); => Templates.getFileNamingTemplate(GetLibraryBook(), template, dirFullPath, extension);
[TestMethod] [TestMethod]
public void null_extension() => Tests("f.txt", @"C:\foo\bar", null, @"C:\foo\bar\f.txt"); [DataRow("f.txt", @"C:\foo\bar", null, @"C:\foo\bar\f.txt", PlatformID.Win32NT)]
[DataRow("f.txt", @"/foo/bar", null, @"/foo/bar/f.txt", PlatformID.Unix)]
[DataRow("f.txt", @"C:\foo\bar", "ext", @"C:\foo\bar\f.txt.ext", PlatformID.Win32NT)]
[DataRow("f.txt", @"/foo/bar", "ext", @"/foo/bar/f.txt.ext", PlatformID.Unix)]
[DataRow("f", @"C:\foo\bar", "ext", @"C:\foo\bar\f.ext", PlatformID.Win32NT)]
[DataRow("f", @"/foo/bar", "ext", @"/foo/bar/f.ext", PlatformID.Unix)]
[DataRow("<id>", @"C:\foo\bar", "ext", @"C:\foo\bar\asin.ext", PlatformID.Win32NT)]
[DataRow("<id>", @"/foo/bar", "ext", @"/foo/bar/asin.ext", PlatformID.Unix)]
[DataRow("<bitrate> - <samplerate> - <channels>", @"C:\foo\bar", "ext", @"C:\foo\bar\128 - 44100 - 2.ext", PlatformID.Win32NT)]
[DataRow("<bitrate> - <samplerate> - <channels>", @"/foo/bar", "ext", @"/foo/bar/128 - 44100 - 2.ext", PlatformID.Unix)]
[DataRow("<year> - <channels>", @"C:\foo\bar", "ext", @"C:\foo\bar\2017 - 2.ext", PlatformID.Win32NT)]
[DataRow("<year> - <channels>", @"/foo/bar", "ext", @"/foo/bar/2017 - 2.ext", PlatformID.Unix)]
public void Tests(string template, string dirFullPath, string extension, string expected, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
Templates.getFileNamingTemplate(GetLibraryBook(), template, dirFullPath, extension)
.GetFilePath(Replacements)
.PathWithoutPrefix
.Should().Be(expected);
}
[TestMethod] [TestMethod]
[DataRow("f.txt", @"C:\foo\bar", "ext", @"C:\foo\bar\f.txt.ext")] [DataRow(@"C:\a\b", @"C:\a\b\foobar.ext", PlatformID.Win32NT)]
[DataRow("f", @"C:\foo\bar", "ext", @"C:\foo\bar\f.ext")] [DataRow(@"/a/b", @"/a/b/foobar.ext", PlatformID.Unix)]
[DataRow("<id>", @"C:\foo\bar", "ext", @"C:\foo\bar\asin.ext")] public void IfSeries_empty(string directory, string expected, PlatformID platformID)
[DataRow("<bitrate> - <samplerate> - <channels>", @"C:\foo\bar", "ext", @"C:\foo\bar\128 - 44100 - 2.ext")] {
[DataRow("<year> - <channels>", @"C:\foo\bar", "ext", @"C:\foo\bar\2017 - 2.ext")] if (Environment.OSVersion.Platform == platformID)
public void Tests(string template, string dirFullPath, string extension, string expected) Templates.getFileNamingTemplate(GetLibraryBook(), "foo<if series-><-if series>bar", directory, "ext")
=> Templates.getFileNamingTemplate(GetLibraryBook(), template, dirFullPath, extension) .GetFilePath(Replacements)
.GetFilePath(Replacements) .PathWithoutPrefix
.PathWithoutPrefix .Should().Be(expected);
.Should().Be(expected); }
[TestMethod] [TestMethod]
public void IfSeries_empty() [DataRow(@"C:\a\b", @"C:\a\b\foobar.ext", PlatformID.Win32NT)]
=> Templates.getFileNamingTemplate(GetLibraryBook(), "foo<if series-><-if series>bar", @"C:\a\b", "ext") [DataRow(@"/a/b", @"/a/b/foobar.ext", PlatformID.Unix)]
.GetFilePath(Replacements) public void IfSeries_no_series(string directory, string expected, PlatformID platformID)
.PathWithoutPrefix {
.Should().Be(@"C:\a\b\foobar.ext"); if (Environment.OSVersion.Platform == platformID)
Templates.getFileNamingTemplate(GetLibraryBook(null), "foo<if series->-<series>-<id>-<-if series>bar", directory, "ext")
.GetFilePath(Replacements)
.PathWithoutPrefix
.Should().Be(expected);
}
[TestMethod] [TestMethod]
public void IfSeries_no_series() [DataRow(@"C:\a\b", @"C:\a\b\foo-Sherlock Holmes-asin-bar.ext", PlatformID.Win32NT)]
=> Templates.getFileNamingTemplate(GetLibraryBook(null), "foo<if series->-<series>-<id>-<-if series>bar", @"C:\a\b", "ext") [DataRow(@"/a/b", @"/a/b/foo-Sherlock Holmes-asin-bar.ext", PlatformID.Unix)]
.GetFilePath(Replacements) public void IfSeries_with_series(string directory, string expected, PlatformID platformID)
.PathWithoutPrefix {
.Should().Be(@"C:\a\b\foobar.ext"); if (Environment.OSVersion.Platform == platformID)
Templates.getFileNamingTemplate(GetLibraryBook(), "foo<if series->-<series>-<id>-<-if series>bar", directory, "ext")
[TestMethod] .GetFilePath(Replacements)
public void IfSeries_with_series() .PathWithoutPrefix
=> Templates.getFileNamingTemplate(GetLibraryBook(), "foo<if series->-<series>-<id>-<-if series>bar", @"C:\a\b", "ext") .Should().Be(expected);
.GetFilePath(Replacements) }
.PathWithoutPrefix
.Should().Be(@"C:\a\b\foo-Sherlock Holmes-asin-bar.ext");
} }
} }
@ -256,7 +278,7 @@ namespace Templates_File_Tests
public class GetErrors public class GetErrors
{ {
[TestMethod] [TestMethod]
public void null_is_invalid() => Tests(null, new[] { Templates.ERROR_NULL_IS_INVALID }); public void null_is_invalid() => Tests(null, Environment.OSVersion.Platform, new[] { Templates.ERROR_NULL_IS_INVALID });
[TestMethod] [TestMethod]
public void empty_is_valid() => valid_tests(""); public void empty_is_valid() => valid_tests("");
@ -267,19 +289,23 @@ namespace Templates_File_Tests
[TestMethod] [TestMethod]
[DataRow(@"foo")] [DataRow(@"foo")]
[DataRow(@"<id>")] [DataRow(@"<id>")]
public void valid_tests(string template) => Tests(template, Array.Empty<string>()); public void valid_tests(string template) => Tests(template, Environment.OSVersion.Platform, Array.Empty<string>());
[TestMethod] [TestMethod]
[DataRow(@"C:\", Templates.ERROR_INVALID_FILE_NAME_CHAR)] [DataRow(@"C:\", PlatformID.Win32NT, Templates.ERROR_INVALID_FILE_NAME_CHAR)]
[DataRow(@"\foo", Templates.ERROR_INVALID_FILE_NAME_CHAR)] [DataRow(@"/", PlatformID.Unix, Templates.ERROR_INVALID_FILE_NAME_CHAR)]
[DataRow(@"/foo", Templates.ERROR_INVALID_FILE_NAME_CHAR)] [DataRow(@"\foo", PlatformID.Win32NT, Templates.ERROR_INVALID_FILE_NAME_CHAR)]
[DataRow(@"C:\", Templates.ERROR_INVALID_FILE_NAME_CHAR)] [DataRow(@"/foo", PlatformID.Win32NT, Templates.ERROR_INVALID_FILE_NAME_CHAR)]
public void Tests(string template, params string[] expected) [DataRow(@"/foo", PlatformID.Unix, Templates.ERROR_INVALID_FILE_NAME_CHAR)]
public void Tests(string template, PlatformID platformID, params string[] expected)
{ {
var result = Templates.File.GetErrors(template); if (Environment.OSVersion.Platform == platformID)
result.Count().Should().Be(expected.Length); {
result.Should().BeEquivalentTo(expected); var result = Templates.File.GetErrors(template);
result.Count().Should().Be(expected.Length);
result.Should().BeEquivalentTo(expected);
}
} }
} }
@ -287,21 +313,29 @@ namespace Templates_File_Tests
public class IsValid public class IsValid
{ {
[TestMethod] [TestMethod]
public void null_is_invalid() => Tests(null, false); public void null_is_invalid() => Tests(null, false, Environment.OSVersion.Platform);
[TestMethod] [TestMethod]
public void empty_is_valid() => Tests("", true); public void empty_is_valid() => Tests("", true, Environment.OSVersion.Platform);
[TestMethod] [TestMethod]
public void whitespace_is_valid() => Tests(" ", true); public void whitespace_is_valid() => Tests(" ", true, Environment.OSVersion.Platform);
[TestMethod] [TestMethod]
[DataRow(@"C:\", false)] [DataRow(@"C:\", false, PlatformID.Win32NT)]
[DataRow(@"foo", true)] [DataRow(@"/", false, PlatformID.Unix)]
[DataRow(@"\foo", false)] [DataRow(@"foo", true, PlatformID.Win32NT)]
[DataRow(@"/foo", false)] [DataRow(@"foo", true, PlatformID.Unix)]
[DataRow(@"<id>", true)] [DataRow(@"\foo", false, PlatformID.Win32NT)]
public void Tests(string template, bool expected) => Templates.File.IsValid(template).Should().Be(expected); [DataRow(@"\foo", true, PlatformID.Unix)]
[DataRow(@"/foo", false, PlatformID.Win32NT)]
[DataRow(@"<id>", true, PlatformID.Win32NT)]
[DataRow(@"<id>", true, PlatformID.Unix)]
public void Tests(string template, bool expected, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
Templates.File.IsValid(template).Should().Be(expected);
}
} }
// same as Templates.Folder.GetWarnings // same as Templates.Folder.GetWarnings
@ -331,28 +365,34 @@ namespace Templates_ChapterFile_Tests
public class GetWarnings public class GetWarnings
{ {
[TestMethod] [TestMethod]
public void null_is_invalid() => Tests(null, new[] { Templates.ERROR_NULL_IS_INVALID }); public void null_is_invalid() => Tests(null, null, new[] { Templates.ERROR_NULL_IS_INVALID });
[TestMethod] [TestMethod]
public void empty_has_warnings() => Tests("", Templates.WARNING_EMPTY, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG); public void empty_has_warnings() => Tests("", null, Templates.WARNING_EMPTY, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG);
[TestMethod] [TestMethod]
public void whitespace_has_warnings() => Tests(" ", Templates.WARNING_WHITE_SPACE, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG); public void whitespace_has_warnings() => Tests(" ", null, Templates.WARNING_WHITE_SPACE, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG);
[TestMethod] [TestMethod]
[DataRow("<ch#>")] [DataRow("<ch#>")]
[DataRow("<ch#> <id>")] [DataRow("<ch#> <id>")]
public void valid_tests(string template) => Tests(template, Array.Empty<string>()); public void valid_tests(string template) => Tests(template, null, Array.Empty<string>());
[TestMethod] [TestMethod]
[DataRow(@"no tags", Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)] [DataRow(@"no tags", null, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)]
[DataRow(@"<id>\foo\bar", Templates.ERROR_INVALID_FILE_NAME_CHAR, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)] [DataRow(@"<id>\foo\bar", true, Templates.ERROR_INVALID_FILE_NAME_CHAR, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)]
[DataRow("<chapter count> -- chapter tag but not ch# or ch_#", Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)] [DataRow(@"<id>/foo/bar", false, Templates.ERROR_INVALID_FILE_NAME_CHAR, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)]
public void Tests(string template, params string[] expected) [DataRow("<chapter count> -- chapter tag but not ch# or ch_#", null, Templates.WARNING_NO_TAGS, Templates.WARNING_NO_CHAPTER_NUMBER_TAG)]
public void Tests(string template, bool? windows, params string[] expected)
{ {
var result = Templates.ChapterFile.GetWarnings(template); if(windows is null
result.Count().Should().Be(expected.Length); || (windows is true && Environment.OSVersion.Platform is PlatformID.Win32NT)
result.Should().BeEquivalentTo(expected); || (windows is false && Environment.OSVersion.Platform is PlatformID.Unix))
{
var result = Templates.ChapterFile.GetWarnings(template);
result.Count().Should().Be(expected.Length);
result.Should().BeEquivalentTo(expected);
}
} }
} }
@ -408,10 +448,15 @@ namespace Templates_ChapterFile_Tests
static readonly ReplacementCharacters Default = ReplacementCharacters.Default; static readonly ReplacementCharacters Default = ReplacementCharacters.Default;
[TestMethod] [TestMethod]
[DataRow("[<id>] <ch# 0> of <ch count> - <ch title>", @"C:\foo\", "txt", 6, 10, "chap", @"C:\foo\[asin] 06 of 10 - chap.txt")] [DataRow("[<id>] <ch# 0> of <ch count> - <ch title>", @"C:\foo\", "txt", 6, 10, "chap", @"C:\foo\[asin] 06 of 10 - chap.txt", PlatformID.Win32NT)]
[DataRow("<ch#>", @"C:\foo\", "txt", 6, 10, "chap", @"C:\foo\6.txt")] [DataRow("[<id>] <ch# 0> of <ch count> - <ch title>", @"/foo/", "txt", 6, 10, "chap", @"/foo/[asin] 06 of 10 - chap.txt", PlatformID.Unix)]
public void Tests(string template, string dir, string ext, int pos, int total, string chapter, string expected) [DataRow("<ch#>", @"C:\foo\", "txt", 6, 10, "chap", @"C:\foo\6.txt", PlatformID.Win32NT)]
=> Templates.ChapterFile.GetPortionFilename(GetLibraryBook(), template, new() { OutputFileName = $"xyz.{ext}", PartsPosition = pos, PartsTotal = total, Title = chapter }, dir, Default) [DataRow("<ch#>", @"/foo/", "txt", 6, 10, "chap", @"/foo/6.txt", PlatformID.Unix)]
.Should().Be(expected); public void Tests(string template, string dir, string ext, int pos, int total, string chapter, string expected, PlatformID platformID)
{
if (Environment.OSVersion.Platform == platformID)
Templates.ChapterFile.GetPortionFilename(GetLibraryBook(), template, new() { OutputFileName = $"xyz.{ext}", PartsPosition = pos, PartsTotal = total, Title = chapter }, dir, Default)
.Should().Be(expected);
}
} }
} }