public void TryParseDouble_FromDecimalAndNullableDecimal()
        {
            #region double - full method
            var success = ConvertibleDecimalValue.TryParseDouble();

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

            #region double - without DefaultValue param
            success = ConvertibleDecimalValue.TryParseDouble();

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

            #region double? - full method
            success = ConvertibleNullableDecimalValue.TryParseDouble(DefaultValue);
            var invalid1 = ((decimal?)null).TryParseDouble(DefaultValue);

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

            #region double? - without DefaultValue param
            success  = ConvertibleNullableDecimalValue.TryParseDouble();
            invalid1 = ((decimal?)null).TryParseDouble();

            Assert.AreEqual(success, ConvertibleDoubleValue, "Error converting double? to double");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting double? to double");
            #endregion
        }
Пример #2
0
        public void TryParseDecimal_FromIntAndNullableInt()
        {
            #region decimal - full method
            var success = ConvertibleIntValue.TryParseDecimal(DefaultValue);

            Assert.AreEqual(success, ConvertibleDecimalValueInt, "Error converting decimal to decimal");
            #endregion

            #region decimal - without DefaultValue param
            success = ConvertibleIntValue.TryParseDecimal();

            Assert.AreEqual(success, ConvertibleDecimalValueInt, "Error converting decimal to decimal");
            #endregion

            #region decimal? - full method
            success = ConvertibleNullableIntValue.TryParseDecimal(DefaultValue);
            var invalid1 = ((int?)null).TryParseDecimal(DefaultValue);

            Assert.AreEqual(success, ConvertibleDecimalValueInt, "Error converting decimal? to decimal");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting decimal? to decimal");
            #endregion

            #region decimal? - without DefaultValue param
            success  = ConvertibleNullableIntValue.TryParseDecimal();
            invalid1 = ((int?)null).TryParseDecimal();

            Assert.AreEqual(success, ConvertibleDecimalValueInt, "Error converting decimal? to decimal");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting decimal? to decimal");
            #endregion
        }
        public void TryParseDouble_FromLongAndNullableLong()
        {
            #region long - full method
            var success = ConvertibleLongValue.TryParseDouble(DefaultValue);

            Assert.AreEqual(success, ConvertibleDoubleValueInt, "Error converting long to double");
            #endregion

            #region long - without DefaultValue param
            success = ConvertibleLongValue.TryParseDouble();

            Assert.AreEqual(success, ConvertibleDoubleValueInt, "Error converting long to double");
            #endregion

            #region long? - full method
            success = ConvertibleNullableLongValue.TryParseDouble(DefaultValue);
            var invalid1 = ((long?)null).TryParseDouble(DefaultValue);

            Assert.AreEqual(success, ConvertibleDoubleValueInt, "Error converting long? to double");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting long? to double");
            #endregion

            #region long? - without DefaultValue param
            success  = ConvertibleNullableLongValue.TryParseDouble();
            invalid1 = ((long?)null).TryParseDouble();

            Assert.AreEqual(success, ConvertibleDoubleValueInt, "Error converting long? to double");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting long? to double");
            #endregion
        }
Пример #4
0
        public void TryParseFloat_FromIntAndNullableInt()
        {
            #region float - full method
            var success = ConvertibleIntValue.TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValueInt, "Error converting float to float");
            #endregion

            #region float - without DefaultValue param
            success = ConvertibleIntValue.TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValueInt, "Error converting float to float");
            #endregion

            #region float? - full method
            success = ConvertibleNullableIntValue.TryParseFloat(DefaultValue);
            var invalid1 = ((int?)null).TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValueInt, "Error converting float? to float");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting float? to float");
            #endregion

            #region float? - without DefaultValue param
            success  = ConvertibleNullableIntValue.TryParseFloat();
            invalid1 = ((int?)null).TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValueInt, "Error converting float? to float");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting float? to float");
            #endregion
        }
Пример #5
0
        public void TryParseInt_FromObject()
        {
            var objectValid   = (object)1;
            var objectInvalid = (object)"a";

            Assert.AreEqual(objectValid.TryParseInt(), Int32.Parse(objectValid.ToString()), "Error converting object value to int32");
            Assert.AreEqual(objectInvalid.TryParseInt(), BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting object value to int32");

            Assert.AreEqual(objectValid.TryParseInt(2), Int32.Parse(objectValid.ToString()), "Error converting object value to int32");
            Assert.AreEqual(objectInvalid.TryParseInt(2), 2, "Error converting object value to int32");
        }
Пример #6
0
        public void TryParseInt_FromFloatAndNullableFloat()
        {
            const float invalidValue1         = float.MaxValue;
            float?      nullableInvalidValue1 = float.MaxValue;
            const float invalidValue2         = float.MinValue;
            float?      nullableInvalidValue2 = float.MinValue;

            #region float - full method
            var success  = ConvertibleFloatValue.TryParseInt(DefaultValue);
            var invalid1 = invalidValue1.TryParseInt(DefaultValue);
            var invalid2 = invalidValue2.TryParseInt(DefaultValue);

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting float to int");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting float to int");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting float to int");
            #endregion

            #region float - without DefaultValue param
            success  = ConvertibleFloatValue.TryParseInt();
            invalid1 = invalidValue1.TryParseInt();
            invalid2 = invalidValue2.TryParseInt();

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting float to int");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting float to int");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting float to int");
            #endregion

            #region float? - full method
            success  = ConvertibleNullableFloatValue.TryParseInt(DefaultValue);
            invalid1 = nullableInvalidValue1.TryParseInt(DefaultValue);
            invalid2 = nullableInvalidValue2.TryParseInt(DefaultValue);

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting float? to int");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting float? to int");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting float? to int");
            #endregion

            #region float? - without DefaultValue param
            success  = ConvertibleNullableFloatValue.TryParseInt();
            invalid1 = nullableInvalidValue1.TryParseInt();
            invalid2 = nullableInvalidValue2.TryParseInt();

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting float? to int");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting float? to int");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting float? to int");
            #endregion
        }
Пример #7
0
        public void TryParseInt_FromDoubleAndNullableDouble()
        {
            const double invalidValue1         = double.MaxValue;
            double?      nullableInvalidValue1 = double.MaxValue;
            const double invalidValue2         = double.MinValue;
            double?      nullableInvalidValue2 = double.MinValue;

            #region double - full method
            var success  = ConvertibleDoubleValue.TryParseInt(DefaultValue);
            var invalid1 = invalidValue1.TryParseInt(DefaultValue);
            var invalid2 = invalidValue2.TryParseInt(DefaultValue);

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

            #region double - without DefaultValue param
            success  = ConvertibleDoubleValue.TryParseInt();
            invalid1 = invalidValue1.TryParseInt();
            invalid2 = invalidValue2.TryParseInt();

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

            #region double? - full method
            success  = ConvertibleNullableDoubleValue.TryParseInt(DefaultValue);
            invalid1 = nullableInvalidValue1.TryParseInt(DefaultValue);
            invalid2 = nullableInvalidValue2.TryParseInt(DefaultValue);

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting double? to int");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting double? to int");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting double? to int");
            #endregion

            #region double? - without DefaultValue param
            success  = ConvertibleNullableDoubleValue.TryParseInt();
            invalid1 = nullableInvalidValue1.TryParseInt();
            invalid2 = nullableInvalidValue2.TryParseInt();

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting double? to int");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting double? to int");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting double? to int");
            #endregion
        }
Пример #8
0
        public void TryParseInt_FromLongAndNullableLong()
        {
            const long invalidValue1         = long.MaxValue;
            long?      nullableInvalidValue1 = long.MaxValue;
            const long invalidValue2         = long.MinValue;
            long?      nullableInvalidValue2 = long.MinValue;

            #region long - full method
            var success  = ConvertibleLongValue.TryParseInt(DefaultValue);
            var invalid1 = invalidValue1.TryParseInt(DefaultValue);
            var invalid2 = invalidValue2.TryParseInt(DefaultValue);

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

            #region long - without DefaultValue param
            success  = ConvertibleLongValue.TryParseInt();
            invalid1 = invalidValue1.TryParseInt();
            invalid2 = invalidValue2.TryParseInt();

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

            #region long? - full method
            success  = ConvertibleNullableLongValue.TryParseInt(DefaultValue);
            invalid1 = nullableInvalidValue1.TryParseInt(DefaultValue);
            invalid2 = nullableInvalidValue2.TryParseInt(DefaultValue);

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting long? to int");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting long? to int");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting long? to int");
            #endregion

            #region long? - without DefaultValue param
            success  = ConvertibleNullableLongValue.TryParseInt();
            invalid1 = nullableInvalidValue1.TryParseInt();
            invalid2 = nullableInvalidValue2.TryParseInt();

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting long? to int");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting long? to int");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting long? to int");
            #endregion
        }
        public void TryParseDouble_NullableDouble()
        {
            #region full method
            var success = ConvertibleNullableDoubleValue.TryParseDouble(DefaultValue);
            var invalid = ((double?)null).TryParseDouble(DefaultValue);

            Assert.AreEqual(success, ConvertibleDoubleValue, "Error converting nullable double to double");
            Assert.AreEqual(invalid, DefaultValue, "Error converting nullable double to double");
            #endregion

            #region simple method
            success = ConvertibleNullableDoubleValue.TryParseDouble();
            invalid = ((double?)null).TryParseDouble();

            Assert.AreEqual(success, ConvertibleDoubleValue, "Error converting nullable double to double");
            Assert.AreEqual(invalid, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting nullable double to double");
            #endregion
        }
Пример #10
0
        public void TryParseLong_FromShortAndNullableShort()
        {
            #region byte - full method
            var success = ConvertibleShortValue.TryParseLong();
            Assert.AreEqual(success, ConvertibleLongValue, "Error converting byte to long");
            #endregion

            #region byte? - full method
            success = ConvertibleNullableShortValue.TryParseLong(DefaultValue);
            var invalid1 = ((short?)null).TryParseLong(DefaultValue);

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

            #region byte? - without DefaultValue param
            success  = ConvertibleNullableShortValue.TryParseLong();
            invalid1 = ((short?)null).TryParseLong();

            Assert.AreEqual(success, ConvertibleLongValue, "Error converting byte? to long");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting byte? to long");
            #endregion
        }
Пример #11
0
        public void TryParseInt_FromByteAndNullableByte()
        {
            #region byte - full method
            var success = ConvertibleByteValue.TryParseInt();
            Assert.AreEqual(success, ConvertibleIntValue, "Error converting byte to int");
            #endregion

            #region byte? - full method
            success = ConvertibleNullableByteValue.TryParseInt(DefaultValue);
            var invalid1 = ((byte?)null).TryParseInt(DefaultValue);

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting byte? to int");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting byte? to int");
            #endregion

            #region byte? - without DefaultValue param
            success  = ConvertibleNullableByteValue.TryParseInt();
            invalid1 = ((byte?)null).TryParseInt();

            Assert.AreEqual(success, ConvertibleIntValue, "Error converting byte? to int");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultIntConversionValue(), "Error converting byte? to int");
            #endregion
        }
Пример #12
0
        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
        }
Пример #13
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
        }
Пример #14
0
        public void TryParseIntArray()
        {
            const string successValue1 = "10";
            const string successValue2 = "20";
            var          errorValue1   = double.MaxValue.ToString(CultureInfo.InvariantCulture);
            var          errorValue2   = double.MinValue.ToString(CultureInfo.InvariantCulture);

            var successByteValues = new[] { successValue1.TryParseInt(), successValue2.TryParseInt() };
            var successValues     = new[] { successValue1, successValue2 };
            var errorValues       = new[] { errorValue1, errorValue2 };
            var mixedValues       = new[] { successValue1, successValue2, errorValue1, errorValue2 };

            #region full method (with number format), without default, dont return defult conversion
            var successConversion = string.Join(",", successValues).TryParseIntArray(null, false,
                                                                                     BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                                     BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            var falseConversions = string.Join(",", errorValues).TryParseIntArray(null, false,
                                                                                  BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                                  BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(), "Error converting string to int array");

            var mixedConversions = string.Join(",", mixedValues).TryParseIntArray(null, false,
                                                                                  BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                                  BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(mixedConversions.Count() == successByteValues.Count(), "Error converting string to int array");
            #endregion

            #region partial method (with number format), without default
            successConversion = string.Join(",", successValues).TryParseIntArray(null,
                                                                                 BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                                 BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            falseConversions = string.Join(",", errorValues).TryParseIntArray(null,
                                                                              BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                              BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");

            mixedConversions = string.Join(",", mixedValues).TryParseIntArray(null,
                                                                              BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                              BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to int array");
            #endregion

            #region simple method (with number format)
            successConversion = string.Join(",", successValues).TryParseIntArray(
                BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            falseConversions = string.Join(",", errorValues).TryParseIntArray(
                BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");

            mixedConversions = string.Join(",", mixedValues).TryParseIntArray(
                BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to int array");
            #endregion

            #region full method (without number format), without default, dont return defult conversion
            successConversion = string.Join(",", successValues).TryParseIntArray(null, false);
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            falseConversions = string.Join(",", errorValues).TryParseIntArray(null, false);
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");

            mixedConversions = string.Join(",", mixedValues).TryParseIntArray(null, false);
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to int array");
            #endregion

            #region partial method (without number format), without default
            successConversion = string.Join(",", successValues).TryParseIntArray(null);
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            falseConversions = string.Join(",", errorValues).TryParseIntArray(null);
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");

            mixedConversions = string.Join(",", mixedValues).TryParseIntArray(null);
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to int array");
            #endregion

            #region simple method (without number format)
            successConversion = string.Join(",", successValues).TryParseIntArray();
            Assert.IsNotNull(successConversion, "Error converting string to int array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to int array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to int array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to int array");

            falseConversions = string.Join(",", errorValues).TryParseIntArray();
            Assert.IsNotNull(falseConversions, "Error converting string to int array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");

            mixedConversions = string.Join(",", mixedValues).TryParseIntArray();
            Assert.IsNotNull(mixedConversions, "Error converting string to int array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultIntConversionValue())), "Error converting string to int array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to int array");
            #endregion
        }