using System; using Tessera.Text; using UnitTestSharp; namespace Tessera.UnitTests.Text { public class PossessiveAgreementRulesTests : TestFixture { public class FixedAgreement : PossessiveAgreementRules { private readonly string possessive; public FixedAgreement(string possessive, int genderCount, int maxLength) : base(genderCount, maxLength) { this.possessive = possessive; } protected override string Choose(Referent who, GrammarRole role, Agreement agreement) { return possessive; } } public static string German(Referent who, GrammarRole role, int gender, GrammarRole nounRole, bool isPlural = false) { return GermanPossessiveAgreement.Instance.Write(who, role, new Agreement(gender, isPlural, nounRole)); } public class CtorTests : TestFixture { public void KeepsTheGenderCountAndTheLongestPossessive() { var rules = new FixedAgreement("ab", genderCount: 2, maxLength: 5); CheckEqual(2, rules.GenderCount); CheckEqual(5, rules.MaxLength); } public void NoGenders_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedAgreement("", genderCount: 0, maxLength: 1); } public void TooManyGenders_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedAgreement("", genderCount: Noun.MaxGenderCount + 1, maxLength: 1); } public void ALengthOfZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FixedAgreement("", genderCount: 1, maxLength: 0); } } public class WriteTests : TestFixture { private static readonly Agreement Given = new Agreement(gender: 0, isPlural: false, GrammarRole.Subject); public void WhatTheLanguageChooses_IsWritten() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckEqual("ab", rules.Write(Referent.Male, GrammarRole.Possessive, Given)); } public void APossessivePronoun_IsAccepted() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckEqual("ab", rules.Write(Referent.Male, GrammarRole.PossessivePronoun, Given)); } public void ARoleThatIsNotAPossessive_Throws() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckThrow(typeof(ArgumentException)); rules.Write(Referent.Male, GrammarRole.Subject, Given); } public void ANonExistentReferent_Throws() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckThrow(typeof(ArgumentOutOfRangeException)); rules.Write((Referent)GrammarRules.ReferentCount, GrammarRole.Possessive, Given); } public void ANegativeReferent_Throws() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckThrow(typeof(ArgumentOutOfRangeException)); rules.Write((Referent)(-1), GrammarRole.Possessive, Given); } public void NothingToAgreeWith_Throws() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckThrow(typeof(InvalidOperationException)); rules.Write(Referent.Male, GrammarRole.Possessive, default); } public void AGenderTheLanguageLacks_Throws() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckThrow(typeof(InvalidOperationException)); rules.Write(Referent.Male, GrammarRole.Possessive, new Agreement(gender: 1, isPlural: false, GrammarRole.Subject)); } public void APossessiveLongerThanPromised_Throws() { var rules = new FixedAgreement("abc", genderCount: 1, maxLength: 2); CheckThrow(typeof(InvalidOperationException)); rules.Write(Referent.Male, GrammarRole.Possessive, Given); } public void APossessiveAsLongAsPromised_IsWritten() { var rules = new FixedAgreement("ab", genderCount: 1, maxLength: 2); CheckEqual("ab", rules.Write(Referent.Male, GrammarRole.Possessive, Given)); } } public class GermanPossessiveAgreementTests : TestFixture { public void TheLimits_AreThreeGendersAndSixCharacters() { CheckEqual(3, GermanPossessiveAgreement.Instance.GenderCount); CheckEqual(6, GermanPossessiveAgreement.Instance.MaxLength); } public void BeforeANoun_AMansFollowsTheGenderNumberAndCase() { string[][] expected = { new[] { "sein", "seine", "sein", "seine" }, new[] { "seinen", "seine", "sein", "seine" }, new[] { "seinem", "seiner", "seinem", "seinen" }, new[] { "seines", "seiner", "seines", "seiner" }, }; for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(expected[role][gender], German(Referent.Male, GrammarRole.Possessive, gender, (GrammarRole)role)); } CheckEqual(expected[role][3], German(Referent.Male, GrammarRole.Possessive, 0, (GrammarRole)role, isPlural: true)); } } public void BeforeANoun_AWomansTakesIhr() { CheckEqual("ihr", German(Referent.Female, GrammarRole.Possessive, 0, GrammarRole.Subject)); CheckEqual("ihre", German(Referent.Female, GrammarRole.Possessive, 1, GrammarRole.Subject)); CheckEqual("ihren", German(Referent.Female, GrammarRole.Possessive, 0, GrammarRole.Object)); CheckEqual("ihrem", German(Referent.Female, GrammarRole.Possessive, 2, GrammarRole.Dative)); CheckEqual("ihrer", German(Referent.Female, GrammarRole.Possessive, 1, GrammarRole.Genitive)); CheckEqual("ihres", German(Referent.Female, GrammarRole.Possessive, 0, GrammarRole.Genitive)); } public void BeforeANoun_AThingsIsTheSameAsAMans() { for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(German(Referent.Male, GrammarRole.Possessive, gender, (GrammarRole)role), German(Referent.Neuter, GrammarRole.Possessive, gender, (GrammarRole)role)); } } } public void Alone_FollowsTheGenderNumberAndCase() { string[][] expected = { new[] { "seiner", "seine", "seines", "seine" }, new[] { "seinen", "seine", "seines", "seine" }, new[] { "seinem", "seiner", "seinem", "seinen" }, new[] { "seines", "seiner", "seines", "seiner" }, }; for (int role = 0; role < GrammarRules.NounRoleCount; ++role) { for (int gender = 0; gender < 3; ++gender) { CheckEqual(expected[role][gender], German(Referent.Male, GrammarRole.PossessivePronoun, gender, (GrammarRole)role)); } CheckEqual(expected[role][3], German(Referent.Male, GrammarRole.PossessivePronoun, 0, (GrammarRole)role, isPlural: true)); } } public void Alone_AWomansTakesIhr() { CheckEqual("ihrer", German(Referent.Female, GrammarRole.PossessivePronoun, 0, GrammarRole.Subject)); CheckEqual("ihre", German(Referent.Female, GrammarRole.PossessivePronoun, 1, GrammarRole.Object)); CheckEqual("ihres", German(Referent.Female, GrammarRole.PossessivePronoun, 2, GrammarRole.Subject)); } public void APluralNoun_IgnoresItsGender() { for (int gender = 0; gender < 3; ++gender) { CheckEqual("seinen", German(Referent.Male, GrammarRole.Possessive, gender, GrammarRole.Dative, isPlural: true)); } } public void SingularThey_Throws() { CheckThrow(typeof(InvalidOperationException)); German(Referent.They, GrammarRole.Possessive, 0, GrammarRole.Subject); } public void SingularTheyAlone_Throws() { CheckThrow(typeof(InvalidOperationException)); German(Referent.They, GrammarRole.PossessivePronoun, 0, GrammarRole.Subject); } public void AFourthGender_Throws() { CheckThrow(typeof(InvalidOperationException)); German(Referent.Male, GrammarRole.Possessive, 3, GrammarRole.Subject); } } } }