using System; using Tessera.Text; using UnitTestSharp; namespace Tessera.UnitTests.Text { public class ArticleRulesTests : TestFixture { public class FixedArticles : ArticleRules { private readonly string article; public FixedArticles(string article, int genderCount, int maxLength) : base(genderCount, maxLength) { this.article = article; } protected override string Choose(ArticleKind kind, Noun noun, GrammarRole role) { return article; } } public static string English(ArticleKind kind, string text, bool isPlural = false, bool isName = false, InitialSound sound = InitialSound.Auto) { return EnglishArticles.Instance.Write(kind, new Noun(text, isPlural: isPlural, isName: isName, sound: sound), GrammarRole.Subject); } public static string German(ArticleKind kind, int gender, GrammarRole role, bool isPlural = false, bool isName = false) { return GermanArticles.Instance.Write(kind, new Noun("x", gender: gender, isPlural: isPlural, isName: isName), role); } public class CtorTests : TestFixture { public void KeepsTheGenderCountAndTheLongestArticle() { var rules = new FixedArticles("ab ", genderCount: 2, maxLength: 5); CheckEqual(2, rules.GenderCount); CheckEqual(5, rules.MaxLength); } public void NoGenders_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedArticles("", genderCount: 0, maxLength: 1); } public void TooManyGenders_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedArticles("", genderCount: Noun.MaxGenderCount + 1, maxLength: 1); } public void ANegativeLength_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedArticles("", genderCount: 1, maxLength: -1); } } public class WriteTests : TestFixture { public void WhatTheLanguageChooses_IsWritten() { var rules = new FixedArticles("ab ", genderCount: 1, maxLength: 3); CheckEqual("ab ", rules.Write(ArticleKind.Definite, new Noun("x"), GrammarRole.Subject)); } public void ANonExistentKind_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); EnglishArticles.Instance.Write((ArticleKind)3, new Noun("x"), GrammarRole.Subject); } public void ANegativeKind_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); EnglishArticles.Instance.Write((ArticleKind)(-1), new Noun("x"), GrammarRole.Subject); } public void ARoleANounDoesntTake_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); EnglishArticles.Instance.Write(ArticleKind.Definite, new Noun("x"), GrammarRole.Possessive); } public void ANegativeRole_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); EnglishArticles.Instance.Write(ArticleKind.Definite, new Noun("x"), (GrammarRole)(-1)); } public void AGenderTheLanguageLacks_Throws() { CheckThrow(typeof(InvalidOperationException)); EnglishArticles.Instance.Write(ArticleKind.Definite, new Noun("x", gender: 1), GrammarRole.Subject); } public void TheLastGenderTheLanguageHas_IsWritten() { CheckEqual("das ", German(ArticleKind.Definite, GermanArticles.Neuter, GrammarRole.Subject)); } public void AnArticleLongerThanPromised_Throws() { var rules = new FixedArticles("abcd", genderCount: 1, maxLength: 3); CheckThrow(typeof(InvalidOperationException)); rules.Write(ArticleKind.Definite, new Noun("x"), GrammarRole.Subject); } public void AnArticleAsLongAsPromised_IsWritten() { var rules = new FixedArticles("abc", genderCount: 1, maxLength: 3); CheckEqual("abc", rules.Write(ArticleKind.Definite, new Noun("x"), GrammarRole.Subject)); } } public class EnglishArticlesTests : TestFixture { public void TheLimits_AreOneGenderAndFourCharacters() { CheckEqual(1, EnglishArticles.Instance.GenderCount); CheckEqual(4, EnglishArticles.Instance.MaxLength); } public void Definite_IsThe() { CheckEqual("the ", English(ArticleKind.Definite, "sword")); } public void Definite_IsTheForAPlural() { CheckEqual("the ", English(ArticleKind.Definite, "swords", isPlural: true)); } public void Definite_IsNothingForAName() { CheckEqual("", English(ArticleKind.Definite, "Excalibur", isName: true)); } public void Definite_IgnoresTheRole() { var noun = new Noun("sword"); CheckEqual("the ", EnglishArticles.Instance.Write(ArticleKind.Definite, noun, GrammarRole.Genitive)); } public void None_IsNothing() { CheckEqual("", English(ArticleKind.None, "sword")); } public void Indefinite_IsAForAConsonant() { CheckEqual("a ", English(ArticleKind.Indefinite, "sword")); } public void Indefinite_IsAnForAVowel() { CheckEqual("an ", English(ArticleKind.Indefinite, "apple")); } public void Indefinite_IsAnForACapitalVowel() { CheckEqual("an ", English(ArticleKind.Indefinite, "Apple")); } public void Indefinite_IsNothingForAPlural() { CheckEqual("", English(ArticleKind.Indefinite, "swords", isPlural: true)); } public void Indefinite_IsNothingForAName() { CheckEqual("", English(ArticleKind.Indefinite, "Ada", isName: true)); } public void Indefinite_IsAForAVowelLetterThatSoundsLikeAConsonant() { string[] words = { "unicorn", "user", "unique sword", "union", "university", "one-handed sword", "once-lost map", "European", "utensil", "useful tool", "usual", "utility belt", "eulogy", "ewe", "ukulele", "utopia", "uterus", }; foreach (string word in words) { CheckEqual("a ", English(ArticleKind.Indefinite, word)); } } public void Indefinite_IsAnForAConsonantLetterThatSoundsLikeAVowel() { string[] words = { "hour", "hourglass", "honest man", "honor", "honour", "heir" }; foreach (string word in words) { CheckEqual("an ", English(ArticleKind.Indefinite, word)); } } public void Indefinite_IsAnForAVowelStartThatLooksLikeAConsonantStart() { string[] words = { "unimportant thing", "uninvited guest", "unidentified object", "onerous task" }; foreach (string word in words) { CheckEqual("an ", English(ArticleKind.Indefinite, word)); } } public void Indefinite_IsAForAnOrdinaryConsonantH() { CheckEqual("a ", English(ArticleKind.Indefinite, "hotel")); } public void Indefinite_IsAnForTheOtherVowelLetters() { string[] words = { "egg", "icicle", "orb", "umbrella", "axe" }; foreach (string word in words) { CheckEqual("an ", English(ArticleKind.Indefinite, word)); } } public void AVowelSoundNoun_OverridesTheLetters() { CheckEqual("an ", English(ArticleKind.Indefinite, "FBI agent", sound: InitialSound.Vowel)); } public void AConsonantSoundNoun_OverridesTheLetters() { CheckEqual("a ", English(ArticleKind.Indefinite, "apple", sound: InitialSound.Consonant)); } } public class WriteBeforeTests : TestFixture { public void ALanguageThatNeedsGender_Throws() { var rules = new FixedArticles("x", genderCount: 1, maxLength: 1); CheckThrow(typeof(InvalidOperationException)); rules.WriteBefore("sword", 0, InitialSound.Auto); } public void German_Throws() { CheckThrow(typeof(InvalidOperationException)); GermanArticles.Instance.WriteBefore("Schwert", 0, InitialSound.Auto); } public void English_WritesAnBeforeAVowelSoundAndAOtherwise() { CheckEqual("an", EnglishArticles.Instance.WriteBefore("old", 0, InitialSound.Auto)); CheckEqual("a", EnglishArticles.Instance.WriteBefore("rusty", 0, InitialSound.Auto)); } public void English_ReadsTheWordFromWhereItStarts() { CheckEqual("an", EnglishArticles.Instance.WriteBefore("very old", 5, InitialSound.Auto)); CheckEqual("a", EnglishArticles.Instance.WriteBefore("an old rusty", 7, InitialSound.Auto)); } public void English_FollowsTheSoundGiven() { CheckEqual("an", EnglishArticles.Instance.WriteBefore("FBI", 0, InitialSound.Vowel)); CheckEqual("a", EnglishArticles.Instance.WriteBefore("orb", 0, InitialSound.Consonant)); } public void English_UsesTheSameLettersAsANounDoes() { CheckEqual("an", EnglishArticles.Instance.WriteBefore("hour", 0, InitialSound.Auto)); CheckEqual("a", EnglishArticles.Instance.WriteBefore("unicorn", 0, InitialSound.Auto)); } public void ATooLongArticle_Throws() { var rules = new LongBeforeArticles(); CheckThrow(typeof(InvalidOperationException)); rules.WriteBefore("x", 0, InitialSound.Auto); } } public class LongBeforeArticles : ArticleRules { public LongBeforeArticles() : base(genderCount: 1, maxLength: 2) { } protected override string Choose(ArticleKind kind, Noun noun, GrammarRole role) { return ""; } protected override string ChooseBefore(string text, int start, InitialSound sound) { return "abc"; } } public class StartsWithVowelSoundTests : TestFixture { public void AWordInsideTheText_IsReadFromWhereItStarts() { CheckTrue(EnglishArticles.StartsWithVowelSound("the orb", 4, InitialSound.Auto)); CheckFalse(EnglishArticles.StartsWithVowelSound("an orb sword", 7, InitialSound.Auto)); CheckFalse(EnglishArticles.StartsWithVowelSound("an orb unicorn", 7, InitialSound.Auto)); } public void ThePrefixesOfAShortWord_DoNotRunPastTheText() { CheckFalse(EnglishArticles.StartsWithVowelSound("ho", 0, InitialSound.Auto)); CheckFalse(EnglishArticles.StartsWithVowelSound("a ho", 2, InitialSound.Auto)); } public void ALetterRule_ReadsTheFirstLetters() { CheckTrue(EnglishArticles.StartsWithVowelSound(new Noun("orb"))); CheckFalse(EnglishArticles.StartsWithVowelSound(new Noun("unicorn"))); CheckFalse(EnglishArticles.StartsWithVowelSound(new Noun("sword"))); } public void TheNounsOwnSound_Wins() { CheckTrue(EnglishArticles.StartsWithVowelSound(new Noun("sword", sound: InitialSound.Vowel))); CheckFalse(EnglishArticles.StartsWithVowelSound(new Noun("orb", sound: InitialSound.Consonant))); } } public class GermanArticlesTests : TestFixture { public void TheLimits_AreThreeGendersAndSixCharacters() { CheckEqual(3, GermanArticles.Instance.GenderCount); CheckEqual(6, GermanArticles.Instance.MaxLength); } public void Definite_FollowsTheGenderInEveryCase() { string[,] expected = { { "der ", "die ", "das " }, { "den ", "die ", "das " }, { "dem ", "der ", "dem " }, { "des ", "der ", "des " }, }; for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(expected[role, gender], German(ArticleKind.Definite, gender, (GrammarRole)role)); } } } public void Definite_IsOneFormForAPluralOfAnyGender() { string[] expected = { "die ", "die ", "den ", "der " }; for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(expected[role], German(ArticleKind.Definite, gender, (GrammarRole)role, isPlural: true)); } } } public void Indefinite_FollowsTheGenderInEveryCase() { string[,] expected = { { "ein ", "eine ", "ein " }, { "einen ", "eine ", "ein " }, { "einem ", "einer ", "einem " }, { "eines ", "einer ", "eines " }, }; for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(expected[role, gender], German(ArticleKind.Indefinite, gender, (GrammarRole)role)); } } } public void Indefinite_IsNothingForAPlural() { CheckEqual("", German(ArticleKind.Indefinite, 0, GrammarRole.Subject, isPlural: true)); } public void AName_TakesNoArticle() { CheckEqual("", German(ArticleKind.Definite, 1, GrammarRole.Subject, isName: true)); CheckEqual("", German(ArticleKind.Indefinite, 1, GrammarRole.Subject, isName: true)); } public void None_IsNothing() { CheckEqual("", German(ArticleKind.None, 0, GrammarRole.Dative)); } public void AFourthGender_Throws() { CheckThrow(typeof(InvalidOperationException)); German(ArticleKind.Definite, 3, GrammarRole.Subject); } } } }