jellyfin-server/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SsaParserTests.cs

89 lines
3.4 KiB
C#
Raw Normal View History

2021-01-08 22:03:02 +00:00
using System;
2021-01-09 15:42:21 +00:00
using System.Collections.Generic;
2021-01-08 22:03:02 +00:00
using System.Globalization;
using System.IO;
2021-01-09 15:42:21 +00:00
using System.Text;
2021-01-08 22:03:02 +00:00
using System.Threading;
using MediaBrowser.MediaEncoding.Subtitles;
2021-01-09 15:42:21 +00:00
using MediaBrowser.Model.MediaInfo;
2021-01-08 22:21:50 +00:00
using Microsoft.Extensions.Logging.Abstractions;
2021-01-08 22:03:02 +00:00
using Xunit;
namespace Jellyfin.MediaEncoding.Subtitles.Tests
{
public class SsaParserTests
{
2021-01-09 15:42:21 +00:00
private readonly SsaParser _parser = new SsaParser(new NullLogger<AssParser>());
[Theory]
[MemberData(nameof(Parse_MultipleDialogues_TestData))]
public void Parse_MultipleDialogues_Success(string ssa, IReadOnlyList<SubtitleTrackEvent> expectedSubtitleTrackEvents)
2021-01-09 15:42:21 +00:00
{
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
{
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, CancellationToken.None);
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
for (int i = 0; i < expectedSubtitleTrackEvents.Count; ++i)
{
SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
Assert.Equal(expected.Id, actual.Id);
Assert.Equal(expected.Text, actual.Text);
Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
}
}
}
public static IEnumerable<object[]> Parse_MultipleDialogues_TestData()
{
yield return new object[]
{
@"[Events]
Format: Layer, Start, End, Text
Dialogue: ,0:00:01.18,0:00:01.85,dialogue1
Dialogue: ,0:00:02.18,0:00:02.85,dialogue2
Dialogue: ,0:00:03.18,0:00:03.85,dialogue3
",
new List<SubtitleTrackEvent>
{
new SubtitleTrackEvent("1", "dialogue1")
{
StartPositionTicks = 11800000,
EndPositionTicks = 18500000
},
new SubtitleTrackEvent("2", "dialogue2")
{
StartPositionTicks = 21800000,
EndPositionTicks = 28500000
},
new SubtitleTrackEvent("3", "dialogue3")
{
StartPositionTicks = 31800000,
EndPositionTicks = 38500000
}
}
};
}
2021-01-08 22:03:02 +00:00
[Fact]
public void Parse_Valid_Success()
{
using (var stream = File.OpenRead("Test Data/example.ssa"))
{
2021-01-09 15:42:21 +00:00
var parsed = _parser.Parse(stream, CancellationToken.None);
2021-01-08 22:03:02 +00:00
Assert.Single(parsed.TrackEvents);
var trackEvent = parsed.TrackEvents[0];
Assert.Equal("1", trackEvent.Id);
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
Assert.Equal("{\\pos(400,570)}Like an angel with pity on nobody", trackEvent.Text);
}
}
}
}