You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReplaceTests.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using NUnit.Framework;
  5. using Novacode;
  6. namespace UnitTests
  7. {
  8. [TestFixture]
  9. public class RegExTest
  10. {
  11. private readonly Dictionary<string, string> _testPatterns = new Dictionary<string, string>
  12. {
  13. { "COURT NAME", "Fred Frump" },
  14. { "Case Number", "cr-md-2011-1234567" }
  15. };
  16. [Test]
  17. public void ReplaceText_Can_ReplaceViaFunctionHandler()
  18. {
  19. using (var replaceDoc = DocX.Load(Path.Combine(TestHelper.DirectoryWithFiles, "ReplaceTests.docx")))
  20. {
  21. foreach (var t in replaceDoc.Tables)
  22. {
  23. // each table has 1 row and 3 columns
  24. Assert.IsTrue(t.Rows[0].Cells.Count == 3);
  25. Assert.IsTrue(t.ColumnCount == 3);
  26. Assert.IsTrue(t.Rows.Count == 1);
  27. Assert.IsTrue(t.RowCount == 1);
  28. }
  29. // Make sure the origional strings are in the document.
  30. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  31. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  32. // There are only two patterns, even though each pattern is used more than once
  33. Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
  34. // Make sure the new strings are not in the document.
  35. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  36. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  37. // Do the replacing
  38. replaceDoc.ReplaceText("<(.*?)>", ReplaceTextHandler, false, RegexOptions.IgnoreCase);
  39. // Make sure the origional string are no longer in the document.
  40. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  41. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  42. // Make sure the new strings are now in the document.
  43. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  44. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  45. // Make sure the replacement worked.
  46. Assert.IsTrue(replaceDoc.Text
  47. == "\t\t\t\t\t\t\t\t\t\t\t\t\t\tThese two tables should look identical:\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
  48. }
  49. }
  50. private string ReplaceTextHandler(string findStr)
  51. {
  52. if (_testPatterns.ContainsKey(findStr))
  53. {
  54. return _testPatterns[findStr];
  55. }
  56. return findStr;
  57. }
  58. }
  59. }