Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SplitTextTests.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /// This class tests the SplitText function of Novacode.Paragraph
  12. /// </summary>
  13. [TestClass]
  14. public class SplitTextTests
  15. {
  16. public SplitTextTests()
  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 TestSplitText()
  61. {
  62. // The test text element to split
  63. Text t = new Text(0, new XElement(DocX.w + "t", "Hello I am a string"));
  64. #region Split at index 0
  65. /*
  66. * Split t at index 0.
  67. * This will cause the left side of the split to be null and the right side to be equal to t.
  68. */
  69. XElement[] splitText_indexZero = Text.SplitText(t, 0);
  70. // Check if my expectations have been met
  71. Assert.IsNull(splitText_indexZero[0]);
  72. Assert.AreEqual(t.Xml.ToString(), splitText_indexZero[1].ToString());
  73. #endregion
  74. #region Split at index 1
  75. XElement[] splitText_indexOne = Text.SplitText(t, 1);
  76. // The result I expect to get from splitText1
  77. XElement splitText_indexOne_left = new XElement(DocX.w + "t", "H");
  78. XElement splitText_indexOne_right = new XElement(DocX.w + "t", "ello I am a string");
  79. // Check if my expectations have been met
  80. Assert.AreEqual(splitText_indexOne_left.ToString(), splitText_indexOne[0].ToString());
  81. Assert.AreEqual(splitText_indexOne_right.ToString(), splitText_indexOne[1].ToString());
  82. #endregion
  83. #region Split near the middle causing starting and ending spaces
  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[] splitText_nearMiddle = Text.SplitText(t, 11);
  89. // The result I expect to get from splitText1
  90. XElement splitText_nearMiddle_left = new XElement(DocX.w + "t", new object[] { new XAttribute(XNamespace.Xml + "space", "preserve"), "Hello I am " });
  91. XElement splitText_nearMiddle_right = new XElement(DocX.w + "t", new object[] { new XAttribute(XNamespace.Xml + "space", "preserve"), " a string" });
  92. // Check if my expectations have been met
  93. Assert.AreEqual(splitText_nearMiddle_left.ToString(), splitText_nearMiddle[0].ToString());
  94. Assert.AreEqual(splitText_nearMiddle_right.ToString(), splitText_nearMiddle[1].ToString());
  95. #endregion
  96. #region Split at text.Value.Length - 1
  97. XElement[] splitText_indexOneFromLength = Text.SplitText(t, t.Value.Length - 1);
  98. // The result I expect to get from splitText1
  99. XElement splitText_indexOneFromLength_left = new XElement(DocX.w + "t", "Hello I am a strin");
  100. XElement splitText_indexOneFromLength_right = new XElement(DocX.w + "t", "g");
  101. // Check if my expectations have been met
  102. Assert.AreEqual(splitText_indexOneFromLength_left.ToString(), splitText_indexOneFromLength[0].ToString());
  103. Assert.AreEqual(splitText_indexOneFromLength_right.ToString(), splitText_indexOneFromLength[1].ToString());
  104. #endregion
  105. #region Split at index text.Value.Length
  106. /*
  107. * Split the text at index text.Value.Length.
  108. * This will cause the left side of the split to be equal to text and the right side to be null.
  109. */
  110. XElement[] splitText_indexLength = Text.SplitText(t, t.Value.Length);
  111. // Check if my expectations have been met
  112. Assert.AreEqual(t.Xml.ToString(), splitText_indexLength[0].ToString());
  113. Assert.IsNull(splitText_indexLength[1]);
  114. #endregion
  115. }
  116. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  117. public void TestSplitText_IndexLessThanTextStartIndex()
  118. {
  119. // The test text element to split
  120. Text t = new Text(0, new XElement(DocX.w + "t", "Hello I am a string"));
  121. /*
  122. * Split t at a negative index.
  123. * This will cause an argument out of range exception to be thrown.
  124. */
  125. Text.SplitText(t, t.StartIndex - 1);
  126. }
  127. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  128. public void TestSplitText_IndexGreaterThanTextEndIndex()
  129. {
  130. // The test text element to split
  131. Text t = new Text(0, new XElement(DocX.w + "t", "Hello I am a string"));
  132. /*
  133. * Split t at an index greater than its text length.
  134. * This will cause an argument out of range exception to be thrown.
  135. */
  136. Text.SplitText(t, t.EndIndex + 1);
  137. }
  138. [TestMethod]
  139. public void TestSplitTextOfLengthOne()
  140. {
  141. // The test text element to split
  142. Text t = new Text(0, new XElement(DocX.w + "tab"));
  143. XElement[] splitTextOfLengthOne;
  144. #region Split before
  145. splitTextOfLengthOne = Text.SplitText(t, t.StartIndex);
  146. // Check if my expectations have been met
  147. Assert.AreEqual(t.Xml.ToString(), splitTextOfLengthOne[0].ToString());
  148. Assert.IsNull(splitTextOfLengthOne[1]);
  149. #endregion
  150. #region Split after
  151. splitTextOfLengthOne = Text.SplitText(t, t.EndIndex);
  152. // Check if my expectations have been met
  153. Assert.IsNull(splitTextOfLengthOne[0]);
  154. Assert.AreEqual(t.Xml.ToString(), splitTextOfLengthOne[1].ToString());
  155. #endregion
  156. }
  157. }
  158. }