From f60aa36e81de3bb39fd0d392626131b497c0f10e Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 28 Jun 2014 17:00:27 +0200 Subject: [PATCH] Added unit tests for FixEllipsesStart --- src/Forms/ChooseLanguage.cs | 2 +- src/Forms/FixCommonErrors.cs | 122 ++++++----------------- src/Logic/Forms/FixCommonErrorsHelper.cs | 83 +++++++++++++++ src/SubtitleEdit.csproj | 3 +- src/Test/FixCommonErrorsTest.cs | 70 ++++++++++++- 5 files changed, 182 insertions(+), 98 deletions(-) create mode 100644 src/Logic/Forms/FixCommonErrorsHelper.cs diff --git a/src/Forms/ChooseLanguage.cs b/src/Forms/ChooseLanguage.cs index 5efe98b36..9cc5e2cf7 100644 --- a/src/Forms/ChooseLanguage.cs +++ b/src/Forms/ChooseLanguage.cs @@ -129,7 +129,7 @@ namespace Nikse.SubtitleEdit.Forms } else if (e.Shift && e.Control && e.Alt && e.KeyCode == Keys.C) { - XmlDeserializerGenerator.GenerateCSharpXmlDeserializerForLanguageStructure(); + LanguageDeserializerGenerator.GenerateCSharpXmlDeserializerForLanguage(); } else if (e.KeyCode == Keys.F1) { diff --git a/src/Forms/FixCommonErrors.cs b/src/Forms/FixCommonErrors.cs index 6fdd9241f..44adfef46 100644 --- a/src/Forms/FixCommonErrors.cs +++ b/src/Forms/FixCommonErrors.cs @@ -9,6 +9,7 @@ using System.Windows.Forms; using Nikse.SubtitleEdit.Logic; using Nikse.SubtitleEdit.Logic.OCR; using Nikse.SubtitleEdit.Logic.SubtitleFormats; +using Nikse.SubtitleEdit.Logic.Forms; namespace Nikse.SubtitleEdit.Forms { @@ -3435,38 +3436,38 @@ namespace Nikse.SubtitleEdit.Forms { Paragraph p = _subtitle.Paragraphs[i]; var text = p.Text; - if (!text.Contains("..") || !AllowFix(p, fixAction)) - continue; - - var oldText = text; - if (!text.Contains(Environment.NewLine)) + if (text.Contains("..") && AllowFix(p, fixAction)) { - text = FixEllipsesStartHelper(text); - if(oldText != text) + var oldText = text; + if (!text.Contains(Environment.NewLine)) { - p.Text = text; - fixCount++; - _totalErrors++; - AddFixToListView(p, fixAction, oldText, text); + text = FixCommonErrorsHelper.FixEllipsesStartHelper(text); + if (oldText != text) + { + p.Text = text; + fixCount++; + _totalErrors++; + AddFixToListView(p, fixAction, oldText, text); + } } - } - else - { - var lines = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); - var fixedParagraph = string.Empty; - for (int k = 0; k < lines.Length; k++) + else { - var line = lines[k]; - fixedParagraph += Environment.NewLine + FixEllipsesStartHelper(line); - fixedParagraph = fixedParagraph.Trim(); - } + var lines = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + var fixedParagraph = string.Empty; + for (int k = 0; k < lines.Length; k++) + { + var line = lines[k]; + fixedParagraph += Environment.NewLine + FixCommonErrorsHelper.FixEllipsesStartHelper(line); + fixedParagraph = fixedParagraph.Trim(); + } - if (fixedParagraph != text) - { - p.Text = fixedParagraph; - fixCount++; - _totalErrors++; - AddFixToListView(p, fixAction, oldText, fixedParagraph); + if (fixedParagraph != text) + { + p.Text = fixedParagraph; + fixCount++; + _totalErrors++; + AddFixToListView(p, fixAction, oldText, fixedParagraph); + } } } } @@ -3476,72 +3477,7 @@ namespace Nikse.SubtitleEdit.Forms LogStatus(_language.FixEllipsesStart, string.Format(_language.XFixEllipsesStart, fixCount)); } - private string FixEllipsesStartHelper(string text) - { - if (text == null || text.Trim().Length < 4) - return text; - if (!text.Contains("..")) - return text; - - if (text.StartsWith("...")) - { - text = text.TrimStart('.'); - } - - text = text.Replace("-..", "- .."); - var tag = "- ..."; - if (text.StartsWith(tag)) - { - text = "- " + text.Substring(tag.Length, text.Length - tag.Length); - while (text.StartsWith("- .")) - { - text = "- " + text.Substring(3, text.Length - 3); - text = text.Replace(" ", " "); - } - } - - tag = "..."; - if (text.StartsWith(tag)) - { - text = "" + text.Substring(tag.Length, text.Length - tag.Length); - while (text.StartsWith(".")) - text = "" + text.Substring(4, text.Length - 4); - } - tag = " ..."; - if (text.StartsWith(tag)) - { - text = "" + text.Substring(tag.Length, text.Length - tag.Length); - while (text.StartsWith(".")) - text = "" + text.Substring(4, text.Length - 4); - } - - tag = "- ..."; - if (text.StartsWith(tag)) - { - text = "- " + text.Substring(tag.Length, text.Length - tag.Length); - while (text.StartsWith("- .")) - text = "- " + text.Substring(6, text.Length - 6); - } - tag = "- ..."; - if (text.StartsWith(tag)) - { - text = "- " + text.Substring(tag.Length, text.Length - tag.Length); - while (text.StartsWith("- .")) - text = "- " + text.Substring(6, text.Length - 6); - } - - // Narrator:... Hello foo! - text = text.Replace(":..", ": .."); - tag = ": .."; - if(text.Contains(tag)) - { - text = text.Replace(": ..", ": "); - while (text.Contains(": .")) - text = text.Replace(": .", ": "); - } - text = text.Replace(" ", " "); - return text; - } + private string FixMissingOpenBracket(string text, string openB) { diff --git a/src/Logic/Forms/FixCommonErrorsHelper.cs b/src/Logic/Forms/FixCommonErrorsHelper.cs new file mode 100644 index 000000000..50f0a24cb --- /dev/null +++ b/src/Logic/Forms/FixCommonErrorsHelper.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Nikse.SubtitleEdit.Logic.Forms +{ + public static class FixCommonErrorsHelper + { + + public static string FixEllipsesStartHelper(string text) + { + if (text == null || text.Trim().Length < 4) + return text; + if (!text.Contains("..")) + return text; + + if (text.StartsWith("...")) + { + text = text.TrimStart('.').TrimStart(); + } + + text = text.Replace("-..", "- .."); + var tag = "- ..."; + if (text.StartsWith(tag)) + { + text = "- " + text.Substring(tag.Length, text.Length - tag.Length); + while (text.StartsWith("- .")) + { + text = "- " + text.Substring(3, text.Length - 3); + text = text.Replace(" ", " "); + } + } + + tag = "..."; + if (text.StartsWith(tag)) + { + text = "" + text.Substring(tag.Length, text.Length - tag.Length); + while (text.StartsWith(".")) + text = "" + text.Substring(4, text.Length - 4); + while (text.StartsWith(" ")) + text = "" + text.Substring(4, text.Length - 4); + } + tag = " ..."; + if (text.StartsWith(tag)) + { + text = "" + text.Substring(tag.Length, text.Length - tag.Length); + while (text.StartsWith(".")) + text = "" + text.Substring(4, text.Length - 4); + while (text.StartsWith(" ")) + text = "" + text.Substring(4, text.Length - 4); + } + + tag = "- ..."; + if (text.StartsWith(tag)) + { + text = "- " + text.Substring(tag.Length, text.Length - tag.Length); + while (text.StartsWith("- .")) + text = "- " + text.Substring(6, text.Length - 6); + } + tag = "- ..."; + if (text.StartsWith(tag)) + { + text = "- " + text.Substring(tag.Length, text.Length - tag.Length); + while (text.StartsWith("- .")) + text = "- " + text.Substring(6, text.Length - 6); + } + + // Narrator:... Hello foo! + text = text.Replace(":..", ": .."); + tag = ": .."; + if (text.Contains(tag)) + { + text = text.Replace(": ..", ": "); + while (text.Contains(": .")) + text = text.Replace(": .", ": "); + } + text = text.Replace(" ", " "); + return text; + } + + } +} diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 0a1c64572..eac06931a 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -760,6 +760,7 @@ + @@ -1137,7 +1138,7 @@ - + diff --git a/src/Test/FixCommonErrorsTest.cs b/src/Test/FixCommonErrorsTest.cs index 2642b187d..a24b50c94 100644 --- a/src/Test/FixCommonErrorsTest.cs +++ b/src/Test/FixCommonErrorsTest.cs @@ -3,6 +3,7 @@ using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; using Nikse.SubtitleEdit.Forms; using Nikse.SubtitleEdit.Logic; +using Nikse.SubtitleEdit.Logic.Forms; namespace Test { @@ -571,6 +572,72 @@ namespace Test } #endregion + #region Ellipses start + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartNormal1() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("...But that is true."); + Assert.AreEqual(result, "But that is true."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartNormal2() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("... But that is true."); + Assert.AreEqual(result, "But that is true."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartNormal3() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("Kurt: ... true but bad."); + Assert.AreEqual(result, "Kurt: true but bad."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartNormal4() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("Kurt: ... true but bad."); + Assert.AreEqual(result, "Kurt: true but bad."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartItalic1() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("...But that is true."); + Assert.AreEqual(result, "But that is true."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartItalic2() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("... But that is true."); + Assert.AreEqual(result, "But that is true."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartItalic3() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("Kurt: ... true but bad."); + Assert.AreEqual(result, "Kurt: true but bad."); + } + + [TestMethod] + [DeploymentItem("SubtitleEdit.exe")] + public void FixEllipsesStartItalic4() + { + var result = FixCommonErrorsHelper.FixEllipsesStartHelper("Kurt: ... true but bad."); + Assert.AreEqual(result, "Kurt: true but bad."); + } + + #endregion [TestMethod] [DeploymentItem("SubtitleEdit.exe")] @@ -602,8 +669,5 @@ namespace Test Assert.AreEqual(target._subtitle.Paragraphs[0].Text, "Yeah, see, that's not mine."); } - - - } }