using Amphora; using Glass.Editing; using System; using UnitTestSharp; namespace Glass.UnitTests.Editing { public class SourceChunksTests : TestFixture { public static string Digits(int count) { var chars = new char[count]; for (int i = 0; i < count; ++i) { chars[i] = (char)('0' + i % 10); } return new string(chars); } public class CtorTests : TestFixture { public void NullSource_Throws() { CheckThrow(typeof(ArgumentNullException)); new SourceChunks(null); } public void StartsWithNothingSelected() { CheckEqual(0, new SourceChunks(new StringSource("hello")).Length); } } public class SelectTests : TestFixture { public void SetsTheLength() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); CheckEqual(3, chunks.Length); } public void TheWholeSource_IsAllowed() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(0, 5); CheckEqual("hello", chunks.ToText()); } public void AnEmptyRangeAtTheEnd_IsAllowed() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(5, 0); CheckEqual(0, chunks.Length); } public void ANegativeStart_Throws() { var chunks = new SourceChunks(new StringSource("hello")); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.Select(-1, 2); } public void ANegativeCount_Throws() { var chunks = new SourceChunks(new StringSource("hello")); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.Select(2, -1); } public void ARangePastTheEnd_Throws() { var chunks = new SourceChunks(new StringSource("hello")); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.Select(3, 3); } public void ARangeWhoseEndWouldOverflow_Throws() { var chunks = new SourceChunks(new StringSource("hello")); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.Select(2, int.MaxValue); } public void AFailedSelect_KeepsTheRangeBefore() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); try { chunks.Select(3, 3); } catch (ArgumentOutOfRangeException) { } CheckEqual("ell", chunks.ToText()); } } public class GetChunkTests : TestFixture { public void AShortRange_IsOneChunkOfJustThatRange() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); CheckEqual("ell", chunks.GetChunk(0).ToString()); } public void AStartInsideTheRange_RunsToTheRangesEnd() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); CheckEqual("ll", chunks.GetChunk(1).ToString()); } public void ALongRange_IsCutIntoChunksOfTheChunkSize() { string text = Digits(SourceChunks.ChunkSize * 2 + 5); var chunks = new SourceChunks(new StringSource(text)); chunks.Select(0, text.Length); CheckEqual(SourceChunks.ChunkSize, chunks.GetChunk(0).Length); CheckEqual(SourceChunks.ChunkSize, chunks.GetChunk(SourceChunks.ChunkSize).Length); CheckEqual(5, chunks.GetChunk(SourceChunks.ChunkSize * 2).Length); } public void ARangeOfExactlyOneChunk_IsOneChunk() { string text = Digits(SourceChunks.ChunkSize); var chunks = new SourceChunks(new StringSource(text)); chunks.Select(0, text.Length); CheckEqual(text, chunks.GetChunk(0).ToString()); } public void AStartAtTheLength_Throws() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.GetChunk(3); } public void ANegativeStart_Throws() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(1, 3); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.GetChunk(-1); } public void AnEmptyRange_HasNoChunk() { var chunks = new SourceChunks(new StringSource("hello")); chunks.Select(2, 0); CheckThrow(typeof(ArgumentOutOfRangeException)); chunks.GetChunk(0); } public void OnPagedText_ChunksAcrossPagesJoinToTheRange() { string text = Digits(PagedText.DefaultPageSize * 5 + 17); var chunks = new SourceChunks(new PagedText(text)); chunks.Select(100, text.Length - 250); CheckEqual(text.Substring(100, text.Length - 250), chunks.ToText()); } public void OnPagedText_AChunkEdgeInsideASurrogatePair_StillJoinsIntact() { string text = "x" + string.Concat(System.Linq.Enumerable.Repeat("\U0001F600", SourceChunks.ChunkSize)); var chunks = new SourceChunks(new PagedText(text)); chunks.Select(0, text.Length); Check(char.IsHighSurrogate(chunks.GetChunk(0)[SourceChunks.ChunkSize - 1])); CheckEqual(text, chunks.ToText()); } public void OnPagedText_AChunkEdgeInsideACrLf_StillJoinsIntact() { string text = "x" + string.Concat(System.Linq.Enumerable.Repeat("\r\n", SourceChunks.ChunkSize)); var chunks = new SourceChunks(new PagedText(text)); chunks.Select(0, text.Length); CheckEqual('\r', chunks.GetChunk(0)[SourceChunks.ChunkSize - 1]); CheckEqual(text, chunks.ToText()); } } } }