aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs
blob: 0b8b1f644b4706ce6ffaa09c324823a7362e1478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Emby.Naming.ExternalFiles;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Globalization;
using Moq;
using Xunit;

namespace Jellyfin.Naming.Tests.ExternalFiles;

public class ExternalPathParserTests
{
    private readonly ExternalPathParser _audioPathParser;
    private readonly ExternalPathParser _subtitlePathParser;

    public ExternalPathParserTests()
    {
        var englishCultureDto = new CultureDto("English", "English", "en", new[] { "eng" });
        var frenchCultureDto = new CultureDto("French", "French", "fr", new[] { "fre", "fra" });
        var hindiCultureDto = new CultureDto("Hindi", "Hindi", "hi", new[] { "hin" });

        var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
        localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex("en.*", RegexOptions.IgnoreCase)))
            .Returns(englishCultureDto);
        localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex("fr.*", RegexOptions.IgnoreCase)))
            .Returns(frenchCultureDto);
        localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex("hi.*", RegexOptions.IgnoreCase)))
            .Returns(hindiCultureDto);

        _audioPathParser = new ExternalPathParser(new NamingOptions(), localizationManager.Object, DlnaProfileType.Audio);
        _subtitlePathParser = new ExternalPathParser(new NamingOptions(), localizationManager.Object, DlnaProfileType.Subtitle);
    }

    [Theory]
    [InlineData("")]
    [InlineData("MyVideo.ass")]
    [InlineData("MyVideo.mks")]
    [InlineData("MyVideo.sami")]
    [InlineData("MyVideo.srt")]
    [InlineData("MyVideo.m4v")]
    public void ParseFile_AudioExtensionsNotMatched_ReturnsNull(string path)
    {
        Assert.Null(_audioPathParser.ParseFile(path, string.Empty));
    }

    [Theory]
    [InlineData("MyVideo.aa")]
    [InlineData("MyVideo.aac")]
    [InlineData("MyVideo.flac")]
    [InlineData("MyVideo.m4a")]
    [InlineData("MyVideo.mka")]
    [InlineData("MyVideo.mp3")]
    public void ParseFile_AudioExtensionsMatched_ReturnsPath(string path)
    {
        var actual = _audioPathParser.ParseFile(path, string.Empty);
        Assert.NotNull(actual);
        Assert.Equal(path, actual!.Path);
    }

    [Theory]
    [InlineData("")]
    [InlineData("MyVideo.aa")]
    [InlineData("MyVideo.aac")]
    [InlineData("MyVideo.flac")]
    [InlineData("MyVideo.mka")]
    [InlineData("MyVideo.m4v")]
    public void ParseFile_SubtitleExtensionsNotMatched_ReturnsNull(string path)
    {
        Assert.Null(_subtitlePathParser.ParseFile(path, string.Empty));
    }

    [Theory]
    [InlineData("MyVideo.ass")]
    [InlineData("MyVideo.mks")]
    [InlineData("MyVideo.sami")]
    [InlineData("MyVideo.srt")]
    [InlineData("MyVideo.vtt")]
    public void ParseFile_SubtitleExtensionsMatched_ReturnsPath(string path)
    {
        var actual = _subtitlePathParser.ParseFile(path, string.Empty);
        Assert.NotNull(actual);
        Assert.Equal(path, actual!.Path);
    }

    [Theory]
    [InlineData("", null, null)]
    [InlineData(".default", null, null, true, false)]
    [InlineData(".forced", null, null, false, true)]
    [InlineData(".foreign", null, null, false, true)]
    [InlineData(".default.forced", null, null, true, true)]
    [InlineData(".forced.default", null, null, true, true)]
    [InlineData(".DEFAULT.FORCED", null, null, true, true)]
    [InlineData(".en", null, "eng")]
    [InlineData(".EN", null, "eng")]
    [InlineData(".hi", null, "hin")]
    [InlineData(".fr.en", "fr", "eng")]
    [InlineData(".en.fr", "en", "fre")]
    [InlineData(".title.en.fr", "title.en", "fre")]
    [InlineData(".Title Goes Here", "Title Goes Here", null)]
    [InlineData(".Title.with.Separator", "Title.with.Separator", null)]
    [InlineData(".title.en.default.forced", "title", "eng", true, true)]
    [InlineData(".forced.default.en.title", "title", "eng", true, true)]
    [InlineData(".sdh.en.title", "title", "eng", false, false, true)]
    [InlineData(".en.cc.title", "title", "eng", false, false, true)]
    [InlineData(".hi.en.title", "title", "eng", false, false, true)]
    [InlineData(".en.hi.title", "title", "eng", false, false, true)]
    [InlineData(".Subs for Chinese Audio.eng", "Subs for Chinese Audio", "eng", false, false, false)]
    public void ParseFile_ExtraTokens_ParseToValues(string tokens, string? title, string? language, bool isDefault = false, bool isForced = false, bool isHearingImpaired = false)
    {
        var path = "My.Video" + tokens + ".srt";

        var actual = _subtitlePathParser.ParseFile(path, tokens);

        Assert.NotNull(actual);
        Assert.Equal(title, actual!.Title);
        Assert.Equal(language, actual.Language);
        Assert.Equal(isDefault, actual.IsDefault);
        Assert.Equal(isForced, actual.IsForced);
        Assert.Equal(isHearingImpaired, actual.IsHearingImpaired);
    }
}