Working on unit tests for OcrFixReplaceList.

This commit is contained in:
niksedk 2014-09-27 01:50:59 +02:00 committed by XhmikosR
parent 1be3dc458a
commit 24f3b9f2bc
3 changed files with 272 additions and 27 deletions

View File

@ -55,27 +55,27 @@ namespace Nikse.SubtitleEdit.Logic
_wholeLineReplaceList = LoadReplaceList(doc, "WholeLines");
_regExList = LoadRegExList(doc, "RegularExpressions");
foreach (var kp in LoadReplaceList(userDoc, "WholeWords"))
{
if (!WordReplaceList.ContainsKey(kp.Key))
WordReplaceList.Add(kp.Key, kp.Value);
}
foreach (var kp in LoadReplaceList(userDoc, "RemovedWholeWords"))
{
if (WordReplaceList.ContainsKey(kp.Key))
WordReplaceList.Remove(kp.Key);
}
foreach (var kp in LoadReplaceList(userDoc, "PartialLines"))
foreach (var kp in LoadReplaceList(userDoc, "WholeWords"))
{
if (!PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key))
PartialLineWordBoundaryReplaceList.Add(kp.Key, kp.Value);
if (!WordReplaceList.ContainsKey(kp.Key))
WordReplaceList.Add(kp.Key, kp.Value);
}
foreach (var kp in LoadReplaceList(userDoc, "RemovedPartialLines"))
{
if (PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key))
PartialLineWordBoundaryReplaceList.Remove(kp.Key);
}
foreach (var kp in LoadReplaceList(userDoc, "PartialLines"))
{
if (!PartialLineWordBoundaryReplaceList.ContainsKey(kp.Key))
PartialLineWordBoundaryReplaceList.Add(kp.Key, kp.Value);
}
}
public static OcrFixReplaceList FromLanguageId(string languageId)
@ -689,9 +689,21 @@ namespace Nikse.SubtitleEdit.Logic
{
if (word.Contains(' '))
{
return DeletePartialLineFromWordList(word);
if (DeletePartialLineFromWordList(word))
{
if (PartialLineWordBoundaryReplaceList.ContainsKey(word))
PartialLineWordBoundaryReplaceList.Remove(word);
return true;
}
return false;
}
return DeleteWordFromWordList(word);
if (DeleteWordFromWordList(word))
{
if (WordReplaceList.ContainsKey(word))
WordReplaceList.Remove(word);
return true;
}
return false;
}
private bool DeleteWordFromWordList(string fromWord)
@ -730,28 +742,27 @@ namespace Nikse.SubtitleEdit.Logic
bool removed = false;
if (userDictionary.ContainsKey((word)))
{
userDictionary.Remove(word);
XmlNode wholeWordsNode = userDoc.DocumentElement.SelectSingleNode(replaceListName);
if (wholeWordsNode != null)
{
wholeWordsNode.RemoveAll();
foreach (var kvp in userDictionary)
{
if (kvp.Key != word)
{
XmlNode newNode = userDoc.CreateNode(XmlNodeType.Element, elementName, null);
XmlAttribute aFrom = userDoc.CreateAttribute("from");
XmlAttribute aTo = userDoc.CreateAttribute("to");
aFrom.InnerText = kvp.Key;
aTo.InnerText = kvp.Value;
newNode.Attributes.Append(aTo);
newNode.Attributes.Append(aFrom);
wholeWordsNode.AppendChild(newNode);
}
XmlNode newNode = userDoc.CreateNode(XmlNodeType.Element, elementName, null);
XmlAttribute aFrom = userDoc.CreateAttribute("from");
XmlAttribute aTo = userDoc.CreateAttribute("to");
aFrom.InnerText = kvp.Key;
aTo.InnerText = kvp.Value;
newNode.Attributes.Append(aTo);
newNode.Attributes.Append(aFrom);
wholeWordsNode.AppendChild(newNode);
}
userDoc.Save(ReplaceListXmlFileNameUser);
removed = true;
}
}
else if (dictionary.ContainsKey((word)))
if (dictionary.ContainsKey((word)))
{
XmlNode wholeWordsNode = userDoc.DocumentElement.SelectSingleNode("Removed" + replaceListName);
if (wholeWordsNode != null)
@ -822,9 +833,21 @@ namespace Nikse.SubtitleEdit.Logic
{
if (fromWord.Contains(' '))
{
return SavePartialLineToWordList(fromWord, toWord);
if (SavePartialLineToWordList(fromWord, toWord))
{
if (!PartialLineWordBoundaryReplaceList.ContainsKey(fromWord))
PartialLineWordBoundaryReplaceList.Add(fromWord, toWord);
return true;
}
return false;
}
return SaveWordToWordList(fromWord, toWord);
if (SaveWordToWordList(fromWord, toWord))
{
if (!WordReplaceList.ContainsKey(fromWord))
WordReplaceList.Add(fromWord, toWord);
return true;
}
return false;
}
private bool SaveWordToWordList(string fromWord, string toWord)
@ -859,10 +882,9 @@ namespace Nikse.SubtitleEdit.Logic
throw new ArgumentNullException("dictionary");
if (userDictionary == null)
throw new ArgumentNullException("userDictionary");
if (dictionary.ContainsKey(fromWord))
return false;
if (userDictionary.ContainsKey(fromWord))
return false;
userDictionary.Add(fromWord, toWord);
XmlNode wholeWordsNode = userDoc.DocumentElement.SelectSingleNode(replaceListName);
if (wholeWordsNode != null)

View File

@ -0,0 +1,222 @@
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Logic;
namespace Test.Logic
{
[TestClass]
public class OcrFixReplaceListTest
{
[TestMethod]
public void OcrFixReplaceListAddWord()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.WordReplaceList.Clear();
// Act
fixList.AddWordOrPartial("from", "to");
// Assert
Assert.IsTrue(fixList.WordReplaceList["from"] == "to");
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListAddWordReload()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.WordReplaceList.Clear();
fixList.AddWordOrPartial("from", "to");
// Act
fixList = new OcrFixReplaceList(fileName);
// Assert
Assert.IsTrue(fixList.WordReplaceList["from"] == "to");
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListAddPartialLine()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
// Act
fixList.AddWordOrPartial("from me", "to you");
// Assert
Assert.IsTrue(fixList.PartialLineWordBoundaryReplaceList["from me"] == "to you");
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListAddPartialLineReload()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
fixList.AddWordOrPartial("from me", "to you");
// Act
fixList = new OcrFixReplaceList(fileName);
// Assert
Assert.IsTrue(fixList.PartialLineWordBoundaryReplaceList["from me"] == "to you");
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListRemoveWord()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
fixList.AddWordOrPartial("from", "to");
// Act
fixList.RemoveWordOrPartial("from");
// Assert
Assert.IsTrue(!fixList.WordReplaceList.ContainsKey("from"));
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListRemoveWordReload()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
fixList.AddWordOrPartial("from", "to");
fixList.RemoveWordOrPartial("from");
// Act
fixList = new OcrFixReplaceList(fileName);
// Assert
Assert.IsTrue(!fixList.WordReplaceList.ContainsKey("from"));
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListRemovePartialLine()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
fixList.AddWordOrPartial("from me", "to you");
// Act
fixList.RemoveWordOrPartial("from me");
// Assert
Assert.IsTrue(!fixList.WordReplaceList.ContainsKey("from me"));
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
[TestMethod]
public void OcrFixReplaceListRemovePartialLineReload()
{
// Arrange
string fileName = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid() + ".xml");
var fixList = new OcrFixReplaceList(fileName);
fixList.PartialLineWordBoundaryReplaceList.Clear();
fixList.AddWordOrPartial("from me", "to you");
fixList = new OcrFixReplaceList(fileName);
fixList.RemoveWordOrPartial("from me");
// Act
fixList = new OcrFixReplaceList(fileName);
// Assert
Assert.IsTrue(!fixList.WordReplaceList.ContainsKey("from me"));
// Clean up
try
{
File.Delete(fileName);
}
catch
{
}
}
}
}

View File

@ -46,6 +46,7 @@
<Compile Include="FixCommonErrorsTest.cs" />
<Compile Include="languageTest.cs" />
<Compile Include="Logic\HtmlUtilsTest.cs" />
<Compile Include="Logic\OcrFixReplaceListTest.cs" />
<Compile Include="RemoveTextForHearImpairedTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SubtitleFormatsTest.cs" />