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.

SplitRunTests.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Novacode;
  7. using System.Xml.Linq;
  8. namespace UnitTests
  9. {
  10. /// <summary>
  11. /// Summary description for SplitRunTests
  12. /// </summary>
  13. [TestClass]
  14. public class SplitRunTests
  15. {
  16. public SplitRunTests()
  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 TestSplitRun()
  61. {
  62. // The test text element to split
  63. Run r = new Run(0, 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 world") }));
  64. #region Split at index 0
  65. /*
  66. * Split r at index 0.
  67. * This will cause the left side of the split to be null and the right side to be equal to r.
  68. */
  69. XElement[] splitRun_indexZero = Run.SplitRun(r, 0);
  70. // Check if my expectations have been met
  71. Assert.IsNull(splitRun_indexZero[0]);
  72. Assert.AreEqual(r.Xml.ToString(), splitRun_indexZero[1].ToString());
  73. #endregion
  74. #region Split at index 1
  75. XElement[] splitRun_indexOne = Run.SplitRun(r, 1);
  76. // The result I expect to get from splitRun_indexOne
  77. XElement splitRun_indexOne_left = 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", "H") });
  78. XElement splitRun_indexOne_right = 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", "ello world") });
  79. // Check if my expectations have been met
  80. Assert.AreEqual(splitRun_indexOne_left.ToString(), splitRun_indexOne[0].ToString());
  81. Assert.AreEqual(splitRun_indexOne_right.ToString(), splitRun_indexOne[1].ToString());
  82. #endregion
  83. #region Split near the middle
  84. /*
  85. * Split the text at index 11.
  86. * This will cause the left side of the split to end with a space and the right to start with a space.
  87. */
  88. XElement[] splitRun_nearMiddle = Run.SplitRun(r, 5);
  89. // The result I expect to get from splitRun_nearMiddle
  90. XElement splitRun_nearMiddle_left = 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") });
  91. XElement splitRun_nearMiddle_right = 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" }) });
  92. // Check if my expectations have been met
  93. Assert.AreEqual(splitRun_nearMiddle_left.ToString(), splitRun_nearMiddle[0].ToString());
  94. Assert.AreEqual(splitRun_nearMiddle_right.ToString(), splitRun_nearMiddle[1].ToString());
  95. #endregion
  96. #region Split at index Length - 1
  97. XElement[] splitRun_indexOneFromLength = Run.SplitRun(r, Paragraph.GetElementTextLength(r.Xml) - 1);
  98. // The result I expect to get from splitRun_indexOne
  99. XElement splitRun_indexOneFromLength_left = 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 worl") });
  100. XElement splitRun_indexOneFromLength_right = 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", "d") });
  101. // Check if my expectations have been met
  102. Assert.AreEqual(splitRun_indexOneFromLength_left.ToString(), splitRun_indexOneFromLength[0].ToString());
  103. Assert.AreEqual(splitRun_indexOneFromLength_right.ToString(), splitRun_indexOneFromLength[1].ToString());
  104. #endregion
  105. #region Split at index Length
  106. /*
  107. * Split r at index Length.
  108. * This will cause the left side of the split to equal to r and the right side to be null.
  109. */
  110. XElement[] splitRun_indexLength = Run.SplitRun(r, Paragraph.GetElementTextLength(r.Xml));
  111. // Check if my expectations have been met
  112. Assert.AreEqual(r.Xml.ToString(), splitRun_indexLength[0].ToString());
  113. Assert.IsNull(splitRun_indexLength[1]);
  114. #endregion
  115. }
  116. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  117. public void TestSplitRun_IndexLessThanTextStartIndex()
  118. {
  119. // The test text element to split
  120. Run r = new Run(0, 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 world") }));
  121. /*
  122. * Split r at a negative index.
  123. * This will cause an argument out of range exception to be thrown.
  124. */
  125. Run.SplitRun(r, r.StartIndex - 1);
  126. }
  127. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  128. public void TestSplitRun_IndexGreaterThanTextEndIndex()
  129. {
  130. // The test text element to split
  131. Run r = new Run(0, 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 world") }));
  132. /*
  133. * Split r at a length + 1.
  134. * This will cause an argument out of range exception to be thrown.
  135. */
  136. Run.SplitRun(r, r.EndIndex + 1);
  137. }
  138. [TestMethod]
  139. public void TestSplitRunOfLengthOne()
  140. {
  141. // The test text element to split
  142. Run r = new Run(0, 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 + "br") }));
  143. XElement[] splitRunOfLengthOne;
  144. #region Split before
  145. splitRunOfLengthOne = Run.SplitRun(r, r.StartIndex);
  146. // Check if my expectations have been met
  147. Assert.AreEqual(r.Xml.ToString(), splitRunOfLengthOne[0].ToString());
  148. Assert.IsNull(splitRunOfLengthOne[1]);
  149. #endregion
  150. #region Split after
  151. splitRunOfLengthOne = Run.SplitRun(r, r.EndIndex);
  152. // Check if my expectations have been met
  153. Assert.IsNull(splitRunOfLengthOne[0]);
  154. Assert.AreEqual(r.Xml.ToString(), splitRunOfLengthOne[1].ToString());
  155. #endregion
  156. }
  157. }
  158. }