using System; using System.Collections.Generic; using System.Linq; using Amphora; using Tessera.Unicode; using UnitTestSharp; namespace Tessera.UnitTests.Unicode { public class WordsTests : TestFixture { public static readonly BoundaryTestFile.Case[] Cases = BoundaryTestFile.ReadCases("WordBreakTest.txt"); public const string Acute = "́"; public const string SoftHyphen = "­"; public const string Alef = "א"; public const string Bet = "ב"; // Katakana KA and TA. public const string Katakana = "カタ"; public static readonly string IndicatorU = GraphemeSamples.IndicatorU; public static readonly string IndicatorS = GraphemeSamples.IndicatorS; // A contraction, a decimal, a flag, Hebrew with a geresh, a CR LF, an emoji joined to a joiner and spaces. public static readonly string Mixed = "can't 3.14 " + IndicatorU + IndicatorS + " " + Alef + "'" + Bet + "\r\n" + "a‍" + GraphemeSamples.Woman + " e" + Acute + "!"; /// /// Returns the cases `found` disagrees with, given each case's expected boundaries, up to the first ten. /// public static string Disagreements(Func found) { var mismatches = new List(); foreach (BoundaryTestFile.Case testCase in Cases) { int[] expected = GraphemesTests.ExpectedBoundaries(testCase); int[] actual = found(testCase.Text); if (!expected.SequenceEqual(actual) && mismatches.Count < 10) { mismatches.Add($"[{LineBreakSamples.CodePointsOf(testCase.Text)}] expected " + $"{string.Join(",", expected)}, was {string.Join(",", actual)}"); } } return string.Join("; ", mismatches); } public static string[] SegmentsOf(string text) { var returnMe = new List(); int start = 0; while (start < text.Length) { int end = Words.NextBoundary(text, start); returnMe.Add(text.Substring(start, end - start)); start = end; } return returnMe.ToArray(); } public class IsBoundaryTests : TestFixture { public void AgreesWithWordBreakTest_AtEveryBoundary() { CheckEqual("", Disagreements(text => Enumerable.Range(0, text.Length + 1) .Where(position => Words.IsBoundary(text, position)).ToArray())); } public void OnAView_MatchesTheStringAtEveryPosition() { StringView view = ViewSamples.ViewOf(Mixed); for (int i = 0; i <= Mixed.Length; ++i) { CheckEqual(Words.IsBoundary(Mixed, i), Words.IsBoundary(view, i)); } } public void StartAndEndOfText_AreBoundaries() { Check(Words.IsBoundary("ab", 0)); Check(Words.IsBoundary("ab", 2)); } public void InsideASurrogatePair_IsNot() { CheckFalse(Words.IsBoundary(GraphemeSamples.Woman, 1)); } public void InsideACrLf_IsNot() { CheckFalse(Words.IsBoundary("\r\n", 1)); } public void AroundALineFeed_AreBoundaries() { Check(Words.IsBoundary("a\nb", 1)); Check(Words.IsBoundary("a\nb", 2)); } public void BeforeAMarkAfterALineFeed_IsOne() { Check(Words.IsBoundary("\n" + Acute, 1)); } public void BetweenAJoinerAndAnEmoji_IsNot() { CheckFalse(Words.IsBoundary("‍" + GraphemeSamples.Woman, 1)); } public void BetweenTwoSpaces_IsNot() { CheckFalse(Words.IsBoundary("a b", 2)); } public void BeforeAMarkOrFormatControl_IsNot() { CheckFalse(Words.IsBoundary("e" + Acute, 1)); CheckFalse(Words.IsBoundary("a" + SoftHyphen + "b", 1)); } public void BetweenLettersSeparatedByAFormatControl_IsNot() { CheckFalse(Words.IsBoundary("a" + SoftHyphen + "b", 2)); } public void AfterAMarkAtTheStartOfTheText_IsOne() { Check(Words.IsBoundary(Acute + "a", 1)); } public void BetweenTwoLetters_IsNot() { CheckFalse(Words.IsBoundary("ab", 1)); } public void AroundAnApostropheBetweenLetters_IsNot() { CheckFalse(Words.IsBoundary("can't", 3)); CheckFalse(Words.IsBoundary("can't", 4)); } public void BeforeAnApostropheThatEndsAWord_IsOne() { Check(Words.IsBoundary("dogs' x", 4)); } public void AroundAColonBetweenLetters_IsNot() { CheckFalse(Words.IsBoundary("c:a", 1)); CheckFalse(Words.IsBoundary("c:a", 2)); } public void AfterAColonThatStartsTheText_IsOne() { Check(Words.IsBoundary(":a", 1)); } public void AfterAHebrewLetterBeforeAnApostrophe_IsNot() { CheckFalse(Words.IsBoundary(Alef + "'", 1)); } public void AroundAQuotationMarkBetweenHebrewLetters_IsNot() { string text = Alef + "\"" + Bet; CheckFalse(Words.IsBoundary(text, 1)); CheckFalse(Words.IsBoundary(text, 2)); } public void BeforeAQuotationMarkAfterALatinLetter_IsOne() { Check(Words.IsBoundary("a\"b", 1)); } public void BetweenDigits_IsNot() { CheckFalse(Words.IsBoundary("12", 1)); } public void BetweenALetterAndADigit_IsNot() { CheckFalse(Words.IsBoundary("a1", 1)); CheckFalse(Words.IsBoundary("1a", 1)); } public void AroundAPointOrCommaInANumber_IsNot() { CheckFalse(Words.IsBoundary("3.14", 1)); CheckFalse(Words.IsBoundary("3.14", 2)); CheckFalse(Words.IsBoundary("1,000", 1)); CheckFalse(Words.IsBoundary("1,000", 2)); } public void AfterAPointThatEndsANumber_IsOne() { Check(Words.IsBoundary("3. x", 1)); } public void BetweenKatakana_IsNot() { CheckFalse(Words.IsBoundary(Katakana, 1)); } public void AroundAnUnderscore_IsNot() { CheckFalse(Words.IsBoundary("a_b", 1)); CheckFalse(Words.IsBoundary("a_b", 2)); CheckFalse(Words.IsBoundary("__", 1)); } public void BetweenAnIdeographAndALetter_IsOne() { Check(Words.IsBoundary("中a", 1)); } public void BetweenPunctuationMarks_IsOne() { Check(Words.IsBoundary("!?", 1)); } public void RegionalIndicators_PairFromTheStartOfTheirRun() { string flagAndOne = IndicatorU + IndicatorS + GraphemeSamples.IndicatorF; CheckFalse(Words.IsBoundary(flagAndOne, 2)); Check(Words.IsBoundary(flagAndOne, 4)); } public void RegionalIndicatorsWithAMarkBetween_StillPair() { CheckFalse(Words.IsBoundary(IndicatorU + Acute + IndicatorS, 3)); } public void NullString_Throws() { CheckThrow(typeof(ArgumentNullException)); Words.IsBoundary(null, 0); } public void NegativePosition_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Words.IsBoundary("ab", -1); } public void PositionPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Words.IsBoundary("ab", 3); } } public class NextBoundaryTests : TestFixture { public void WalkingForward_StopsAtEveryBoundaryOfWordBreakTest() { CheckEqual("", Disagreements(text => { var returnMe = new List { 0 }; while (returnMe[returnMe.Count - 1] < text.Length) { returnMe.Add(Words.NextBoundary(text, returnMe[returnMe.Count - 1])); } return returnMe.ToArray(); })); } public void OnAView_MatchesTheStringAtEveryPosition() { StringView view = ViewSamples.ViewOf(Mixed); for (int i = 0; i <= Mixed.Length; ++i) { CheckEqual(Words.NextBoundary(Mixed, i), Words.NextBoundary(view, i)); } } public void SplitsASentence_IntoWordsSpacesAndPunctuation() { CheckEqual(new[] { "Don't", " ", "panic", ",", " ", "3.5", " ", "dogs", "!" }, SegmentsOf("Don't panic, 3.5 dogs!")); } public void AtTheEnd_StaysThere() { CheckEqual(2, Words.NextBoundary("ab", 2)); } public void NullString_Throws() { CheckThrow(typeof(ArgumentNullException)); Words.NextBoundary(null, 0); } public void PositionPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Words.NextBoundary("ab", 3); } } public class PreviousBoundaryTests : TestFixture { public void WalkingBack_StopsAtEveryBoundaryOfWordBreakTest() { CheckEqual("", Disagreements(text => { var returnMe = new List { text.Length }; while (returnMe[0] > 0) { returnMe.Insert(0, Words.PreviousBoundary(text, returnMe[0])); } return returnMe.ToArray(); })); } public void OnAView_MatchesTheStringAtEveryPosition() { StringView view = ViewSamples.ViewOf(Mixed); for (int i = 0; i <= Mixed.Length; ++i) { CheckEqual(Words.PreviousBoundary(Mixed, i), Words.PreviousBoundary(view, i)); } } public void FromInsideAWord_GoesToItsStart() { CheckEqual(4, Words.PreviousBoundary("one can't", 7)); } public void AtTheStart_StaysThere() { CheckEqual(0, Words.PreviousBoundary("ab", 0)); } public void NullString_Throws() { CheckThrow(typeof(ArgumentNullException)); Words.PreviousBoundary(null, 0); } public void NegativePosition_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Words.PreviousBoundary("ab", -1); } } public class IsWordLikeTests : TestFixture { public void Letters_Are() { Check(Words.IsWordLike(new StringView("can't"))); } public void Digits_Are() { Check(Words.IsWordLike(new StringView("42"))); } public void AnIdeograph_Is() { Check(Words.IsWordLike(new StringView("中"))); } public void ALetterOutsideTheBasicPlane_Is() { Check(Words.IsWordLike(new StringView(char.ConvertFromUtf32(0x10400)))); } public void SpacesPunctuationAndEmoji_AreNot() { CheckFalse(Words.IsWordLike(new StringView(" "))); CheckFalse(Words.IsWordLike(new StringView("!"))); CheckFalse(Words.IsWordLike(new StringView("_"))); CheckFalse(Words.IsWordLike(new StringView(GraphemeSamples.Woman))); } public void Nothing_IsNot() { CheckFalse(Words.IsWordLike(new StringView(""))); } } } }