Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections.Generic;
  2. using System.Text.RegularExpressions;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Novacode;
  5. namespace UnitTests
  6. {
  7. [TestClass]
  8. public class RegExTest
  9. {
  10. private readonly Dictionary<string, string> _testPatterns = new Dictionary<string, string>
  11. {
  12. { "COURT NAME", "Fred Frump" },
  13. { "Case Number", "cr-md-2011-1234567" }
  14. };
  15. private readonly TestHelper _testHelper;
  16. public RegExTest()
  17. {
  18. _testHelper = new TestHelper();
  19. }
  20. [TestMethod]
  21. public void ReplaceText_Can_ReplaceViaFunctionHandler()
  22. {
  23. using (var replaceDoc = DocX.Load(_testHelper.DirectoryWithFiles + "ReplaceTests.docx"))
  24. {
  25. foreach (var t in replaceDoc.Tables)
  26. {
  27. // each table has 1 row and 3 columns
  28. Assert.IsTrue(t.Rows[0].Cells.Count == 3);
  29. Assert.IsTrue(t.ColumnCount == 3);
  30. Assert.IsTrue(t.Rows.Count == 1);
  31. Assert.IsTrue(t.RowCount == 1);
  32. }
  33. // Make sure the origional strings are in the document.
  34. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
  35. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
  36. // There are only two patterns, even though each pattern is used more than once
  37. Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
  38. // Make sure the new strings are not in the document.
  39. Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
  40. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
  41. // Do the replacing
  42. replaceDoc.ReplaceText("<(.*?)>", ReplaceTextHandler, false, RegexOptions.IgnoreCase);
  43. // Make sure the origional string are no longer in the document.
  44. Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 0);
  45. Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 0);
  46. // Make sure the new strings are now in the document.
  47. Assert.IsTrue(replaceDoc.FindAll("FRED FRUMP").Count == 2);
  48. Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 2);
  49. // Make sure the replacement worked.
  50. Assert.IsTrue(replaceDoc.Text
  51. == "\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");
  52. }
  53. }
  54. private string ReplaceTextHandler(string findStr)
  55. {
  56. if (_testPatterns.ContainsKey(findStr))
  57. {
  58. return _testPatterns[findStr];
  59. }
  60. return findStr;
  61. }
  62. }
  63. }