commit
5922c35d69
@ -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 on the left of the <c>needle</c>.
|
||||
/// </summary>
|
||||
/// <param name="haystack">The string to seek.</param>
|
||||
/// <param name="needle">The needle to find.</param>
|
||||
/// <returns>The part left of the <paramref name="needle" />.</returns>
|
||||
public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
|
||||
{
|
||||
var pos = haystack.IndexOf(needle);
|
||||
return pos == -1 ? haystack : haystack[..pos];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the part on the left of the <c>needle</c>.
|
||||
/// </summary>
|
||||
/// <param name="haystack">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> haystack, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
|
||||
{
|
||||
var pos = haystack.IndexOf(needle, stringComparison);
|
||||
return pos == -1 ? haystack : haystack[..pos];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using Xunit;
|
||||
|
||||
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 expectedResult)
|
||||
{
|
||||
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 expectedResult)
|
||||
{
|
||||
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 expectedResult)
|
||||
{
|
||||
var result = str.AsSpan().LeftPart(needle, stringComparison).ToString();
|
||||
Assert.Equal(expectedResult, result);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using Emby.Server.Implementations.HttpServer;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.HttpServer
|
||||
{
|
||||
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 RemoveControlCharacters_ValidArgs_Correct(string? input, string? result)
|
||||
{
|
||||
Assert.Equal(result, ResponseFilter.RemoveControlCharacters(input));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
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 GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult)
|
||||
{
|
||||
Assert.Equal(expectedResult, 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<ArgumentException>(() => PathExtensions.GetAttributeValue(input, attribute));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue