using System; using System.Globalization; using Tessera.Text; using UnitTestSharp; using static Tessera.UnitTests.Text.GrammarRulesSamples; namespace Tessera.UnitTests.Text { public class GrammarRulesTests : TestFixture { public class CtorTests : TestFixture { public void KeepsTheTagAndTheLongestPronoun() { var rules = new GrammarRules("xx", Table(subject: "longest")); CheckEqual("xx", rules.Tag); CheckEqual(7, rules.MaxPronounLength); CheckEqual("longest", rules.Pronoun(Referent.Male, GrammarRole.Subject)); } public void ANullEntry_IsLeftOutOfTheLongestPronoun() { string[][] table = Table(); table[0][0] = null; CheckEqual(1, new GrammarRules("xx", table).MaxPronounLength); } public void ANullTag_Throws() { CheckThrow(typeof(ArgumentNullException)); new GrammarRules(null, Table()); } public void NullPronouns_Throw() { CheckThrow(typeof(ArgumentNullException)); new GrammarRules("xx", null); } public void TooFewRows_Throw() { CheckThrow(typeof(ArgumentException)); new GrammarRules("xx", new string[GrammarRules.ReferentCount - 1][]); } public void ANullRow_Throws() { string[][] table = Table(); table[2] = null; CheckThrow(typeof(ArgumentException)); new GrammarRules("xx", table); } public void ARowOfTheWrongLength_Throws() { string[][] table = Table(); table[1] = new string[GrammarRules.RoleCount + 1]; CheckThrow(typeof(ArgumentException)); new GrammarRules("xx", table); } public void AnEmptyPronoun_Throws() { string[][] table = Table(); table[3][4] = ""; CheckThrow(typeof(ArgumentException)); new GrammarRules("xx", table); } } public class ForTests : TestFixture { public void English_IsEnglishInAnyRegion() { CheckTrue(ReferenceEquals(GrammarRules.English, GrammarRules.For(new CultureInfo("en-US")))); CheckTrue(ReferenceEquals(GrammarRules.English, GrammarRules.For(new CultureInfo("en-GB")))); } public void German_IsGerman() { CheckTrue(ReferenceEquals(GrammarRules.German, GrammarRules.For(new CultureInfo("de-DE")))); } public void ALanguageWithNoRules_IsNone() { CheckTrue(ReferenceEquals(GrammarRules.None, GrammarRules.For(new CultureInfo("fr-FR")))); } public void ANullCulture_Throws() { CheckThrow(typeof(ArgumentNullException)); GrammarRules.For(null); } } public class PronounTests : TestFixture { public void English_WritesEachRoleOfEachReferent() { string[] expected = { "he him him his his his himself", "she her her her her hers herself", "it it it its its its itself", "they them them their their theirs themself", }; for (int referent = 0; referent < GrammarRules.ReferentCount; ++referent) { var written = new string[GrammarRules.RoleCount]; for (int role = 0; role < GrammarRules.RoleCount; ++role) { written[role] = GrammarRules.English.Pronoun((Referent)referent, (GrammarRole)role); } CheckEqual(expected[referent], string.Join(" ", written)); } } public void German_WritesTheRolesThatStandAlone() { CheckEqual("er", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Subject)); CheckEqual("ihn", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Object)); CheckEqual("ihm", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Dative)); CheckEqual("seiner", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Genitive)); CheckEqual("sich", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Reflexive)); CheckEqual("sie", GrammarRules.German.Pronoun(Referent.Female, GrammarRole.Subject)); CheckEqual("sie", GrammarRules.German.Pronoun(Referent.Female, GrammarRole.Object)); CheckEqual("ihr", GrammarRules.German.Pronoun(Referent.Female, GrammarRole.Dative)); CheckEqual("ihrer", GrammarRules.German.Pronoun(Referent.Female, GrammarRole.Genitive)); CheckEqual("es", GrammarRules.German.Pronoun(Referent.Neuter, GrammarRole.Subject)); CheckEqual("ihm", GrammarRules.German.Pronoun(Referent.Neuter, GrammarRole.Dative)); } public void German_ThrowsForAPossessive() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Possessive); } public void German_ThrowsForAPossessivePronoun() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.Female, GrammarRole.PossessivePronoun); } public void German_ThrowsForThey() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.They, GrammarRole.Subject); } public void None_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.None.Pronoun(Referent.Male, GrammarRole.Subject); } public void ANonExistentReferent_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); GrammarRules.English.Pronoun((Referent)GrammarRules.ReferentCount, GrammarRole.Subject); } public void ANegativeReferent_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); GrammarRules.English.Pronoun((Referent)(-1), GrammarRole.Subject); } public void ANonExistentRole_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); GrammarRules.English.Pronoun(Referent.Male, (GrammarRole)GrammarRules.RoleCount); } public void ANegativeRole_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); GrammarRules.English.Pronoun(Referent.Male, (GrammarRole)(-1)); } } public class ArticleTests : TestFixture { public void English_WritesItsArticles() { CheckEqual("a ", GrammarRules.English.Article(ArticleKind.Indefinite, new Noun("sword"), GrammarRole.Subject)); CheckEqual("the ", GrammarRules.English.Article(ArticleKind.Definite, new Noun("sword"), GrammarRole.Subject)); } public void German_WritesItsArticles() { var mann = new Noun("Mann", gender: GermanArticles.Masculine); CheckEqual("dem ", GrammarRules.German.Article(ArticleKind.Definite, mann, GrammarRole.Dative)); } public void None_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.None.Article(ArticleKind.Definite, new Noun("x"), GrammarRole.Subject); } public void RulesWithNoArticleRules_Throw() { var rules = new GrammarRules("xx", Table()); CheckThrow(typeof(InvalidOperationException)); rules.Article(ArticleKind.Definite, new Noun("x"), GrammarRole.Subject); } public void RulesWithTheirOwnArticleRules_UseThem() { var rules = new GrammarRules("xx", Table(), GermanArticles.Instance); CheckEqual("die ", rules.Article(ArticleKind.Definite, new Noun("x", gender: 1), GrammarRole.Subject)); } public void TheLongestArticle_IsTheRulesOwn() { CheckEqual(4, GrammarRules.English.MaxArticleLength); CheckEqual(6, GrammarRules.German.MaxArticleLength); CheckEqual(6, new GrammarRules("xx", Table(), GermanArticles.Instance).MaxArticleLength); } public void NoArticleRules_HaveNoLongestArticle() { CheckEqual(0, GrammarRules.None.MaxArticleLength); CheckEqual(0, new GrammarRules("xx", Table()).MaxArticleLength); } } public class ArticleBeforeTests : TestFixture { public void English_WritesAnArticleForTheNextWord() { CheckEqual("an", GrammarRules.English.ArticleBefore("old", 0, InitialSound.Auto)); CheckEqual("a", GrammarRules.English.ArticleBefore("rusty", 0, InitialSound.Auto)); } public void German_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.ArticleBefore("Schwert", 0, InitialSound.Auto); } public void None_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.None.ArticleBefore("old", 0, InitialSound.Auto); } public void RulesWithNoArticleRules_Throw() { var rules = new GrammarRules("xx", Table()); CheckThrow(typeof(InvalidOperationException)); rules.ArticleBefore("old", 0, InitialSound.Auto); } } public class PossessiveTests : TestFixture { public void English_WritesItsSuffixes() { CheckEqual("'s", GrammarRules.English.Possessive("Ada")); CheckEqual("'", GrammarRules.English.Possessive("James")); } public void German_WritesItsSuffixes() { CheckEqual("s", GrammarRules.German.Possessive("Ada")); CheckEqual("'", GrammarRules.German.Possessive("Hans")); } public void None_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.None.Possessive("Ada"); } public void RulesWithNoPossessiveRules_Throw() { var rules = new GrammarRules("xx", Table(), GermanArticles.Instance); CheckThrow(typeof(InvalidOperationException)); rules.Possessive("Ada"); } public void RulesWithTheirOwnPossessiveRules_UseThem() { var rules = new GrammarRules("xx", Table(), possessives: GermanPossessives.Instance); CheckEqual("s", rules.Possessive("Ada")); } public void TheLongestSuffix_IsTheRulesOwn() { CheckEqual(2, GrammarRules.English.MaxPossessiveLength); CheckEqual(1, GrammarRules.German.MaxPossessiveLength); CheckEqual(1, new GrammarRules("xx", Table(), possessives: GermanPossessives.Instance) .MaxPossessiveLength); } public void NoPossessiveRules_HaveNoLongestSuffix() { CheckEqual(0, GrammarRules.None.MaxPossessiveLength); CheckEqual(0, new GrammarRules("xx", Table()).MaxPossessiveLength); } } public class PronounWithAgreementTests : TestFixture { private static readonly Agreement DativeFeminine = new Agreement(gender: 1, isPlural: false, GrammarRole.Dative); public void German_WritesAPossessiveThatAgrees() { CheckEqual("seiner", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine)); CheckEqual("ihrer", GrammarRules.German.Pronoun(Referent.Female, GrammarRole.PossessivePronoun, DativeFeminine)); } public void German_WritesTheOtherRolesAsBefore() { CheckEqual("ihm", GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Dative, DativeFeminine)); } public void German_ThrowsForAPossessiveWithNoNoun() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.Male, GrammarRole.Possessive); } public void German_ThrowsForAPossessivePronounWithNoNoun() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.Male, GrammarRole.PossessivePronoun, default); } public void German_ThrowsForTheyEvenWithANoun() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.German.Pronoun(Referent.They, GrammarRole.Possessive, DativeFeminine); } public void English_IgnoresTheNoun() { CheckEqual("his", GrammarRules.English.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine)); CheckEqual("hers", GrammarRules.English.Pronoun(Referent.Female, GrammarRole.PossessivePronoun, DativeFeminine)); } public void None_Throws() { CheckThrow(typeof(InvalidOperationException)); GrammarRules.None.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine); } public void RulesWithNoAgreement_ThrowForAnUnwrittenPossessive() { var rules = new GrammarRules("xx", Table()); string[][] table = Table(); table[0][4] = null; var withHole = new GrammarRules("xx", table); CheckEqual("x", rules.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine)); CheckThrow(typeof(InvalidOperationException)); withHole.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine); } public void RulesWithTheirOwnAgreement_UseItForAnUnwrittenPossessive() { string[][] table = Table(); table[0][4] = null; var rules = new GrammarRules("xx", table, possessiveAgreement: GermanPossessiveAgreement.Instance); CheckEqual("seiner", rules.Pronoun(Referent.Male, GrammarRole.Possessive, DativeFeminine)); } public void TheLongestPronoun_CountsThePossessiveTheRulesWrite() { string[][] table = Table(); var rules = new GrammarRules("xx", table, possessiveAgreement: GermanPossessiveAgreement.Instance); CheckEqual(6, rules.MaxPronounLength); } } public class MaxPronounLengthTests : TestFixture { public void English_IsTheLengthOfThemself() { CheckEqual(8, GrammarRules.English.MaxPronounLength); } public void German_IgnoresWhatItCantWrite() { CheckEqual(6, GrammarRules.German.MaxPronounLength); } public void None_IsZero() { CheckEqual(0, GrammarRules.None.MaxPronounLength); } } } }