diff --git a/MediaBrowser.Common/Providers/ProviderIdParsers.cs b/MediaBrowser.Common/Providers/ProviderIdParsers.cs index 26eaccac5..64c2e1976 100644 --- a/MediaBrowser.Common/Providers/ProviderIdParsers.cs +++ b/MediaBrowser.Common/Providers/ProviderIdParsers.cs @@ -22,12 +22,10 @@ namespace MediaBrowser.Common.Providers /// True if parsing was successful, false otherwise. public static bool TryFindImdbId(ReadOnlySpan text, [NotNullWhen(true)] out ReadOnlySpan imdbId) { - var tt = ImdbPrefix.AsSpan(); - // imdb id is at least 9 chars (tt + 7 numbers) while (text.Length >= 2 + ImdbMinNumbers) { - var ttPos = text.IndexOf(tt); + var ttPos = text.IndexOf(ImdbPrefix); if (ttPos == -1) { imdbId = default; diff --git a/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs b/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs index 1ce54c59b..ef9d31cc1 100644 --- a/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs +++ b/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs @@ -7,59 +7,79 @@ namespace Jellyfin.Common.Tests.Providers public class ProviderIdParserTests { [Theory] - [InlineData("tt123456", false, null)] - [InlineData("tt1234567", true, "tt1234567")] - [InlineData("tt12345678", true, "tt12345678")] - [InlineData("https://www.imdb.com/title/tt123456", false, null)] - [InlineData("https://www.imdb.com/title/tt1234567", true, "tt1234567")] - [InlineData("https://www.imdb.com/title/tt12345678", true, "tt12345678")] - [InlineData(@"multiline\nhttps://www.imdb.com/title/tt1234567", true, "tt1234567")] - [InlineData(@"multiline\nhttps://www.imdb.com/title/tt12345678", true, "tt12345678")] - [InlineData("Jellyfin", false, null)] - [InlineData("tt1234567tt7654321", true, "tt1234567")] - [InlineData("tt12345678tt7654321", true, "tt12345678")] - [InlineData("tt123456789", true, "tt12345678")] - public void Parse_Imdb(string text, bool shouldSucceed, string? imdbId) + [InlineData("tt1234567", "tt1234567")] + [InlineData("tt12345678", "tt12345678")] + [InlineData("https://www.imdb.com/title/tt1234567", "tt1234567")] + [InlineData("https://www.imdb.com/title/tt12345678", "tt12345678")] + [InlineData(@"multiline\nhttps://www.imdb.com/title/tt1234567", "tt1234567")] + [InlineData(@"multiline\nhttps://www.imdb.com/title/tt12345678", "tt12345678")] + [InlineData("tt1234567tt7654321", "tt1234567")] + [InlineData("tt12345678tt7654321", "tt12345678")] + [InlineData("tt123456789", "tt12345678")] + public void FindImdbId_Valid_Success(string text, string expected) { - var succeeded = ProviderIdParsers.TryFindImdbId(text, out ReadOnlySpan parsedId); - Assert.Equal(shouldSucceed, succeeded); - Assert.Equal(imdbId ?? Span.Empty.ToString(), parsedId.ToString()); + Assert.True(ProviderIdParsers.TryFindImdbId(text, out ReadOnlySpan parsedId)); + Assert.Equal(expected, parsedId.ToString()); } [Theory] - [InlineData("https://www.themoviedb.org/movie/30287-fallo", true, "30287")] - [InlineData("themoviedb.org/movie/30287", true, "30287")] - [InlineData("https://www.themoviedb.org/movie/fallo-30287", false, null)] - [InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)] - public void Parse_TmdbMovie(string text, bool shouldSucceed, string? tmdbId) + [InlineData("tt123456")] + [InlineData("https://www.imdb.com/title/tt123456")] + [InlineData("Jellyfin")] + public void FindImdbId_Invalid_Success(string text) { - var succeeded = ProviderIdParsers.TryFindTmdbMovieId(text, out ReadOnlySpan parsedId); - Assert.Equal(shouldSucceed, succeeded); - Assert.Equal(tmdbId ?? Span.Empty.ToString(), parsedId.ToString()); + Assert.False(ProviderIdParsers.TryFindImdbId(text, out _)); } [Theory] - [InlineData("https://www.themoviedb.org/tv/1668-friends", true, "1668")] - [InlineData("themoviedb.org/tv/1668", true, "1668")] - [InlineData("https://www.themoviedb.org/tv/friends-1668", false, null)] - [InlineData("https://www.themoviedb.org/movie/30287-fallo", false, null)] - public void Parse_TmdbSeries(string text, bool shouldSucceed, string? tmdbId) + [InlineData("https://www.themoviedb.org/movie/30287-fallo", "30287")] + [InlineData("themoviedb.org/movie/30287", "30287")] + public void FindTmdbMovieId_Valid_Success(string text, string expected) { - var succeeded = ProviderIdParsers.TryFindTmdbSeriesId(text, out ReadOnlySpan parsedId); - Assert.Equal(shouldSucceed, succeeded); - Assert.Equal(tmdbId ?? Span.Empty.ToString(), parsedId.ToString()); + Assert.True(ProviderIdParsers.TryFindTmdbMovieId(text, out ReadOnlySpan parsedId)); + Assert.Equal(expected, parsedId.ToString()); } [Theory] - [InlineData("https://www.thetvdb.com/?tab=series&id=121361", true, "121361")] - [InlineData("thetvdb.com/?tab=series&id=121361", true, "121361")] - [InlineData("thetvdb.com/?tab=series&id=Jellyfin121361", false, null)] - [InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)] - public void Parse_Tvdb(string text, bool shouldSucceed, string? tvdbId) + [InlineData("https://www.themoviedb.org/movie/fallo-30287")] + [InlineData("https://www.themoviedb.org/tv/1668-friends")] + public void FindTmdbMovieId_Invalid_Success(string text) { - var succeeded = ProviderIdParsers.TryFindTvdbId(text, out ReadOnlySpan parsedId); - Assert.Equal(shouldSucceed, succeeded); - Assert.Equal(tvdbId ?? Span.Empty.ToString(), parsedId.ToString()); + Assert.False(ProviderIdParsers.TryFindTmdbMovieId(text, out _)); + } + + [Theory] + [InlineData("https://www.themoviedb.org/tv/1668-friends", "1668")] + [InlineData("themoviedb.org/tv/1668", "1668")] + public void FindTmdbSeriesId_Valid_Success(string text, string expected) + { + Assert.True(ProviderIdParsers.TryFindTmdbSeriesId(text, out ReadOnlySpan parsedId)); + Assert.Equal(expected, parsedId.ToString()); + } + + [Theory] + [InlineData("https://www.themoviedb.org/tv/friends-1668")] + [InlineData("https://www.themoviedb.org/movie/30287-fallo")] + public void FindTmdbSeriesId_Invalid_Success(string text) + { + Assert.False(ProviderIdParsers.TryFindTmdbSeriesId(text, out _)); + } + + [Theory] + [InlineData("https://www.thetvdb.com/?tab=series&id=121361", "121361")] + [InlineData("thetvdb.com/?tab=series&id=121361", "121361")] + public void FindTvdbId_Valid_Success(string text, string expected) + { + Assert.True(ProviderIdParsers.TryFindTvdbId(text, out ReadOnlySpan parsedId)); + Assert.Equal(expected, parsedId.ToString()); + } + + [Theory] + [InlineData("thetvdb.com/?tab=series&id=Jellyfin121361")] + [InlineData("https://www.themoviedb.org/tv/1668-friends")] + public void FindTvdbId_Invalid_Success(string text) + { + Assert.False(ProviderIdParsers.TryFindTvdbId(text, out _)); } } }