using System; using Amphora; using Tessera.Unicode; using UnitTestSharp; namespace Tessera.UnitTests.Unicode { public class CodePointsTests : TestFixture { public static readonly string Pair = char.ConvertFromUtf32(0x20BB7); // Combining acute accent, a mark in the Basic Multilingual Plane. public static readonly string Acute = ((char)0x301).ToString(); // Musical symbol combining stem, a mark that needs a surrogate pair. public static readonly string AstralMark = char.ConvertFromUtf32(0x1D165); public static readonly string ZeroWidthJoiner = ((char)0x200D).ToString(); // A pair, a mark on a letter, two marks, a CR LF, a mark of its own on a letter, a joiner, and plain letters. public static readonly string Mixed = "ab" + Pair + "e" + Acute + Acute + "x\r\ny" + AstralMark + "z" + ZeroWidthJoiner + "q" + Acute; public class NextEndInAViewTests : TestFixture { public void MatchesTheStringAtEveryPosition() { StringView view = ViewSamples.ViewOf(Mixed); for (int i = 0; i <= Mixed.Length; ++i) { CheckEqual(CodePoints.NextEnd(Mixed, i), CodePoints.NextEnd(view, i)); } } public void PositionPastTheView_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.NextEnd(ViewSamples.ViewOf("abc"), 4); } } public class PreviousStartInAViewTests : TestFixture { public void MatchesTheStringAtEveryPosition() { StringView view = ViewSamples.ViewOf(Mixed); for (int i = 0; i <= Mixed.Length; ++i) { CheckEqual(CodePoints.PreviousStart(Mixed, i), CodePoints.PreviousStart(view, i)); } } public void PositionPastTheView_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.PreviousStart(ViewSamples.ViewOf("abc"), 4); } } public class NextEndTests : TestFixture { public void Character_AdvancesOne() { CheckEqual(1, CodePoints.NextEnd("abc", 0)); } public void Pair_AdvancesTwo() { CheckEqual(3, CodePoints.NextEnd("a" + Pair + "b", 1)); } public void FromInsideAPair_AdvancesOne() { CheckEqual(3, CodePoints.NextEnd("a" + Pair + "b", 2)); } public void UnpairedHighSurrogate_AdvancesOne() { CheckEqual(2, CodePoints.NextEnd("a" + (char)0xD842, 1)); CheckEqual(2, CodePoints.NextEnd("a" + (char)0xD842 + "b", 1)); } public void AtTheEnd_StaysThere() { CheckEqual(3, CodePoints.NextEnd("abc", 3)); } public void StepsOnlyOverTheCodePointEvenWhenMarksFollow() { CheckEqual(1, CodePoints.NextEnd("e" + Acute, 0)); } public void NullText_Throws() { CheckThrow(typeof(ArgumentNullException)); CodePoints.NextEnd(null, 0); } public void NegativePosition_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.NextEnd("abc", -1); } public void PositionPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.NextEnd("abc", 4); } } public class PreviousStartTests : TestFixture { public void Character_StepsBackOne() { CheckEqual(2, CodePoints.PreviousStart("abc", 3)); } public void Pair_StepsBackTwo() { CheckEqual(1, CodePoints.PreviousStart("a" + Pair + "b", 3)); } public void FromInsideAPair_StepsBackOne() { CheckEqual(1, CodePoints.PreviousStart("a" + Pair + "b", 2)); } public void UnpairedLowSurrogate_StepsBackOne() { CheckEqual(1, CodePoints.PreviousStart("a" + (char)0xDFB7, 2)); } public void AtTheStart_StaysThere() { CheckEqual(0, CodePoints.PreviousStart("abc", 0)); } public void NullText_Throws() { CheckThrow(typeof(ArgumentNullException)); CodePoints.PreviousStart(null, 0); } public void NegativePosition_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.PreviousStart("abc", -1); } public void PositionPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); CodePoints.PreviousStart("abc", 4); } } } }