소스 검색

Added Unit tests for Insert and Remove text.

Added bug fix for RemoveText when index is greater than possible.
Removed a few function overloads by utilising .NET 4.0 optional parameters.
master
coffeycathal_cp 15 년 전
부모
커밋
1a4beec19b
3개의 변경된 파일66개의 추가작업 그리고 361개의 파일을 삭제
  1. 12
    0
      DocX.sln
  2. 8
    327
      DocX/Paragraph.cs
  3. 46
    34
      UnitTests/UnitTest1.cs

+ 12
- 0
DocX.sln 파일 보기

@@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocX", "DocX\DocX.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{03479485-7974-4F61-95D2-22648055C4AD}"
EndProject
Global
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 3
@@ -57,6 +59,16 @@ Global
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|x86.ActiveCfg = Release|Any CPU
{03479485-7974-4F61-95D2-22648055C4AD}.Debug|Any CPU.ActiveCfg = Debug|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Debug|Mixed Platforms.Build.0 = Debug|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Debug|x86.ActiveCfg = Debug|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Debug|x86.Build.0 = Debug|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Release|Any CPU.ActiveCfg = Release|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Release|Mixed Platforms.ActiveCfg = Release|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Release|Mixed Platforms.Build.0 = Release|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Release|x86.ActiveCfg = Release|x86
{03479485-7974-4F61-95D2-22648055C4AD}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

+ 8
- 327
DocX/Paragraph.cs 파일 보기

@@ -1434,8 +1434,10 @@ namespace Novacode
internal Run GetFirstRunEffectedByEdit(int index, EditType type = EditType.ins)
{
int len = HelperFunctions.GetText(Xml).Length;
// Make sure we are looking within an acceptable index range.
if (index < 0 || index > HelperFunctions.GetText(Xml).Length)
if (index < 0 || ((type == EditType.ins && index > len) || (type == EditType.del && index >= len)))
throw new ArgumentOutOfRangeException();
// Need some memory that can be updated by the recursive search for the XElement to Split.
@@ -1525,108 +1527,6 @@ namespace Novacode
);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
/// p.InsertText(0, "Start: ", true);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <example>
/// Inserting tabs using the \t switch.
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tStart:\t" at the begining of every paragraph and flag it as a change.
/// p.InsertText(0, "\tStart:\t", true);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="index">The index position of the insertion.</param>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
public void InsertText(int index, string value, bool trackChanges = false)
{
InsertText(index, value, trackChanges, null);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "End: " at the end of every Paragraph and flag it as a change.
/// p.InsertText("End: ", true);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <example>
/// Inserting tabs using the \t switch.
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tEnd" at the end of every paragraph and flag it as a change.
/// p.InsertText("\tEnd", true);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
public void InsertText(string value, bool trackChanges)
{
List<XElement> newRuns = HelperFunctions.FormatInput(value, null);
Xml.Add(newRuns);
HelperFunctions.RenumberIDs(Document);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
@@ -1682,7 +1582,7 @@ namespace Novacode
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
/// <param name="formatting">The text formatting.</param>
public void InsertText(string value, bool trackChanges, Formatting formatting)
public void InsertText(string value, bool trackChanges = false, Formatting formatting = null)
{
List<XElement> newRuns = HelperFunctions.FormatInput(value, formatting.Xml);
Xml.Add(newRuns);
@@ -1746,7 +1646,7 @@ namespace Novacode
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
/// <param name="formatting">The text formatting.</param>
public void InsertText(int index, string value, bool trackChanges, Formatting formatting)
public void InsertText(int index, string value, bool trackChanges=false, Formatting formatting = null)
{
// Timestamp to mark the start of insert
DateTime now = DateTime.Now;
@@ -2599,45 +2499,6 @@ namespace Novacode
return this;
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
/// <param name="cp">The custom property to display.</param>
/// <param name="f">The formatting to use for this text.</param>
/// <example>
/// Create, add and display a custom property in a document.
/// <code>
/// // Load a document
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Create a custom property.
/// CustomProperty name = new CustomProperty("name", "Cathal Coffey");
///
/// // Add this custom property to this document.
/// document.AddCustomProperty(name);
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.Bold = true;
/// f.Size = 14;
/// f.StrikeThrough = StrickThrough.strike;
///
/// // Insert a new paragraph.
/// Paragraph p = document.InsertParagraph("Author: ", false, f);
///
/// // Insert a field of type document property to display the custom property name
/// p.InsertDocProperty(name, f);
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public DocProperty InsertDocProperty(CustomProperty cp, Formatting f)
{
return InsertDocProperty(cp, false, f);
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
@@ -2672,7 +2533,7 @@ namespace Novacode
/// }// Release this document from memory.
/// </code>
/// </example>
public DocProperty InsertDocProperty(CustomProperty cp, bool trackChanges, Formatting f)
public DocProperty InsertDocProperty(CustomProperty cp, bool trackChanges = false, Formatting f = null)
{
XElement e = new XElement
(
@@ -2695,70 +2556,6 @@ namespace Novacode
return new DocProperty(Document, xml);
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
/// <param name="cp">The custom property to display.</param>
/// <example>
/// Create, add and display a custom property in a document.
/// <code>
/// // Load a document
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Create a custom property.
/// CustomProperty name = new CustomProperty("name", "Cathal Coffey");
///
/// // Add this custom property to this document.
/// document.AddCustomProperty(name);
///
/// // Insert a new paragraph.
/// Paragraph p = document.InsertParagraph("Author: ", false);
///
/// // Insert a field of type document property to display the custom property name
/// p.InsertDocProperty(name);
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public DocProperty InsertDocProperty(CustomProperty cp)
{
return InsertDocProperty(cp, false, new Formatting());
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
/// <param name="cp">The custom property to display.</param>
/// <example>
/// Create, add and display a custom property in a document.
/// <code>
/// // Load a document
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Create a custom property.
/// CustomProperty name = new CustomProperty("name", "Cathal Coffey");
///
/// // Add this custom property to this document.
/// document.AddCustomProperty(name);
///
/// // Insert a new paragraph.
/// Paragraph p = document.InsertParagraph("Author: ", false);
///
/// // Insert a field of type document property to display the custom property name and track this change.
/// p.InsertDocProperty(name, true);
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public DocProperty InsertDocProperty(CustomProperty cp, bool trackChanges)
{
return InsertDocProperty(cp, trackChanges, new Formatting());
}
/// <summary>
/// Removes characters from a Novacode.DocX.Paragraph.
/// </summary>
@@ -2902,97 +2699,15 @@ namespace Novacode
/// </example>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="index">The position to begin deleting characters.</param>
/// <param name="trackChanges">Track changes</param>
public void RemoveText(int index, bool trackChanges)
public void RemoveText(int index, bool trackChanges = false)
{
RemoveText(index, Text.Length - index, trackChanges);
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Replace all instances of the string "wrong" with the string "right" and ignore case.
/// p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="newValue">A System.String to replace all occurances of oldValue.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="options">A bitwise OR combination of RegexOption enumeration options.</param>
/// <param name="trackChanges">Track changes</param>
public void ReplaceText(string oldValue, string newValue, bool trackChanges, RegexOptions options)
{
ReplaceText(oldValue, newValue, trackChanges, options, null, null, MatchFormattingOptions.SubsetMatch);
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // The formatting to apply to the inserted text.
/// Formatting newFormatting = new Formatting();
/// newFormatting.Size = 22;
/// newFormatting.UnderlineStyle = UnderlineStyle.dotted;
/// newFormatting.Bold = true;
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// /*
/// * Replace all instances of the string "wrong" with the string "right" and ignore case.
/// * Each inserted instance of "wrong" should use the Formatting newFormatting.
/// */
/// p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase, newFormatting);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="newValue">A System.String to replace all occurances of oldValue.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="options">A bitwise OR combination of RegexOption enumeration options.</param>
/// <param name="trackChanges">Track changes</param>
/// <param name="newFormatting">The formatting to apply to the text being inserted.</param>
public void ReplaceText(string oldValue, string newValue, bool trackChanges, RegexOptions options, Formatting newFormatting)
{
ReplaceText(oldValue, newValue, trackChanges, options, null, null, MatchFormattingOptions.SubsetMatch);
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
@@ -3045,7 +2760,7 @@ namespace Novacode
/// <param name="newFormatting">The formatting to apply to the text being inserted.</param>
/// <param name="matchFormatting">The formatting that the text must match in order to be replaced.</param>
/// <param name="fo">How should formatting be matched?</param>
public void ReplaceText(string oldValue, string newValue, bool trackChanges, RegexOptions options, Formatting newFormatting, Formatting matchFormatting, MatchFormattingOptions fo)
public void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch)
{
MatchCollection mc = Regex.Matches(this.Text, Regex.Escape(oldValue), options);
@@ -3199,40 +2914,6 @@ namespace Novacode
return query;
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Replace all instances of the string "wrong" with the string "right".
/// p.ReplaceText("wrong", "right", false);
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="newValue">A System.String to replace all occurances of oldValue.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="trackChanges">Track changes</param>
public void ReplaceText(string oldValue, string newValue, bool trackChanges = false)
{
ReplaceText(oldValue, newValue, trackChanges, RegexOptions.None, null, null, MatchFormattingOptions.SubsetMatch);
}
}
public class Run : DocXElement

+ 46
- 34
UnitTests/UnitTest1.cs 파일 보기

@@ -177,6 +177,7 @@ namespace UnitTests
Assert.Fail();
}
catch (ArgumentException e) { }
catch (Exception e) { Assert.Fail(); }
// Try and remove a Hyperlink at an index greater than the last.
// This should throw an exception.
@@ -186,6 +187,7 @@ namespace UnitTests
Assert.Fail();
}
catch (ArgumentException e) {}
catch (Exception e) { Assert.Fail(); }
p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
@@ -205,6 +207,10 @@ namespace UnitTests
p1.ReplaceText("Pear", "Apple"); Assert.IsTrue(p1.Text == "Orange Apple Orange Orange Apple Orange");
p1.ReplaceText("Orange", "Pear"); Assert.IsTrue(p1.Text == "Pear Apple Pear Pear Apple Pear");
// Try and replace text that dosen't exist in the Paragraph.
string old = p1.Text;
p1.ReplaceText("foo", "bar"); Assert.IsTrue(p1.Text.Equals(old));
// Difficult
Paragraph p2 = document.InsertParagraph("Apple Pear Apple Apple Pear Apple");
p2.ReplaceText(" ", "\t"); Assert.IsTrue(p2.Text == "Apple\tPear\tApple\tApple\tPear\tApple");
@@ -229,6 +235,26 @@ namespace UnitTests
p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl");
p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl");
// Try and remove text at an index greater than the last.
// This should throw an exception.
try
{
p1.RemoveText(p1.Text.Length, 1);
Assert.Fail();
}
catch (ArgumentOutOfRangeException e) { }
catch (Exception e) { Assert.Fail(); }
// Try and remove text at a negative index.
// This should throw an exception.
try
{
p1.RemoveText(-1, 1);
Assert.Fail();
}
catch (ArgumentOutOfRangeException e) { }
catch (Exception e) { Assert.Fail(); }
// Difficult
//<p>
// <r><t>A</t></r>
@@ -320,6 +346,26 @@ namespace UnitTests
p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-");
p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-");
// Try and insert text at an index greater than the last + 1.
// This should throw an exception.
try
{
p1.InsertText(p1.Text.Length + 1, "-");
Assert.Fail();
}
catch (ArgumentOutOfRangeException e) { }
catch (Exception e) { Assert.Fail(); }
// Try and insert text at a negative index.
// This should throw an exception.
try
{
p1.InsertText(-1, "-");
Assert.Fail();
}
catch (ArgumentOutOfRangeException e) { }
catch (Exception e) { Assert.Fail(); }
// Difficult
//<p>
// <r><t>A</t></r>
@@ -391,19 +437,6 @@ namespace UnitTests
[TestMethod]
public void Test_Document_Paragraphs()
{
// This document contains a run with two text next to each other.
// <run>
// <text>Hello World</text>
// <text>foo</text>
// </run>
using (DocX document = DocX.Load(@"C:\Users\Cathal\Desktop\Bug.docx"))
{
Paragraph p = document.Paragraphs[0];
Assert.IsTrue(p.Text == "Hello worldfoo");
p.RemoveText("Hello world".Length, 3, false);
Assert.IsTrue(p.Text == "Hello world");
}
// Load the document 'Paragraphs.docx'
using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
{
@@ -428,27 +461,6 @@ namespace UnitTests
Assert.IsTrue(p2_text == "Paragraph 2");
Assert.IsTrue(p3_text == "Paragraph 3");
// Create a string to append to each Paragraph.
string appended_text = "foo bar foo";
// Test the appending of text to each Paragraph.
Assert.IsTrue(p1.Append(appended_text).Text == p1_text + appended_text);
Assert.IsTrue(p2.Append(appended_text).Text == p2_text + appended_text);
Assert.IsTrue(p3.Append(appended_text).Text == p3_text + appended_text);
// Test FindAll
List<int> p1_foos = p1.FindAll("foo");
Assert.IsTrue(p1_foos.Count() == 2 && p1_foos[0] == 11 && p1_foos[1] == 19);
// Test ReplaceText
p2.ReplaceText("foo", "bar", false);
Assert.IsTrue(p2.Text == "Paragraph 2bar bar bar");
// Test RemoveText
p3.RemoveText(1, 3, false);
Assert.IsTrue(p3.Text == "Pgraph 3foo bar foo");
// Its important that each Paragraph knows the PackagePart it belongs to.
document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));

Loading…
취소
저장