using System; using Glass.Editing; using UnitTestSharp; namespace Glass.UnitTests.Editing { public class StringSourceTests : TestFixture { public static readonly string[] Samples = { "", "one line", PagedTextTests.Breaks, "\r\n", "ends with a break\r\n", "\n\nstarts with two", "pair " + PagedTextTests.Pair + " inside", }; public class CtorTests : TestFixture { public void NullText_Throws() { CheckThrow(typeof(ArgumentNullException)); new StringSource(null); } public void CountsTheHardLines() { CheckEqual(1, new StringSource("").HardLineCount); CheckEqual(13, new StringSource(PagedTextTests.Breaks).HardLineCount); CheckEqual(2, new StringSource("a\r\nb").HardLineCount); CheckEqual(2, new StringSource("ab\r\n").HardLineCount); } } public class VersionTests : TestFixture { public void NeverChanges() { CheckEqual(new StringSource("abc").Version, new StringSource("a longer one").Version); } } public class IndexerTests : TestFixture { public void ReadsEachChar() { var source = new StringSource("abc"); CheckEqual('a', source[0]); CheckEqual('c', source[2]); } public void NegativeIndex_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); char ignored = new StringSource("abc")[-1]; } public void IndexAtTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); char ignored = new StringSource("abc")[3]; } } public class CopyToTests : TestFixture { public void CopiesIntoTheMiddleOfTheDestination() { var destination = new char[6]; new StringSource("abcdef").CopyTo(1, 3, destination, 2); CheckEqual("\0\0bcd\0", new string(destination)); } public void ARangePastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").CopyTo(2, 2, new char[4], 0); } } public class SliceTests : TestFixture { public void ViewsTheStringWithoutTouchingTheScratch() { char[] scratch = null; var view = new StringSource("abcdef").Slice(2, 3, ref scratch); CheckEqual("cde", view.ToString()); CheckNull(scratch); } public void ARangePastTheEnd_Throws() { char[] scratch = null; CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").Slice(2, 2, ref scratch); } } public class StartOfLineTests : TestFixture { public void AnIndexOutside_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").StartOfLine(4); } } public class EndOfLineTests : TestFixture { public void AnIndexOutside_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").EndOfLine(-1); } } public class LengthAtTests : TestFixture { public void AnIndexOutside_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").LengthAt(4); } } public class LengthBeforeTests : TestFixture { public void AnIndexOutside_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").LengthBefore(4); } } public class LineNumberOfTests : TestFixture { public void AnIndexOutside_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("abc").LineNumberOf(4); } } public class StartOfLineNumberTests : TestFixture { public void PastTheLastLine_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("one line").StartOfLineNumber(1); } public void Negative_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new StringSource("one line").StartOfLineNumber(-1); } } // The paged text is the reference, since its own tests check it against HardBreaks. public class AgreesWithPagedTextTests : TestFixture { public void OnTheLengthAndLineCount() { foreach (string sample in Samples) { var source = new StringSource(sample); PagedText paged = PagedTextTests.Paged(sample); CheckEqual(paged.Length, source.Length); CheckEqual(paged.HardLineCount, source.HardLineCount); } } public void OnTheLineQueriesAtEveryIndex() { foreach (string sample in Samples) { var source = new StringSource(sample); PagedText paged = PagedTextTests.Paged(sample); for (int index = 0; index <= sample.Length; ++index) { CheckEqual(paged.StartOfLine(index), source.StartOfLine(index)); CheckEqual(paged.EndOfLine(index), source.EndOfLine(index)); CheckEqual(paged.LengthAt(index), source.LengthAt(index)); CheckEqual(paged.LengthBefore(index), source.LengthBefore(index)); CheckEqual(paged.LineNumberOf(index), source.LineNumberOf(index)); } } } public void OnTheStartOfEachLineNumber() { foreach (string sample in Samples) { var source = new StringSource(sample); PagedText paged = PagedTextTests.Paged(sample); for (int line = 0; line < paged.HardLineCount; ++line) { CheckEqual(paged.StartOfLineNumber(line), source.StartOfLineNumber(line)); } } } } } }