Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SplitEditTests.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System.Xml.Linq;
  7. using Novacode;
  8. namespace UnitTests
  9. {
  10. /// <summary>
  11. /// Summary description for SplitEditTests
  12. /// </summary>
  13. [TestClass]
  14. public class SplitEditTests
  15. {
  16. public SplitEditTests()
  17. {
  18. //
  19. // TODO: Add constructor logic here
  20. //
  21. }
  22. private TestContext testContextInstance;
  23. /// <summary>
  24. ///Gets or sets the test context which provides
  25. ///information about and functionality for the current test run.
  26. ///</summary>
  27. public TestContext TestContext
  28. {
  29. get
  30. {
  31. return testContextInstance;
  32. }
  33. set
  34. {
  35. testContextInstance = value;
  36. }
  37. }
  38. #region Additional test attributes
  39. //
  40. // You can use the following additional attributes as you write your tests:
  41. //
  42. // Use ClassInitialize to run code before running the first test in the class
  43. // [ClassInitialize()]
  44. // public static void MyClassInitialize(TestContext testContext) { }
  45. //
  46. // Use ClassCleanup to run code after all tests in a class have run
  47. // [ClassCleanup()]
  48. // public static void MyClassCleanup() { }
  49. //
  50. // Use TestInitialize to run code before running each test
  51. // [TestInitialize()]
  52. // public void MyTestInitialize() { }
  53. //
  54. // Use TestCleanup to run code after each test has run
  55. // [TestCleanup()]
  56. // public void MyTestCleanup() { }
  57. //
  58. #endregion
  59. [TestMethod]
  60. public void TestSplitEdit()
  61. {
  62. // The test text element to split
  63. XElement run1 = new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "Hello"));
  64. XElement run2 = new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), " world"));
  65. XElement edit = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), run1, run2);
  66. Paragraph p = new Paragraph(edit);
  67. #region Split at index 0
  68. XElement[] splitEdit = p.SplitEdit(edit, 0, EditType.del);
  69. Assert.IsNull(splitEdit[0]);
  70. Assert.AreEqual(edit.ToString(), splitEdit[1].ToString());
  71. #endregion
  72. #region Split at index 1
  73. /*
  74. * Split the text at index 5.
  75. * This will cause the left side of the split to end with a space and the right to start with a space.
  76. */
  77. XElement[] splitEdit_indexOne = p.SplitEdit(edit, 1, EditType.del);
  78. // The result I expect to get from splitRun_nearMiddle
  79. XElement splitEdit_indexOne_left = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "H")));
  80. XElement splitEdit_indexOne_right = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r",new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "ello")), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), " world")));
  81. // Check if my expectations have been met
  82. Assert.AreEqual(splitEdit_indexOne_left.ToString(), splitEdit_indexOne[0].ToString());
  83. Assert.AreEqual(splitEdit_indexOne_right.ToString(), splitEdit_indexOne[1].ToString());
  84. #endregion
  85. #region Split near the middle
  86. /*
  87. * Split the text at index 5.
  88. * This will cause the left side of the split to end with a space and the right to start with a space.
  89. */
  90. XElement[] splitEdit_nearMiddle = p.SplitEdit(edit, 5, EditType.del);
  91. // The result I expect to get from splitRun_nearMiddle
  92. XElement splitEdit_nearMiddle_left = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "Hello")));
  93. XElement splitEdit_nearMiddle_right = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), " world")));
  94. // Check if my expectations have been met
  95. Assert.AreEqual(splitEdit_nearMiddle_left.ToString(), splitEdit_nearMiddle[0].ToString());
  96. Assert.AreEqual(splitEdit_nearMiddle_right.ToString(), splitEdit_nearMiddle[1].ToString());
  97. #endregion
  98. #region Split at index Length - 1
  99. /*
  100. * Split the text at index 5.
  101. * This will cause the left side of the split to end with a space and the right to start with a space.
  102. */
  103. XElement[] splitEdit_indexOneFromLength = p.SplitEdit(edit, Paragraph.GetElementTextLength(edit) - 1, EditType.del);
  104. // The result I expect to get from splitRun_nearMiddle
  105. XElement splitEdit_OneFromLength_left = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "Hello")), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), " worl")));
  106. XElement splitEdit_OneFromLength_right = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "d")));
  107. // Check if my expectations have been met
  108. Assert.AreEqual(splitEdit_OneFromLength_left.ToString(), splitEdit_indexOneFromLength[0].ToString());
  109. Assert.AreEqual(splitEdit_OneFromLength_right.ToString(), splitEdit_indexOneFromLength[1].ToString());
  110. #endregion
  111. #region Split at index Length
  112. XElement[] splitEdit_indexZero = p.SplitEdit(edit, Paragraph.GetElementTextLength(edit), EditType.del);
  113. Assert.AreEqual(edit.ToString(), splitEdit_indexZero[0].ToString());
  114. Assert.IsNull(splitEdit_indexZero[1]);
  115. #endregion
  116. }
  117. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  118. public void TestSplitEdit_IndexLessThanTextStartIndex()
  119. {
  120. // The test text element to split
  121. XElement run1 = new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", "Hello"));
  122. XElement run2 = new XElement(DocX.w + "r", new XElement(DocX.w + "rPr", new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0"))), new XElement(DocX.w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), " world"));
  123. XElement edit = new XElement(DocX.w + "ins", new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), run1, run2);
  124. Paragraph p = new Paragraph(edit);
  125. /*
  126. * Split r at a negative index.
  127. * This will cause an argument out of range exception to be thrown.
  128. */
  129. p.SplitEdit(edit, -1, EditType.del);
  130. }
  131. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  132. public void TestSplitEdit_IndexGreaterThanTextEndIndex()
  133. {
  134. // The test text element to split
  135. XElement run1 = new XElement(DocX.w + "r", new object[] { new XElement(DocX.w + "rPr", new object[] { new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0")) }), new XElement(DocX.w + "t", "Hello") });
  136. XElement run2 = new XElement(DocX.w + "r", new object[] { new XElement(DocX.w + "rPr", new object[] { new XElement(DocX.w + "b"), new XElement(DocX.w + "i"), new XElement(DocX.w + "color", new XAttribute(DocX.w + "val", "7030A0")) }), new XElement(DocX.w + "t", new object[] { new XAttribute(XNamespace.Xml + "space", "preserve"), " world" }) });
  137. XElement edit = new XElement(DocX.w + "ins", new object[] { new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), run1, run2 });
  138. Paragraph p = new Paragraph(edit);
  139. /*
  140. * Split r at a negative index.
  141. * This will cause an argument out of range exception to be thrown.
  142. */
  143. p.SplitEdit(edit, Paragraph.GetElementTextLength(edit) + 1, EditType.del);
  144. }
  145. [TestMethod]
  146. public void TestSplitEditOfLengthOne()
  147. {
  148. XElement edit = new XElement(DocX.w + "ins", new object[] { new XAttribute(DocX.w + "id", "0"), new XAttribute(DocX.w + "author", "t-cathco"), new XAttribute(DocX.w + "date", "2009-02-17T21:09:00Z"), new XElement(DocX.w + "r", new XElement(DocX.w + "tab"))});
  149. Paragraph p = new Paragraph(edit);
  150. XElement[] splitEditOfLengthOne;
  151. #region Split before
  152. splitEditOfLengthOne = p.SplitEdit(edit, 0, EditType.del);
  153. // Check if my expectations have been met
  154. Assert.AreEqual(edit.ToString(), splitEditOfLengthOne[0].ToString());
  155. Assert.IsNull(splitEditOfLengthOne[1]);
  156. #endregion
  157. #region Split after
  158. splitEditOfLengthOne = p.SplitEdit(edit, Paragraph.GetElementTextLength(edit), EditType.del);
  159. // Check if my expectations have been met
  160. Assert.IsNull(splitEditOfLengthOne[0]);
  161. Assert.AreEqual(edit.ToString(), splitEditOfLengthOne[1].ToString());
  162. #endregion
  163. }
  164. }
  165. }