using System; using System.Collections.Generic; using Tessera.Text; using Tessera.Typesetting; using UnitTestSharp; using Tessera.UnitTests.Text; namespace Tessera.UnitTests.Typesetting { public class FaceRunsTests : TestFixture { public static readonly IFontMetrics Base = FacedMeasuringTests.Narrow; public static readonly IFontMetrics Bold = FacedMeasuringTests.Wide; public static readonly IFontMetrics Italic = LayoutFonts.Face(letterWidth: 11); public class CtorTests : TestFixture { public void NullBaseFont_Throws() { CheckThrow(typeof(ArgumentNullException)); new FaceRuns(null); } public void KeepsTheBaseFont() { CheckTrue(ReferenceEquals(Base, new FaceRuns(Base).BaseFont)); } public void SetsEverythingInTheBaseFont() { var faces = new FaceRuns(Base); CheckTrue(ReferenceEquals(Base, faces.FontAt(0))); CheckTrue(ReferenceEquals(Base, faces.FontAt(1000))); } } public class IsUniformTests : TestFixture { public void NoRuns_IsTrue() { CheckTrue(new FaceRuns(Base).IsUniform); } public void ARunInAnotherFont_IsFalse() { var faces = new FaceRuns(Base); faces.Add(2, 4, Bold); CheckFalse(faces.IsUniform); } public void OnlyRunsInTheBaseFont_IsTrue() { var faces = new FaceRuns(Base); faces.Add(2, 4, Base); CheckTrue(faces.IsUniform); } public void AfterClearing_IsTrue() { var faces = new FaceRuns(Base); faces.Add(2, 4, Bold); faces.Clear(); CheckTrue(faces.IsUniform); } } public class IsPlainTests : TestFixture { public void NoRuns_IsTrue() { CheckTrue(new FaceRuns(Base).IsPlain); } public void ARunInAnotherFont_IsFalse() { var faces = new FaceRuns(Base); faces.Add(2, 4, Bold); CheckFalse(faces.IsPlain); } public void ACounter_IsFalse() { var faces = new FaceRuns(Base); faces.AddCounter(new CounterRun(1, 3, 30, CounterAlignment.Right, '0', 8)); CheckFalse(faces.IsPlain); } public void AfterClearing_IsTrue() { var faces = new FaceRuns(Base); faces.Add(2, 4, Bold); faces.AddCounter(new CounterRun(5, 7, 30, CounterAlignment.Right, '0', 8)); faces.Clear(); CheckTrue(faces.IsPlain); } } public class AddTests : TestFixture { public void ARun_SetsItsCharactersInItsFont() { var faces = new FaceRuns(Base); faces.Add(2, 5, Bold); CheckTrue(ReferenceEquals(Base, faces.FontAt(1))); CheckTrue(ReferenceEquals(Bold, faces.FontAt(2))); CheckTrue(ReferenceEquals(Bold, faces.FontAt(4))); CheckTrue(ReferenceEquals(Base, faces.FontAt(5))); } public void ARunFromTheStart_SetsItsCharactersInItsFont() { var faces = new FaceRuns(Base); faces.Add(0, 3, Bold); CheckTrue(ReferenceEquals(Bold, faces.FontAt(0))); CheckTrue(ReferenceEquals(Bold, faces.FontAt(2))); CheckTrue(ReferenceEquals(Base, faces.FontAt(3))); } public void RunsApart_LeaveTheBaseFontBetweenThem() { var faces = new FaceRuns(Base); faces.Add(1, 2, Bold); faces.Add(4, 6, Italic); CheckTrue(ReferenceEquals(Bold, faces.FontAt(1))); CheckTrue(ReferenceEquals(Base, faces.FontAt(2))); CheckTrue(ReferenceEquals(Base, faces.FontAt(3))); CheckTrue(ReferenceEquals(Italic, faces.FontAt(4))); CheckTrue(ReferenceEquals(Italic, faces.FontAt(5))); CheckTrue(ReferenceEquals(Base, faces.FontAt(6))); } public void RunsInDifferentFontsThatMeet_StaySeparate() { var faces = new FaceRuns(Base); faces.Add(1, 3, Bold); faces.Add(3, 5, Italic); CheckTrue(ReferenceEquals(Bold, faces.FontAt(2))); CheckTrue(ReferenceEquals(Italic, faces.FontAt(3))); CheckTrue(ReferenceEquals(Base, faces.FontAt(5))); } public void RunsInTheSameFontThatMeet_AreOne() { var faces = new FaceRuns(Base); faces.Add(1, 3, Bold); faces.Add(3, 5, Bold); CheckEqual(3, faces.Count); CheckTrue(ReferenceEquals(Bold, faces.FontAt(4))); CheckTrue(ReferenceEquals(Base, faces.FontAt(5))); } public void RunsInTheSameFontWithAGapBetween_StayApart() { var faces = new FaceRuns(Base); faces.Add(1, 3, Bold); faces.Add(4, 5, Bold); CheckEqual(5, faces.Count); CheckTrue(ReferenceEquals(Base, faces.FontAt(3))); } public void ARunInTheBaseFont_ChangesNothing() { var faces = new FaceRuns(Base); faces.Add(1, 3, Base); CheckEqual(1, faces.Count); } public void ARunInTheBaseFontBetweenOthers_StillLeavesThemApart() { var faces = new FaceRuns(Base); faces.Add(0, 2, Bold); faces.Add(2, 4, Base); faces.Add(4, 6, Bold); CheckEqual(4, faces.Count); CheckTrue(ReferenceEquals(Base, faces.FontAt(3))); CheckTrue(ReferenceEquals(Bold, faces.FontAt(4))); } public void NullFont_Throws() { var faces = new FaceRuns(Base); CheckThrow(typeof(ArgumentNullException)); faces.Add(1, 3, null); } public void EmptyRun_Throws() { var faces = new FaceRuns(Base); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(2, 2, Bold); } public void BackwardsRun_Throws() { var faces = new FaceRuns(Base); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(3, 2, Bold); } public void NegativeStart_Throws() { var faces = new FaceRuns(Base); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(-1, 2, Bold); } public void RunOverlappingTheLast_Throws() { var faces = new FaceRuns(Base); faces.Add(2, 6, Bold); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(5, 8, Italic); } public void RunBeforeTheLast_Throws() { var faces = new FaceRuns(Base); faces.Add(4, 6, Bold); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(0, 2, Italic); } public void OverlapInTheBaseFont_StillThrows() { var faces = new FaceRuns(Base); faces.Add(2, 6, Bold); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.Add(3, 4, Base); } } public class ClearTests : TestFixture { public void ForgetsEveryRun() { var faces = new FaceRuns(Base); faces.Add(1, 3, Bold); faces.Clear(); CheckEqual(1, faces.Count); CheckTrue(ReferenceEquals(Base, faces.FontAt(2))); } public void LetsRunsStartOverFromTheStart() { var faces = new FaceRuns(Base); faces.Add(5, 8, Bold); faces.Clear(); faces.Add(0, 2, Italic); CheckTrue(ReferenceEquals(Italic, faces.FontAt(1))); } public void KeepsTheBaseFont() { var faces = new FaceRuns(Base); faces.Clear(); CheckTrue(ReferenceEquals(Base, faces.BaseFont)); } } public class FontAtTests : TestFixture { public void BeforeTheText_IsTheFirstRunsFont() { var faces = new FaceRuns(Base); faces.Add(0, 2, Bold); CheckTrue(ReferenceEquals(Bold, faces.FontAt(-1))); } public void EveryRun_IsFoundWhateverHowManyThereAre() { var fonts = new[] { Bold, Italic, Bold, Italic, Bold, Italic, Bold }; var faces = new FaceRuns(Base); for (int i = 0; i < fonts.Length; ++i) { faces.Add(i * 3 + 1, i * 3 + 3, fonts[i]); } for (int i = 0; i < fonts.Length; ++i) { CheckTrue(ReferenceEquals(Base, faces.FontAt(i * 3))); CheckTrue(ReferenceEquals(fonts[i], faces.FontAt(i * 3 + 1))); CheckTrue(ReferenceEquals(fonts[i], faces.FontAt(i * 3 + 2))); } } public void PastTheLastRun_IsTheBaseFont() { var faces = new FaceRuns(Base); faces.Add(1, 3, Bold); CheckTrue(ReferenceEquals(Base, faces.FontAt(int.MaxValue))); } } public class CountTests : TestFixture { public void CountsTheBaseFontStretchesBetweenAndAfterRuns() { var faces = new FaceRuns(Base); faces.Add(2, 5, Bold); CheckEqual(3, faces.Count); } } public class StartOfTests : TestFixture { public void IsWhereEachRunStarts() { var faces = new FaceRuns(Base); faces.Add(2, 5, Bold); CheckEqual(0, faces.StartOf(0)); CheckEqual(2, faces.StartOf(1)); CheckEqual(5, faces.StartOf(2)); } } public class FontOfTests : TestFixture { public void IsTheFontOfEachRun() { var faces = new FaceRuns(Base); faces.Add(2, 5, Bold); CheckTrue(ReferenceEquals(Base, faces.FontOf(0))); CheckTrue(ReferenceEquals(Bold, faces.FontOf(1))); CheckTrue(ReferenceEquals(Base, faces.FontOf(2))); } } private static CounterRun CounterAt(int start, int end) { return new CounterRun(start, end, room: 30, CounterAlignment.Right, zeroDigit: '0', digitWidth: 8); } public class CounterCountTests : TestFixture { public void NoCounters_IsZero() { CheckEqual(0, new FaceRuns(Base).CounterCount); } public void IsHowManyWereAdded() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 3)); faces.AddCounter(CounterAt(5, 6)); CheckEqual(2, faces.CounterCount); } public void AddingACounter_LeavesTheFontsUniform() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 3)); CheckTrue(faces.IsUniform); } } public class CounterAtTests : TestFixture { public void IsTheCounterAtThatIndexInTextOrder() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 3)); faces.AddCounter(CounterAt(5, 6)); CheckEqual(1, faces.CounterAt(0).Start); CheckEqual(5, faces.CounterAt(1).Start); CheckEqual(6, faces.CounterAt(1).End); } } public class AddCounterTests : TestFixture { public void CountersThatTouch_AreAllowed() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 3)); faces.AddCounter(CounterAt(3, 5)); CheckEqual(2, faces.CounterCount); } public void ACounterThatOverlapsTheLast_Throws() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 4)); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.AddCounter(CounterAt(3, 6)); } public void ACounterBeforeTheLast_Throws() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(5, 7)); CheckThrow(typeof(ArgumentOutOfRangeException)); faces.AddCounter(CounterAt(1, 2)); } } public class CounterClearTests : TestFixture { public void ForgetsEveryCounter() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(1, 3)); faces.Clear(); CheckEqual(0, faces.CounterCount); } public void LetsCountersStartOverFromTheStart() { var faces = new FaceRuns(Base); faces.AddCounter(CounterAt(5, 8)); faces.Clear(); faces.AddCounter(CounterAt(0, 2)); CheckEqual(1, faces.CounterCount); } } public class MeasureCounterTests : TestFixture { private static CounterSpec SpecOf(NumberFormat format, NumberKind kind, int digits, int precision = 0, CounterAlignment alignment = CounterAlignment.Right) { return new CounterSpec(format, kind, digits, precision, negative: false, alignment); } public void ACount_ReservesTheWidestDigitsAndTheSeparators() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(2, 8, SpecOf(NumberFormat.Invariant, NumberKind.Count, 5), font); CheckEqual(5 * 8 + CounterBoxTests.CommaWidth, counter.Room); } public void ACount_KeepsWhereItIsAndHowItSits() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(2, 8, SpecOf(NumberFormat.Invariant, NumberKind.Count, 5, alignment: CounterAlignment.Left), font); CheckEqual(2, counter.Start); CheckEqual(8, counter.End); CheckEqual(CounterAlignment.Left, counter.Alignment); } public void TheDigitCell_IsTheWidestDigitOfTheFace() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 3, SpecOf(NumberFormat.Invariant, NumberKind.Count, 3), font); CheckEqual(8, counter.DigitWidth); CheckEqual('0', counter.ZeroDigit); } public void AFixedNumber_ReservesItsDecimalSeparatorAndPlaces() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 4, SpecOf(NumberFormat.Invariant, NumberKind.Fixed, 2, precision: 1), font); CheckEqual(3 * 8 + CounterBoxTests.PeriodWidth, counter.Room); } public void APercent_ReservesItsSign() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 5, SpecOf(NumberFormat.Invariant, NumberKind.Percent, 2, precision: 1), font); CheckEqual(3 * 8 + CounterBoxTests.PeriodWidth + CounterBoxTests.InvalidWidth, counter.Room); } public void ALanguageWithOtherDigits_ReservesTheWidestOfThem() { NumberFormat arabic = NumberFormat.For("ar-EG"); MetricsFont font = CounterBoxTests.FontOf(new[] { 2, 3, 4, 5, 6, 7, 8, 9, 1, 2 }, zeroDigit: arabic.ZeroDigit); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 3, SpecOf(arabic, NumberKind.Count, 3), font); CheckEqual(3 * 9, counter.Room); CheckEqual(arabic.ZeroDigit, counter.ZeroDigit); } public void ALanguageThatGroupsWithASpace_ReservesTheSpace() { var spaced = new NumberFormat(" ", 3, 3, "-", ".", "NaN", "Infinity", "e", ShortNotation.English); MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 6, SpecOf(spaced, NumberKind.Count, 5), font); CheckEqual(5 * 8 + CounterBoxTests.SpaceWidth, counter.Room); } private static MetricsFont FontWithMarkers(int afternoonWidth, int morningWidth) { return CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, letterWidths: new Dictionary { { 'A', morningWidth }, { 'P', afternoonWidth }, { 'M', 12 }, { ':', 3 } }); } public void AClockWhoseMorningMarkerIsWider_ReservesTheMorningMarker() { MetricsFont font = FontWithMarkers(afternoonWidth: 6, morningWidth: 10); var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Clock, 0, ClockFormat.Hours12.Pack(), negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 8, spec, font); CheckEqual(4 * 8 + 3 + CounterBoxTests.SpaceWidth + 10 + 12, counter.Room); } public void AClockWhoseAfternoonMarkerIsWider_ReservesTheAfternoonMarker() { MetricsFont font = FontWithMarkers(afternoonWidth: 10, morningWidth: 6); var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Clock, 0, ClockFormat.Hours12.Pack(), negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 8, spec, font); CheckEqual(4 * 8 + 3 + CounterBoxTests.SpaceWidth + 10 + 12, counter.Room); } public void ADuration_ReservesTheWidestDigitsAndItsUnits() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, letterWidths: new Dictionary { { 'h', 6 }, { 'm', 11 } }); var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Duration, 2, new DurationFormat().Pack(), negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 7, spec, font); CheckEqual(4 * 8 + 6 + CounterBoxTests.SpaceWidth + 11, counter.Room); } private static NumberFormat ThousandsAndMillions() { ShortTier thousands = ShortNotationSamples.Tier(3, "{0}K"); ShortTier millions = ShortNotationSamples.Tier(6, "{0}M"); var notation = new ShortNotation(3, new[] { thousands, thousands, thousands, millions, millions, millions }, PluralRules.None); return new NumberFormat(",", 3, 3, "-", ".", "NaN", "Infinity", "e", notation); } private static MetricsFont FontWithWideMillions() { return CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, letterWidths: new Dictionary { { 'K', 10 }, { 'M', 60 }, { 'e', 7 } }); } public void AShortNumberWhoseSuffixIsWide_ReservesTheDigitsAndSeparatorOfTheFewestWholeDigits() { MetricsFont font = FontWithWideMillions(); var spec = new CounterSpec(ThousandsAndMillions(), NumberKind.ShortExponent, 0, 3, negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 5, spec, font); CheckEqual(3 * 8 + CounterBoxTests.PeriodWidth + 60, counter.Room); } public void AShortNumberWithFewSignificantDigits_ReservesTheMostWholeDigits() { MetricsFont font = FontWithWideMillions(); var spec = new CounterSpec(ThousandsAndMillions(), NumberKind.ShortExponent, 0, 1, negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 4, spec, font); CheckEqual(3 * 8 + 60, counter.Room); } public void ANegativeShortNumber_ReservesItsMinusSign() { MetricsFont font = FontWithWideMillions(); var spec = new CounterSpec(ThousandsAndMillions(), NumberKind.ShortExponent, 0, 1, negative: true, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 5, spec, font); CheckEqual(5 + 3 * 8 + 60, counter.Room); } public void AShortNumberWhoseScientificFormIsWidest_ReservesTheScientificForm() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, letterWidths: new Dictionary { { 'K', 10 }, { 'M', 11 }, { 'e', 7 } }); var spec = new CounterSpec(ThousandsAndMillions(), NumberKind.ShortExponent, 0, 3, negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 5, spec, font); CheckEqual(6 * 8 + CounterBoxTests.PeriodWidth + 7 + 5, counter.Room); } public void AScientificNumber_ReservesItsMantissaAndBothSignsOfItsExponent() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, letterWidths: new Dictionary { { 'e', 7 } }); var spec = new CounterSpec(NumberFormat.Invariant, NumberKind.Scientific, 2, 3, negative: false, CounterAlignment.Right); CounterRun counter = new FaceRuns(font).MeasureCounter(0, 8, spec, font); CheckEqual(5 * 8 + CounterBoxTests.PeriodWidth + 7 + 5, counter.Room); } public void TheFaceMeasuredIn_NeedNotBeTheBase() { MetricsFont baseFont = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits); MetricsFont wide = CounterBoxTests.FontOf(new[] { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 }); CounterRun counter = new FaceRuns(baseFont).MeasureCounter(0, 3, SpecOf(NumberFormat.Invariant, NumberKind.Count, 3), wide); CheckEqual(60, counter.Room); } } } }