Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions for subtitle time duration in milliseconds #9047

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Test/Logic/UnknownFormatImporterCsvTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core.Common;

namespace Test.Logic
Expand Down Expand Up @@ -48,9 +49,14 @@ public void TestUnknownCsv3()
Assert.AreEqual(2, subtitle.Paragraphs.Count);
Assert.AreEqual("Ja. Ich habe den Test gemacht.", subtitle.Paragraphs[1].Text);

// assert the duration of milliseconds
Assert.AreEqual(10000, subtitle.Paragraphs[1].StartTime.TotalMilliseconds);
Assert.AreEqual(12000, subtitle.Paragraphs[1].EndTime.TotalMilliseconds);

//TODO: fails on appveyor... why?
//Assert.AreEqual("10,000", subtitle.Paragraphs[1].StartTime.ToShortString());
//Assert.AreEqual("12,000", subtitle.Paragraphs[1].EndTime.ToShortString());
// Assert formatting of timecodes
// Assert.AreEqual("10,000", subtitle.Paragraphs[1].StartTime.ToShortString());
// Assert.AreEqual("12,000", subtitle.Paragraphs[1].EndTime.ToShortString());
}
}
}
34 changes: 25 additions & 9 deletions src/libse/Common/UnknownFormatImporterCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public Subtitle AutoGuessImport(List<string> lines)

var csvLines = ReadCsvLines(headers, lines, separator);

var subtitle = MakeSubtitle(csvLines);

return subtitle;
return MakeSubtitle(csvLines);
}

private Subtitle MakeSubtitle(List<CsvLine> csvLines)
Expand Down Expand Up @@ -249,20 +247,38 @@ private bool HasValidHeader(List<string> headers)
private static char DetectSeparator(List<string> lines)
{
const char defaultSeparator = ',';
char[] potentialSeparators = { defaultSeparator, ';', '\t' };

if (lines == null || lines.Count < 2)
if (lines == null)
{
return defaultSeparator;
}

var noSpaceLines = lines.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
if (lines.Count == 1)
{
var semicolonCount = lines[0].Count(c => c == ';');
var commaCount = lines[0].Count(c => c == ',');
var tabCount = lines[0].Count(c => c == '\t');

if (semicolonCount > commaCount)
{
return semicolonCount > tabCount ? ';' : '\t';
}

return commaCount > tabCount ? defaultSeparator : '\t';
}

char[] potentialSeparators = { defaultSeparator, ';', '\t' };

var noSpaceLines = lines
.Where(p => !string.IsNullOrWhiteSpace(p))
.Take(2).ToArray();

foreach (var separator in potentialSeparators)
{
var firstLineParts = noSpaceLines[0].Split(separator);
var secondLineParts = noSpaceLines[1].Split(separator);
var firstLineTokens = noSpaceLines[0].Split(separator);
var secondLineTokens = noSpaceLines[1].Split(separator);

if (firstLineParts.Length > 1 && secondLineParts.Length == firstLineParts.Length)
if (firstLineTokens.Length > 1 && firstLineTokens.Length == secondLineTokens.Length)
{
return separator;
}
Expand Down