PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19using ICSharpCode.AvalonEdit.Document;
 20using System;
 21using System.Linq;
 22using NUnit.Framework;
 23
 24namespace ICSharpCode.AvalonEdit.Editing
 25{
 26	[TestFixture]
 27	public class TextSegmentReadOnlySectionTests
 28	{
 29		TextSegmentCollection<TextSegment> segments;
 30		TextSegmentReadOnlySectionProvider<TextSegment> provider;
 31		
 32		[SetUp]
 33		public void SetUp()
 34		{
 35			segments = new TextSegmentCollection<TextSegment>();
 36			provider = new TextSegmentReadOnlySectionProvider<TextSegment>(segments);
 37		}
 38		
 39		[Test]
 40		public void InsertionPossibleWhenNothingIsReadOnly()
 41		{
 42			Assert.IsTrue(provider.CanInsert(0));
 43			Assert.IsTrue(provider.CanInsert(100));
 44		}
 45		
 46		[Test]
 47		public void DeletionPossibleWhenNothingIsReadOnly()
 48		{
 49			var result = provider.GetDeletableSegments(new SimpleSegment(10, 20)).ToList();
 50			Assert.AreEqual(1, result.Count);
 51			Assert.AreEqual(10, result[0].Offset);
 52			Assert.AreEqual(20, result[0].Length);
 53		}
 54		
 55		[Test]
 56		public void EmptyDeletionPossibleWhenNothingIsReadOnly()
 57		{
 58			var result = provider.GetDeletableSegments(new SimpleSegment(10, 0)).ToList();
 59			Assert.AreEqual(1, result.Count);
 60			Assert.AreEqual(10, result[0].Offset);
 61			Assert.AreEqual(0, result[0].Length);
 62		}
 63		
 64		[Test]
 65		public void InsertionPossibleBeforeReadOnlySegment()
 66		{
 67			segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
 68			Assert.IsTrue(provider.CanInsert(5));
 69		}
 70		
 71		[Test]
 72		public void InsertionPossibleAtStartOfReadOnlySegment()
 73		{
 74			segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
 75			Assert.IsTrue(provider.CanInsert(10));
 76		}
 77		
 78		[Test]
 79		public void InsertionImpossibleInsideReadOnlySegment()
 80		{
 81			segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
 82			Assert.IsFalse(provider.CanInsert(11));
 83			Assert.IsFalse(provider.CanInsert(12));
 84			Assert.IsFalse(provider.CanInsert(13));
 85			Assert.IsFalse(provider.CanInsert(14));
 86		}
 87		
 88		[Test]
 89		public void InsertionPossibleAtEndOfReadOnlySegment()
 90		{
 91			segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
 92			Assert.IsTrue(provider.CanInsert(15));
 93		}
 94		
 95		[Test]
 96		public void InsertionPossibleBetweenReadOnlySegments()
 97		{
 98			segments.Add(new TextSegment { StartOffset = 10, EndOffset = 15 });
 99			segments.Add(new TextSegment { StartOffset = 15, EndOffset = 20 });
100			Assert.IsTrue(provider.CanInsert(15));
101		}
102		
103		[Test]
104		public void DeletionImpossibleInReadOnlySegment()
105		{
106			segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
107			var result = provider.GetDeletableSegments(new SimpleSegment(11, 2)).ToList();
108			Assert.AreEqual(0, result.Count);
109		}
110		
111		[Test]
112		public void EmptyDeletionImpossibleInReadOnlySegment()
113		{
114			segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
115			var result = provider.GetDeletableSegments(new SimpleSegment(11, 0)).ToList();
116			Assert.AreEqual(0, result.Count);
117		}
118		
119		[Test]
120		public void EmptyDeletionPossibleAtStartOfReadOnlySegment()
121		{
122			segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
123			var result = provider.GetDeletableSegments(new SimpleSegment(10, 0)).ToList();
124			Assert.AreEqual(1, result.Count);
125			Assert.AreEqual(10, result[0].Offset);
126			Assert.AreEqual(0, result[0].Length);
127		}
128		
129		[Test]
130		public void EmptyDeletionPossibleAtEndOfReadOnlySegment()
131		{
132			segments.Add(new TextSegment { StartOffset = 10, Length = 5 });
133			var result = provider.GetDeletableSegments(new SimpleSegment(15, 0)).ToList();
134			Assert.AreEqual(1, result.Count);
135			Assert.AreEqual(15, result[0].Offset);
136			Assert.AreEqual(0, result[0].Length);
137		}
138		
139		[Test]
140		public void DeletionAroundReadOnlySegment()
141		{
142			segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
143			var result = provider.GetDeletableSegments(new SimpleSegment(15, 16)).ToList();
144			Assert.AreEqual(2, result.Count);
145			Assert.AreEqual(15, result[0].Offset);
146			Assert.AreEqual(5, result[0].Length);
147			Assert.AreEqual(25, result[1].Offset);
148			Assert.AreEqual(6, result[1].Length);
149		}
150		
151		[Test]
152		public void DeleteLastCharacterInReadOnlySegment()
153		{
154			segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
155			var result = provider.GetDeletableSegments(new SimpleSegment(24, 1)).ToList();
156			Assert.AreEqual(0, result.Count);
157			/* // we would need this result for the old Backspace code so that the last character doesn't get selected:
158			Assert.AreEqual(1, result.Count);
159			Assert.AreEqual(25, result[0].Offset);
160			Assert.AreEqual(0, result[0].Length);*/
161		}
162		
163		[Test]
164		public void DeleteFirstCharacterInReadOnlySegment()
165		{
166			segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
167			var result = provider.GetDeletableSegments(new SimpleSegment(20, 1)).ToList();
168			Assert.AreEqual(0, result.Count);
169			/* // we would need this result for the old Delete code so that the first character doesn't get selected:
170			Assert.AreEqual(1, result.Count);
171			Assert.AreEqual(2, result[0].Offset);
172			Assert.AreEqual(0, result[0].Length);*/
173		}
174		
175		[Test]
176		public void DeleteWholeReadOnlySegment()
177		{
178			segments.Add(new TextSegment { StartOffset = 20, Length = 5 });
179			var result = provider.GetDeletableSegments(new SimpleSegment(20, 5)).ToList();
180			Assert.AreEqual(0, result.Count);
181		}
182	}
183}