示例#1
0
        public void TryParseDecimal_FromObject()
        {
            var objectValid   = (object)1;
            var objectInvalid = (object)"a";

            Assert.AreEqual(objectValid.TryParseDecimal(), Decimal.Parse(objectValid.ToString()), "Error converting object value to decimal");
            Assert.AreEqual(objectInvalid.TryParseDecimal(), BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting object value to decimal");

            Assert.AreEqual(objectValid.TryParseDecimal(2), Decimal.Parse(objectValid.ToString()), "Error converting object value to decimal");
            Assert.AreEqual(objectInvalid.TryParseDecimal(2), 2, "Error converting object value to decimal");
        }
示例#2
0
        public void TryParseDecimal_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.TryParseDecimal(DefaultValue);
            var invalid1 = invalidValue1.TryParseDecimal(DefaultValue);
            var invalid2 = invalidValue2.TryParseDecimal(DefaultValue);

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

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

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting float to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting float to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting float to decimal");
            #endregion

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

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

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

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting float? to decimal");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting float? to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting float? to decimal");
            #endregion
        }
示例#3
0
        public void TryParseDecimal_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.TryParseDecimal(DefaultValue);
            var invalid1 = invalidValue1.TryParseDecimal(DefaultValue);
            var invalid2 = invalidValue2.TryParseDecimal(DefaultValue);

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

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

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting double to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting double to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting double to decimal");
            #endregion

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

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

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

            Assert.AreEqual(success, ConvertibleDecimalValue, "Error converting double? to decimal");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting double? to decimal");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultDecimalConversionValue(), "Error converting double? to decimal");
            #endregion
        }
示例#4
0
        public void TryParseDecimalArray()
        {
            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.TryParseDecimal(), successValue2.TryParseDecimal() };
            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).TryParseDecimalArray(null, false,
                                                                                         BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                                                         BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to decimal array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to decimal array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to decimal array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to decimal array");

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

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

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

            falseConversions = string.Join(",", errorValues).TryParseDecimalArray(null,
                                                                                  BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                                                                                  BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to decimal array");
            Assert.IsFalse(falseConversions.Any(d => !d.Equals(BasePrimitivesExtensions.GetDefaultDecimalConversionValue())), "Error converting string to decimal array");

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

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

            falseConversions = string.Join(",", errorValues).TryParseDecimalArray(
                BasePrimitivesExtensions.GetDefaultDecimalNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to decimal array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultDecimalConversionValue())), "Error converting string to decimal array");

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

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

            falseConversions = string.Join(",", errorValues).TryParseDecimalArray(null, false);
            Assert.IsNotNull(falseConversions, "Error converting string to decimal array");
            Assert.IsFalse(falseConversions.Any(), "Error converting string to decimal array");

            mixedConversions = string.Join(",", mixedValues).TryParseDecimalArray(null, false);
            Assert.IsNotNull(mixedConversions, "Error converting string to decimal array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a)), "Error converting string to decimal array");
            Assert.IsTrue(mixedConversions.Count() == successByteValues.Count(), "Error converting string to decimal array");
            #endregion

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

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

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

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

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

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