using System.Collections.Generic; using Amphora; using Tessera.Typesetting; using UnitTestSharp; using Tessera.Unicode; namespace Tessera.UnitTests.Typesetting { public class MeasuredWordsTests : TestFixture { private static readonly MetricsFont Narrow = FacedMeasuringTests.Narrow; private static readonly MetricsFont Wide = FacedMeasuringTests.Wide; public static MeasuredWord WordEndingAt(int end) => new MeasuredWord(new LineBreak(end, isMandatory: false), contentWidth: end * 10, leadingKerning: 0, endKerning: 0); /// /// Words ending at each of `ends`, with a spare word in the storage past them, which must never be read. /// public static MeasuredWords Holding(params int[] ends) { var words = new DynamicArray(capacity: ends.Length + 1); foreach (int end in ends) { words.Add(WordEndingAt(end)); } words.Storage[ends.Length] = WordEndingAt(99); return new MeasuredWords(WordMeasurerTests.SharedFont, "", words); } public static List ReadAll(MeasuredWords words) { var returnMe = new List(); while (words.MoveNext()) { returnMe.Add(WordMeasurerTests.Describe(words.CurrentStart, words.Current, words.Ends)); } return returnMe; } /// /// Each word as does, since the widths of what ends them depend on /// the faces. /// public static List FacedWordsOf(MeasuredWords words) { var returnMe = new List(); while (words.MoveNext()) { returnMe.Add(FacedMeasuringTests.Describe(words.CurrentStart, words.Current)); } return returnMe; } public const string ThreeWords = "ab cd\nef"; /// /// One-letter words, far more of them than the first guess at how many a text this long holds. /// public static readonly string ManyShortWords = string.Join(" ", new string('a', 40).ToCharArray()); public class CtorTests : TestFixture { public void GivenWords_KeepsTheFontTextAndWords() { MetricsFont font = WordMeasurerTests.SharedFont; var given = new DynamicArray(); given.Add(WordEndingAt(2)); given.Add(WordEndingAt(5)); var words = new MeasuredWords(font, "ab cd", given); CheckEqual(font, words.Font); CheckEqual("ab cd", words.Text.ToString()); CheckTrue(ReferenceEquals(given, words.Words)); CheckEqual(2, words.Count); } public void Text_HoldsWhatWordMeasurerYields() { CheckEqual(WordMeasurerTests.Words(ThreeWords), ReadAll(new MeasuredWords(WordMeasurerTests.SharedFont, ThreeWords))); } public void Text_WithMoreWordsThanFirstGuessed_KeepsThemAll() { CheckEqual(WordMeasurerTests.Words(ManyShortWords), ReadAll(new MeasuredWords(WordMeasurerTests.SharedFont, ManyShortWords))); } public void EmptyText_HasNoWords() { CheckEqual(0, new MeasuredWords(WordMeasurerTests.SharedFont, "").Count); } public void Text_KeepsTheFontAndText() { MetricsFont font = WordMeasurerTests.SharedFont; var words = new MeasuredWords(font, ThreeWords); CheckEqual(font, words.Font); CheckEqual(ThreeWords, words.Text.ToString()); } public void Text_WithNullFont_Throws() { CheckThrow(typeof(System.ArgumentNullException)); new MeasuredWords(null, "ab"); } public void Text_HasNoFaces() { CheckNull(new MeasuredWords(WordMeasurerTests.SharedFont, ThreeWords).Faces); } public void TextWithFaces_KeepsThemAndMeasuresEachGlyphInItsFont() { FaceRuns faces = FacedMeasuringTests.Runs(Narrow, (3, 5, Wide)); var words = new MeasuredWords(Narrow, "ab cd", faces); CheckTrue(ReferenceEquals(faces, words.Faces)); CheckEqual(FacedMeasuringTests.Words("ab cd", Narrow, (3, 5, Wide)), FacedWordsOf(words)); } public void TextWithFacesAllInTheFont_HasNoFaces() { var words = new MeasuredWords(Narrow, "ab cd", new FaceRuns(Narrow)); CheckNull(words.Faces); } public void TextWithNullFaces_HasNoFaces() { CheckNull(new MeasuredWords(Narrow, "ab cd", faces: null).Faces); } public void TextWithFacesBuiltOnAnotherFont_Throws() { FaceRuns faces = FacedMeasuringTests.Runs(Wide, (3, 5, Narrow)); CheckThrow(typeof(System.ArgumentException)); new MeasuredWords(Narrow, "ab cd", faces); } } public class CapacityTests : TestFixture { public void IsTheStorageNotTheCount() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, "", new DynamicArray(capacity: 4)); CheckEqual(4, words.Capacity); } } public class MeasureTests : TestFixture { /// /// Built once, since building it mid-test would count against an allocation test. /// public static readonly MeasuredWords AllocationWords = new MeasuredWords(WordMeasurerTests.SharedFont, ManyShortWords); public void ReplacesTheWordsAndText() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, "xy zw"); words.Measure(WordMeasurerTests.SharedFont, ThreeWords); CheckEqual(WordMeasurerTests.Words(ThreeWords), ReadAll(words)); CheckEqual(ThreeWords, words.Text.ToString()); } public void ShorterText_KeepsTheStorage() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, ManyShortWords); int capacity = words.Capacity; words.Measure(WordMeasurerTests.SharedFont, ThreeWords); CheckEqual(capacity, words.Capacity); } public void LongerText_GrowsToHoldEveryWord() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, "ab"); words.Measure(WordMeasurerTests.SharedFont, ManyShortWords); CheckEqual(WordMeasurerTests.Words(ManyShortWords), ReadAll(words)); } public void NoStorage_Grows() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, "", new DynamicArray()); words.Measure(WordMeasurerTests.SharedFont, ThreeWords); CheckEqual(WordMeasurerTests.Words(ThreeWords), ReadAll(words)); } public void PartwayThroughReading_StartsFromTheFirstWord() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, "xy zw"); words.MoveNext(); words.Measure(WordMeasurerTests.SharedFont, ThreeWords); CheckEqual(-1, words.CurrentIndex); } public void NewFont_IsKeptAndEndsMeasureWithIt() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, ThreeWords); words.Measure(LineWrapperTests.OnePixelFont, ThreeWords); var word = new MeasuredWord(new LineBreak(3, isMandatory: false, trailingSpaces: 1), contentWidth: 2, leadingKerning: 0, endKerning: 0); CheckEqual(LineWrapperTests.OnePixelFont, words.Font); CheckEqual(1, words.Ends.TrailingWidthOf(word)); } public void NullFont_Throws() { var words = new MeasuredWords(WordMeasurerTests.SharedFont, ThreeWords); CheckThrow(typeof(System.ArgumentNullException)); words.Measure(null, "xy"); } [AllocationTest] public void Remeasuring_AllocatesNothing() { AllocationWords.Measure(AllocationWords.Font, ThreeWords); CheckNoAllocations(); CheckEqual(3, AllocationWords.Count); } private static readonly FaceRuns AllocationFaces = FacedMeasuringTests.Runs(Narrow, (3, 5, Wide)); public static readonly MeasuredWords FacedAllocationWords = new MeasuredWords(Narrow, ManyShortWords, AllocationFaces); [AllocationTest] public void RemeasuringWithFaces_AllocatesNothing() { FacedAllocationWords.Measure(Narrow, ThreeWords, AllocationFaces); CheckNoAllocations(); CheckEqual(3, FacedAllocationWords.Count); } public void Faces_ReplaceTheWordsAndText() { var words = new MeasuredWords(Narrow, "xy zw"); FaceRuns faces = FacedMeasuringTests.Runs(Narrow, (3, 5, Wide)); words.Measure(Narrow, "ab cd", faces); CheckTrue(ReferenceEquals(faces, words.Faces)); CheckEqual("ab cd", words.Text.ToString()); CheckEqual(FacedMeasuringTests.Words("ab cd", Narrow, (3, 5, Wide)), FacedWordsOf(words)); } public void MeasuringWithoutFacesAfterWithThem_DropsThem() { var words = new MeasuredWords(Narrow, "ab cd", FacedMeasuringTests.Runs(Narrow, (3, 5, Wide))); words.Measure(Narrow, "ab cd"); CheckNull(words.Faces); CheckEqual(FacedMeasuringTests.Words("ab cd", Narrow, (7, 8, Wide)), FacedWordsOf(words)); } public void FacesAllInTheFont_MeasureThePlainWayAndDropThem() { var words = new MeasuredWords(Narrow, "ab cd", FacedMeasuringTests.Runs(Narrow, (3, 5, Wide))); words.Measure(Narrow, "ab cd", new FaceRuns(Narrow)); CheckNull(words.Faces); } public void NullFaces_MeasureThePlainWayAndDropTheOldOnes() { var words = new MeasuredWords(Narrow, "ab cd", FacedMeasuringTests.Runs(Narrow, (3, 5, Wide))); words.Measure(Narrow, "ab cd", faces: null); CheckNull(words.Faces); } public void FacesOnANewFont_KeepThatFontAndMeasureItsEnds() { var words = new MeasuredWords(Narrow, "ab"); var word = new MeasuredWord(new LineBreak(3, isMandatory: false, trailingSpaces: 1), contentWidth: 2, leadingKerning: 0, endKerning: 0); words.Measure(Wide, "ab cd", FacedMeasuringTests.Runs(Wide, (3, 5, Narrow))); CheckEqual(Wide, words.Font); CheckEqual(6, words.Ends.TrailingWidthOf(word)); } public void FacesBuiltOnAnotherFont_Throw() { var words = new MeasuredWords(Narrow, "ab"); FaceRuns faces = FacedMeasuringTests.Runs(Wide, (3, 5, Narrow)); CheckThrow(typeof(System.ArgumentException)); words.Measure(Narrow, "ab cd", faces); } public void PartwayThroughReadingWithFaces_StartsFromTheFirstWord() { var words = new MeasuredWords(Narrow, "xy zw"); words.MoveNext(); words.Measure(Narrow, "ab cd", FacedMeasuringTests.Runs(Narrow, (3, 5, Wide))); CheckEqual(-1, words.CurrentIndex); } } public class EndsTests : TestFixture { public void MeasureWithTheFontsSpace() { var word = new MeasuredWord(new LineBreak(3, isMandatory: false, trailingSpaces: 1), contentWidth: 20, leadingKerning: 0, endKerning: 0); CheckEqual(LayoutFonts.SpaceWidth, Holding().Ends.TrailingWidthOf(word)); } } public class MoveNextTests : TestFixture { public void ReadsTheCountedWordsInOrder_AndNoFurther() { CheckEqual(new[] { "0..2..2 lead 0 content 20 trailing 0 hyphen 0", "2..5..5 lead 0 content 50 trailing 0 hyphen 0", }, ReadAll(Holding(2, 5))); } public void NoWords_ReadsNothing() { CheckFalse(Holding().MoveNext()); } public void PastTheEnd_StaysFalse() { MeasuredWords words = Holding(2); ReadAll(words); CheckFalse(words.MoveNext()); } } public class RewindTests : TestFixture { public void ReadsTheWordsAgain() { MeasuredWords words = Holding(2, 5); List firstRead = ReadAll(words); words.Rewind(); CheckEqual(firstRead, ReadAll(words)); } } public class CurrentIndexTests : TestFixture { public void CountsWordsFromZero() { MeasuredWords words = Holding(2, 5); words.MoveNext(); words.MoveNext(); CheckEqual(1, words.CurrentIndex); } } } }