using System; using Glass.Text; using UnitTestSharp; namespace Glass.UnitTests.Text { public class PluralOperandsTests : TestFixture { public class CtorTests : TestFixture { public void KeepsEveryOperand() { var operands = new PluralOperands(1.5m, 1m, 1, 1, 5, 5, 3); CheckEqual(1.5m, operands.N); CheckEqual(1m, operands.I); CheckEqual(1, operands.V); CheckEqual(1, operands.W); CheckEqual(5L, operands.F); CheckEqual(5L, operands.T); CheckEqual(3, operands.C); } } public class OfWholeTests : TestFixture { public void AWholeNumber_HasNoDecimals() { PluralOperands operands = PluralOperands.OfWhole(42); CheckEqual(42m, operands.N); CheckEqual(42m, operands.I); CheckEqual(0, operands.V); CheckEqual(0, operands.W); CheckEqual(0L, operands.F); CheckEqual(0L, operands.T); CheckEqual(0, operands.C); } public void TheLargestMagnitude_IsKept() { CheckEqual((decimal)ulong.MaxValue, PluralOperands.OfWhole(ulong.MaxValue).I); } } public class OfDecimalTests : TestFixture { public void AWholeNumberShownWithADecimal_KeepsItsVisibleZero() { PluralOperands operands = PluralOperands.OfDecimal(1.0m, 1); CheckEqual(1m, operands.N); CheckEqual(1m, operands.I); CheckEqual(1, operands.V); CheckEqual(0, operands.W); CheckEqual(0L, operands.F); CheckEqual(0L, operands.T); } public void TrailingZeros_CountInVAndFButNotInWAndT() { PluralOperands operands = PluralOperands.OfDecimal(1.50m, 2); CheckEqual(2, operands.V); CheckEqual(1, operands.W); CheckEqual(50L, operands.F); CheckEqual(5L, operands.T); } public void LeadingZerosOfTheFraction_AreKeptInTheCountsButNotTheDigits() { PluralOperands operands = PluralOperands.OfDecimal(12.05m, 2); CheckEqual(12m, operands.I); CheckEqual(2, operands.V); CheckEqual(2, operands.W); CheckEqual(5L, operands.F); CheckEqual(5L, operands.T); } public void NineDecimals_AreAllowed() { PluralOperands operands = PluralOperands.OfDecimal(0.000000005m, 9); CheckEqual(9, operands.V); CheckEqual(9, operands.W); CheckEqual(5L, operands.F); } public void NoDecimals_HasNoFraction() { PluralOperands operands = PluralOperands.OfDecimal(7m, 0); CheckEqual(0, operands.V); CheckEqual(0L, operands.F); } public void TheNumberItself_IsN() { CheckEqual(12.34m, PluralOperands.OfDecimal(12.34m, 2).N); } public void ANegativeNumber_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); PluralOperands.OfDecimal(-1m, 1); } public void TenDecimals_Throw() { CheckThrow(typeof(ArgumentOutOfRangeException)); PluralOperands.OfDecimal(1m, 10); } public void ANegativeDecimalCount_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); PluralOperands.OfDecimal(1m, -1); } } public class TryOfTests : TestFixture { public void ACount_IsWhole() { CheckTrue(PluralOperands.TryOf(Styles.Count(1234), out PluralOperands operands)); CheckEqual(1234m, operands.N); CheckEqual(0, operands.V); } public void APlainNumber_IsWhole() { CheckTrue(PluralOperands.TryOf(Styles.Plain(7), out PluralOperands operands)); CheckEqual(7m, operands.I); } public void ANegativeNumber_UsesItsMagnitude() { CheckTrue(PluralOperands.TryOf(Styles.Count(-5), out PluralOperands operands)); CheckEqual(5m, operands.N); } public void TheMostNegativeNumber_UsesItsMagnitude() { CheckTrue(PluralOperands.TryOf(Styles.Count(long.MinValue), out PluralOperands operands)); CheckEqual(9223372036854775808m, operands.N); } public void AFixedNumber_IsRoundedAsItIsShown() { CheckTrue(PluralOperands.TryOf(Styles.Fixed(1.005, 2), out PluralOperands operands)); CheckEqual(1.01m, operands.N); CheckEqual(2, operands.V); CheckEqual(1L, operands.F); } public void AFixedNumberThatShowsAZeroDecimal_KeepsIt() { CheckTrue(PluralOperands.TryOf(Styles.Fixed(1, 1), out PluralOperands operands)); CheckEqual(1m, operands.I); CheckEqual(1, operands.V); CheckEqual(0, operands.W); } public void ANegativeFixedNumber_UsesItsMagnitude() { CheckTrue(PluralOperands.TryOf(Styles.Fixed(-2.5, 1), out PluralOperands operands)); CheckEqual(2.5m, operands.N); } public void AFixedNumberThatRoundsToZero_IsZero() { CheckTrue(PluralOperands.TryOf(Styles.Fixed(-0.001, 2), out PluralOperands operands)); CheckEqual(0m, operands.N); CheckEqual(2, operands.V); } public void AFixedNumberShownWithNoDecimals_HasNone() { CheckTrue(PluralOperands.TryOf(Styles.Fixed(2.5, 0), out PluralOperands operands)); CheckEqual(3m, operands.N); CheckEqual(0, operands.V); } public void NaN_HasNoOperands() { CheckFalse(PluralOperands.TryOf(Styles.Fixed(double.NaN, 2), out _)); } public void Infinities_HaveNoOperands() { CheckFalse(PluralOperands.TryOf(Styles.Fixed(double.PositiveInfinity, 2), out _)); CheckFalse(PluralOperands.TryOf(Styles.Fixed(double.NegativeInfinity, 2), out _)); } public void AValueBeyondWhatIsShown_HasNoOperands() { CheckFalse(PluralOperands.TryOf(Styles.Fixed(1e30, 2), out _)); } public void AScientificNumber_Throws() { CheckThrow(typeof(ArgumentException)); PluralOperands.TryOf(Styles.Scientific(1234, 3), out _); } public void AShortExponentNumber_Throws() { CheckThrow(typeof(ArgumentException)); PluralOperands.TryOf(Styles.ShortExponent(1234, 3), out _); } public void AString_Throws() { CheckThrow(typeof(ArgumentException)); PluralOperands.TryOf("abc", out _); } } } }