using System; using Tessera.Typesetting; using UnitTestSharp; namespace Tessera.UnitTests.Typesetting { public class FontFamilyTests : TestFixture { private static MetricsFont Face(int letterWidth = 10, string page = "family.png") => LayoutFonts.Face(letterWidth, page: page); public class CtorTests : TestFixture { public void NullRegular_Throws() { CheckThrow(typeof(ArgumentNullException)); new FontFamily(null); } public void RegularAlone_LeavesTheOthersOut() { MetricsFont regular = Face(); var family = new FontFamily(regular); CheckTrue(ReferenceEquals(regular, family.Regular)); CheckNull(family.Bold); CheckNull(family.Italic); CheckNull(family.BoldItalic); } public void Faces_AreKept() { MetricsFont regular = Face(10); MetricsFont bold = Face(11); MetricsFont italic = Face(12); MetricsFont boldItalic = Face(13); var family = new FontFamily(regular, bold, italic, boldItalic); CheckTrue(ReferenceEquals(bold, family.Bold)); CheckTrue(ReferenceEquals(italic, family.Italic)); CheckTrue(ReferenceEquals(boldItalic, family.BoldItalic)); } public void EveryFace_IsToldItsFamily() { MetricsFont regular = Face(10); MetricsFont bold = Face(11); MetricsFont italic = Face(12); MetricsFont boldItalic = Face(13); var family = new FontFamily(regular, bold, italic, boldItalic); CheckTrue(ReferenceEquals(family, regular.Family)); CheckTrue(ReferenceEquals(family, bold.Family)); CheckTrue(ReferenceEquals(family, italic.Family)); CheckTrue(ReferenceEquals(family, boldItalic.Family)); } public void BoldOnAnotherPage_Throws() { MetricsFont regular = Face(); MetricsFont bold = Face(page: "other.png"); CheckThrow(typeof(ArgumentException)); new FontFamily(regular, bold); } public void ItalicOnAnotherPage_Throws() { MetricsFont regular = Face(); MetricsFont italic = Face(page: "other.png"); CheckThrow(typeof(ArgumentException)); new FontFamily(regular, italic: italic); } public void BoldItalicOnAnotherPage_Throws() { MetricsFont regular = Face(); MetricsFont boldItalic = Face(page: "other.png"); CheckThrow(typeof(ArgumentException)); new FontFamily(regular, boldItalic: boldItalic); } public void AFaceAlreadyInAFamily_Throws() { MetricsFont taken = Face(); new FontFamily(taken); CheckThrow(typeof(InvalidOperationException)); new FontFamily(Face(), bold: taken); } public void AFaceAlreadyInAFamily_LeavesTheOthersWithoutOne() { MetricsFont taken = Face(); new FontFamily(taken); MetricsFont regular = Face(); try { new FontFamily(regular, bold: Face(), italic: taken); } catch (InvalidOperationException) { } CheckNull(regular.Family); } } public class IsBoldTests : TestFixture { private static FontFamily Complete(out MetricsFont[] faces) { faces = new[] { Face(10), Face(11), Face(12), Face(13) }; return new FontFamily(faces[0], faces[1], faces[2], faces[3]); } public void TheBoldFaces_Are() { FontFamily family = Complete(out MetricsFont[] faces); CheckTrue(family.IsBold(faces[1])); CheckTrue(family.IsBold(faces[3])); } public void TheOtherFaces_Arent() { FontFamily family = Complete(out MetricsFont[] faces); CheckFalse(family.IsBold(faces[0])); CheckFalse(family.IsBold(faces[2])); } public void AFontOfNoFamily_Isnt() { CheckFalse(Complete(out _).IsBold(Face())); } public void NoFont_Isnt() { CheckFalse(new FontFamily(Face()).IsBold(null)); } } public class IsItalicTests : TestFixture { private static FontFamily Complete(out MetricsFont[] faces) { faces = new[] { Face(10), Face(11), Face(12), Face(13) }; return new FontFamily(faces[0], faces[1], faces[2], faces[3]); } public void TheItalicFaces_Are() { FontFamily family = Complete(out MetricsFont[] faces); CheckTrue(family.IsItalic(faces[2])); CheckTrue(family.IsItalic(faces[3])); } public void TheOtherFaces_Arent() { FontFamily family = Complete(out MetricsFont[] faces); CheckFalse(family.IsItalic(faces[0])); CheckFalse(family.IsItalic(faces[1])); } public void NoFont_Isnt() { CheckFalse(new FontFamily(Face()).IsItalic(null)); } } public class FaceForTests : TestFixture { public void EachCombination_IsItsFace() { MetricsFont[] faces = { Face(10), Face(11), Face(12), Face(13) }; var family = new FontFamily(faces[0], faces[1], faces[2], faces[3]); CheckTrue(ReferenceEquals(faces[0], family.FaceFor(bold: false, italic: false))); CheckTrue(ReferenceEquals(faces[1], family.FaceFor(bold: true, italic: false))); CheckTrue(ReferenceEquals(faces[2], family.FaceFor(bold: false, italic: true))); CheckTrue(ReferenceEquals(faces[3], family.FaceFor(bold: true, italic: true))); } public void AMissingFace_IsNull() { var family = new FontFamily(Face()); CheckNull(family.FaceFor(bold: true, italic: false)); CheckNull(family.FaceFor(bold: false, italic: true)); CheckNull(family.FaceFor(bold: true, italic: true)); } } public class SharePagesTests : TestFixture { private static MetricsFont Paged(params string[] pages) { return new MetricsFont(new MetricsFont.Data { InvalidGlyph = new GlyphMetrics { Width = 8 }, LineHeight = 12, Pages = pages, }); } public void FontsNamingTheSamePage_Do() { CheckTrue(FontFamily.SharePages(Paged("sheet.png"), Paged("sheet.png"))); } public void FontsNamingDifferentPages_Dont() { CheckFalse(FontFamily.SharePages(Paged("sheet.png"), Paged("other.png"))); } public void FontsNamingTheSamePagesInADifferentOrder_Dont() { CheckFalse(FontFamily.SharePages(Paged("a.png", "b.png"), Paged("b.png", "a.png"))); } public void FontsNamingADifferentNumberOfPages_Dont() { CheckFalse(FontFamily.SharePages(Paged("a.png"), Paged("a.png", "b.png"))); } public void FontsNamingNoPages_Do() { CheckTrue(FontFamily.SharePages(Face(page: null), Face(page: null))); } public void ANamedPageAndNoPages_Dont() { CheckFalse(FontFamily.SharePages(Paged("a.png"), Face(page: null))); CheckFalse(FontFamily.SharePages(Face(page: null), Paged("a.png"))); } } } }