using System; using UnitTestSharp; namespace Amphora.UnitTests { public class ChunkedTextTests : TestFixture { public class ShortChunks : IChunkedText { private readonly string text; private readonly int size; public ShortChunks(string text, int size) { this.text = text; this.size = size; } public int Length => text.Length; public StringView GetChunk(int start) { return new StringView(text, start, Math.Min(start + size, text.Length)); } } public class StringChunksTests : TestFixture { public void CtorThrowsOnNull() { CheckThrow(typeof(ArgumentNullException)); new StringChunks(null); } public void LengthIsTheStringsLength() { CheckEqual(5, new StringChunks("hello").Length); } public void LengthOfAnEmptyStringIsZero() { CheckEqual(0, new StringChunks("").Length); } public void TheChunkAtZeroIsTheWholeString() { CheckEqual("hello", new StringChunks("hello").GetChunk(0).ToString()); } public void TheChunkAtAnIndexRunsToTheEnd() { CheckEqual("llo", new StringChunks("hello").GetChunk(2).ToString()); } public void TheChunkAtTheLastIndexIsTheLastCharacter() { CheckEqual("o", new StringChunks("hello").GetChunk(4).ToString()); } public void AStartAtTheLengthThrows() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringChunks("hello").GetChunk(5); } public void ANegativeStartThrows() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringChunks("hello").GetChunk(-1); } public void AnyStartOnAnEmptyStringThrows() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringChunks("").GetChunk(0); } } public class ToTextTests : TestFixture { public void OneChunk_ReturnsTheText() { CheckEqual("hello", new StringChunks("hello").ToText()); } public void ManyChunks_ReturnsTheTextInOrderWithNoGapOrOverlap() { CheckEqual("hello, world", new ShortChunks("hello, world", size: 5).ToText()); } public void AChunkSizeThatDividesTheLength_ReturnsTheText() { CheckEqual("abcdef", new ShortChunks("abcdef", size: 3).ToText()); } public void AChunkOfOneCharacter_ReturnsTheText() { CheckEqual("abc", new ShortChunks("abc", size: 1).ToText()); } public void AnEmptyText_ReturnsAnEmptyString() { CheckEqual("", new ShortChunks("", size: 4).ToText()); } } } }