using System; using System.Globalization; using Amphora; using Glass.Text; using UnitTestSharp; using Tessera.Text; using static Tessera.Text.Plurals; using static Tessera.Text.Styles; namespace Glass.UnitTests { public class LocalizedLineTests : TestFixture { public static TextPieces Boxes(long count) { return $"{Bold(Count(count))} boxes"; } public static TextPieces Struck(long count) { return $"{Strikethrough(Count(count))} boxes"; } public static LocaleBehavior German() { var locale = new LocaleBehavior(); locale.SetCulture(new CultureInfo("de-DE")); return locale; } public class IsHeldTests : TestFixture { public void IsFalseBeforeALineIsSet() { CheckFalse(new LocalizedLine().IsHeld); } public void IsTrueOnceALineIsSet() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); CheckTrue(held.IsHeld); } public void IsFalseOnceCleared() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); held.Clear(); CheckFalse(held.IsHeld); } } public class SetTests : TestFixture { public void WritesTheLineInTheLanguage() { var held = new LocalizedLine(); CheckEqual("1,234 boxes", held.Set(Boxes(1234), new LocaleBehavior()).ToString()); CheckEqual("1.234 boxes", held.Set(Boxes(1234), German()).ToString()); } public void FillsTheSpansOfTheStyledHoles() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); CheckEqual(1, held.Spans.Count); CheckEqual(0, held.Spans[0].Start); CheckEqual(5, held.Spans[0].End); } public void AnotherLine_ReplacesTheSpans() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); held.Set($"no styles", new LocaleBehavior()); CheckEqual(0, held.Spans.Count); } public void ANullLocale_Throws() { CheckThrow(typeof(ArgumentNullException)); new LocalizedLine().Set(Boxes(1), null); } public void ANullLocale_LeavesTheHeldLineAlone() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); try { held.Set(Boxes(1), null); } catch (ArgumentNullException) { } CheckEqual("1,234 boxes", held.Write().ToString()); } public void ALongLine_IsWrittenInFull() { var held = new LocalizedLine(); string words = new string('x', 500); StringView written = held.Set($"{words} {Count(5)}", new LocaleBehavior()); CheckEqual(words + " 5", written.ToString()); } } public class IsStaleTests : TestFixture { public void IsFalseWhenNothingIsHeld() { CheckFalse(new LocalizedLine().IsStale); } public void IsFalseRightAfterASet() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); CheckFalse(held.IsStale); } public void IsTrueOnceTheLanguageChanges() { var held = new LocalizedLine(); LocaleBehavior locale = new LocaleBehavior(); held.Set(Boxes(1234), locale); locale.SetCulture(new CultureInfo("de-DE")); CheckTrue(held.IsStale); } public void IsFalseOnceWrittenAgain() { var held = new LocalizedLine(); LocaleBehavior locale = new LocaleBehavior(); held.Set(Boxes(1234), locale); locale.SetCulture(new CultureInfo("de-DE")); held.Write(); CheckFalse(held.IsStale); } public void IsFalseOnceCleared() { var held = new LocalizedLine(); LocaleBehavior locale = new LocaleBehavior(); held.Set(Boxes(1234), locale); locale.SetCulture(new CultureInfo("de-DE")); held.Clear(); CheckFalse(held.IsStale); } } public class WriteTests : TestFixture { public void WritesTheLineInTheLanguageAsItIsNow() { var held = new LocalizedLine(); LocaleBehavior locale = new LocaleBehavior(); held.Set(Boxes(1234), locale); locale.SetCulture(new CultureInfo("de-DE")); CheckEqual("1.234 boxes", held.Write().ToString()); } public void WithNothingHeld_Throws() { CheckThrow(typeof(InvalidOperationException)); new LocalizedLine().Write(); } public void AfterAClear_Throws() { var held = new LocalizedLine(); held.Set(Boxes(1234), new LocaleBehavior()); held.Clear(); CheckThrow(typeof(InvalidOperationException)); held.Write(); } } } }