Пример #1
0
        public void TryParseLong_FromString()
        {
            var invalidValue1 = double.MaxValue.ToString(CultureInfo.InvariantCulture);
            var invalidValue2 = double.MinValue.ToString(CultureInfo.InvariantCulture);

            #region full method
            var success = ConvertibleStringValue.TryParseLong(DefaultValue,
                                                              BasePrimitivesExtensions.GetDefaultIntAllowDefaultConversion(),
                                                              BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                              BasePrimitivesExtensions.GetCurrentCulture());

            var invalid1 = invalidValue1.TryParseLong(DefaultValue,
                                                      BasePrimitivesExtensions.GetDefaultIntAllowDefaultConversion(),
                                                      BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                      BasePrimitivesExtensions.GetCurrentCulture());

            var invalid2 = invalidValue2.TryParseLong(DefaultValue,
                                                      BasePrimitivesExtensions.GetDefaultIntAllowDefaultConversion(),
                                                      BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                      BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseLong(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseLong(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseLong(BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseLong(BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseLong(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultIntAllowDefaultConversion());

            invalid2 = invalidValue2.TryParseLong(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultIntAllowDefaultConversion());

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

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

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

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

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