Jelajahi Sumber

Add some simple tests

Bond_009 5 tahun lalu
induk
melakukan
7f4a229cd2

+ 4 - 0
Emby.Server.Implementations/HttpServer/ResponseFilter.cs

@@ -82,6 +82,10 @@ namespace Emby.Server.Implementations.HttpServer
             {
                 return null;
             }
+            else if (inString.Length == 0)
+            {
+                return inString;
+            }
 
             var newString = new StringBuilder(inString.Length);
 

+ 4 - 2
Emby.Server.Implementations/Library/MediaStreamSelector.cs

@@ -35,7 +35,8 @@ namespace Emby.Server.Implementations.Library
             return null;
         }
 
-        public static int? GetDefaultSubtitleStreamIndex(List<MediaStream> streams,
+        public static int? GetDefaultSubtitleStreamIndex(
+            List<MediaStream> streams,
             string[] preferredLanguages,
             SubtitlePlaybackMode mode,
             string audioTrackLanguage)
@@ -115,7 +116,8 @@ namespace Emby.Server.Implementations.Library
                  .ThenBy(i => i.Index);
         }
 
-        public static void SetSubtitleStreamScores(List<MediaStream> streams,
+        public static void SetSubtitleStreamScores(
+            List<MediaStream> streams,
             string[] preferredLanguages,
             SubtitlePlaybackMode mode,
             string audioTrackLanguage)

+ 12 - 10
Emby.Server.Implementations/Library/PathExtensions.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 using System.Text.RegularExpressions;
 
@@ -12,24 +14,24 @@ namespace Emby.Server.Implementations.Library
         /// Gets the attribute value.
         /// </summary>
         /// <param name="str">The STR.</param>
-        /// <param name="attrib">The attrib.</param>
+        /// <param name="attribute">The attrib.</param>
         /// <returns>System.String.</returns>
-        /// <exception cref="ArgumentNullException">attrib</exception>
-        public static string GetAttributeValue(this string str, string attrib)
+        /// <exception cref="ArgumentException"><c>str</c> or <c>attribute</c> is empty.</exception>
+        public static string? GetAttributeValue(this string str, string attribute)
         {
-            if (string.IsNullOrEmpty(str))
+            if (str.Length == 0)
             {
-                throw new ArgumentNullException(nameof(str));
+                throw new ArgumentException("String can't be empty.", nameof(str));
             }
 
-            if (string.IsNullOrEmpty(attrib))
+            if (attribute.Length == 0)
             {
-                throw new ArgumentNullException(nameof(attrib));
+                throw new ArgumentException("String can't be empty.", nameof(attribute));
             }
 
-            string srch = "[" + attrib + "=";
+            string srch = "[" + attribute + "=";
             int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
-            if (start > -1)
+            if (start != -1)
             {
                 start += srch.Length;
                 int end = str.IndexOf(']', start);
@@ -37,7 +39,7 @@ namespace Emby.Server.Implementations.Library
             }
 
             // for imdbid we also accept pattern matching
-            if (string.Equals(attrib, "imdbid", StringComparison.OrdinalIgnoreCase))
+            if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
             {
                 var m = Regex.Match(str, "tt([0-9]{7,8})", RegexOptions.IgnoreCase);
                 return m.Success ? m.Value : null;

+ 2 - 0
Emby.Server.Implementations/Library/ResolverHelper.cs

@@ -118,10 +118,12 @@ namespace Emby.Server.Implementations.Library
             {
                 throw new ArgumentNullException(nameof(fileSystem));
             }
+
             if (item == null)
             {
                 throw new ArgumentNullException(nameof(item));
             }
+
             if (args == null)
             {
                 throw new ArgumentNullException(nameof(args));

+ 2 - 14
Emby.Server.Implementations/Services/StringMapTypeDeserializer.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Reflection;
+using MediaBrowser.Common.Extensions;
 
 namespace Emby.Server.Implementations.Services
 {
@@ -81,7 +82,7 @@ namespace Emby.Server.Implementations.Services
                 if (propertySerializerEntry.PropertyType == typeof(bool))
                 {
                     //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
-                    propertyTextValue = LeftPart(propertyTextValue, ',');
+                    propertyTextValue = StringExtensions.LeftPart(propertyTextValue, ',').ToString();
                 }
 
                 var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
@@ -95,19 +96,6 @@ namespace Emby.Server.Implementations.Services
 
             return instance;
         }
-
-        public static string LeftPart(string strVal, char needle)
-        {
-            if (strVal == null)
-            {
-                return null;
-            }
-
-            var pos = strVal.IndexOf(needle);
-            return pos == -1
-                ? strVal
-                : strVal.Substring(0, pos);
-        }
     }
 
     internal static class TypeAccessor

+ 4 - 16
Emby.Server.Implementations/Services/UrlExtensions.cs

@@ -1,4 +1,5 @@
 using System;
+using MediaBrowser.Common.Extensions;
 
 namespace Emby.Server.Implementations.Services
 {
@@ -13,25 +14,12 @@ namespace Emby.Server.Implementations.Services
         public static string GetMethodName(this Type type)
         {
             var typeName = type.FullName != null // can be null, e.g. generic types
-                ? LeftPart(type.FullName, "[[")   // Generic Fullname
-                    .Replace(type.Namespace + ".", string.Empty) // Trim Namespaces
-                    .Replace("+", ".") // Convert nested into normal type
+                ? StringExtensions.LeftPart(type.FullName, "[[", StringComparison.Ordinal).ToString() // Generic Fullname
+                    .Replace(type.Namespace + ".", string.Empty, StringComparison.Ordinal) // Trim Namespaces
+                    .Replace("+", ".", StringComparison.Ordinal) // Convert nested into normal type
                 : type.Name;
 
             return type.IsGenericParameter ? "'" + typeName : typeName;
         }
-
-        private static string LeftPart(string strVal, string needle)
-        {
-            if (strVal == null)
-            {
-                return null;
-            }
-
-            var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
-            return pos == -1
-                ? strVal
-                : strVal.Substring(0, pos);
-        }
     }
 }

+ 3 - 13
Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.Net;
 using System.Net.Mime;
+using MediaBrowser.Common.Extensions;
 using MediaBrowser.Common.Net;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http.Extensions;
@@ -216,14 +217,14 @@ namespace Emby.Server.Implementations.SocketSharp
                     pi = pi.Slice(1);
                 }
 
-                format = LeftPart(pi, '/');
+                format = pi.LeftPart('/');
                 if (format.Length > FormatMaxLength)
                 {
                     return null;
                 }
             }
 
-            format = LeftPart(format, '.');
+            format = format.LeftPart('.');
             if (format.Contains("json", StringComparison.OrdinalIgnoreCase))
             {
                 return "application/json";
@@ -235,16 +236,5 @@ namespace Emby.Server.Implementations.SocketSharp
 
             return null;
         }
-
-        public static ReadOnlySpan<char> LeftPart(ReadOnlySpan<char> strVal, char needle)
-        {
-            if (strVal == null)
-            {
-                return null;
-            }
-
-            var pos = strVal.IndexOf(needle);
-            return pos == -1 ? strVal : strVal.Slice(0, pos);
-        }
     }
 }

+ 2 - 1
MediaBrowser.Api/Playback/BaseStreamingService.cs

@@ -321,7 +321,7 @@ namespace MediaBrowser.Api.Playback
             var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
 
             // enable throttling when NOT using hardware acceleration
-            if (encodingOptions.HardwareAccelerationType == string.Empty)
+            if (string.IsNullOrEmpty(encodingOptions.HardwareAccelerationType))
             {
                 return state.InputProtocol == MediaProtocol.File &&
                        state.RunTimeTicks.HasValue &&
@@ -330,6 +330,7 @@ namespace MediaBrowser.Api.Playback
                        state.VideoType == VideoType.VideoFile &&
                        !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase);
             }
+
             return false;
         }
 

+ 2 - 0
MediaBrowser.Common/Extensions/BaseExtensions.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 using System.Security.Cryptography;
 using System.Text;

+ 2 - 0
MediaBrowser.Common/Extensions/CopyToExtensions.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System.Collections.Generic;
 
 namespace MediaBrowser.Common.Extensions

+ 2 - 0
MediaBrowser.Common/Extensions/MethodNotAllowedException.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 
 namespace MediaBrowser.Common.Extensions

+ 2 - 0
MediaBrowser.Common/Extensions/ProcessExtensions.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 using System.Diagnostics;
 using System.Threading;

+ 1 - 0
MediaBrowser.Common/Extensions/RateLimitExceededException.cs

@@ -1,3 +1,4 @@
+#nullable enable
 #pragma warning disable CS1591
 
 using System;

+ 2 - 0
MediaBrowser.Common/Extensions/ResourceNotFoundException.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 
 namespace MediaBrowser.Common.Extensions

+ 2 - 0
MediaBrowser.Common/Extensions/ShuffleExtensions.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 using System.Collections.Generic;
 

+ 37 - 0
MediaBrowser.Common/Extensions/StringExtensions.cs

@@ -0,0 +1,37 @@
+#nullable enable
+
+using System;
+
+namespace MediaBrowser.Common.Extensions
+{
+    /// <summary>
+    /// Extensions methods to simplify string operations.
+    /// </summary>
+    public static class StringExtensions
+    {
+        /// <summary>
+        /// Returns the part left of the <c>needle</c>.
+        /// </summary>
+        /// <param name="str">The string to seek.</param>
+        /// <param name="needle">The needle to find.</param>
+        /// <returns>The part left of the <c>needle</c>.</returns>
+        public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> str, char needle)
+        {
+            var pos = str.IndexOf(needle);
+            return pos == -1 ? str : str[..pos];
+        }
+
+        /// <summary>
+        /// Returns the part left of the <c>needle</c>.
+        /// </summary>
+        /// <param name="str">The string to seek.</param>
+        /// <param name="needle">The needle to find.</param>
+        /// <param name="stringComparison">One of the enumeration values that specifies the rules for the search.</param>
+        /// <returns>The part left of the <c>needle</c>.</returns>
+        public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> str, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
+        {
+            var pos = str.IndexOf(needle, stringComparison);
+            return pos == -1 ? str : str[..pos];
+        }
+    }
+}

+ 4 - 4
MediaBrowser.Model/Entities/ProviderIdsExtensions.cs

@@ -20,7 +20,7 @@ namespace MediaBrowser.Model.Entities
         }
 
         /// <summary>
-        /// Gets a provider id
+        /// Gets a provider id.
         /// </summary>
         /// <param name="instance">The instance.</param>
         /// <param name="provider">The provider.</param>
@@ -31,7 +31,7 @@ namespace MediaBrowser.Model.Entities
         }
 
         /// <summary>
-        /// Gets a provider id
+        /// Gets a provider id.
         /// </summary>
         /// <param name="instance">The instance.</param>
         /// <param name="name">The name.</param>
@@ -53,7 +53,7 @@ namespace MediaBrowser.Model.Entities
         }
 
         /// <summary>
-        /// Sets a provider id
+        /// Sets a provider id.
         /// </summary>
         /// <param name="instance">The instance.</param>
         /// <param name="name">The name.</param>
@@ -89,7 +89,7 @@ namespace MediaBrowser.Model.Entities
         }
 
         /// <summary>
-        /// Sets a provider id
+        /// Sets a provider id.
         /// </summary>
         /// <param name="instance">The instance.</param>
         /// <param name="provider">The provider.</param>

+ 35 - 0
tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs

@@ -0,0 +1,35 @@
+using System;
+using MediaBrowser.Common.Extensions;
+using Xunit;
+
+namespace Jellyfin.Common.Tests.Extensions
+{
+    public class StringExtensionsTests
+    {
+        [Theory]
+        [InlineData("Banana split", ' ', "Banana")]
+        [InlineData("Banana split", 'q', "Banana split")]
+        public void LeftPartCharTest(string str, char needle, string result)
+        {
+            Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString());
+        }
+
+        [Theory]
+        [InlineData("Banana split", " ", "Banana")]
+        [InlineData("Banana split test", " split", "Banana")]
+        public void LeftPartWithoutStringComparisonTest(string str, string needle, string result)
+        {
+            Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString());
+        }
+
+        [Theory]
+        [InlineData("Banana split", " ", StringComparison.Ordinal, "Banana")]
+        [InlineData("Banana split test", " split", StringComparison.Ordinal, "Banana")]
+        [InlineData("Banana split test", " Split", StringComparison.Ordinal, "Banana split test")]
+        [InlineData("Banana split test", " Splït", StringComparison.InvariantCultureIgnoreCase, "Banana split test")]
+        public void LeftPartTest(string str, string needle, StringComparison stringComparison, string result)
+        {
+            Assert.Equal(result, str.AsSpan().LeftPart(needle, stringComparison).ToString());
+        }
+    }
+}

+ 16 - 0
tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs

@@ -0,0 +1,16 @@
+using Emby.Server.Implementations.HttpServer;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.HttpServer
+{
+    public class HttpServerTests
+    {
+        [Theory]
+        [InlineData("This is a clean string.", "This is a clean string.")]
+        [InlineData("This isn't \n\ra clean string.", "This isn't a clean string.")]
+        public void RemoveControlCharactersTest(string input, string result)
+        {
+            Assert.Equal(result, ResponseFilter.RemoveControlCharacters(input));
+        }
+    }
+}

+ 17 - 0
tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs

@@ -0,0 +1,17 @@
+using Emby.Server.Implementations.Library;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Library
+{
+    public class PathExtensionsTests
+    {
+        [Theory]
+        [InlineData("Superman: Red Son [imdbid=tt10985510]", "imdbid", "tt10985510")]
+        [InlineData("Superman: Red Son - tt10985510", "imdbid", "tt10985510")]
+        [InlineData("Superman: Red Son", "imdbid", null)]
+        public void GetAttributeValueTest(string input, string attribute, string? result)
+        {
+            Assert.Equal(result, PathExtensions.GetAttributeValue(input, attribute));
+        }
+    }
+}