using System;
using System.Collections.Generic;
using System.Linq;
using Amphora;
using Tessera.Editing;
using Tessera.Unicode;
using Tessera.UnitTests.Editing;
using UnitTestSharp;
using static Tessera.UnitTests.Unicode.GraphemeSamples;
namespace Tessera.UnitTests.Unicode
{
public class GraphemesTests : TestFixture
{
public static readonly BoundaryTestFile.Case[] Cases = BoundaryTestFile.ReadCases("GraphemeBreakTest.txt");
///
/// The boundaries a case's file line marks, including its start and end.
///
public static int[] ExpectedBoundaries(BoundaryTestFile.Case testCase)
{
var returnMe = new List { 0 };
returnMe.AddRange(testCase.Boundaries.Where(each => each.Breaks).Select(each => each.Index));
returnMe.Add(testCase.Text.Length);
return returnMe.ToArray();
}
///
/// 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 = 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 class IsBoundaryTests : TestFixture
{
public void AgreesWithGraphemeBreakTest_AtEveryBoundary()
{
CheckEqual("", Disagreements(text => Enumerable.Range(0, text.Length + 1)
.Where(position => Graphemes.IsBoundary(text, position)).ToArray()));
}
public void OnATextSource_AgreesWithGraphemeBreakTest()
{
CheckEqual("", Disagreements(text => Enumerable.Range(0, text.Length + 1)
.Where(position => Graphemes.IsBoundary(new StringSource(text), position)).ToArray()));
}
public void OnAView_MatchesTheStringAtEveryPosition()
{
StringView view = ViewSamples.ViewOf(Mixed);
for (int i = 0; i <= Mixed.Length; ++i)
{
CheckEqual(Graphemes.IsBoundary(Mixed, i), Graphemes.IsBoundary(view, i));
}
}
public void StartAndEndOfText_AreBoundaries()
{
Check(Graphemes.IsBoundary("ab", 0));
Check(Graphemes.IsBoundary("ab", 2));
}
public void EmptyText_HasABoundary()
{
Check(Graphemes.IsBoundary("", 0));
}
public void BetweenTwoLetters_IsOne()
{
Check(Graphemes.IsBoundary("ab", 1));
}
public void InsideASurrogatePair_IsNot()
{
CheckFalse(Graphemes.IsBoundary(Woman, 1));
}
public void InsideACrLf_IsNot()
{
CheckFalse(Graphemes.IsBoundary("\r\n", 1));
}
public void BeforeACarriageReturn_IsOne()
{
Check(Graphemes.IsBoundary("a\r", 1));
}
public void BeforeAMarkThatFollowsALineFeed_IsOne()
{
Check(Graphemes.IsBoundary("\n" + Acute, 1));
}
public void BeforeACombiningMark_IsNot()
{
CheckFalse(Graphemes.IsBoundary("e" + Acute, 1));
}
public void BeforeAMarkOnASpace_IsNot()
{
CheckFalse(Graphemes.IsBoundary(" " + Acute, 1));
}
public void BeforeAMarkThatNeedsAPair_IsNot()
{
CheckFalse(Graphemes.IsBoundary("a" + char.ConvertFromUtf32(0x1D165), 1));
}
public void AroundAnUnpairedSurrogate_AreBoundaries()
{
Check(Graphemes.IsBoundary("a\uD842b", 1));
Check(Graphemes.IsBoundary("a\uD842b", 2));
}
public void InsideHangulJamoThatSpellOneSyllable_IsNot()
{
CheckFalse(Graphemes.IsBoundary(Jamo, 1));
CheckFalse(Graphemes.IsBoundary(Jamo, 2));
}
public void BeforeATrailingJamoAfterAClosedSyllable_IsNot()
{
CheckFalse(Graphemes.IsBoundary("각ᆨ", 1));
}
public void BetweenTwoHangulSyllables_IsOne()
{
Check(Graphemes.IsBoundary("가가", 1));
}
public void BeforeASpacingMarkThatIsALetter_IsNot()
{
CheckFalse(Graphemes.IsBoundary("กำ", 1));
}
public void AfterAPrependedSign_IsNot()
{
CheckFalse(Graphemes.IsBoundary("1", 1));
}
public void InsideAConjunct_IsNot()
{
CheckFalse(Graphemes.IsBoundary(Conjunct, 1));
CheckFalse(Graphemes.IsBoundary(Conjunct, 2));
}
public void BeforeAConsonantAfterAPlainMark_IsOne()
{
Check(Graphemes.IsBoundary("क́ष", 2));
}
public void InsideAnEmojiJoinedByAZeroWidthJoiner_IsNot()
{
string family = Woman + "" + Girl;
CheckFalse(Graphemes.IsBoundary(family, 2));
CheckFalse(Graphemes.IsBoundary(family, 3));
}
public void BeforeAnEmojiJoinedAfterASkinTone_IsNot()
{
CheckFalse(Graphemes.IsBoundary(ThumbsUp + MediumSkinTone + "" + ThumbsUp, 5));
}
public void AfterAJoinerOnALetter_IsOne()
{
Check(Graphemes.IsBoundary("ab", 2));
}
public void AfterAJoinerAtTheStart_IsOne()
{
Check(Graphemes.IsBoundary("" + Woman, 1));
}
public void RegionalIndicators_PairFromTheStartOfTheirRun()
{
string flagAndOne = IndicatorU + IndicatorS + IndicatorF;
CheckFalse(Graphemes.IsBoundary(flagAndOne, 2));
Check(Graphemes.IsBoundary(flagAndOne, 4));
}
public void RegionalIndicatorsAfterALetter_PairFromTheirOwnStart()
{
CheckFalse(Graphemes.IsBoundary("a" + IndicatorU + IndicatorS, 3));
}
public void NullString_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.IsBoundary((string)null, 0);
}
public void NullSource_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.IsBoundary((ITextSource)null, 0);
}
public void NegativePosition_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.IsBoundary("ab", -1);
}
public void PositionPastTheEnd_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.IsBoundary("ab", 3);
}
}
public class IsRegionalIndicatorTests : TestFixture
{
public void TheFirstAndLastIndicators_Are()
{
Check(Graphemes.IsRegionalIndicator('\uD83C', '\uDDE6'));
Check(Graphemes.IsRegionalIndicator('\uD83C', '\uDDFF'));
}
public void TheNeighboursOfTheRange_AreNot()
{
CheckFalse(Graphemes.IsRegionalIndicator('\uD83C', '\uDDE5'));
CheckFalse(Graphemes.IsRegionalIndicator('\uD83C', '\uDE00'));
CheckFalse(Graphemes.IsRegionalIndicator('\uD83D', '\uDDE6'));
}
}
public class IndicatorsBeforeTests : TestFixture
{
public void CountsTheWholeRun()
{
var text = new StringView("a" + IndicatorU + IndicatorS + IndicatorF);
CheckEqual(3, Graphemes.IndicatorsBefore(text, 7));
}
public void AfterSomethingElse_IsZero()
{
CheckEqual(0, Graphemes.IndicatorsBefore(new StringView(IndicatorU + "a"), 3));
}
public void FromInsideAnIndicator_IsZero()
{
CheckEqual(0, Graphemes.IndicatorsBefore(new StringView(IndicatorU + IndicatorS), 3));
}
public void NegativePosition_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.IndicatorsBefore(new StringView("ab"), -1);
}
public void PositionPastTheEnd_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.IndicatorsBefore(new StringView("ab"), 3);
}
}
public class NextEndTests : TestFixture
{
[IgnoreTest]
public int[] WalkForward(string text)
{
var returnMe = new List { 0 };
while (returnMe[returnMe.Count - 1] < text.Length)
{
returnMe.Add(Graphemes.NextEnd(text, returnMe[returnMe.Count - 1]));
}
return returnMe.ToArray();
}
public void WalkingForward_StopsAtEveryBoundaryOfGraphemeBreakTest()
{
CheckEqual("", Disagreements(WalkForward));
}
public void OnAView_MatchesTheStringAtEveryPosition()
{
StringView view = ViewSamples.ViewOf(Mixed);
for (int i = 0; i <= Mixed.Length; ++i)
{
CheckEqual(Graphemes.NextEnd(Mixed, i), Graphemes.NextEnd(view, i));
}
}
public void FromALetter_SkipsItsMarks()
{
CheckEqual(3, Graphemes.NextEnd("e" + Acute + Acute + "x", 0));
}
public void FromMarksAtTheStartOfTheText_TakesThemAll()
{
CheckEqual(2, Graphemes.NextEnd(Acute + Acute + "x", 0));
}
public void FromInsideACluster_EndsThatCluster()
{
CheckEqual(3, Graphemes.NextEnd(Conjunct + "x", 1));
}
public void FromInsideASurrogatePair_EndsThePair()
{
CheckEqual(3, Graphemes.NextEnd("a" + Woman + "b", 2));
}
public void AtTheEnd_StaysThere()
{
CheckEqual(2, Graphemes.NextEnd("ab", 2));
}
public void OnATextSource_EndsTheClusterLikeTheString()
{
CheckEqual(3, Graphemes.NextEnd(new StringSource(Conjunct + "x"), 1));
}
public void NullString_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.NextEnd((string)null, 0);
}
public void NullSource_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.NextEnd((ITextSource)null, 0);
}
public void PositionPastTheEnd_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.NextEnd("ab", 3);
}
}
public class PreviousStartTests : TestFixture
{
[IgnoreTest]
public int[] WalkBack(string text)
{
var returnMe = new List { text.Length };
while (returnMe[0] > 0)
{
returnMe.Insert(0, Graphemes.PreviousStart(text, returnMe[0]));
}
return returnMe.ToArray();
}
public void WalkingBack_StopsAtEveryBoundaryOfGraphemeBreakTest()
{
CheckEqual("", Disagreements(WalkBack));
}
public void OnAView_MatchesTheStringAtEveryPosition()
{
StringView view = ViewSamples.ViewOf(Mixed);
for (int i = 0; i <= Mixed.Length; ++i)
{
CheckEqual(Graphemes.PreviousStart(Mixed, i), Graphemes.PreviousStart(view, i));
}
}
public void FromAfterAFlag_GoesBackOverBothIndicators()
{
CheckEqual(1, Graphemes.PreviousStart("x" + IndicatorU + IndicatorS, 5));
}
public void FromInsideACluster_StartsThatCluster()
{
CheckEqual(1, Graphemes.PreviousStart("x" + Conjunct, 3));
}
public void FromInsideASurrogatePair_StartsThePair()
{
CheckEqual(1, Graphemes.PreviousStart("a" + Woman + "b", 2));
}
public void AtTheStart_StaysThere()
{
CheckEqual(0, Graphemes.PreviousStart("ab", 0));
}
public void OnATextSource_StartsTheClusterLikeTheString()
{
CheckEqual(1, Graphemes.PreviousStart(new StringSource("x" + IndicatorU + IndicatorS), 5));
}
public void NullString_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.PreviousStart((string)null, 0);
}
public void NullSource_Throws()
{
CheckThrow(typeof(ArgumentNullException));
Graphemes.PreviousStart((ITextSource)null, 0);
}
public void NegativePosition_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
Graphemes.PreviousStart("ab", -1);
}
}
}
}