From 7f4a229cd2fee89fdd8329c9c9f907e381d45c46 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sun, 19 Apr 2020 15:18:28 +0200 Subject: [PATCH 1/3] Add some simple tests --- .../HttpServer/ResponseFilter.cs | 4 ++ .../Library/MediaStreamSelector.cs | 6 ++- .../Library/PathExtensions.cs | 22 ++++++----- .../Library/ResolverHelper.cs | 2 + .../Services/StringMapTypeDeserializer.cs | 16 +------- .../Services/UrlExtensions.cs | 20 ++-------- .../SocketSharp/WebSocketSharpRequest.cs | 16 ++------ .../Playback/BaseStreamingService.cs | 3 +- .../Extensions/BaseExtensions.cs | 2 + .../Extensions/CopyToExtensions.cs | 2 + .../Extensions/MethodNotAllowedException.cs | 2 + .../Extensions/ProcessExtensions.cs | 2 + .../Extensions/RateLimitExceededException.cs | 1 + .../Extensions/ResourceNotFoundException.cs | 2 + .../Extensions/ShuffleExtensions.cs | 2 + .../Extensions/StringExtensions.cs | 37 +++++++++++++++++++ .../Entities/ProviderIdsExtensions.cs | 8 ++-- .../Extensions/StringExtensionsTests.cs | 35 ++++++++++++++++++ .../HttpServer/ResponseFilterTests.cs | 16 ++++++++ .../Library/PathExtensionsTests.cs | 17 +++++++++ 20 files changed, 155 insertions(+), 60 deletions(-) create mode 100644 MediaBrowser.Common/Extensions/StringExtensions.cs create mode 100644 tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs create mode 100644 tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs create mode 100644 tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs diff --git a/Emby.Server.Implementations/HttpServer/ResponseFilter.cs b/Emby.Server.Implementations/HttpServer/ResponseFilter.cs index 5e0466629d..4089aa578e 100644 --- a/Emby.Server.Implementations/HttpServer/ResponseFilter.cs +++ b/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); diff --git a/Emby.Server.Implementations/Library/MediaStreamSelector.cs b/Emby.Server.Implementations/Library/MediaStreamSelector.cs index 6b9f4d052c..e27145a1d2 100644 --- a/Emby.Server.Implementations/Library/MediaStreamSelector.cs +++ b/Emby.Server.Implementations/Library/MediaStreamSelector.cs @@ -35,7 +35,8 @@ namespace Emby.Server.Implementations.Library return null; } - public static int? GetDefaultSubtitleStreamIndex(List streams, + public static int? GetDefaultSubtitleStreamIndex( + List 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 streams, + public static void SetSubtitleStreamScores( + List streams, string[] preferredLanguages, SubtitlePlaybackMode mode, string audioTrackLanguage) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index 1d61ed57eb..b74cad067b 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/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. /// /// The STR. - /// The attrib. + /// The attrib. /// System.String. - /// attrib - public static string GetAttributeValue(this string str, string attrib) + /// str or attribute is empty. + 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; diff --git a/Emby.Server.Implementations/Library/ResolverHelper.cs b/Emby.Server.Implementations/Library/ResolverHelper.cs index 34dcbbe285..7ca15b4e55 100644 --- a/Emby.Server.Implementations/Library/ResolverHelper.cs +++ b/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)); diff --git a/Emby.Server.Implementations/Services/StringMapTypeDeserializer.cs b/Emby.Server.Implementations/Services/StringMapTypeDeserializer.cs index 23e22afd58..56e23d5492 100644 --- a/Emby.Server.Implementations/Services/StringMapTypeDeserializer.cs +++ b/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 diff --git a/Emby.Server.Implementations/Services/UrlExtensions.cs b/Emby.Server.Implementations/Services/UrlExtensions.cs index 5d4407f3b8..483c63ade7 100644 --- a/Emby.Server.Implementations/Services/UrlExtensions.cs +++ b/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); - } } } diff --git a/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs b/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs index 1781df8b51..9c638f4395 100644 --- a/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs +++ b/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 LeftPart(ReadOnlySpan strVal, char needle) - { - if (strVal == null) - { - return null; - } - - var pos = strVal.IndexOf(needle); - return pos == -1 ? strVal : strVal.Slice(0, pos); - } } } diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 7705393576..928ca16128 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/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; } diff --git a/MediaBrowser.Common/Extensions/BaseExtensions.cs b/MediaBrowser.Common/Extensions/BaseExtensions.cs index 08964420e7..40020093b6 100644 --- a/MediaBrowser.Common/Extensions/BaseExtensions.cs +++ b/MediaBrowser.Common/Extensions/BaseExtensions.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Security.Cryptography; using System.Text; diff --git a/MediaBrowser.Common/Extensions/CopyToExtensions.cs b/MediaBrowser.Common/Extensions/CopyToExtensions.cs index 2ecbc6539b..94bf7c7401 100644 --- a/MediaBrowser.Common/Extensions/CopyToExtensions.cs +++ b/MediaBrowser.Common/Extensions/CopyToExtensions.cs @@ -1,3 +1,5 @@ +#nullable enable + using System.Collections.Generic; namespace MediaBrowser.Common.Extensions diff --git a/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs b/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs index 48e758ee4c..258bd6662c 100644 --- a/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs +++ b/MediaBrowser.Common/Extensions/MethodNotAllowedException.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; namespace MediaBrowser.Common.Extensions diff --git a/MediaBrowser.Common/Extensions/ProcessExtensions.cs b/MediaBrowser.Common/Extensions/ProcessExtensions.cs index c747871222..2f52ba196a 100644 --- a/MediaBrowser.Common/Extensions/ProcessExtensions.cs +++ b/MediaBrowser.Common/Extensions/ProcessExtensions.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Diagnostics; using System.Threading; diff --git a/MediaBrowser.Common/Extensions/RateLimitExceededException.cs b/MediaBrowser.Common/Extensions/RateLimitExceededException.cs index 95802a4626..7c7bdaa92f 100644 --- a/MediaBrowser.Common/Extensions/RateLimitExceededException.cs +++ b/MediaBrowser.Common/Extensions/RateLimitExceededException.cs @@ -1,3 +1,4 @@ +#nullable enable #pragma warning disable CS1591 using System; diff --git a/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs b/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs index 22130c5a1e..ebac9d8e6b 100644 --- a/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs +++ b/MediaBrowser.Common/Extensions/ResourceNotFoundException.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; namespace MediaBrowser.Common.Extensions diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs index 0432f36b57..459bec1105 100644 --- a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs +++ b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Collections.Generic; diff --git a/MediaBrowser.Common/Extensions/StringExtensions.cs b/MediaBrowser.Common/Extensions/StringExtensions.cs new file mode 100644 index 0000000000..2ac29f8e52 --- /dev/null +++ b/MediaBrowser.Common/Extensions/StringExtensions.cs @@ -0,0 +1,37 @@ +#nullable enable + +using System; + +namespace MediaBrowser.Common.Extensions +{ + /// + /// Extensions methods to simplify string operations. + /// + public static class StringExtensions + { + /// + /// Returns the part left of the needle. + /// + /// The string to seek. + /// The needle to find. + /// The part left of the needle. + public static ReadOnlySpan LeftPart(this ReadOnlySpan str, char needle) + { + var pos = str.IndexOf(needle); + return pos == -1 ? str : str[..pos]; + } + + /// + /// Returns the part left of the needle. + /// + /// The string to seek. + /// The needle to find. + /// One of the enumeration values that specifies the rules for the search. + /// The part left of the needle. + public static ReadOnlySpan LeftPart(this ReadOnlySpan str, ReadOnlySpan needle, StringComparison stringComparison = default) + { + var pos = str.IndexOf(needle, stringComparison); + return pos == -1 ? str : str[..pos]; + } + } +} diff --git a/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs b/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs index cd387bd546..922eb4ca79 100644 --- a/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs +++ b/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs @@ -20,7 +20,7 @@ namespace MediaBrowser.Model.Entities } /// - /// Gets a provider id + /// Gets a provider id. /// /// The instance. /// The provider. @@ -31,7 +31,7 @@ namespace MediaBrowser.Model.Entities } /// - /// Gets a provider id + /// Gets a provider id. /// /// The instance. /// The name. @@ -53,7 +53,7 @@ namespace MediaBrowser.Model.Entities } /// - /// Sets a provider id + /// Sets a provider id. /// /// The instance. /// The name. @@ -89,7 +89,7 @@ namespace MediaBrowser.Model.Entities } /// - /// Sets a provider id + /// Sets a provider id. /// /// The instance. /// The provider. diff --git a/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs b/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs new file mode 100644 index 0000000000..89e7e8fde2 --- /dev/null +++ b/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()); + } + } +} diff --git a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs new file mode 100644 index 0000000000..42d128dc68 --- /dev/null +++ b/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)); + } + } +} diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs new file mode 100644 index 0000000000..7053ed3297 --- /dev/null +++ b/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)); + } + } +} From 958681cdffddc7ea24d92d126badb3372cfa5d41 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Mon, 20 Apr 2020 10:16:22 +0200 Subject: [PATCH 2/3] Cover more branches --- .../Extensions/StringExtensionsTests.cs | 6 +++--- .../HttpServer/ResponseFilterTests.cs | 6 ++++-- .../Library/PathExtensionsTests.cs | 12 +++++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs b/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs index 89e7e8fde2..0ed7673fdb 100644 --- a/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs +++ b/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs @@ -9,7 +9,7 @@ namespace Jellyfin.Common.Tests.Extensions [Theory] [InlineData("Banana split", ' ', "Banana")] [InlineData("Banana split", 'q', "Banana split")] - public void LeftPartCharTest(string str, char needle, string result) + public void LeftPart_ValidArgsCharNeedle_Correct(string str, char needle, string result) { Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString()); } @@ -17,7 +17,7 @@ namespace Jellyfin.Common.Tests.Extensions [Theory] [InlineData("Banana split", " ", "Banana")] [InlineData("Banana split test", " split", "Banana")] - public void LeftPartWithoutStringComparisonTest(string str, string needle, string result) + public void LeftPart_ValidArgsWithoutStringComparison_Correct(string str, string needle, string result) { Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString()); } @@ -27,7 +27,7 @@ namespace Jellyfin.Common.Tests.Extensions [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) + public void LeftPart_ValidArgs_Correct(string str, string needle, StringComparison stringComparison, string result) { Assert.Equal(result, str.AsSpan().LeftPart(needle, stringComparison).ToString()); } diff --git a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs index 42d128dc68..39bd94b598 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/ResponseFilterTests.cs @@ -3,12 +3,14 @@ using Xunit; namespace Jellyfin.Server.Implementations.Tests.HttpServer { - public class HttpServerTests + public class ResponseFilterTests { [Theory] + [InlineData(null, null)] + [InlineData("", "")] [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) + public void RemoveControlCharacters_ValidArgs_Correct(string? input, string? result) { Assert.Equal(result, ResponseFilter.RemoveControlCharacters(input)); } diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index 7053ed3297..d2ed0d9257 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -1,3 +1,4 @@ +using System; using Emby.Server.Implementations.Library; using Xunit; @@ -9,9 +10,18 @@ namespace Jellyfin.Server.Implementations.Tests.Library [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) + public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? result) { Assert.Equal(result, PathExtensions.GetAttributeValue(input, attribute)); } + + [Theory] + [InlineData("", "")] + [InlineData("Superman: Red Son [imdbid=tt10985510]", "")] + [InlineData("", "imdbid")] + public void GetAttributeValue_EmptyString_ThrowsArgumentException(string input, string attribute) + { + Assert.Throws(() => PathExtensions.GetAttributeValue(input, attribute)); + } } } From c430a7ed8faa40788c32b89852310981b7c1cf83 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Tue, 21 Apr 2020 10:18:26 +0200 Subject: [PATCH 3/3] Address comments --- .../Library/PathExtensions.cs | 2 +- .../Extensions/StringExtensions.cs | 22 +++++++++---------- .../Extensions/StringExtensionsTests.cs | 20 ++++++++++++----- .../Library/PathExtensionsTests.cs | 4 ++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index b74cad067b..06ff3e611b 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -16,7 +16,7 @@ namespace Emby.Server.Implementations.Library /// The STR. /// The attrib. /// System.String. - /// str or attribute is empty. + /// or is empty. public static string? GetAttributeValue(this string str, string attribute) { if (str.Length == 0) diff --git a/MediaBrowser.Common/Extensions/StringExtensions.cs b/MediaBrowser.Common/Extensions/StringExtensions.cs index 2ac29f8e52..7643017412 100644 --- a/MediaBrowser.Common/Extensions/StringExtensions.cs +++ b/MediaBrowser.Common/Extensions/StringExtensions.cs @@ -10,28 +10,28 @@ namespace MediaBrowser.Common.Extensions public static class StringExtensions { /// - /// Returns the part left of the needle. + /// Returns the part on the left of the needle. /// - /// The string to seek. + /// The string to seek. /// The needle to find. - /// The part left of the needle. - public static ReadOnlySpan LeftPart(this ReadOnlySpan str, char needle) + /// The part left of the . + public static ReadOnlySpan LeftPart(this ReadOnlySpan haystack, char needle) { - var pos = str.IndexOf(needle); - return pos == -1 ? str : str[..pos]; + var pos = haystack.IndexOf(needle); + return pos == -1 ? haystack : haystack[..pos]; } /// - /// Returns the part left of the needle. + /// Returns the part on the left of the needle. /// - /// The string to seek. + /// The string to seek. /// The needle to find. /// One of the enumeration values that specifies the rules for the search. /// The part left of the needle. - public static ReadOnlySpan LeftPart(this ReadOnlySpan str, ReadOnlySpan needle, StringComparison stringComparison = default) + public static ReadOnlySpan LeftPart(this ReadOnlySpan haystack, ReadOnlySpan needle, StringComparison stringComparison = default) { - var pos = str.IndexOf(needle, stringComparison); - return pos == -1 ? str : str[..pos]; + var pos = haystack.IndexOf(needle, stringComparison); + return pos == -1 ? haystack : haystack[..pos]; } } } diff --git a/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs b/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs index 0ed7673fdb..8bf613f05f 100644 --- a/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs +++ b/tests/Jellyfin.Common.Tests/Extensions/StringExtensionsTests.cs @@ -7,29 +7,37 @@ namespace Jellyfin.Common.Tests.Extensions public class StringExtensionsTests { [Theory] + [InlineData("", 'q', "")] [InlineData("Banana split", ' ', "Banana")] [InlineData("Banana split", 'q', "Banana split")] - public void LeftPart_ValidArgsCharNeedle_Correct(string str, char needle, string result) + public void LeftPart_ValidArgsCharNeedle_Correct(string str, char needle, string expectedResult) { - Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString()); + var result = str.AsSpan().LeftPart(needle).ToString(); + Assert.Equal(expectedResult, result); } [Theory] + [InlineData("", "", "")] + [InlineData("", "q", "")] + [InlineData("Banana split", "", "")] [InlineData("Banana split", " ", "Banana")] [InlineData("Banana split test", " split", "Banana")] - public void LeftPart_ValidArgsWithoutStringComparison_Correct(string str, string needle, string result) + public void LeftPart_ValidArgsWithoutStringComparison_Correct(string str, string needle, string expectedResult) { - Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString()); + var result = str.AsSpan().LeftPart(needle).ToString(); + Assert.Equal(expectedResult, result); } [Theory] + [InlineData("", "", StringComparison.Ordinal, "")] [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 LeftPart_ValidArgs_Correct(string str, string needle, StringComparison stringComparison, string result) + public void LeftPart_ValidArgs_Correct(string str, string needle, StringComparison stringComparison, string expectedResult) { - Assert.Equal(result, str.AsSpan().LeftPart(needle, stringComparison).ToString()); + var result = str.AsSpan().LeftPart(needle, stringComparison).ToString(); + Assert.Equal(expectedResult, result); } } } diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index d2ed0d9257..c771f5f4ae 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -10,9 +10,9 @@ namespace Jellyfin.Server.Implementations.Tests.Library [InlineData("Superman: Red Son [imdbid=tt10985510]", "imdbid", "tt10985510")] [InlineData("Superman: Red Son - tt10985510", "imdbid", "tt10985510")] [InlineData("Superman: Red Son", "imdbid", null)] - public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? result) + public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult) { - Assert.Equal(result, PathExtensions.GetAttributeValue(input, attribute)); + Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute)); } [Theory]