예제 #1
0
        public void TryParseShort_FromString()
        {
            var invalidValue1 = long.MaxValue.ToString(CultureInfo.InvariantCulture);
            var invalidValue2 = long.MinValue.ToString(CultureInfo.InvariantCulture);

            #region full method
            var success = ConvertibleStringValue.TryParseShort(DefaultValue,
                                                               BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(),
                                                               BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                               BasePrimitivesExtensions.GetCurrentCulture());

            var invalid1 = invalidValue1.TryParseShort(DefaultValue,
                                                       BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(),
                                                       BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                       BasePrimitivesExtensions.GetCurrentCulture());

            var invalid2 = invalidValue2.TryParseShort(DefaultValue,
                                                       BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(),
                                                       BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                       BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseShort(DefaultValue,
                                                   BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                   BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseShort(DefaultValue,
                                                   BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                   BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseShort(BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                   BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseShort(BasePrimitivesExtensions.GetDefaultShortNumberStyle(),
                                                   BasePrimitivesExtensions.GetCurrentCulture());

            Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short");
            #endregion

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

            invalid1 = invalidValue1.TryParseShort(DefaultValue,
                                                   BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion());

            invalid2 = invalidValue2.TryParseShort(DefaultValue,
                                                   BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion());

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

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

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

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

            Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short");
            #endregion
        }
        public void TryParseDouble_FromString()
        {
            const string invalidValue1 = "";
            const string invalidValue2 = "b";

            #region full method
            var success = ConvertibleStringValue.TryParseDouble(DefaultValue,
                                                                BasePrimitivesExtensions.GetDefaultDoubleAllowDefaultConversion(),
                                                                BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                                BasePrimitivesExtensions.GetCurrentCulture());

            var invalid1 = invalidValue1.TryParseDouble(DefaultValue,
                                                        BasePrimitivesExtensions.GetDefaultDoubleAllowDefaultConversion(),
                                                        BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                        BasePrimitivesExtensions.GetCurrentCulture());

            var invalid2 = invalidValue2.TryParseDouble(DefaultValue,
                                                        BasePrimitivesExtensions.GetDefaultDoubleAllowDefaultConversion(),
                                                        BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                        BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseDouble(DefaultValue,
                                                    BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                    BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseDouble(DefaultValue,
                                                    BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                    BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseDouble(BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                    BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseDouble(BasePrimitivesExtensions.GetDefaultDoubleNumberStyle(),
                                                    BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseDouble(DefaultValue,
                                                    BasePrimitivesExtensions.GetDefaultDoubleAllowDefaultConversion());

            invalid2 = invalidValue2.TryParseDouble(DefaultValue,
                                                    BasePrimitivesExtensions.GetDefaultDoubleAllowDefaultConversion());

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

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

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

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

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