using System.Linq; using Glass.Typesetting; using UnitTestSharp; namespace Glass.UnitTests.Typesetting { public class LineBreakTests : TestFixture { public class CtorTests : TestFixture { public void KeepsPositionAndWhetherItsMandatory() { var lineBreak = new LineBreak(7, isMandatory: true); CheckEqual(7, lineBreak.Position); CheckTrue(lineBreak.IsMandatory); } public void Optional_KeepsPositionAndIsNotMandatory() { var lineBreak = new LineBreak(7, isMandatory: false); CheckEqual(7, lineBreak.Position); CheckFalse(lineBreak.IsMandatory); } public void ByDefault_HasNoTrailingSpacesAndEndsPlain() { var lineBreak = new LineBreak(7, isMandatory: true); CheckEqual(0, lineBreak.TrailingSpaces); CheckEqual(LineBreakEnding.Plain, lineBreak.Ending); } public void KeepsTrailingSpacesAndEnding() { var lineBreak = new LineBreak(9, isMandatory: true, trailingSpaces: 3, ending: LineBreakEnding.CarriageReturnLineFeed); CheckEqual(new object[] { 9, true, 3, LineBreakEnding.CarriageReturnLineFeed }, new object[] { lineBreak.Position, lineBreak.IsMandatory, lineBreak.TrailingSpaces, lineBreak.Ending, }); } public void LargestPositionAndSpaces_StayApartFromEachOtherAndTheFlags() { var full = new LineBreak(LineBreak.MaxPosition, isMandatory: true, trailingSpaces: LineBreak.MaxPosition, ending: LineBreakEnding.CarriageReturnLineFeed); var empty = new LineBreak(LineBreak.MaxPosition, isMandatory: false); CheckEqual(new object[] { LineBreak.MaxPosition, true, LineBreak.MaxPosition, LineBreakEnding.CarriageReturnLineFeed }, new object[] { full.Position, full.IsMandatory, full.TrailingSpaces, full.Ending }); CheckEqual(new object[] { LineBreak.MaxPosition, false, 0, LineBreakEnding.Plain }, new object[] { empty.Position, empty.IsMandatory, empty.TrailingSpaces, empty.Ending }); } } public class HardBreakLengthTests : TestFixture { public void IsTheLengthOfWhatEndsTheWord() { CheckEqual(new[] { 0, 0, 1, 2 }, new[] { new LineBreak(5, isMandatory: true, ending: LineBreakEnding.Plain).HardBreakLength, new LineBreak(5, isMandatory: true, ending: LineBreakEnding.SoftHyphen).HardBreakLength, new LineBreak(5, isMandatory: true, ending: LineBreakEnding.HardBreak).HardBreakLength, new LineBreak(5, isMandatory: true, ending: LineBreakEnding.CarriageReturnLineFeed) .HardBreakLength, }); } } public class SpacesEndTests : TestFixture { public void IsBeforeTheHardBreak() { CheckEqual(7, new LineBreak(9, isMandatory: true, trailingSpaces: 2, ending: LineBreakEnding.CarriageReturnLineFeed).SpacesEnd); } } public class ContentEndTests : TestFixture { public void IsBeforeTheSpacesAndTheHardBreak() { CheckEqual(6, new LineBreak(9, isMandatory: true, trailingSpaces: 2, ending: LineBreakEnding.HardBreak).ContentEnd); } } public class IsAfterSoftHyphenTests : TestFixture { public const char SoftHyphen = LineBreakIterator.SoftHyphen; public const char Hyphen = (char)0x2010; public static bool[] FlagsIn(string text) => LineBreakIteratorTests.BreaksIn(text).Select(each => each.IsAfterSoftHyphen).ToArray(); public void BreakRightAfterASoftHyphen_IsMarked() { CheckEqual(new[] { true, false }, FlagsIn($"ab{SoftHyphen}cd")); } public void SoftHyphenEndingTheText_MarksTheLastBreak() { CheckEqual(new[] { true }, FlagsIn($"ab{SoftHyphen}")); } public void OtherHyphenOfTheSameClass_IsNotMarked() { CheckEqual(new[] { false, false }, FlagsIn($"ab{Hyphen}cd")); } public void SoftHyphenCarryingAMark_StillMarksTheBreak() { CheckEqual(new[] { true, false }, FlagsIn($"ab{SoftHyphen}{(char)0x0301}cd")); } public void SoftHyphenBeforeASpace_DoesNotMarkTheBreakAfterTheSpace() { CheckEqual(new[] { false, false }, FlagsIn($"ab{SoftHyphen} cd")); } } } }