Example #1
0
        public void TryParseLong_FromObject()
        {
            var objectValid   = (object)1;
            var objectInvalid = (object)"a";

            Assert.AreEqual(objectValid.TryParseLong(), Int64.Parse(objectValid.ToString()), "Error converting object value to int64");
            Assert.AreEqual(objectInvalid.TryParseLong(), BasePrimitivesExtensions.GetDefaultLongConversionValue(), "Error converting object value to int64");

            Assert.AreEqual(objectValid.TryParseLong(2), Int64.Parse(objectValid.ToString()), "Error converting object value to int64");
            Assert.AreEqual(objectInvalid.TryParseLong(2), 2, "Error converting object value to int64");
        }
Example #2
0
        public void TryParseLong_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.TryParseLong(DefaultValue);
            var invalid1 = invalidValue1.TryParseLong(DefaultValue);
            var invalid2 = invalidValue2.TryParseLong(DefaultValue);

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

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

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

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

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

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

            Assert.AreEqual(success, ConvertibleLongValue, "Error converting float? to long");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultLongConversionValue(), "Error converting float? to long");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultLongConversionValue(), "Error converting float? to long");
            #endregion
        }
Example #3
0
        public void TryParseLong_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.TryParseLong(DefaultValue);
            var invalid1 = invalidValue1.TryParseLong(DefaultValue);
            var invalid2 = invalidValue2.TryParseLong(DefaultValue);

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

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

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

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

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

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

            Assert.AreEqual(success, ConvertibleLongValue, "Error converting double? to long");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultLongConversionValue(), "Error converting double? to long");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultLongConversionValue(), "Error converting double? to long");
            #endregion
        }
Example #4
0
        public void TryParseLongArray()
        {
            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.TryParseLong(), successValue2.TryParseLong() };
            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).TryParseLongArray(null, false,
                                                                                      BasePrimitivesExtensions.GetDefaultIntNumberStyle(),
                                                                                      BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to long array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to long array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to long array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to long array");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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