/AvalonEdit/ICSharpCode.AvalonEdit.Tests/Editing/TextSegmentReadOnlySectionTests.cs

http://github.com/icsharpcode/ILSpy · C# · 183 lines · 139 code · 19 blank · 25 comment · 0 complexity · 4c806c581ee51c40167de6e448977bc8 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using ICSharpCode.AvalonEdit.Document;
  19. using System;
  20. using System.Linq;
  21. using NUnit.Framework;
  22. namespace ICSharpCode.AvalonEdit.Editing
  23. {
  24. [TestFixture]
  25. public class TextSegmentReadOnlySectionTests
  26. {
  27. TextSegmentCollection<TextSegment> segments;
  28. TextSegmentReadOnlySectionProvider<TextSegment> provider;
  29. [SetUp]
  30. public void SetUp()
  31. {
  32. segments = new TextSegmentCollection<TextSegment>();
  33. provider = new TextSegmentReadOnlySectionProvider<TextSegment>(segments);
  34. }
  35. [Test]
  36. public void InsertionPossibleWhenNothingIsReadOnly()
  37. {
  38. Assert.IsTrue(provider.CanInsert(0));
  39. Assert.IsTrue(provider.CanInsert(100));
  40. }
  41. [Test]
  42. public void DeletionPossibleWhenNothingIsReadOnly()
  43. {
  44. var result = provider.GetDeletableSegments(new SimpleSegment(10, 20)).ToList();
  45. Assert.AreEqual(1, result.Count);
  46. Assert.AreEqual(10, result[0].Offset);
  47. Assert.AreEqual(20, result[0].Length);
  48. }
  49. [Test]
  50. public void EmptyDeletionPossibleWhenNothingIsReadOnly()
  51. {
  52. var result = provider.GetDeletableSegments(new SimpleSegment(10, 0)).ToList();
  53. Assert.AreEqual(1, result.Count);
  54. Assert.AreEqual(10, result[0].Offset);
  55. Assert.AreEqual(0, result[0].Length);
  56. }
  57. [Test]
  58. public void InsertionPossibleBeforeReadOnlySegment()
  59. {
  60. segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
  61. Assert.IsTrue(provider.CanInsert(5));
  62. }
  63. [Test]
  64. public void InsertionPossibleAtStartOfReadOnlySegment()
  65. {
  66. segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
  67. Assert.IsTrue(provider.CanInsert(10));
  68. }
  69. [Test]
  70. public void InsertionImpossibleInsideReadOnlySegment()
  71. {
  72. segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
  73. Assert.IsFalse(provider.CanInsert(11));
  74. Assert.IsFalse(provider.CanInsert(12));
  75. Assert.IsFalse(provider.CanInsert(13));
  76. Assert.IsFalse(provider.CanInsert(14));
  77. }
  78. [Test]
  79. public void InsertionPossibleAtEndOfReadOnlySegment()
  80. {
  81. segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
  82. Assert.IsTrue(provider.CanInsert(15));
  83. }
  84. [Test]
  85. public void InsertionPossibleBetweenReadOnlySegments()
  86. {
  87. segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
  88. segments.Add(new TextSegment { StartOffset = 15, EndOffset = 20 });
  89. Assert.IsTrue(provider.CanInsert(15));
  90. }
  91. [Test]
  92. public void DeletionImpossibleInReadOnlySegment()
  93. {
  94. segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
  95. var result = provider.GetDeletableSegments(new SimpleSegment(11, 2)).ToList();
  96. Assert.AreEqual(0, result.Count);
  97. }
  98. [Test]
  99. public void EmptyDeletionImpossibleInReadOnlySegment()
  100. {
  101. segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
  102. var result = provider.GetDeletableSegments(new SimpleSegment(11, 0)).ToList();
  103. Assert.AreEqual(0, result.Count);
  104. }
  105. [Test]
  106. public void EmptyDeletionPossibleAtStartOfReadOnlySegment()
  107. {
  108. segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
  109. var result = provider.GetDeletableSegments(new SimpleSegment(10, 0)).ToList();
  110. Assert.AreEqual(1, result.Count);
  111. Assert.AreEqual(10, result[0].Offset);
  112. Assert.AreEqual(0, result[0].Length);
  113. }
  114. [Test]
  115. public void EmptyDeletionPossibleAtEndOfReadOnlySegment()
  116. {
  117. segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
  118. var result = provider.GetDeletableSegments(new SimpleSegment(15, 0)).ToList();
  119. Assert.AreEqual(1, result.Count);
  120. Assert.AreEqual(15, result[0].Offset);
  121. Assert.AreEqual(0, result[0].Length);
  122. }
  123. [Test]
  124. public void DeletionAroundReadOnlySegment()
  125. {
  126. segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
  127. var result = provider.GetDeletableSegments(new SimpleSegment(15, 16)).ToList();
  128. Assert.AreEqual(2, result.Count);
  129. Assert.AreEqual(15, result[0].Offset);
  130. Assert.AreEqual(5, result[0].Length);
  131. Assert.AreEqual(25, result[1].Offset);
  132. Assert.AreEqual(6, result[1].Length);
  133. }
  134. [Test]
  135. public void DeleteLastCharacterInReadOnlySegment()
  136. {
  137. segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
  138. var result = provider.GetDeletableSegments(new SimpleSegment(24, 1)).ToList();
  139. Assert.AreEqual(0, result.Count);
  140. /* // we would need this result for the old Backspace code so that the last character doesn't get selected:
  141. Assert.AreEqual(1, result.Count);
  142. Assert.AreEqual(25, result[0].Offset);
  143. Assert.AreEqual(0, result[0].Length);*/
  144. }
  145. [Test]
  146. public void DeleteFirstCharacterInReadOnlySegment()
  147. {
  148. segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
  149. var result = provider.GetDeletableSegments(new SimpleSegment(20, 1)).ToList();
  150. Assert.AreEqual(0, result.Count);
  151. /* // we would need this result for the old Delete code so that the first character doesn't get selected:
  152. Assert.AreEqual(1, result.Count);
  153. Assert.AreEqual(2, result[0].Offset);
  154. Assert.AreEqual(0, result[0].Length);*/
  155. }
  156. [Test]
  157. public void DeleteWholeReadOnlySegment()
  158. {
  159. segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
  160. var result = provider.GetDeletableSegments(new SimpleSegment(20, 5)).ToList();
  161. Assert.AreEqual(0, result.Count);
  162. }
  163. }
  164. }