using System; using System.Globalization; using System.Threading; using Amphora; using Tessera.Text; using UnitTestSharp; using Tessera.UnitTests.Text; using static Tessera.Text.Grammar; using static Tessera.Text.Plurals; using static Tessera.Text.Styles; namespace Tessera.UnitTests.Text { public class GrammarTests : TestFixture { public static string RenderIn(GrammarRules grammar, in TextPieces line) { char[] scratch = null; return line.ViewIn(NumberFormat.Invariant, grammar, ref scratch).ToString(); } public class PronounTests : TestFixture { public void IsANumberOfThePronounKind_HoldingTheReferentAndTheRole() { StyledText hole = Pronoun(Referent.Female, GrammarRole.Reflexive); CheckTrue(hole.IsNumber); CheckEqual(NumberKind.Pronoun, hole.Kind); CheckEqual((long)Referent.Female, hole.Number); CheckEqual((int)GrammarRole.Reflexive, hole.Precision); } public void ANonExistentReferent_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pronoun((Referent)GrammarRules.ReferentCount, GrammarRole.Subject); } public void ANonExistentRole_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pronoun(Referent.Male, (GrammarRole)GrammarRules.RoleCount); } public void ANegativeReferent_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pronoun((Referent)(-1), GrammarRole.Subject); } public void ANegativeRole_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pronoun(Referent.Male, (GrammarRole)(-1)); } } public class NamedHoleTests : TestFixture { public void He_IsTheSubject() { CheckEqual((int)GrammarRole.Subject, He(Referent.Male).Precision); } public void Him_IsTheObject() { CheckEqual((int)GrammarRole.Object, Him(Referent.Male).Precision); } public void His_IsThePossessive() { CheckEqual((int)GrammarRole.Possessive, His(Referent.Male).Precision); } public void Hers_IsThePossessivePronoun() { CheckEqual((int)GrammarRole.PossessivePronoun, Hers(Referent.Male).Precision); } public void Himself_IsTheReflexive() { CheckEqual((int)GrammarRole.Reflexive, Himself(Referent.Male).Precision); } public void EachKeepsWhoItWasGiven() { CheckEqual((long)Referent.They, He(Referent.They).Number); CheckEqual((long)Referent.Neuter, Him(Referent.Neuter).Number); CheckEqual((long)Referent.Female, His(Referent.Female).Number); CheckEqual((long)Referent.Male, Hers(Referent.Male).Number); CheckEqual((long)Referent.They, Himself(Referent.They).Number); } } public class AppendFormattedPronounTests : TestFixture { public void ASentenceWithTwoPeople_ChoosesEachWordByItsOwnReferent() { Referent speaker = Referent.Female; Referent listener = Referent.Male; CheckEqual("What did she say to him?", RenderIn(GrammarRules.English, $"What did {He(speaker)} say to {Him(listener)}?")); } public void ASentenceWithAPossessiveAndAReflexive_WritesBoth() { Referent who = Referent.They; CheckEqual("Then they drew their sword and hit themself.", RenderIn(GrammarRules.English, $"Then {He(who)} drew {His(who)} sword and hit {Himself(who)}.")); } public void APossessivePronoun_StandsAlone() { CheckEqual("The sword is hers.", RenderIn(GrammarRules.English, $"The sword is {Hers(Referent.Female)}.")); } public void TheSameLine_ChangesWithTheRules() { TextPieces line = $"{He(Referent.Male)} saw {Him(Referent.Female)}"; CheckEqual("he saw her", RenderIn(GrammarRules.English, line)); CheckEqual("er saw sie", RenderIn(GrammarRules.German, line)); } public void APronounBetweenNumbers_IsNotTakenForANumber() { CheckEqual("5 she 7", RenderIn(GrammarRules.English, $"{Plain(5)} {He(Referent.Female)} {Plain(7)}")); } public void ANumberAfterEveryPronoun_IsStillANumber() { CheckEqual("he him 12", RenderIn(GrammarRules.English, $"{He(Referent.Male)} {Him(Referent.Male)} {Plain(12)}")); } public void APronounAmongOtherKindsOfNumber_IsStillAPronoun() { CheckEqual("1,234 it 25% hers", RenderIn(GrammarRules.English, $"{Count(1234)} {He(Referent.Neuter)} {Percent(0.25)} {Hers(Referent.Female)}")); } public void ManyPronouns_EachWritesItsOwnWord() { Referent man = Referent.Male; Referent woman = Referent.Female; CheckEqual("he she he she he she he she", RenderIn(GrammarRules.English, $"{He(man)} {He(woman)} {He(man)} {He(woman)} {He(man)} {He(woman)} {He(man)} {He(woman)}")); } public void ALineWithNoRules_ThrowsWhenItHoldsAPronoun() { char[] scratch = null; TextPieces line = $"{He(Referent.Male)} left"; CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, ref scratch); } public void ALineWithNoRules_WritesWhenItHoldsNone() { char[] scratch = null; TextPieces line = $"{Count(1234)} boxes"; CheckEqual("1,234 boxes", line.ViewIn(NumberFormat.Invariant, GrammarRules.None, ref scratch).ToString()); } public void APronounTheLanguageCantWrite_Throws() { CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.German, $"{His(Referent.Male)} Schwert"); } public void ABufferTooSmall_GrowsToHoldTheLongestPronoun() { char[] scratch = new char[1]; TextPieces line = $"{Himself(Referent.They)}"; CheckEqual("themself", line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch).ToString()); CheckTrue(scratch.Length >= 8); } public void ANullGrammar_Throws() { char[] scratch = null; TextPieces line = $"{He(Referent.Male)}"; CheckThrow(typeof(ArgumentNullException)); line.ViewIn(NumberFormat.Invariant, null, ref scratch); } public void AStyledPronoun_KeepsItsLookOverItsWords() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"Then {Bold(He(Referent.Female))} left"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("Then she left", view.ToString()); CheckEqual(1, spans.Count); CheckEqual(5, spans[0].Start); CheckEqual(8, spans[0].End); } public void ToString_WritesEnglish() { TextPieces line = $"{He(Referent.Female)} said so"; CheckEqual("she said so", line.ToString()); } } public class CapitalTests : TestFixture { public void AString_StartsWithACapital() { CheckEqual("Sword", RenderIn(GrammarRules.English, $"{Capital("sword")}")); } public void OnlyTheFirstCharacter_Changes() { CheckEqual("SWORD and Axe", RenderIn(GrammarRules.English, $"{Capital("sWORD and Axe")}")); } public void AnEmptyString_WritesNothing() { CheckEqual("[]", RenderIn(GrammarRules.English, $"[{Capital("")}]")); } public void ANonLetter_IsLeftAlone() { CheckEqual("1st", RenderIn(GrammarRules.English, $"{Capital("1st")}")); } public void ANonAsciiLetter_BecomesItsCapital() { CheckEqual("École", RenderIn(GrammarRules.English, $"{Capital("école")}")); } public void ALetterWithNoCapital_IsLeftAlone() { CheckEqual("ßa", RenderIn(GrammarRules.English, $"{Capital("ßa")}")); } public void ALetterOutsideTheBasicPlane_BecomesItsCapital() { string small = char.ConvertFromUtf32(0x10428); string capital = char.ConvertFromUtf32(0x10400); CheckEqual(capital + "x", RenderIn(GrammarRules.English, $"{Capital(small + "x")}")); } public void ALoneSurrogate_IsLeftAlone() { string lone = "\uD801"; CheckEqual(lone, RenderIn(GrammarRules.English, $"{Capital(lone)}")); } public void ThePronounsOfAnyReferent_BecomeCapitals() { CheckEqual("She left", RenderIn(GrammarRules.English, $"{Capital(He(Referent.Female))} left")); CheckEqual("Themself", RenderIn(GrammarRules.English, $"{Capital(Himself(Referent.They))}")); } public void APronounInAnotherLanguage_BecomesACapital() { CheckEqual("Er schlief", RenderIn(GrammarRules.German, $"{Capital(He(Referent.Male))} schlief")); } public void ANumber_IsUnchanged() { CheckEqual("1,234 -5", RenderIn(GrammarRules.English, $"{Capital(Count(1234))} {Capital(Count(-5))}")); } public void OnlyTheHoleItWraps_Changes() { CheckEqual("a B c", RenderIn(GrammarRules.English, $"{"a"} {Capital("b")} {"c"}")); } public void SeveralHolesAmongLiterals_EachKeepTheirOwnFlag() { CheckEqual("x Y and Z but w", RenderIn(GrammarRules.English, $"{"x"} {Capital("y")} and {Capital("z")} but {"w"}")); } public void ALiteralBesideAHole_IsNeverChanged() { CheckEqual("the Sword", RenderIn(GrammarRules.English, $"the {Capital("sword")}")); } public void ALaterSegmentPastThirtyTwo_StillCapitalizes() { var line = new TextPieces(0, 0); for (int index = 0; index < 20; ++index) { line.AppendFormatted("a"); } for (int index = 0; index < 14; ++index) { line.AppendFormatted(Plain(1)); } line.AppendFormatted(Capital("z")); CheckEqual(35, line.SegmentCount); CheckEqual("aaaaaaaaaaaaaaaaaaaa11111111111111Z", RenderIn(GrammarRules.English, line)); } public void ItIsTheSameInEveryCulture() { CultureInfo previous = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); CheckEqual("Ilk", RenderIn(GrammarRules.English, $"{Capital("ilk")}")); } finally { Thread.CurrentThread.CurrentCulture = previous; } } public void ALookLaidAfter_KeepsTheCapital() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"Then {Bold(Capital("she"))} left"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("Then She left", view.ToString()); CheckEqual(1, spans.Count); CheckEqual(5, spans[0].Start); CheckEqual(8, spans[0].End); } public void ALookLaidBefore_KeepsTheLook() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"Then {Capital(Bold("she"))} left"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("Then She left", view.ToString()); CheckEqual(1, spans.Count); } public void ACapitalAroundAPronounWithNoRules_StillThrows() { char[] scratch = null; TextPieces line = $"{Capital(He(Referent.Male))}"; CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, ref scratch); } public void TheAmountOfAPluralLine_IsUnchanged() { char[] scratch = null; PluralLine line = Plural(Count(1), one: $"{Capital(Amount)} box", other: $"{Capital(Amount)} boxes"); CheckEqual("1 box", line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch).ToString()); } public void AVariantsText_BecomesACapital() { char[] scratch = null; PluralLine line = Plural(Count(2), one: $"{Capital("box")}", other: $"{Capital("boxes")}"); CheckEqual("Boxes", line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch).ToString()); } public void TheFlag_IsSetOnTheCopyAndNotTheOriginal() { StyledText plain = "sword"; StyledText capital = Capital(plain); CheckFalse(plain.Capitalized); CheckTrue(capital.Capitalized); CheckEqual("sword", capital.Text); } public void TheFlag_SurvivesALook() { CheckTrue(Bold(Capital(He(Referent.Male))).Capitalized); } public void ABufferTooSmall_GrowsAsItDoesWithoutACapital() { char[] scratch = new char[1]; TextPieces line = $"{Capital("abc")}"; CheckEqual("Abc", line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch).ToString()); } } public class TheAndABareTests : TestFixture { public void The_HoldsTheFormAndTheTraits() { var noun = new Noun("Kinder", gender: 2, isPlural: true, forms: new[] { null, null, "Kindern", null }); StyledText hole = The(noun, GrammarRole.Dative); CheckTrue(hole.IsNumber); CheckEqual(NumberKind.Article, hole.Kind); CheckEqual("Kindern", hole.Text); CheckEqual(NounHole.Pack(noun, ArticleKind.Definite, GrammarRole.Dative), hole.Number); } public void A_AsksForTheIndefiniteArticle() { var noun = new Noun("sword"); CheckEqual(NounHole.Pack(noun, ArticleKind.Indefinite, GrammarRole.Subject), A(noun).Number); } public void Bare_AsksForNoArticle() { var noun = new Noun("sword"); CheckEqual(NounHole.Pack(noun, ArticleKind.None, GrammarRole.Subject), Bare(noun).Number); } public void TheRole_IsTheSubjectUnlessGiven() { var noun = new Noun("sword", forms: new[] { "S", "O", "D", "G" }); CheckEqual("S", The(noun).Text); CheckEqual("O", A(noun, GrammarRole.Object).Text); CheckEqual("G", Bare(noun, GrammarRole.Genitive).Text); } public void ARoleANounDoesntTake_Throws() { var noun = new Noun("sword"); CheckThrow(typeof(ArgumentOutOfRangeException)); The(noun, GrammarRole.Reflexive); } public void ADefaultNoun_Throws() { CheckThrow(typeof(InvalidOperationException)); The(default); } } public class AppendFormattedNounTests : TestFixture { public static readonly Noun Sword = new Noun("sword"); public static readonly Noun Apple = new Noun("apple"); public static readonly Noun Swords = new Noun("swords", isPlural: true); public static readonly Noun Ada = new Noun("ada", isName: true); public void TheAndAIn_English() { CheckEqual("You found a sword and an apple, then the sword fell.", RenderIn(GrammarRules.English, $"You found {A(Sword)} and {A(Apple)}, then {The(Sword)} fell.")); } public void APluralTakesNoIndefiniteArticle() { CheckEqual("You found swords.", RenderIn(GrammarRules.English, $"You found {A(Swords)}.")); } public void ANameTakesNoArticle() { CheckEqual("ada waved.", RenderIn(GrammarRules.English, $"{The(Ada)} waved.")); } public void ABareNoun_IsItsFormAlone() { CheckEqual("sword", RenderIn(GrammarRules.English, $"{Bare(Sword)}")); } public void German_ChoosesByGenderAndRole() { var mann = new Noun("Mann", gender: GermanArticles.Masculine); var frau = new Noun("Frau", gender: GermanArticles.Feminine); CheckEqual("der Mann sah eine Frau", RenderIn(GrammarRules.German, $"{The(mann)} sah {A(frau, GrammarRole.Object)}")); CheckEqual("er half dem Mann", RenderIn(GrammarRules.German, $"er half {The(mann, GrammarRole.Dative)}")); CheckEqual("das Gift der Frau", RenderIn(GrammarRules.German, $"das Gift {The(frau, GrammarRole.Genitive)}")); } public void German_UsesTheFormOfTheRole() { var kinder = new Noun("Kinder", gender: GermanArticles.Neuter, isPlural: true, forms: new[] { null, null, "Kindern", null }); CheckEqual("den Kindern", RenderIn(GrammarRules.German, $"{The(kinder, GrammarRole.Dative)}")); CheckEqual("die Kinder", RenderIn(GrammarRules.German, $"{The(kinder)}")); } public void ANounAmongNumbersAndPronouns_IsNotTakenForEither() { CheckEqual("3 the sword she 7", RenderIn(GrammarRules.English, $"{Plain(3)} {The(Sword)} {He(Referent.Female)} {Plain(7)}")); } public void ManyNouns_EachWritesItsOwnArticle() { CheckEqual("a sword an apple a sword an apple a sword an apple", RenderIn(GrammarRules.English, $"{A(Sword)} {A(Apple)} {A(Sword)} {A(Apple)} {A(Sword)} {A(Apple)}")); } public void ALookCoversTheArticleAndTheNoun() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"Then {Bold(The(Sword))} fell"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("Then the sword fell", view.ToString()); CheckEqual(2, spans.Count); CheckEqual(5, spans[0].Start); CheckEqual(9, spans[0].End); CheckEqual(9, spans[1].Start); CheckEqual(14, spans[1].End); } public void ANameWithALook_HasOneSpan() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"Then {Bold(The(Ada))} fell"; line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual(1, spans.Count); } public void ACapital_StartsTheArticle() { CheckEqual("The sword", RenderIn(GrammarRules.English, $"{Capital(The(Sword))}")); CheckEqual("An apple", RenderIn(GrammarRules.English, $"{Capital(A(Apple))}")); } public void ACapital_PassesToTheNounWhenThereIsNoArticle() { CheckEqual("Ada waved.", RenderIn(GrammarRules.English, $"{Capital(The(Ada))} waved.")); CheckEqual("Swords", RenderIn(GrammarRules.English, $"{Capital(A(Swords))}")); } public void ACapital_OnABareNoun_StartsTheNoun() { CheckEqual("Sword", RenderIn(GrammarRules.English, $"{Capital(Bare(Sword))}")); } public void ACapital_IsNotCarriedToTheNextHoleAfterAnEmptyHole() { CheckEqual("abc", RenderIn(GrammarRules.English, $"{Capital("")}{"abc"}")); } public void ACapital_IsNotCarriedPastTheNoun() { CheckEqual("Ada waved", RenderIn(GrammarRules.English, $"{Capital(The(Ada))}{" waved"}")); } public void ALineWithNoRules_ThrowsWhenItHoldsAnArticle() { char[] scratch = null; TextPieces line = $"{The(Sword)}"; CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, ref scratch); } public void RulesWithNoArticles_ThrowWhenALineHoldsOne() { var rules = new GrammarRules("xx", GrammarRulesSamples.Table()); CheckThrow(typeof(InvalidOperationException)); RenderIn(rules, $"{The(Sword)}"); } public void ANounOfAGenderTheLanguageLacks_Throws() { var feminine = new Noun("sword", gender: 1); CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.English, $"{The(feminine)}"); } public void ABufferTooSmall_GrowsToHoldTheLongestArticle() { char[] scratch = new char[1]; var mann = new Noun("Mann", gender: GermanArticles.Masculine); TextPieces line = $"{A(mann, GrammarRole.Dative)}"; CheckEqual("einem Mann", line.ViewIn(NumberFormat.Invariant, GrammarRules.German, ref scratch).ToString()); CheckTrue(scratch.Length >= 10); } public void SixteenNouns_FillEveryNumberSlot() { var line = new TextPieces(0, 0); for (int index = 0; index < 16; ++index) { line.AppendFormatted(The(Sword)); } string expected = ""; for (int index = 0; index < 16; ++index) { expected += "the sword"; } CheckEqual(32, line.SegmentCount); CheckEqual(expected, RenderIn(GrammarRules.English, line)); } public void ANounPastTheNumberSlots_Throws() { var line = new TextPieces(0, 0); for (int index = 0; index < 16; ++index) { line.AppendFormatted(The(Sword)); } CheckThrow(typeof(InvalidOperationException)); line.AppendFormatted(The(Sword)); } public void APluralLineVariant_WritesItsNounsWithTheGrammar() { char[] scratch = null; PluralLine line = Plural(Count(2), one: $"{Amount} {Bare(Sword)}", other: $"{Amount} {Bare(Swords)} for {The(Sword)}"); CheckEqual("2 swords for the sword", line.ViewIn(NumberFormat.Invariant, PluralRules.English, GrammarRules.English, ref scratch).ToString()); } public void ToString_WritesEnglish() { TextPieces line = $"{A(Apple)}"; CheckEqual("an apple", line.ToString()); } } public class AOrAnTests : TestFixture { public void IsAnArticleHoleWithNoText() { StyledText hole = AOrAn(); CheckTrue(hole.IsNumber); CheckEqual(NumberKind.Article, hole.Kind); CheckEqual("", hole.Text); CheckTrue(NounHole.IsPeek(hole.Number)); CheckEqual(InitialSound.Auto, NounHole.SoundOf(hole.Number)); } public void TheSoundGiven_IsKept() { CheckEqual(InitialSound.Vowel, NounHole.SoundOf(AOrAn(InitialSound.Vowel).Number)); CheckEqual(InitialSound.Consonant, NounHole.SoundOf(AOrAn(InitialSound.Consonant).Number)); } public void ASoundThatIsntOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); AOrAn((InitialSound)7); } } public class AppendFormattedAOrAnTests : TestFixture { public static readonly string Old = "old"; public static readonly string Rusty = "rusty"; public static readonly string Sword = "sword"; public static readonly string Fbi = "FBI"; public static readonly string Empty = ""; public static readonly Noun Blade = new Noun("blade"); public void AVowelSound_WritesAn() { CheckEqual("an old sword", RenderIn(GrammarRules.English, $"{AOrAn()} {Old} {Sword}")); } public void AConsonantSound_WritesA() { CheckEqual("a rusty sword", RenderIn(GrammarRules.English, $"{AOrAn()} {Rusty} {Sword}")); } public void TheNextWordInLiteralText_IsRead() { CheckEqual("You found an orb", RenderIn(GrammarRules.English, $"You found {AOrAn()} orb")); CheckEqual("You found a very old sword", RenderIn(GrammarRules.English, $"You found {AOrAn()} very old {Sword}")); } public void AnEmptyStringAfterIt_IsSkipped() { CheckEqual("an old sword", RenderIn(GrammarRules.English, $"{AOrAn()}{Empty} {Old} {Sword}")); } public void AStyledString_IsRead() { CheckEqual("an old sword", RenderIn(GrammarRules.English, $"{AOrAn()} {Bold(Old)} {Sword}")); } public void TheSoundGiven_BeatsTheLetters() { CheckEqual("a FBI agent", RenderIn(GrammarRules.English, $"{AOrAn()} {Fbi} agent")); CheckEqual("an FBI agent", RenderIn(GrammarRules.English, $"{AOrAn(InitialSound.Vowel)} {Fbi} agent")); } public void ACapital_MakesTheArticleACapital() { CheckEqual("An old sword", RenderIn(GrammarRules.English, $"{Capital(AOrAn())} {Old} {Sword}")); } public void ManyArticles_EachReadsItsOwnWord() { CheckEqual("an old sword and a rusty sword", RenderIn(GrammarRules.English, $"{AOrAn()} {Old} {Sword} and {AOrAn()} {Rusty} {Sword}")); } public void ANounHoleBeforeIt_StillWritesItsOwnArticle() { CheckEqual("a blade an old sword", RenderIn(GrammarRules.English, $"{A(Blade)} {AOrAn()} {Old} {Sword}")); } public void ANumberAfterTheWord_IsFine() { CheckEqual("an old 3", RenderIn(GrammarRules.English, $"{AOrAn()} {Old} {Count(3)}")); } public void ALookOnTheArticle_CoversOnlyTheArticle() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"{Bold(AOrAn())} {Old} {Sword}"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("an old sword", view.ToString()); CheckEqual(1, spans.Count); CheckEqual(0, spans[0].Start); CheckEqual(2, spans[0].End); } public void AScratchThatIsTooSmall_IsReplaced() { char[] scratch = new char[1]; TextPieces line = $"{AOrAn()} {Old}"; CheckEqual("an old", line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch).ToString()); } public void WritingTheLine_AllocatesNothing() { char[] scratch = new char[64]; TextPieces warmUp = $"{AOrAn()} {Old} {Sword}"; warmUp.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch); long before = GC.GetAllocatedBytesForCurrentThread(); TextPieces line = $"{AOrAn()} {Old} {Sword}"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch); long allocated = GC.GetAllocatedBytesForCurrentThread() - before; CheckEqual(0L, allocated); CheckEqual("an old sword", view.ToString()); } public void ANumberRightAfterIt_Throws() { CheckThrow(typeof(InvalidOperationException)); TextPieces line = $"{AOrAn()} {Count(3)}"; } public void ANumberAfterOnlyAnEmptyString_Throws() { CheckThrow(typeof(InvalidOperationException)); TextPieces line = $"{AOrAn()} {Empty} {Count(3)}"; } public void APronounRightAfterIt_Throws() { CheckThrow(typeof(InvalidOperationException)); TextPieces line = $"{AOrAn()} {He(Referent.Male)}"; } public void ANounHoleRightAfterIt_Throws() { CheckThrow(typeof(InvalidOperationException)); TextPieces line = $"{AOrAn()} {A(Blade)}"; } public void AnotherArticleRightAfterIt_Throws() { CheckThrow(typeof(InvalidOperationException)); TextPieces line = $"{AOrAn()} {AOrAn()} {Old}"; } public void NothingAfterIt_ThrowsWhenWritten() { TextPieces line = $"{AOrAn()}"; CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.English, line); } public void OnlySpacesAfterIt_ThrowWhenWritten() { TextPieces line = $"{AOrAn()} "; CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.English, line); } public void OnlyAnEmptyStringAfterIt_ThrowsWhenWritten() { TextPieces line = $"{AOrAn()} {Empty}"; CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.English, line); } public void German_Throws() { TextPieces line = $"{AOrAn()} {Old}"; CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.German, line); } public void ALanguageWithNoRules_Throws() { TextPieces line = $"{AOrAn()} {Old}"; CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.None, line); } } public class PossessiveAgreementHoleTests : TestFixture { public static readonly Noun Axt = new Noun("Axt", gender: GermanArticles.Feminine); public static readonly Noun Schwert = new Noun("Schwert", gender: GermanArticles.Neuter); public static readonly Noun Mann = new Noun("Mann", gender: GermanArticles.Masculine); public static readonly Noun Kinder = new Noun("Kinder", gender: GermanArticles.Neuter, isPlural: true, forms: new[] { null, null, "Kindern", null }); public void His_HoldsTheReferentAndAgreesWithTheNoun() { StyledText hole = His(Referent.Female, Axt, GrammarRole.Dative); CheckEqual(NumberKind.Pronoun, hole.Kind); CheckEqual((int)GrammarRole.Possessive, hole.Precision); CheckEqual(PronounHole.Pack(Referent.Female, new Agreement(Axt, GrammarRole.Dative)), hole.Number); } public void Hers_AsksForThePossessivePronounAndAgreesWithTheNoun() { StyledText hole = Hers(Referent.Male, Schwert); CheckEqual((int)GrammarRole.PossessivePronoun, hole.Precision); CheckEqual(PronounHole.Pack(Referent.Male, new Agreement(Schwert, GrammarRole.Subject)), hole.Number); } public void ThePossessedRole_IsTheSubjectUnlessGiven() { CheckEqual(PronounHole.Pack(Referent.Male, new Agreement(Axt, GrammarRole.Subject)), His(Referent.Male, Axt).Number); } public void ARoleANounDoesntTake_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); His(Referent.Male, Axt, GrammarRole.Reflexive); } public void ThePronounHoleWithNoNoun_IsUnchanged() { CheckEqual((long)Referent.Female, His(Referent.Female).Number); } public void German_WritesSeinAndIhrByTheNoun() { CheckEqual("seine Axt", RenderIn(GrammarRules.German, $"{His(Referent.Male, Axt)} {Bare(Axt)}")); CheckEqual("sein Schwert", RenderIn(GrammarRules.German, $"{His(Referent.Male, Schwert)} {Bare(Schwert)}")); CheckEqual("ihr Schwert", RenderIn(GrammarRules.German, $"{His(Referent.Female, Schwert)} {Bare(Schwert)}")); } public void German_FollowsTheRoleOfTheNoun() { CheckEqual("er gab seinem Mann", RenderIn(GrammarRules.German, $"er gab {His(Referent.Male, Mann, GrammarRole.Dative)} {Bare(Mann)}")); CheckEqual("er sah seinen Mann", RenderIn(GrammarRules.German, $"er sah {His(Referent.Male, Mann, GrammarRole.Object)} {Bare(Mann)}")); CheckEqual("das Haus ihrer Axt", RenderIn(GrammarRules.German, $"das Haus {His(Referent.Female, Axt, GrammarRole.Genitive)} {Bare(Axt)}")); } public void German_FollowsTheNumberOfTheNoun() { CheckEqual("er gab seinen Kindern", RenderIn(GrammarRules.German, $"er gab {His(Referent.Male, Kinder, GrammarRole.Dative)} {Bare(Kinder, GrammarRole.Dative)}")); } public void German_WritesAPossessiveThatStandsAlone() { CheckEqual("Das Schwert ist seines", RenderIn(GrammarRules.German, $"Das Schwert ist {Hers(Referent.Male, Schwert)}")); CheckEqual("Die Axt ist ihre", RenderIn(GrammarRules.German, $"Die Axt ist {Hers(Referent.Female, Axt)}")); } public void German_ThrowsForAPossessiveWithNoNoun() { char[] scratch = null; TextPieces line = $"{His(Referent.Male)} Axt"; CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, GrammarRules.German, ref scratch); } public void German_ThrowsForTheyWithANoun() { CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.German, $"{His(Referent.They, Axt)} {Bare(Axt)}"); } public void German_ThrowsForANounOfAGenderItLacks() { var fourth = new Noun("x", gender: 3); CheckThrow(typeof(InvalidOperationException)); RenderIn(GrammarRules.German, $"{His(Referent.Male, fourth)} x"); } public void English_IgnoresTheNoun() { var sword = new Noun("sword"); CheckEqual("his sword", RenderIn(GrammarRules.English, $"{His(Referent.Male, sword)} {Bare(sword)}")); CheckEqual("the sword is hers", RenderIn(GrammarRules.English, $"the sword is {Hers(Referent.Female, sword)}")); } public void ACapital_StartsThePossessive() { CheckEqual("Seine Axt", RenderIn(GrammarRules.German, $"{Capital(His(Referent.Male, Axt))} {Bare(Axt)}")); } public void ABufferTooSmall_GrowsToHoldTheLongestPossessive() { char[] scratch = new char[1]; TextPieces line = $"{Hers(Referent.Male, Schwert, GrammarRole.Genitive)}"; CheckEqual("seines", line.ViewIn(NumberFormat.Invariant, GrammarRules.German, ref scratch).ToString()); CheckTrue(scratch.Length >= 6); } public void AmongNumbersNounsAndPronouns_EachKeepsItsOwnWords() { CheckEqual("3 seine die Axt er 7", RenderIn(GrammarRules.German, $"{Plain(3)} {His(Referent.Male, Axt)} {The(Axt)} {He(Referent.Male)} {Plain(7)}")); } public void ALookCoversThePossessive() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"{Bold(His(Referent.Male, Axt))} Axt"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.German, ref scratch, spans); CheckEqual("seine Axt", view.ToString()); CheckEqual(1, spans.Count); CheckEqual(5, spans[0].End); } } public class PossessiveHoleTests : TestFixture { public void AString_IsFlaggedAndKeepsItsText() { StyledText hole = Possessive("Ada"); CheckTrue(hole.Possessed); CheckEqual("Ada", hole.Text); } public void TheOriginal_IsNotChanged() { StyledText plain = "Ada"; Possessive(plain); CheckFalse(plain.Possessed); } public void ANoun_IsFlaggedAndKeepsItsArticle() { StyledText hole = Possessive(The(new Noun("owner"))); CheckTrue(hole.Possessed); CheckEqual(NumberKind.Article, hole.Kind); } public void ALook_KeepsThePossessive() { CheckTrue(Bold(Possessive("Ada")).Possessed); } public void ACapital_KeepsThePossessive() { CheckTrue(Capital(Possessive("Ada")).Possessed); } public void APossessive_KeepsTheCapital() { CheckTrue(Possessive(Capital("Ada")).Capitalized); } public void ANumber_Throws() { CheckThrow(typeof(ArgumentException)); Possessive(Count(5)); } public void APronoun_Throws() { CheckThrow(typeof(ArgumentException)); Possessive(He(Referent.Male)); } public void TheAmountOfAPluralLine_Throws() { CheckThrow(typeof(ArgumentException)); Possessive(Amount); } public void AnArticleBeforeTheNextWord_Throws() { CheckThrow(typeof(ArgumentException)); Possessive(AOrAn()); } } public class AppendFormattedPossessiveTests : TestFixture { public static readonly Noun Owner = new Noun("owner"); public static readonly Noun Owners = new Noun("owners", isPlural: true); public static readonly Noun Ada = new Noun("ada", isName: true); public void AName_TakesTheSuffix() { CheckEqual("Ada's sword and James' axe", RenderIn(GrammarRules.English, $"{Possessive("Ada")} sword and {Possessive("James")} axe")); } public void German_TakesItsOwnSuffix() { CheckEqual("Adas Schwert und Hans' Axt", RenderIn(GrammarRules.German, $"{Possessive("Ada")} Schwert und {Possessive("Hans")} Axt")); } public void ANounWithAnArticle_TakesTheSuffixAfterTheNoun() { CheckEqual("the owner's sword", RenderIn(GrammarRules.English, $"{Possessive(The(Owner))} sword")); CheckEqual("the owners' sword", RenderIn(GrammarRules.English, $"{Possessive(The(Owners))} sword")); } public void ANameNounWithNoArticle_TakesTheSuffix() { CheckEqual("ada's sword", RenderIn(GrammarRules.English, $"{Possessive(The(Ada))} sword")); } public void ABareNoun_TakesTheSuffix() { CheckEqual("owner's", RenderIn(GrammarRules.English, $"{Possessive(Bare(Owner))}")); } public void AnEmptyString_WritesNothing() { CheckEqual("[]", RenderIn(GrammarRules.English, $"[{Possessive("")}]")); } public void OnlyTheHoleItWraps_TakesTheSuffix() { CheckEqual("Ada Ada's Ada", RenderIn(GrammarRules.English, $"{"Ada"} {Possessive("Ada")} {"Ada"}")); } public void ACapital_StartsTheOwnerAndTheSuffixStaysLast() { CheckEqual("Hans'", RenderIn(GrammarRules.German, $"{Capital(Possessive("hans"))}")); CheckEqual("Ada's", RenderIn(GrammarRules.English, $"{Possessive(Capital("ada"))}")); } public void ACapital_OnANounWithAnArticle_StartsTheArticle() { CheckEqual("The owner's", RenderIn(GrammarRules.English, $"{Capital(Possessive(The(Owner)))}")); } public void ALookCoversTheSuffix() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"{Bold(Possessive("Ada"))} sword"; StringView view = line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual("Ada's sword", view.ToString()); CheckEqual(1, spans.Count); CheckEqual(0, spans[0].Start); CheckEqual(5, spans[0].End); } public void ALookLaidOnThePossessive_CoversTheSuffixToo() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"{Possessive(Bold("Ada"))} sword"; line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual(1, spans.Count); CheckEqual(5, spans[0].End); } public void ANounWithALook_CoversTheSuffixOnTheNoun() { char[] scratch = null; var spans = new DynamicArray(); TextPieces line = $"{Bold(Possessive(The(Owner)))} sword"; line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch, spans); CheckEqual(2, spans.Count); CheckEqual(0, spans[0].Start); CheckEqual(4, spans[0].End); CheckEqual(4, spans[1].Start); CheckEqual(11, spans[1].End); } public void AmongNumbersPronounsAndNouns_EachKeepsItsOwnWords() { CheckEqual("3 Ada's the sword she 7", RenderIn(GrammarRules.English, $"{Plain(3)} {Possessive("Ada")} {The(new Noun("sword"))} {He(Referent.Female)} {Plain(7)}")); } public void ASegmentPastThirtyTwo_StillTakesTheSuffix() { var line = new TextPieces(0, 0); for (int index = 0; index < 20; ++index) { line.AppendFormatted("a"); } for (int index = 0; index < 14; ++index) { line.AppendFormatted(Plain(1)); } line.AppendFormatted(Possessive("z")); CheckEqual("aaaaaaaaaaaaaaaaaaaa11111111111111z's", RenderIn(GrammarRules.English, line)); } public void ABufferTooSmall_GrowsToHoldTheSuffix() { char[] scratch = new char[1]; TextPieces line = $"{Possessive("Ada")}"; CheckEqual("Ada's", line.ViewIn(NumberFormat.Invariant, GrammarRules.English, ref scratch).ToString()); CheckTrue(scratch.Length >= 5); } public void ALineWithNoRules_ThrowsWhenItHoldsAPossessive() { char[] scratch = null; TextPieces line = $"{Possessive("Ada")} sword"; CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, ref scratch); } public void RulesWithNoPossessiveRules_ThrowWhenALineHoldsOne() { var rules = new GrammarRules("xx", GrammarRulesSamples.Table()); CheckThrow(typeof(InvalidOperationException)); RenderIn(rules, $"{Possessive("Ada")} sword"); } public void ToString_WritesEnglish() { TextPieces line = $"{Possessive("Ada")} sword"; CheckEqual("Ada's sword", line.ToString()); } } public class SelectorCtorTests : TestFixture { public void APronoun_IsNotAPluralSelector() { CheckThrow(typeof(ArgumentException)); new TextPieces(0, 0, He(Referent.Male)); } public void ANoun_IsNotAPluralSelector() { CheckThrow(typeof(ArgumentException)); new TextPieces(0, 0, The(new Noun("sword"))); } } public class PluralLineTests : TestFixture { public void AVariantWithAPronoun_ChoosesByTheRulesAndWritesTheGrammar() { char[] scratch = null; PluralLine line = Plural(Count(2), one: $"{Amount} thing is {Hers(Referent.Female)}", other: $"{Amount} things are {Hers(Referent.Female)}"); CheckEqual("2 things are hers", line.ViewIn(NumberFormat.Invariant, PluralRules.English, GrammarRules.English, ref scratch).ToString()); } public void AVariantWithAPronounAndSpans_FillsTheSpans() { char[] scratch = null; var spans = new DynamicArray(); PluralLine line = Plural(Count(1), one: $"{Amount} is {Bold(Him(Referent.Male))}", other: $"{Amount} are {Bold(Him(Referent.Male))}"); StringView view = line.ViewIn(NumberFormat.Invariant, PluralRules.English, GrammarRules.English, ref scratch, spans); CheckEqual("1 is him", view.ToString()); CheckEqual(1, spans.Count); } public void WithNoGrammarRules_AVariantWithAPronounThrows() { char[] scratch = null; PluralLine line = Plural(Count(1), one: $"{He(Referent.Male)}", other: $"{He(Referent.Male)}"); CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch); } public void WithNoGrammarRulesAndSpans_AVariantWithAPronounThrows() { char[] scratch = null; var spans = new DynamicArray(); PluralLine line = Plural(Count(1), one: $"{He(Referent.Male)}", other: $"{He(Referent.Male)}"); CheckThrow(typeof(InvalidOperationException)); line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch, spans); } public void ToString_WritesEnglish() { PluralLine line = Plural(Count(1), one: $"{He(Referent.Male)} one", other: $"{He(Referent.Male)} many"); CheckEqual("he one", line.ToString()); } } } }