Improved tests

This commit is contained in:
Bond_009 2020-04-19 11:57:03 +02:00
parent 16401ec7ae
commit d99536e99f
6 changed files with 80 additions and 94 deletions

View File

@ -1,3 +1,4 @@
#nullable enable
#pragma warning disable CS1591
using System;
@ -22,7 +23,7 @@ namespace Emby.Naming.Audio
{
var filename = Path.GetFileName(path);
if (string.IsNullOrEmpty(filename))
if (path.Length == 0)
{
return false;
}
@ -39,18 +40,22 @@ namespace Emby.Naming.Audio
filename = filename.Replace(')', ' ');
filename = Regex.Replace(filename, @"\s+", " ");
filename = filename.TrimStart();
ReadOnlySpan<char> trimmedFilename = filename.TrimStart();
foreach (var prefix in _options.AlbumStackingPrefixes)
{
if (filename.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) != 0)
if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}
var tmp = filename.Substring(prefix.Length);
var tmp = trimmedFilename.Slice(prefix.Length).Trim();
tmp = tmp.Trim().Split(' ').FirstOrDefault() ?? string.Empty;
int index = tmp.IndexOf(' ');
if (index != -1)
{
tmp = tmp.Slice(0, index);
}
if (int.TryParse(tmp, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
{

View File

@ -1,3 +1,4 @@
#nullable enable
#pragma warning disable CS1591
using System;
@ -11,7 +12,7 @@ namespace Emby.Naming.Audio
{
public static bool IsAudioFile(string path, NamingOptions options)
{
var extension = Path.GetExtension(path) ?? string.Empty;
var extension = Path.GetExtension(path);
return options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
}
}

View File

@ -23,11 +23,6 @@ namespace Emby.Naming.Common
{
}
public EpisodeExpression()
: this(null)
{
}
public string Expression
{
get => _expression;
@ -48,6 +43,6 @@ namespace Emby.Naming.Common
public string[] DateTimeFormats { get; set; }
public Regex Regex => _regex ?? (_regex = new Regex(Expression, RegexOptions.IgnoreCase | RegexOptions.Compiled));
public Regex Regex => _regex ??= new Regex(Expression, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}

View File

@ -1,3 +1,4 @@
#nullable enable
#pragma warning disable CS1591
using System;
@ -16,11 +17,11 @@ namespace Emby.Naming.Subtitles
_options = options;
}
public SubtitleInfo ParseFile(string path)
public SubtitleInfo? ParseFile(string path)
{
if (string.IsNullOrEmpty(path))
if (path.Length == 0)
{
throw new ArgumentNullException(nameof(path));
throw new ArgumentException("String can't be empty.", nameof(path));
}
var extension = Path.GetExtension(path);
@ -52,11 +53,6 @@ namespace Emby.Naming.Subtitles
private string[] GetFlags(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
// Note: the tags need be be surrounded be either a space ( ), hyphen -, dot . or underscore _.
var file = Path.GetFileName(path);

View File

@ -6,61 +6,43 @@ namespace Jellyfin.Naming.Tests.Music
{
public class MultiDiscAlbumTests
{
[Fact]
public void TestMultiDiscAlbums()
private readonly NamingOptions _namingOptions = new NamingOptions();
[Theory]
[InlineData("", false)]
[InlineData(@"blah blah", false)]
[InlineData(@"D:/music/weezer/03 Pinkerton", false)]
[InlineData(@"D:/music/michael jackson/Bad (2012 Remaster)", false)]
[InlineData(@"cd1", true)]
[InlineData(@"disc18", true)]
[InlineData(@"disk10", true)]
[InlineData(@"vol7", true)]
[InlineData(@"volume1", true)]
[InlineData(@"cd 1", true)]
[InlineData(@"disc 1", true)]
[InlineData(@"disk 1", true)]
[InlineData(@"disk", false)]
[InlineData(@"disk ·", false)]
[InlineData(@"disk a", false)]
[InlineData(@"disk volume", false)]
[InlineData(@"disc disc", false)]
[InlineData(@"disk disc 6", false)]
[InlineData(@"cd - 1", true)]
[InlineData(@"disc- 1", true)]
[InlineData(@"disk - 1", true)]
[InlineData(@"Disc 01 (Hugo Wolf · 24 Lieder)", true)]
[InlineData(@"Disc 04 (Encores and Folk Songs)", true)]
[InlineData(@"Disc04 (Encores and Folk Songs)", true)]
[InlineData(@"Disc 04(Encores and Folk Songs)", true)]
[InlineData(@"Disc04(Encores and Folk Songs)", true)]
[InlineData(@"D:/Video/MBTestLibrary/VideoTest/music/.38 special/anth/Disc 2", true)]
[InlineData(@"[1985] Opportunities (Let's make lots of money) (1985)", false)]
[InlineData(@"Blah 04(Encores and Folk Songs)", false)]
public void TestMultiDiscAlbums(string path, bool result)
{
Assert.False(IsMultiDiscAlbumFolder(@"blah blah"));
Assert.False(IsMultiDiscAlbumFolder(@"D:/music/weezer/03 Pinkerton"));
Assert.False(IsMultiDiscAlbumFolder(@"D:/music/michael jackson/Bad (2012 Remaster)"));
var parser = new AlbumParser(_namingOptions);
Assert.True(IsMultiDiscAlbumFolder(@"cd1"));
Assert.True(IsMultiDiscAlbumFolder(@"disc18"));
Assert.True(IsMultiDiscAlbumFolder(@"disk10"));
Assert.True(IsMultiDiscAlbumFolder(@"vol7"));
Assert.True(IsMultiDiscAlbumFolder(@"volume1"));
Assert.True(IsMultiDiscAlbumFolder(@"cd 1"));
Assert.True(IsMultiDiscAlbumFolder(@"disc 1"));
Assert.True(IsMultiDiscAlbumFolder(@"disk 1"));
Assert.False(IsMultiDiscAlbumFolder(@"disk"));
Assert.False(IsMultiDiscAlbumFolder(@"disk ·"));
Assert.False(IsMultiDiscAlbumFolder(@"disk a"));
Assert.False(IsMultiDiscAlbumFolder(@"disk volume"));
Assert.False(IsMultiDiscAlbumFolder(@"disc disc"));
Assert.False(IsMultiDiscAlbumFolder(@"disk disc 6"));
Assert.True(IsMultiDiscAlbumFolder(@"cd - 1"));
Assert.True(IsMultiDiscAlbumFolder(@"disc- 1"));
Assert.True(IsMultiDiscAlbumFolder(@"disk - 1"));
Assert.True(IsMultiDiscAlbumFolder(@"Disc 01 (Hugo Wolf · 24 Lieder)"));
Assert.True(IsMultiDiscAlbumFolder(@"Disc 04 (Encores and Folk Songs)"));
Assert.True(IsMultiDiscAlbumFolder(@"Disc04 (Encores and Folk Songs)"));
Assert.True(IsMultiDiscAlbumFolder(@"Disc 04(Encores and Folk Songs)"));
Assert.True(IsMultiDiscAlbumFolder(@"Disc04(Encores and Folk Songs)"));
Assert.True(IsMultiDiscAlbumFolder(@"D:/Video/MBTestLibrary/VideoTest/music/.38 special/anth/Disc 2"));
}
[Fact]
public void TestMultiDiscAlbums1()
{
Assert.False(IsMultiDiscAlbumFolder(@"[1985] Opportunities (Let's make lots of money) (1985)"));
}
[Fact]
public void TestMultiDiscAlbums2()
{
Assert.False(IsMultiDiscAlbumFolder(@"Blah 04(Encores and Folk Songs)"));
}
private bool IsMultiDiscAlbumFolder(string path)
{
var parser = new AlbumParser(new NamingOptions());
return parser.IsMultiPart(path);
Assert.Equal(result, parser.IsMultiPart(path));
}
}
}

View File

@ -1,4 +1,5 @@
using Emby.Naming.Common;
using System;
using Emby.Naming.Common;
using Emby.Naming.Subtitles;
using Xunit;
@ -6,28 +7,19 @@ namespace Jellyfin.Naming.Tests.Subtitles
{
public class SubtitleParserTests
{
private SubtitleParser GetParser()
{
var options = new NamingOptions();
private readonly NamingOptions _namingOptions = new NamingOptions();
return new SubtitleParser(options);
}
[Fact]
public void TestSubtitles()
[Theory]
[InlineData("The Skin I Live In (2011).srt", null, false, false)]
[InlineData("The Skin I Live In (2011).eng.srt", "eng", false, false)]
[InlineData("The Skin I Live In (2011).eng.default.srt", "eng", true, false)]
[InlineData("The Skin I Live In (2011).eng.forced.srt", "eng", false, true)]
[InlineData("The Skin I Live In (2011).eng.foreign.srt", "eng", false, true)]
[InlineData("The Skin I Live In (2011).eng.default.foreign.srt", "eng", true, true)]
[InlineData("The Skin I Live In (2011).default.foreign.eng.srt", "eng", true, true)]
public void TestSubtitles(string input, string language, bool isDefault, bool isForced)
{
Test("The Skin I Live In (2011).srt", null, false, false);
Test("The Skin I Live In (2011).eng.srt", "eng", false, false);
Test("The Skin I Live In (2011).eng.default.srt", "eng", true, false);
Test("The Skin I Live In (2011).eng.forced.srt", "eng", false, true);
Test("The Skin I Live In (2011).eng.foreign.srt", "eng", false, true);
Test("The Skin I Live In (2011).eng.default.foreign.srt", "eng", true, true);
Test("The Skin I Live In (2011).default.foreign.eng.srt", "eng", true, true);
}
private void Test(string input, string language, bool isDefault, bool isForced)
{
var parser = GetParser();
var parser = new SubtitleParser(_namingOptions);
var result = parser.ParseFile(input);
@ -35,5 +27,20 @@ namespace Jellyfin.Naming.Tests.Subtitles
Assert.Equal(isDefault, result.IsDefault);
Assert.Equal(isForced, result.IsForced);
}
[Theory]
[InlineData("The Skin I Live In (2011).mp4")]
public void TestNonSubtitles(string input)
{
var parser = new SubtitleParser(_namingOptions);
Assert.Null(parser.ParseFile(input));
}
[Fact]
public void TestEmptySubtitlesPath()
{
Assert.Throws<ArgumentException>(() => new SubtitleParser(_namingOptions).ParseFile(string.Empty));
}
}
}