using System.Collections.Generic; using Prism; using System.Linq; using Azimuth; using Tessera.Text; using Tessera.Typesetting; using Tessera.UnitTests.Typesetting; using UnitTestSharp; namespace Tessera.UnitTests.Text { public class TextStyleTests : TestFixture { public class FontTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).Font); } public void Set_IsWhatWasSet() { IFontMetrics font = LayoutFonts.FontWithTenPixelLines; CheckTrue(ReferenceEquals(font, new TextStyle { Font = font }.Font)); } } public class TintColorTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).TintColor); } public void Set_IsWhatWasSet() { CheckEqual((Color?)Colors.Red, new TextStyle { TintColor = Colors.Red }.TintColor); } public void SetToNull_ClearsAnEarlierColor() { CheckNull((new TextStyle { TintColor = Colors.Red } with { TintColor = null }).TintColor); } public void SetToTransparentBlack_IsDifferentFromNone() { CheckEqual((Color?)default(Color), new TextStyle { TintColor = default(Color) }.TintColor); } public void ClearingAnotherSetting_LeavesItSet() { var style = new TextStyle { TintColor = Colors.Red, Scale = 2 } with { Scale = null }; CheckEqual((Color?)Colors.Red, style.TintColor); } } public class CoverageGammaTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).CoverageGamma); } public void Set_IsWhatWasSet() { CheckEqual((Scalar?)2.2, new TextStyle { CoverageGamma = 2.2 }.CoverageGamma); } public void SetToNull_ClearsAnEarlierGamma() { CheckNull((new TextStyle { CoverageGamma = 2 } with { CoverageGamma = null }).CoverageGamma); } public void SetToZero_IsDifferentFromNone() { CheckEqual((Scalar?)0, new TextStyle { CoverageGamma = 0 }.CoverageGamma); } public void ClearingAnotherSetting_LeavesItSet() { var style = new TextStyle { CoverageGamma = 2, TintColor = Colors.Red } with { TintColor = null }; CheckEqual((Scalar?)2, style.CoverageGamma); } } public class StrikethroughTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).Strikethrough); } public void SetToTrue_IsOn() { CheckEqual((bool?)true, new TextStyle { Strikethrough = true }.Strikethrough); } public void SetToFalse_IsDifferentFromNone() { CheckEqual((bool?)false, new TextStyle { Strikethrough = false }.Strikethrough); } public void SetToNull_ClearsAnEarlierSetting() { CheckNull((new TextStyle { Strikethrough = true } with { Strikethrough = null }).Strikethrough); } public void SetToFalseAfterTrue_IsOff() { CheckEqual((bool?)false, (new TextStyle { Strikethrough = true } with { Strikethrough = false }) .Strikethrough); } public void ClearingAnotherSetting_LeavesItSet() { var style = new TextStyle { Strikethrough = true, Scale = 2 } with { Scale = null }; CheckEqual((bool?)true, style.Strikethrough); } public void SettingAnotherSetting_LeavesItAlone() { var style = new TextStyle { Strikethrough = true } with { Scale = 2 }; CheckEqual((bool?)true, style.Strikethrough); } } public class BoldTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).Bold); } public void SetToTrue_IsOn() { CheckEqual((bool?)true, new TextStyle { Bold = true }.Bold); } public void SetToFalse_IsDifferentFromNone() { CheckEqual((bool?)false, new TextStyle { Bold = false }.Bold); } public void SetToNull_ClearsAnEarlierSetting() { CheckNull((new TextStyle { Bold = true } with { Bold = null }).Bold); } public void SetToFalseAfterTrue_IsOff() { CheckEqual((bool?)false, (new TextStyle { Bold = true } with { Bold = false }).Bold); } public void IsKeptApartFromItalicAndStrikethrough() { var style = new TextStyle { Bold = true, Italic = false, Strikethrough = true }; CheckEqual((bool?)true, style.Bold); CheckEqual((bool?)false, style.Italic); CheckEqual((bool?)true, style.Strikethrough); } public void ClearingAnotherSetting_LeavesItSet() { var style = new TextStyle { Bold = true, Scale = 2 } with { Scale = null }; CheckEqual((bool?)true, style.Bold); } } public class ItalicTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).Italic); } public void SetToTrue_IsOn() { CheckEqual((bool?)true, new TextStyle { Italic = true }.Italic); } public void SetToFalse_IsDifferentFromNone() { CheckEqual((bool?)false, new TextStyle { Italic = false }.Italic); } public void SetToNull_ClearsAnEarlierSetting() { CheckNull((new TextStyle { Italic = true } with { Italic = null }).Italic); } public void SetToFalseAfterTrue_IsOff() { CheckEqual((bool?)false, (new TextStyle { Italic = true } with { Italic = false }).Italic); } public void ClearingBold_LeavesItSet() { var style = new TextStyle { Italic = true, Bold = true } with { Bold = null }; CheckEqual((bool?)true, style.Italic); } } public class ScaleTests : TestFixture { public void DefaultsToNone() { CheckNull(default(TextStyle).Scale); } public void Set_IsWhatWasSet() { CheckEqual((Scalar?)3, new TextStyle { Scale = 3 }.Scale); } public void SetToNull_ClearsAnEarlierScale() { CheckNull((new TextStyle { Scale = 2 } with { Scale = null }).Scale); } public void SetToZero_IsDifferentFromNone() { CheckEqual((Scalar?)0, new TextStyle { Scale = 0 }.Scale); } public void ClearingAnotherSetting_LeavesItSet() { var style = new TextStyle { Scale = 2, CoverageGamma = 3 } with { CoverageGamma = null }; CheckEqual((Scalar?)2, style.Scale); } } public class OverlayTests : TestFixture { public void AnInnerStyleThatSetsNothing_LeavesTheOuterStyleAsItWas() { var outer = new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, TintColor = Colors.Red, CoverageGamma = 2, Scale = 3, }; CheckEqual(outer, outer.Overlay(default)); } public void AnOuterStyleThatSetsNothing_TakesTheInnerStyle() { var inner = new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, TintColor = Colors.Red, CoverageGamma = 2, Scale = 3, }; CheckEqual(inner, default(TextStyle).Overlay(inner)); } public void EachSettingTheInnerStyleSets_ReplacesTheOuterOne() { IFontMetrics innerFont = LayoutFonts.Face(letterWidth: 3); var outer = new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, TintColor = Colors.Red, CoverageGamma = 2, Scale = 3, }; var inner = new TextStyle { Font = innerFont, TintColor = Colors.Blue, CoverageGamma = 5, Scale = 7, }; CheckEqual(inner, outer.Overlay(inner)); } public void SettingsOnDifferentSides_AreBothKept() { var outer = new TextStyle { TintColor = Colors.Red }; var inner = new TextStyle { CoverageGamma = 2 }; CheckEqual(new TextStyle { TintColor = Colors.Red, CoverageGamma = 2 }, outer.Overlay(inner)); } public void AnInnerSettingOfZero_ReplacesTheOuterSetting() { CheckEqual((Scalar?)0, new TextStyle { Scale = 3 }.Overlay(new TextStyle { Scale = 0 }).Scale); } public void AnInnerStrikethrough_ReplacesTheOuterOne() { CheckEqual((bool?)true, new TextStyle { Strikethrough = false } .Overlay(new TextStyle { Strikethrough = true }).Strikethrough); } public void AnInnerStrikethroughOff_TurnsOffTheOuterOne() { CheckEqual((bool?)false, new TextStyle { Strikethrough = true } .Overlay(new TextStyle { Strikethrough = false }).Strikethrough); } public void AnInnerStyleThatLeavesStrikethroughAlone_KeepsTheOuterOne() { CheckEqual((bool?)true, new TextStyle { Strikethrough = true } .Overlay(new TextStyle { Scale = 2 }).Strikethrough); } public void AnInnerBold_ReplacesTheOuterOne() { CheckEqual((bool?)true, new TextStyle { Bold = false }.Overlay(new TextStyle { Bold = true }).Bold); } public void AnInnerBoldOff_TurnsOffTheOuterOne() { CheckEqual((bool?)false, new TextStyle { Bold = true }.Overlay(new TextStyle { Bold = false }).Bold); } public void AnInnerStyleThatLeavesBoldAlone_KeepsTheOuterOne() { CheckEqual((bool?)true, new TextStyle { Bold = true }.Overlay(new TextStyle { Scale = 2 }).Bold); } public void BoldAndItalicOnDifferentSides_AreBothKept() { TextStyle style = new TextStyle { Bold = true }.Overlay(new TextStyle { Italic = true }); CheckEqual((bool?)true, style.Bold); CheckEqual((bool?)true, style.Italic); } public void AnInnerItalicOff_TurnsOffTheOuterOne() { CheckEqual((bool?)false, new TextStyle { Italic = true }.Overlay(new TextStyle { Italic = false }).Italic); } public void ThreeStylesChained_LetTheInnermostWin() { var style = new TextStyle { TintColor = Colors.Red, CoverageGamma = 1 } .Overlay(new TextStyle { TintColor = Colors.Green }) .Overlay(new TextStyle { TintColor = Colors.Blue }); CheckEqual(new TextStyle { TintColor = Colors.Blue, CoverageGamma = 1 }, style); } } public class EqualityTests : TestFixture { [UnitTestSharp.AllocationTest] public void Comparing_AllocatesNothing() { var style = new TextStyle { TintColor = Colors.Red, CoverageGamma = 2 }; bool same = style == new TextStyle { TintColor = Colors.Red, CoverageGamma = 2 }; CheckNoAllocations(); CheckTrue(same); } public void EqualStyles_HaveTheSameHashCode() { CheckEqual( new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, Scale = 2, TintColor = Colors.Red, CoverageGamma = 3 }.GetHashCode(), new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, Scale = 2, TintColor = Colors.Red, CoverageGamma = 3 }.GetHashCode()); } public void ASettingSetThenCleared_HashesAsOneNeverSet() { var set = new TextStyle { Scale = 2, TintColor = Colors.Red }; var cleared = set with { Scale = null, TintColor = null }; CheckEqual(default(TextStyle).GetHashCode(), cleared.GetHashCode()); } public void SeparatelyBuiltWithTheSameSettings_AreEqual() { IFontMetrics font = LayoutFonts.FontWithTenPixelLines; CheckEqual(new TextStyle { Font = font, Scale = 2, TintColor = Colors.Red }, new TextStyle { Font = font, Scale = 2, TintColor = Colors.Red }); } public void ASettingSetThenCleared_EqualsOneNeverSet() { CheckEqual(new TextStyle { TintColor = Colors.Red }, new TextStyle { TintColor = Colors.Red, Scale = 2 } with { Scale = null }); } public void StrikethroughOnAndOff_AreNotEqual() { CheckNotEqual(new TextStyle { Strikethrough = true }, new TextStyle { Strikethrough = false }); } public void StrikethroughOff_DiffersFromNeverSet() { CheckNotEqual(default(TextStyle), new TextStyle { Strikethrough = false }); } public void StrikethroughSetThenCleared_HashesAsOneNeverSet() { var cleared = new TextStyle { Strikethrough = true } with { Strikethrough = null }; CheckEqual(default(TextStyle), cleared); CheckEqual(default(TextStyle).GetHashCode(), cleared.GetHashCode()); } public void BoldAndItalicOnAndOff_AreNotEqual() { CheckNotEqual(new TextStyle { Bold = true }, new TextStyle { Bold = false }); CheckNotEqual(new TextStyle { Italic = true }, new TextStyle { Italic = false }); } public void BoldAndItalicSwapped_AreNotEqual() { CheckNotEqual(new TextStyle { Bold = true }, new TextStyle { Italic = true }); } public void BoldAndItalicSetThenCleared_HashAsOneNeverSet() { var cleared = new TextStyle { Bold = true, Italic = false } with { Bold = null, Italic = null }; CheckEqual(default(TextStyle), cleared); CheckEqual(default(TextStyle).GetHashCode(), cleared.GetHashCode()); } public void ASettingSetToZero_DiffersFromOneNeverSet() { CheckNotEqual(default(TextStyle), new TextStyle { Scale = 0 }); } public void DifferingInAnySetting_AreNotEqual() { var style = new TextStyle { Font = LayoutFonts.FontWithTenPixelLines, Scale = 2 }; var variants = new Dictionary { [nameof(TextStyle.Font)] = style with { Font = LayoutFonts.Face(letterWidth: 3) }, [nameof(TextStyle.TintColor)] = style with { TintColor = Colors.Red }, [nameof(TextStyle.CoverageGamma)] = style with { CoverageGamma = 2 }, [nameof(TextStyle.Scale)] = style with { Scale = 3 }, [nameof(TextStyle.Strikethrough)] = style with { Strikethrough = true }, [nameof(TextStyle.Bold)] = style with { Bold = true }, [nameof(TextStyle.Italic)] = style with { Italic = true }, }; CheckEqual(new string[0], variants.Where(variant => variant.Value == style).Select(variant => variant.Key)); } } } }