using System; using System.Linq; using Amphora; using Tessera.Text; using UnitTestSharp; using static Tessera.UnitTests.Text.SpanSamples; namespace Tessera.UnitTests.Text { public class SpanShiftingTests : TestFixture { public static string[] After(DynamicArray spans, int start, int removed, int inserted) { SpanShifting.Apply(spans, start, removed, inserted); return Ranges(spans); } public class ApplyTests : TestFixture { public void NullSpans_Throws() { CheckThrow(typeof(ArgumentNullException)); SpanShifting.Apply(null, 0, 0, 1); } public void ANegativeStart_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Apply(Spans(), -1, 0, 1); } public void ANegativeRemovedLength_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Apply(Spans(), 0, -1, 1); } public void ANegativeInsertedLength_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Apply(Spans(), 0, 1, -1); } public void NoSpans_StaysEmpty() { CheckEqual(new string[0], After(Spans(), 2, 1, 3)); } public void AnEditOfNothing_ChangesNothing() { CheckEqual(new[] { "2-5" }, After(Plain((2, 5)), 3, 0, 0)); } public void ASpanWithNoChars_IsDropped() { CheckEqual(new[] { "2-5" }, After(Plain((1, 1), (2, 5)), 9, 0, 0)); } public void TheOrderOfTheSpans_IsKept() { CheckEqual(new[] { "5-8", "2-4", "3-4" }, After(Plain((3, 6), (0, 2), (1, 2)), 0, 0, 2)); } } public class MergeTests : TestFixture { public void TypingBetweenTwoSpansOfTheSameLook_JoinsThem() { var spans = SameLook((0, 8, SpanGrowth.AtEnd), (8, 22, SpanGrowth.Never)); CheckEqual(new[] { "0-23" }, After(spans, 8, 0, 1)); } public void TypingBetweenTwoSpansOfDifferentLooks_LeavesThemApart() { var spans = Spans((0, 8, SpanGrowth.AtEnd), (8, 22, SpanGrowth.Never)); CheckEqual(new[] { "0-9", "9-23" }, After(spans, 8, 0, 1)); } public void TypingBetweenThemWithoutGrowing_LeavesThemApart() { var spans = SameLook((0, 8, SpanGrowth.Never), (8, 22, SpanGrowth.Never)); CheckEqual(new[] { "0-8", "9-23" }, After(spans, 8, 0, 1)); } public void DeletingTheTextBetweenTwoSpansOfTheSameLook_JoinsThem() { var spans = SameLook((0, 3, SpanGrowth.Never), (5, 8, SpanGrowth.Never)); CheckEqual(new[] { "0-6" }, After(spans, 3, 2, 0)); } public void ReplacingTheTextBetweenTwoSpansOfTheSameLook_LeavesThemApart() { var spans = SameLook((0, 3, SpanGrowth.Never), (5, 8, SpanGrowth.Never)); CheckEqual(new[] { "0-3", "4-7" }, After(spans, 3, 2, 1)); } public void SpansThatMetBeforeTheEdit_AreLeftAlone() { var spans = SameLook((0, 3, SpanGrowth.Never), (3, 6, SpanGrowth.Never)); CheckEqual(new[] { "0-3", "3-6" }, After(spans, 8, 0, 1)); } public void TheJoinedSpan_GrowsAtItsOuterEdges() { var spans = SameLook((0, 8, SpanGrowth.AtStart), (8, 22, SpanGrowth.AtEnd)); SpanShifting.Apply(spans, 8, 0, 0); CheckEqual(SpanGrowth.AtBothEdges, spans[0].Growth); } } public class InsertTests : TestFixture { public void BeforeASpan_MovesIt() { CheckEqual(new[] { "5-8" }, After(Plain((3, 6)), 1, 0, 2)); } public void AfterASpan_LeavesIt() { CheckEqual(new[] { "3-6" }, After(Plain((3, 6)), 8, 0, 2)); } public void InsideASpan_GrowsIt() { CheckEqual(new[] { "3-8" }, After(Plain((3, 6)), 4, 0, 2)); } public void InsideASpan_GrowsItWhateverItsGrowth() { CheckEqual(new[] { "3-8" }, After(Spans((3, 6, SpanGrowth.Never)), 4, 0, 2)); } public void AtTheEndOfASpanThatNeverGrows_LeavesIt() { CheckEqual(new[] { "3-6" }, After(Spans((3, 6, SpanGrowth.Never)), 6, 0, 2)); } public void AtTheEndOfASpanThatGrowsAtItsStart_LeavesIt() { CheckEqual(new[] { "3-6" }, After(Spans((3, 6, SpanGrowth.AtStart)), 6, 0, 2)); } public void AtTheEndOfASpanThatGrowsAtItsEnd_GrowsIt() { CheckEqual(new[] { "3-8" }, After(Spans((3, 6, SpanGrowth.AtEnd)), 6, 0, 2)); } public void AtTheStartOfASpanThatNeverGrows_MovesIt() { CheckEqual(new[] { "5-8" }, After(Spans((3, 6, SpanGrowth.Never)), 3, 0, 2)); } public void AtTheStartOfASpanThatGrowsAtItsEnd_MovesIt() { CheckEqual(new[] { "5-8" }, After(Spans((3, 6, SpanGrowth.AtEnd)), 3, 0, 2)); } public void AtTheStartOfASpanThatGrowsAtItsStart_GrowsIt() { CheckEqual(new[] { "3-8" }, After(Spans((3, 6, SpanGrowth.AtStart)), 3, 0, 2)); } public void AtBothEdges_GrowsAtEither() { CheckEqual(new[] { "3-8" }, After(Spans((3, 6, SpanGrowth.AtBothEdges)), 3, 0, 2)); CheckEqual(new[] { "3-8" }, After(Spans((3, 6, SpanGrowth.AtBothEdges)), 6, 0, 2)); } public void BetweenTwoSpansThatBothGrow_TheOneBeforeTakesIt() { var spans = Spans((0, 3, SpanGrowth.AtBothEdges), (3, 6, SpanGrowth.AtBothEdges)); CheckEqual(new[] { "0-5", "5-8" }, After(spans, 3, 0, 2)); } public void BetweenASpanThatNeverGrowsAndOneThatGrowsAtItsStart_TheOneAfterTakesIt() { var spans = Spans((0, 3, SpanGrowth.Never), (3, 6, SpanGrowth.AtStart)); CheckEqual(new[] { "0-3", "3-8" }, After(spans, 3, 0, 2)); } public void AtTheEndOfSpansInsideEachOther_GrowsAllThatGrowAtTheirEnds() { var spans = Spans((0, 6, SpanGrowth.AtEnd), (2, 6, SpanGrowth.AtEnd), (3, 6, SpanGrowth.Never)); CheckEqual(new[] { "0-8", "2-8", "3-6" }, After(spans, 6, 0, 2)); } public void AtTheEndOfASpanThatGrows_MovesALaterSpanThatNeverGrows() { var spans = Spans((0, 3, SpanGrowth.AtEnd), (3, 6, SpanGrowth.Never)); CheckEqual(new[] { "0-5", "5-8" }, After(spans, 3, 0, 2)); } } public class ReplaceTests : TestFixture { public void BeforeASpan_MovesItByTheDifference() { CheckEqual(new[] { "6-9" }, After(Plain((5, 8)), 0, 2, 3)); CheckEqual(new[] { "4-7" }, After(Plain((5, 8)), 0, 2, 1)); } public void AfterASpan_LeavesIt() { CheckEqual(new[] { "1-3" }, After(Plain((1, 3)), 4, 2, 5)); } public void InsideASpan_ResizesIt() { CheckEqual(new[] { "1-9" }, After(Plain((1, 8)), 3, 2, 3)); CheckEqual(new[] { "1-6" }, After(Plain((1, 8)), 3, 4, 2)); } public void Deleting_ShrinksTheSpanAroundIt() { CheckEqual(new[] { "1-5" }, After(Plain((1, 8)), 3, 3, 0)); } public void ThroughTheEndOfASpan_CutsItBack() { CheckEqual(new[] { "1-4" }, After(Plain((1, 6)), 4, 4, 0)); } public void ThroughTheStartOfASpan_CutsItFromTheFront() { CheckEqual(new[] { "2-3" }, After(Plain((4, 7)), 2, 4, 0)); } public void ThroughAWholeSpan_DropsIt() { CheckEqual(new string[0], After(Plain((3, 5)), 2, 6, 0)); } public void OfExactlyASpanWithNothing_DropsIt() { CheckEqual(new string[0], After(Plain((3, 5)), 3, 2, 0)); } public void OfExactlyASpan_KeepsTheNewTextInIt() { CheckEqual(new[] { "3-6" }, After(Plain((3, 5)), 3, 2, 3)); } public void ThatStartsInASpan_TakesItsLook() { CheckEqual(new[] { "1-8" }, After(Plain((1, 6)), 4, 3, 4)); } public void ThatStartsBeforeASpan_LeavesTheNewTextOutsideIt() { CheckEqual(new[] { "7-8" }, After(Plain((4, 6)), 2, 3, 5)); } public void ThatStartsBeforeASpanAndEndsInIt_ShortensItFromTheFront() { CheckEqual(new[] { "5-7" }, After(Plain((4, 8)), 2, 4, 3)); } public void ThatStartsWhereASpanEnds_DoesNotGrowItEvenAtItsEnd() { CheckEqual(new[] { "1-3" }, After(Spans((1, 3, SpanGrowth.AtEnd)), 3, 2, 4)); } public void ThatEndsWhereASpanStarts_MovesItEvenAtItsStart() { CheckEqual(new[] { "8-10" }, After(Spans((5, 7, SpanGrowth.AtStart)), 2, 3, 6)); } } public class SliceTests : TestFixture { private static string[] Sliced(DynamicArray spans, int from, int length, int to) { var into = new DynamicArray(); SpanShifting.Slice(spans, from, length, to, into); return Ranges(into); } public void AddsSpansInsideTheRangeMoved() { CheckEqual(new[] { "2-5", "7-8" }, Sliced(Plain((12, 15), (17, 18)), 10, 20, 0)); } public void CutsASpanThatCrossesTheStart() { CheckEqual(new[] { "0-3" }, Sliced(Plain((5, 13)), 10, 20, 0)); } public void CutsASpanThatCrossesTheEnd() { CheckEqual(new[] { "4-10" }, Sliced(Plain((14, 40)), 10, 10, 0)); } public void KeepsASpanThatCoversTheWholeRange() { CheckEqual(new[] { "0-10" }, Sliced(Plain((0, 100)), 10, 10, 0)); } public void SkipsSpansOutsideTheRange() { CheckEqual(new string[0], Sliced(Plain((0, 10), (20, 30)), 10, 10, 0)); } public void LandsTheRangeWhereAsked() { CheckEqual(new[] { "52-55" }, Sliced(Plain((12, 15)), 10, 20, 50)); } public void KeepsTheLookAndGrowth() { DynamicArray spans = Spans((12, 15, SpanGrowth.AtEnd)); var into = new DynamicArray(); SpanShifting.Slice(spans, 10, 20, 0, into); CheckEqual(spans[0].Style, into[0].Style); CheckEqual(SpanGrowth.AtEnd, into[0].Growth); } public void AddsAfterWhatIsThere() { var into = Plain((0, 1)); SpanShifting.Slice(Plain((12, 15)), 10, 20, 0, into); CheckEqual(new[] { "0-1", "2-5" }, Ranges(into)); } public void LeavesTheGivenSpansAlone() { DynamicArray spans = Plain((5, 13)); SpanShifting.Slice(spans, 10, 20, 0, new DynamicArray()); CheckEqual(new[] { "5-13" }, Ranges(spans)); } public void NullSpans_Throw() { CheckThrow(typeof(ArgumentNullException)); SpanShifting.Slice(null, 0, 1, 0, new DynamicArray()); } public void NullInto_Throws() { CheckThrow(typeof(ArgumentNullException)); SpanShifting.Slice(Plain((0, 1)), 0, 1, 0, null); } public void NegativeNumbers_Throw() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Slice(Plain((0, 1)), -1, 1, 0, new DynamicArray()); } public void ANegativeLength_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Slice(Plain((0, 1)), 0, -1, 0, new DynamicArray()); } public void ANegativeLandingPlace_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); SpanShifting.Slice(Plain((0, 1)), 0, 1, -1, new DynamicArray()); } } } }