Work on unit tests

This commit is contained in:
niksedk 2023-11-25 16:20:15 +01:00
parent 387c50aba8
commit 40bcfe3525
4 changed files with 57 additions and 10 deletions

View File

@ -0,0 +1,46 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core.Common;
namespace Test.Core
{
[TestClass]
public class MergeShortLinesUtilsTest
{
[TestMethod]
public void ThreeShortLines()
{
var subtitle = new Subtitle();
subtitle.Paragraphs.Add(new Paragraph("How", 0, 200));
subtitle.Paragraphs.Add(new Paragraph("are", 200, 400));
subtitle.Paragraphs.Add(new Paragraph("you?", 400, 600));
var mergedSubtitle = MergeShortLinesUtils.MergeShortLinesInSubtitle(subtitle, 500, 80, true);
Assert.AreEqual(1, mergedSubtitle.Paragraphs.Count);
Assert.AreEqual("How are you?", mergedSubtitle.Paragraphs[0].Text);
}
[TestMethod]
public void ThreeShortLinesNoMergeDueToLength()
{
var subtitle = new Subtitle();
subtitle.Paragraphs.Add(new Paragraph("How", 0, 200));
subtitle.Paragraphs.Add(new Paragraph("are", 200, 400));
subtitle.Paragraphs.Add(new Paragraph("you?", 400, 600));
var mergedSubtitle = MergeShortLinesUtils.MergeShortLinesInSubtitle(subtitle, 500, 2, true);
Assert.AreEqual(3, mergedSubtitle.Paragraphs.Count);
}
[TestMethod]
public void ThreeShortLinesNoMergeDueToGap()
{
var subtitle = new Subtitle();
subtitle.Paragraphs.Add(new Paragraph("How", 0, 200));
subtitle.Paragraphs.Add(new Paragraph("are", 2000, 2400));
subtitle.Paragraphs.Add(new Paragraph("you?", 4400, 4600));
var mergedSubtitle = MergeShortLinesUtils.MergeShortLinesInSubtitle(subtitle, 500, 80, true);
Assert.AreEqual(3, mergedSubtitle.Paragraphs.Count);
}
}
}

View File

@ -64,6 +64,7 @@
<Compile Include="Assa\ResamplerTest.cs" /> <Compile Include="Assa\ResamplerTest.cs" />
<Compile Include="Assa\AssaTimeCodes.cs" /> <Compile Include="Assa\AssaTimeCodes.cs" />
<Compile Include="Core\AudioToTextTest.cs" /> <Compile Include="Core\AudioToTextTest.cs" />
<Compile Include="Core\MergeShortLinesUtilsTest.cs" />
<Compile Include="Core\CsvUtilTest.cs" /> <Compile Include="Core\CsvUtilTest.cs" />
<Compile Include="Core\WebVttHelperTest.cs" /> <Compile Include="Core\WebVttHelperTest.cs" />
<Compile Include="Core\WebVttToAssaTest.cs" /> <Compile Include="Core\WebVttToAssaTest.cs" />

View File

@ -6,11 +6,11 @@ namespace Nikse.SubtitleEdit.Core.Common
{ {
public static Subtitle MergeShortLinesInSubtitle(Subtitle subtitle, double maxMillisecondsBetweenLines, int maxCharacters, bool onlyContinuousLines) public static Subtitle MergeShortLinesInSubtitle(Subtitle subtitle, double maxMillisecondsBetweenLines, int maxCharacters, bool onlyContinuousLines)
{ {
string language = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle); var language = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);
var mergedSubtitle = new Subtitle(); var mergedSubtitle = new Subtitle();
bool lastMerged = false; var lastMerged = false;
Paragraph p = null; Paragraph p = null;
for (int i = 1; i < subtitle.Paragraphs.Count; i++) for (var i = 1; i < subtitle.Paragraphs.Count; i++)
{ {
if (!lastMerged) if (!lastMerged)
{ {
@ -22,12 +22,11 @@ namespace Nikse.SubtitleEdit.Core.Common
{ {
if (Utilities.QualifiesForMerge(p, next, maxMillisecondsBetweenLines, maxCharacters, onlyContinuousLines)) if (Utilities.QualifiesForMerge(p, next, maxMillisecondsBetweenLines, maxCharacters, onlyContinuousLines))
{ {
if (GetStartTag(p.Text) == GetStartTag(next.Text) && if (GetStartTag(p.Text) == GetStartTag(next.Text) && GetEndTag(p.Text) == GetEndTag(next.Text))
GetEndTag(p.Text) == GetEndTag(next.Text))
{ {
string s1 = p.Text.Trim(); var s1 = p.Text.Trim();
s1 = s1.Substring(0, s1.Length - GetEndTag(s1).Length); s1 = s1.Substring(0, s1.Length - GetEndTag(s1).Length);
string s2 = next.Text.Trim(); var s2 = next.Text.Trim();
s2 = s2.Substring(GetStartTag(s2).Length); s2 = s2.Substring(GetStartTag(s2).Length);
p.Text = Utilities.AutoBreakLine(s1 + Environment.NewLine + s2, language); p.Text = Utilities.AutoBreakLine(s1 + Environment.NewLine + s2, language);
} }
@ -71,7 +70,7 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
var endTag = string.Empty; var endTag = string.Empty;
int start = text.LastIndexOf("</", StringComparison.Ordinal); var start = text.LastIndexOf("</", StringComparison.Ordinal);
if (start > 0 && start >= text.Length - 8) if (start > 0 && start >= text.Length - 8)
{ {
endTag = text.Substring(start); endTag = text.Substring(start);
@ -93,11 +92,12 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
var startTag = string.Empty; var startTag = string.Empty;
int end = text.IndexOf('>'); var end = text.IndexOf('>');
if (end > 0 && end < 25) if (end > 0 && end < 25)
{ {
startTag = text.Substring(0, end + 1); startTag = text.Substring(0, end + 1);
} }
return startTag; return startTag;
} }
} }