Browse Source

Merge pull request #2917 from Bond-009/tests

Improved tests
Vasily 5 years ago
parent
commit
aff65adca9

+ 10 - 7
Emby.Naming/Audio/AlbumParser.cs

@@ -1,9 +1,9 @@
+#nullable enable
 #pragma warning disable CS1591
 #pragma warning disable CS1591
 
 
 using System;
 using System;
 using System.Globalization;
 using System.Globalization;
 using System.IO;
 using System.IO;
-using System.Linq;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
 using Emby.Naming.Common;
 using Emby.Naming.Common;
 
 
@@ -21,8 +21,7 @@ namespace Emby.Naming.Audio
         public bool IsMultiPart(string path)
         public bool IsMultiPart(string path)
         {
         {
             var filename = Path.GetFileName(path);
             var filename = Path.GetFileName(path);
-
-            if (string.IsNullOrEmpty(filename))
+            if (filename.Length == 0)
             {
             {
                 return false;
                 return false;
             }
             }
@@ -39,18 +38,22 @@ namespace Emby.Naming.Audio
             filename = filename.Replace(')', ' ');
             filename = filename.Replace(')', ' ');
             filename = Regex.Replace(filename, @"\s+", " ");
             filename = Regex.Replace(filename, @"\s+", " ");
 
 
-            filename = filename.TrimStart();
+            ReadOnlySpan<char> trimmedFilename = filename.TrimStart();
 
 
             foreach (var prefix in _options.AlbumStackingPrefixes)
             foreach (var prefix in _options.AlbumStackingPrefixes)
             {
             {
-                if (filename.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) != 0)
+                if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                 {
                 {
                     continue;
                     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 _))
                 if (int.TryParse(tmp, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
                 {
                 {

+ 2 - 1
Emby.Naming/Audio/AudioFileParser.cs

@@ -1,3 +1,4 @@
+#nullable enable
 #pragma warning disable CS1591
 #pragma warning disable CS1591
 
 
 using System;
 using System;
@@ -11,7 +12,7 @@ namespace Emby.Naming.Audio
     {
     {
         public static bool IsAudioFile(string path, NamingOptions options)
         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);
             return options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
         }
         }
     }
     }

+ 1 - 6
Emby.Naming/Common/EpisodeExpression.cs

@@ -23,11 +23,6 @@ namespace Emby.Naming.Common
         {
         {
         }
         }
 
 
-        public EpisodeExpression()
-            : this(null)
-        {
-        }
-
         public string Expression
         public string Expression
         {
         {
             get => _expression;
             get => _expression;
@@ -48,6 +43,6 @@ namespace Emby.Naming.Common
 
 
         public string[] DateTimeFormats { get; set; }
         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);
     }
     }
 }
 }

+ 4 - 8
Emby.Naming/Subtitles/SubtitleParser.cs

@@ -1,3 +1,4 @@
+#nullable enable
 #pragma warning disable CS1591
 #pragma warning disable CS1591
 
 
 using System;
 using System;
@@ -16,11 +17,11 @@ namespace Emby.Naming.Subtitles
             _options = options;
             _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("File path can't be empty.", nameof(path));
             }
             }
 
 
             var extension = Path.GetExtension(path);
             var extension = Path.GetExtension(path);
@@ -52,11 +53,6 @@ namespace Emby.Naming.Subtitles
 
 
         private string[] GetFlags(string path)
         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 _.
             // Note: the tags need be be surrounded be either a space ( ), hyphen -, dot . or underscore _.
 
 
             var file = Path.GetFileName(path);
             var file = Path.GetFileName(path);

+ 19 - 0
tests/Jellyfin.Model.Tests/Extensions/StringHelperTests.cs

@@ -0,0 +1,19 @@
+using System;
+using MediaBrowser.Model.Extensions;
+using Xunit;
+
+namespace Jellyfin.Model.Tests.Extensions
+{
+    public class StringHelperTests
+    {
+        [Theory]
+        [InlineData("", "")]
+        [InlineData("banana", "Banana")]
+        [InlineData("Banana", "Banana")]
+        [InlineData("ä", "Ä")]
+        public void StringHelper_ValidArgs_Success(string input, string expectedResult)
+        {
+            Assert.Equal(expectedResult, StringHelper.FirstToUpper(input));
+        }
+    }
+}

+ 21 - 0
tests/Jellyfin.Model.Tests/Jellyfin.Model.Tests.csproj

@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <IsPackable>false</IsPackable>
+    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
+    <PackageReference Include="xunit" Version="2.4.1" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
+    <PackageReference Include="coverlet.collector" Version="1.2.1" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="../../MediaBrowser.Model/MediaBrowser.Model.csproj" />
+  </ItemGroup>
+
+</Project>

+ 37 - 53
tests/Jellyfin.Naming.Tests/Music/MultiDiscAlbumTests.cs

@@ -6,61 +6,45 @@ namespace Jellyfin.Naming.Tests.Music
 {
 {
     public class MultiDiscAlbumTests
     public class MultiDiscAlbumTests
     {
     {
-        [Fact]
-        public void TestMultiDiscAlbums()
+        private readonly NamingOptions _namingOptions = new NamingOptions();
+
+        [Theory]
+        [InlineData("", false)]
+        [InlineData("C:/", false)]
+        [InlineData("/home/", 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 AlbumParser_MultidiscPath_Identifies(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));
         }
         }
     }
     }
 }
 }

+ 28 - 21
tests/Jellyfin.Naming.Tests/Subtitles/SubtitleParserTests.cs

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