Added a new unit test to test ReplaceText, based on ReplaceTests.docx The Test fails. This functionality worked in the previous release.master
| return list; | return list; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Find all unique instances of the given Regex Pattern, | |||||
| /// returning the list of the unique strings found | |||||
| /// </summary> | |||||
| /// <param name="str"></param> | |||||
| /// <param name="options"></param> | |||||
| /// <returns></returns> | |||||
| public virtual List<string> FindUniqueByPattern(string pattern, RegexOptions options) | |||||
| { | |||||
| List<string> rawResults = new List<string>(); | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| { // accumulate the search results from all paragraphs | |||||
| List<string> partials = p.FindAllByPattern(pattern, options); | |||||
| rawResults.AddRange(partials); | |||||
| } | |||||
| // this dictionary is used to collect results and test for uniqueness | |||||
| Dictionary<string, int> uniqueResults = new Dictionary<string, int>(); | |||||
| foreach (string currValue in rawResults) | |||||
| { | |||||
| if (!uniqueResults.ContainsKey(currValue)) | |||||
| { // if the dictionary doesn't have it, add it | |||||
| uniqueResults.Add(currValue, 0); | |||||
| } | |||||
| } | |||||
| return uniqueResults.Keys.ToList(); // return the unique list of results | |||||
| } | |||||
| public virtual void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch) | public virtual void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch) | ||||
| { | { | ||||
| // ReplaceText in Headers of the document. | // ReplaceText in Headers of the document. |
| <DefineConstants>DEBUG;TRACE</DefineConstants> | <DefineConstants>DEBUG;TRACE</DefineConstants> | ||||
| <ErrorReport>prompt</ErrorReport> | <ErrorReport>prompt</ErrorReport> | ||||
| <WarningLevel>4</WarningLevel> | <WarningLevel>4</WarningLevel> | ||||
| <DocumentationFile>bin\Debug\DocX.xml</DocumentationFile> | |||||
| <DocumentationFile> | |||||
| </DocumentationFile> | |||||
| <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> | <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> | ||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
| return query; | return query; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Find all unique instances of the given Regex Pattern | |||||
| /// </summary> | |||||
| /// <param name="str"></param> | |||||
| /// <param name="options"></param> | |||||
| /// <returns></returns> | |||||
| public List<string> FindAllByPattern(string str, RegexOptions options) | |||||
| { | |||||
| MatchCollection mc = Regex.Matches(this.Text, str, options); | |||||
| var query = | |||||
| ( | |||||
| from m in mc.Cast<Match>() | |||||
| select m.Value | |||||
| ).ToList(); | |||||
| return query; | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Insert a PageNumber place holder into a Paragraph. | /// Insert a PageNumber place holder into a Paragraph. | ||||
| /// This place holder should only be inserted into a Header or Footer Paragraph. | /// This place holder should only be inserted into a Header or Footer Paragraph. |
| using System.Drawing.Imaging; | using System.Drawing.Imaging; | ||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| using System.IO.Packaging; | using System.IO.Packaging; | ||||
| using System.Text.RegularExpressions; | |||||
| namespace UnitTests | namespace UnitTests | ||||
| { | { | ||||
| directory_documents = String.Join("\\", steps) + "\\documents\\"; | directory_documents = String.Join("\\", steps) + "\\documents\\"; | ||||
| } | } | ||||
| [TestMethod] | |||||
| public void PatternReplacement() | |||||
| { | |||||
| Dictionary<string, string> testPatterns = new Dictionary<string, string>() | |||||
| { | |||||
| {"COURT NAME","Fred Frump"}, | |||||
| {"Case Number","cr-md-2011-1234567"} | |||||
| }; | |||||
| using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx")) | |||||
| { | |||||
| foreach (var p in testPatterns) | |||||
| { | |||||
| replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase); | |||||
| } | |||||
| replaceDoc.SaveAs(directory_documents + "ReplaceResults.docx"); | |||||
| } | |||||
| } | |||||
| [TestMethod] | [TestMethod] | ||||
| public void Test_EverybodyHasAHome_Loaded() | public void Test_EverybodyHasAHome_Loaded() | ||||
| { | { |
| <None Include="documents\Tables.docx" /> | <None Include="documents\Tables.docx" /> | ||||
| <None Include="documents\Template.dotx" /> | <None Include="documents\Template.dotx" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <ProjectReference Include="..\DocX\DocX.csproj"> | |||||
| <Project>{E863D072-AA8B-4108-B5F1-785241B37F67}</Project> | |||||
| <Name>DocX</Name> | |||||
| </ProjectReference> | |||||
| </ItemGroup> | |||||
| <ItemGroup> | <ItemGroup> | ||||
| <Content Include="documents\green.jpg" /> | <Content Include="documents\green.jpg" /> | ||||
| <Content Include="documents\orange.gif" /> | <Content Include="documents\orange.gif" /> | ||||
| <Content Include="documents\red.bmp" /> | <Content Include="documents\red.bmp" /> | ||||
| <Content Include="documents\yellow.tif" /> | <Content Include="documents\yellow.tif" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <ProjectReference Include="..\DocX\DocX.csproj"> | |||||
| <Project>{E863D072-AA8B-4108-B5F1-785241B37F67}</Project> | |||||
| <Name>DocX</Name> | |||||
| </ProjectReference> | |||||
| </ItemGroup> | |||||
| <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | ||||
| <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||||
| Other similar extension points exist, see Microsoft.Common.targets. | Other similar extension points exist, see Microsoft.Common.targets. |