Example #1
0
        public void TryParseDecimal_FromString()
        {
            var invalidValue1 = double.MaxValue.ToString(CultureInfo.InvariantCulture);
            var invalidValue2 = double.MinValue.ToString(CultureInfo.InvariantCulture);

            #region full method
            var success = ConvertibleStringValue.TryParseDecimal(DefaultValue,
                                                                 BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion(),
                                                                 BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                                 BasePrimitivesExtensions.GetCurrentCulture());

            var invalid1 = invalidValue1.TryParseDecimal(DefaultValue,
                                                         BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion(),
                                                         BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                         BasePrimitivesExtensions.GetCurrentCulture());

            var invalid2 = invalidValue2.TryParseDecimal(DefaultValue,
                                                         BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion(),
                                                         BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                         BasePrimitivesExtensions.GetCurrentCulture());

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting string to decimal");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting string to decimal");
            #endregion

            #region without allowZero param
            success = ConvertibleStringValue.TryParseDecimal(DefaultValue,
                                                             BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                             BasePrimitivesExtensions.GetCurrentCulture());

            invalid1 = invalidValue1.TryParseDecimal(DefaultValue,
                                                     BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                     BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseDecimal(DefaultValue,
                                                     BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                     BasePrimitivesExtensions.GetCurrentCulture());

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting string to decimal");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting string to decimal");
            #endregion

            #region without allowZero and DefaultValue params
            success = ConvertibleStringValue.TryParseDecimal(BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                             BasePrimitivesExtensions.GetCurrentCulture());

            invalid1 = invalidValue1.TryParseDecimal(BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                     BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseDecimal(BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                     BasePrimitivesExtensions.GetCurrentCulture());

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting string to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting string to decimal");
            #endregion

            #region without number style and culture params
            success = ConvertibleStringValue.TryParseDecimal(DefaultValue,
                                                             BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion());

            invalid1 = invalidValue1.TryParseDecimal(DefaultValue,
                                                     BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion());

            invalid2 = invalidValue2.TryParseDecimal(DefaultValue,
                                                     BasePrimitivesExtensions.GetDefaultDecimalAllowDefaultConversion());

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting string to decimal");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting string to decimal");
            #endregion

            #region without number style, culture and allowZero params
            success  = ConvertibleStringValue.TryParseDecimal(DefaultValue);
            invalid1 = invalidValue1.TryParseDecimal(DefaultValue);
            invalid2 = invalidValue2.TryParseDecimal(DefaultValue);

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting string to decimal");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting string to decimal");
            #endregion

            #region simple conversion
            success  = ConvertibleStringValue.TryParseDecimal();
            invalid1 = invalidValue1.TryParseDecimal();
            invalid2 = invalidValue2.TryParseDecimal();

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting string to decimal");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting string to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting string to decimal");
            #endregion
        }