using System; using System.Collections; using Amphora; using Prism; using Tessera.Text; using UnitTestSharp; using static Tessera.Text.Plurals; using static Tessera.Text.Styles; namespace Tessera.UnitTests.Text { public class PluralLineTests : TestFixture { public static string Render(in PluralLine line) { return line.ToString(); } public static string In(string tag, in PluralLine line) { char[] scratch = null; return line.ViewIn(NumberFormat.Invariant, PluralRules.For(tag), ref scratch).ToString(); } public static string BoxLine(int boxes) { return Render(Plural(Count(boxes), one: $"There is {Amount} box left.", other: $"There are {Amount} boxes left.")); } public class CtorTests : TestFixture { public void KeepsTheSelectorAndWhetherItIsOrdinal() { var line = new PluralLine(Count(3), $"x", isOrdinal: true); CheckEqual(NumberKind.Count, line.Selector.Kind); CheckEqual(3L, line.Selector.Number); CheckTrue(line.IsOrdinal); } public void IsNotOrdinalUnlessAsked() { CheckFalse(new PluralLine(Count(3), $"x").IsOrdinal); } public void TheLineGiven_IsOther() { var line = new PluralLine(Count(3), $"x"); CheckEqual("x", line.VariantOf(PluralCategory.Other).ToString()); } public void WithoutAnOtherVariant_Throws() { CheckThrow(typeof(ArgumentException)); new PluralLine(Count(3), default); } public void ASelectorWithNoPluralForm_Throws() { CheckThrow(typeof(ArgumentException)); new PluralLine(Percent(0.5), $"x"); } public void AStringSelector_Throws() { CheckThrow(typeof(ArgumentException)); new PluralLine("three", $"x"); } } public class AddTests : TestFixture { public void AnInitializer_AddsExactNumbersAndForms() { var line = new PluralLine(Count(6), $"{Amount} sticks") { { 6, $"Pick up sticks" }, { 13, $"Unlucky for some" }, { None, $"No sticks" }, { Few, $"A handful" }, }; CheckEqual(3, line.ExactCount); CheckEqual("Pick up sticks", line.ExactVariant(6).ToString()); CheckEqual("Unlucky for some", line.ExactVariant(13).ToString()); CheckEqual("No sticks", line.NoneVariant().ToString()); CheckEqual("A handful", line.VariantOf(PluralCategory.Few).ToString()); } public void AnExactVariant_KeepsTheOrderItWasAdded() { var line = new PluralLine(Count(1), $"x") { { 9, $"nine" }, { 2, $"two" } }; CheckEqual(9L, line.ExactNumberAt(0)); CheckEqual(2L, line.ExactNumberAt(1)); CheckEqual("two", line.ExactVariantAt(1).ToString()); } public void TheAmountInAnEntry_IsTheSelector() { var line = new PluralLine(Count(1234), $"x") { { 1234, $"Exactly {Amount}" } }; CheckEqual("Exactly 1,234", Render(line)); } public void TheAmountInAnEntry_FollowsTheSelectorsKind() { var line = new PluralLine(Fixed(2.5, 1), $"x") { { Many, $"{Amount} is many" } }; char[] scratch = null; CheckEqual("2.5 is many", line.VariantOf(PluralCategory.Many).ViewIn(NumberFormat.Invariant, GrammarRules.None, ref scratch).ToString()); } public void AllEightExactNumbers_Fit() { var line = new PluralLine(Count(1), $"x") { { 1, $"a" }, { 2, $"b" }, { 3, $"c" }, { 4, $"d" }, { 5, $"e" }, { 6, $"f" }, { 7, $"g" }, { 8, $"h" }, }; CheckEqual(PluralLine.MaxExactCount, line.ExactCount); CheckEqual("h", line.ExactVariant(8).ToString()); CheckEqual("a", line.ExactVariant(1).ToString()); } public void ANinthExactNumber_Throws() { var line = new PluralLine(Count(1), $"x") { { 1, $"a" }, { 2, $"b" }, { 3, $"c" }, { 4, $"d" }, { 5, $"e" }, { 6, $"f" }, { 7, $"g" }, { 8, $"h" }, }; CheckThrow(typeof(InvalidOperationException)); line.Add(9, $"i"); } public void TheSameExactNumberTwice_Throws() { var line = new PluralLine(Count(1), $"x") { { 6, $"a" } }; CheckThrow(typeof(ArgumentException)); line.Add(6, $"b"); } public void TheSameFormTwice_Throws() { var line = new PluralLine(Count(1), $"x") { { Few, $"a" } }; CheckThrow(typeof(ArgumentException)); line.Add(Few, $"b"); } public void Other_IsAlreadyGivenByTheConstructor() { var line = new PluralLine(Count(1), $"x"); CheckThrow(typeof(ArgumentException)); line.Add(Other, $"y"); } public void AVariantNeverBuilt_Throws() { var line = new PluralLine(Count(1), $"x"); CheckThrow(typeof(ArgumentException)); line.Add(6, default); } public void ANegativeExactNumber_IsAllowed() { var line = new PluralLine(Count(-3), $"x") { { -3, $"minus three" } }; CheckEqual("minus three", Render(line)); } } public class ExactVariantTests : TestFixture { public void ANumberNotAdded_IsNotBuilt() { CheckFalse(new PluralLine(Count(1), $"x") { { 6, $"a" } }.ExactVariant(7).IsBuilt); } public void ASlotPastTheCount_Throws() { var line = new PluralLine(Count(1), $"x") { { 6, $"a" } }; CheckThrow(typeof(ArgumentOutOfRangeException)); line.ExactVariantAt(1); } public void ANegativeSlot_Throws() { var line = new PluralLine(Count(1), $"x") { { 6, $"a" } }; CheckThrow(typeof(ArgumentOutOfRangeException)); line.ExactNumberAt(-1); } public void ANumberSlotPastTheCount_Throws() { var line = new PluralLine(Count(1), $"x") { { 6, $"a" } }; CheckThrow(typeof(ArgumentOutOfRangeException)); line.ExactNumberAt(1); } } public class EnumerateTests : TestFixture { public void Throws() { IEnumerable line = new PluralLine(Count(1), $"x"); CheckThrow(typeof(NotSupportedException)); line.GetEnumerator(); } } public class ExactChoiceTests : TestFixture { private char[] scratch; public static PluralLine Sticks(StyledText selector) { return new PluralLine(selector, $"{Amount} sticks") { { 6, $"Pick up sticks" }, { 13, $"Unlucky for some" }, { None, $"No sticks" }, { One, $"A stick" }, }; } public void AnExactNumber_BeatsTheCategoryAndOther() { CheckEqual("Pick up sticks", Render(Sticks(Count(6)))); CheckEqual("Unlucky for some", Render(Sticks(Count(13)))); } public void ANumberNotNamed_UsesItsForm() { CheckEqual("A stick", Render(Sticks(Count(1)))); CheckEqual("7 sticks", Render(Sticks(Count(7)))); } public void None_BeatsAFormThatCoversZero() { CheckEqual("No sticks", In("fr", Sticks(Count(0)))); } [IgnoreTest] public PluralLine Korobki(long boxes) { return new PluralLine(Count(boxes), $"{Amount} korobki") { { 1, $"Odna korobka" }, { One, $"{Amount} korobka" }, }; } public void AnExactNumberOfOne_BeatsTheFormThatCoversTwentyOne() { CheckEqual("Odna korobka", In("ru", Korobki(1))); CheckEqual("21 korobka", In("ru", Korobki(21))); } public void AFixedNumberShownAsAWholeNumber_Matches() { CheckEqual("Pick up sticks", Render(Sticks(Fixed(6, 1)))); CheckEqual("Pick up sticks", Render(Sticks(Fixed(5.96, 1)))); } public void AFixedNumberShownWithADecimal_DoesNotMatchTheWholeNumber() { CheckEqual("6.1 sticks", Render(Sticks(Fixed(6.1, 1)))); } public void ANegativeNumber_DoesNotMatchItsMagnitude() { CheckEqual("-6 sticks", Render(Sticks(Count(-6)))); } public void ANegativeFixedNumber_DoesNotMatchItsMagnitude() { CheckEqual("-6.0 sticks", Render(Sticks(Fixed(-6, 1)))); } public void ASelectorShownAsASymbol_MatchesNothing() { CheckEqual("NaN sticks", Render(Sticks(Fixed(double.NaN, 1)))); } public void AnOrdinal_MatchesAnExactPlace() { var line = new PluralLine(Plain(1), $"{Amount}th", isOrdinal: true) { { 1, $"first" }, { One, $"{Amount}st" }, }; CheckEqual("first", Render(line)); } public void AnExactNumberInAVariantWithSpans_FillsTheSpans() { var line = new PluralLine(Count(6), $"x") { { 6, $"Got {Tint(Colors.Gold, Amount)} sticks" } }; var spans = new DynamicArray(); char[] scratch = null; line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch, spans); CheckEqual(1, spans.Count); } [UnitTestSharp.AllocationTest] public void ReadingALineWithExactNumbers_AllocatesNothing() { var line = Sticks(Count(6)); line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch); CheckNoAllocations(); } [UnitTestSharp.AllocationTest] public void BuildingALineWithExactNumbers_AllocatesNothing() { PluralLine line = Sticks(Count(6)); CheckNoAllocations(); CheckEqual(3, line.ExactCount); } } public class VariantOfTests : TestFixture { public void ReturnsEachVariantGiven() { PluralLine line = Plural(Count(1), zero: $"z", one: $"o", two: $"t", few: $"f", many: $"m", other: $"x"); CheckEqual("z", line.VariantOf(PluralCategory.Zero).ToString()); CheckEqual("o", line.VariantOf(PluralCategory.One).ToString()); CheckEqual("t", line.VariantOf(PluralCategory.Two).ToString()); CheckEqual("f", line.VariantOf(PluralCategory.Few).ToString()); CheckEqual("m", line.VariantOf(PluralCategory.Many).ToString()); CheckEqual("x", line.VariantOf(PluralCategory.Other).ToString()); } public void AVariantNotGiven_IsNotBuilt() { PluralLine line = Plural(Count(1), other: $"x"); CheckFalse(line.VariantOf(PluralCategory.One).IsBuilt); CheckTrue(line.VariantOf(PluralCategory.Other).IsBuilt); } } public class NoneVariantTests : TestFixture { public void IsTheVariantGivenForExactlyZero() { PluralLine line = Plural(Count(1), none: $"nothing", other: $"x"); CheckEqual("nothing", line.NoneVariant().ToString()); } public void WhenNotGiven_IsNotBuilt() { CheckFalse(Plural(Count(1), other: $"x").NoneVariant().IsBuilt); } } public class CategoryInTests : TestFixture { public void CountsByCardinalRules() { PluralLine one = Plural(Count(1), other: $"x"); PluralLine two = Plural(Count(2), other: $"x"); CheckEqual(PluralCategory.One, one.CategoryIn(NumberFormat.Invariant, PluralRules.English)); CheckEqual(PluralCategory.Other, two.CategoryIn(NumberFormat.Invariant, PluralRules.English)); } public void AnOrdinalLine_UsesOrdinalRules() { PluralLine line = Ordinal(Plain(2), other: $"x"); CheckEqual(PluralCategory.Two, line.CategoryIn(NumberFormat.Invariant, PluralRules.English)); } public void TheLanguagesRulesDecide() { PluralLine line = Plural(Count(2), other: $"x"); CheckEqual(PluralCategory.Few, line.CategoryIn(NumberFormat.Invariant, PluralRules.For("ru"))); } public void ACompactSelector_IsReadWithItsPowerOfTen() { PluralLine line = Plural(ShortExponent(1500000, 2), other: $"x"); CheckEqual(PluralCategory.Many, line.CategoryIn(NumberFormat.Invariant, PluralRules.For("fr"))); } public void NullRules_Throw() { PluralLine line = Plural(Count(1), other: $"x"); CheckThrow(typeof(ArgumentNullException)); line.CategoryIn(NumberFormat.Invariant, null); } } public class ViewInTests : TestFixture { public void English_PicksOneForOneAndOtherForTheRest() { CheckEqual("There is 1 box left.", BoxLine(1)); CheckEqual("There are 2 boxes left.", BoxLine(2)); CheckEqual("There are 0 boxes left.", BoxLine(0)); } public void TheAmount_IsWrittenInTheGivenFormat() { var line = Plural(Count(1234567), other: $"{Amount} boxes"); char[] scratch = null; CheckEqual("1.234.567 boxes", line.ViewIn(NumberFormat.FromCulture( new System.Globalization.CultureInfo("de-DE")), PluralRules.English, ref scratch).ToString()); } public void None_BeatsTheCategoryForExactlyZero() { var line = Plural(Count(0), none: $"No boxes left.", other: $"{Amount} boxes left."); CheckEqual("No boxes left.", Render(line)); } public void None_BeatsAFormThatCoversZero() { var line = Plural(Count(0), none: $"None", one: $"One", other: $"Many"); CheckEqual("None", In("fr", line)); } public void None_IsNotUsedForOtherNumbers() { var line = Plural(Count(1), none: $"None", one: $"One", other: $"Many"); CheckEqual("One", Render(line)); } public void None_IsUsedForAFixedNumberThatShowsAsZero() { var line = Plural(Fixed(0.04, 1), none: $"None", other: $"{Amount}"); CheckEqual("None", Render(line)); } public void None_IsNotUsedForAFixedNumberThatShowsAsNonZero() { var line = Plural(Fixed(0.06, 1), none: $"None", other: $"{Amount}"); CheckEqual("0.1", Render(line)); } public void AFormNotGiven_FallsBackToOther() { var line = Plural(Count(2), one: $"One", other: $"Other"); CheckEqual("Other", In("ru", line)); } [IgnoreTest] public string Boxes(int count) { return In("ru", Plural(Count(count), one: $"{Amount} korobka", few: $"{Amount} korobki", many: $"{Amount} korobok", other: $"{Amount} korobki")); } public void Russian_PicksFromItsFourForms() { CheckEqual("1 korobka", Boxes(1)); CheckEqual("2 korobki", Boxes(2)); CheckEqual("5 korobok", Boxes(5)); CheckEqual("21 korobka", Boxes(21)); CheckEqual("11 korobok", Boxes(11)); } public void AFixedSelector_ChoosesByItsDisplayedDecimals() { CheckEqual("1", Render(Plural(Fixed(1, 0), one: $"{Amount}", other: $"{Amount} more"))); CheckEqual("1.0 more", Render(Plural(Fixed(1, 1), one: $"{Amount}", other: $"{Amount} more"))); } public void ASelectorShownAsASymbol_IsOther() { var line = Plural(Fixed(double.NaN, 1), one: $"One", other: $"{Amount}"); CheckEqual("NaN", Render(line)); } public void OrdinalsFollowTheOrdinalRules() { CheckEqual("1st", OrdinalLine(1)); CheckEqual("2nd", OrdinalLine(2)); CheckEqual("3rd", OrdinalLine(3)); CheckEqual("4th", OrdinalLine(4)); CheckEqual("11th", OrdinalLine(11)); CheckEqual("21st", OrdinalLine(21)); CheckEqual("22nd", OrdinalLine(22)); CheckEqual("23rd", OrdinalLine(23)); CheckEqual("101st", OrdinalLine(101)); } [IgnoreTest] public string OrdinalLine(int place) { return Render(Ordinal(Plain(place), one: $"{Amount}st", two: $"{Amount}nd", few: $"{Amount}rd", other: $"{Amount}th")); } public void NullRules_Throw() { var line = Plural(Count(1), other: $"x"); char[] scratch = null; CheckThrow(typeof(ArgumentNullException)); line.ViewIn(NumberFormat.Invariant, null, ref scratch); } public void NullFormat_Throws() { var line = Plural(Count(1), other: $"x"); char[] scratch = null; CheckThrow(typeof(ArgumentNullException)); line.ViewIn(null, PluralRules.English, ref scratch); } public void TheScratchBuffer_IsReusedAcrossLines() { char[] scratch = null; var first = Plural(Count(1), other: $"a long first variant of the line"); var second = Plural(Count(1), other: $"short"); first.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch); char[] grown = scratch; second.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch); CheckTrue(ReferenceEquals(grown, scratch)); } } public class ViewInWithSpansTests : TestFixture { private char[] scratch; private readonly DynamicArray spans = new DynamicArray(); [IgnoreTest] public string Read(in PluralLine line) { return line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch, spans).ToString(); } [IgnoreTest] public int LengthOfRead(in PluralLine line) { StringView view = line.ViewIn(NumberFormat.Invariant, PluralRules.English, ref scratch, spans); return view.EndIndex - view.StartIndex; } [UnitTestSharp.AllocationTest] public void ReadingAPluralLine_AllocatesNothingOnceTheBuffersHaveGrown() { int boxes = 3; int length = LengthOfRead(Plural(Count(boxes), one: $"There is {Tint(Colors.Gold, Amount)} box.", other: $"There are {Tint(Colors.Gold, Amount)} boxes.")); CheckNoAllocations(); CheckEqual("There are 3 boxes.".Length, length); } public void AStyledAmount_HasASpanOverIt() { string text = Read(Plural(Count(12), other: $"Got {Tint(Colors.Gold, Amount)} boxes")); CheckEqual("Got 12 boxes", text); CheckEqual(1, spans.Count); CheckEqual(new StyleSpan(4, 6, new TextStyle { TintColor = Colors.Gold }), spans[0]); } public void TheSpansOfTheChosenVariant_ReplaceThoseOfAnEarlierRead() { Read(Plural(Count(12), other: $"{Tint(Colors.Gold, Amount)} {Tint(Colors.Red, Amount)}")); Read(Plural(Count(12), other: $"plain")); CheckEqual(0, spans.Count); } } public class ToStringTests : TestFixture { public void IsTheVariantEnglishChooses() { CheckEqual("There is 1 box left.", BoxLine(1)); } public void WritesCountsInTheInvariantFormat() { CheckEqual("1,000 boxes", Render(Plural(Count(1000), other: $"{Amount} boxes"))); } } } }