using System.Collections.Generic; using Glass.Typesetting; using UnitTestSharp; namespace Glass.UnitTests.Typesetting { public class MeasuredWordsTests : TestFixture { 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`, in an array with a spare slot past them, which must never be read. /// public static MeasuredWords Holding(params int[] ends) { var words = new MeasuredWord[ends.Length + 1]; for (int i = 0; i < ends.Length; ++i) { words[i] = WordEndingAt(ends[i]); } words[ends.Length] = WordEndingAt(99); return new MeasuredWords(WordMeasurerTests.TestFont(), "", words, ends.Length); } 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; } public class CtorTests : TestFixture { public void KeepsTheFontTextAndCount() { SpriteFontDefinition font = WordMeasurerTests.TestFont(); var words = new MeasuredWords(font, "ab cd", new MeasuredWord[4], count: 2); CheckEqual(font, words.Font); CheckEqual("ab cd", words.Text.ToString()); CheckEqual(2, words.Count); } } 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(WordMeasurerTests.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); } } } }