Exemplo n.º 1
0
        public void TryParseByte_FromObject()
        {
            var objectValid   = (object)1;
            var objectInvalid = (object)"a";

            Assert.AreEqual(objectValid.TryParseByte(), Byte.Parse(objectValid.ToString()), "Error converting object value to byte");
            Assert.AreEqual(objectInvalid.TryParseByte(), BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting object value to byte");

            Assert.AreEqual(objectValid.TryParseByte(2), Byte.Parse(objectValid.ToString()), "Error converting object value to byte");
            Assert.AreEqual(objectInvalid.TryParseByte(2), 2, "Error converting object value to byte");
        }
Exemplo n.º 2
0
        public void TryParseByte_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.TryParseByte(DefaultValue);
            var invalid1 = invalidValue1.TryParseByte(DefaultValue);
            var invalid2 = invalidValue2.TryParseByte(DefaultValue);

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting float to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting float to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting float to byte");
            #endregion

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

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting float? to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting float? to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting float? to byte");
            #endregion
        }
Exemplo n.º 3
0
        public void TryParseByte_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.TryParseByte(DefaultValue);
            var invalid1 = invalidValue1.TryParseByte(DefaultValue);
            var invalid2 = invalidValue2.TryParseByte(DefaultValue);

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting double to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting double to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting double to byte");
            #endregion

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

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting double? to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting double? to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting double? to byte");
            #endregion
        }
Exemplo n.º 4
0
        public void TryParseByte_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.TryParseByte(DefaultValue);
            var invalid1 = invalidValue1.TryParseByte(DefaultValue);
            var invalid2 = invalidValue2.TryParseByte(DefaultValue);

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting long to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting long to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting long to byte");
            #endregion

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

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting long? to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting long? to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting long? to byte");
            #endregion
        }
Exemplo n.º 5
0
        public void TryParseByte_NullableByte()
        {
            #region full method
            var success = ConvertibleNullableByteValue.TryParseByte(DefaultValue);
            var invalid = ((byte?)null).TryParseByte(DefaultValue);

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

            #region simple method
            success = ConvertibleNullableByteValue.TryParseByte();
            invalid = ((byte?)null).TryParseByte();

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting nullable byte to byte");
            Assert.AreEqual(invalid, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting nullable byte to byte");
            #endregion
        }
Exemplo n.º 6
0
        public void TryParseByteArray()
        {
            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.TryParseByte(), successValue2.TryParseByte() };
            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).TryParseByteArray(null, false,
                                                                                      BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                                                      BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to byte array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to byte array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to byte array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to byte array");

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

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

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

            falseConversions = string.Join(",", errorValues).TryParseByteArray(null,
                                                                               BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                                               BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to byte array");
            Assert.IsFalse(falseConversions.Any(f => !f.Equals(BasePrimitivesExtensions.GetDefaultByteConversionValue())), "Error converting string to byte array");

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

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

            falseConversions = string.Join(",", errorValues).TryParseByteArray(
                BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to byte array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultByteConversionValue())), "Error converting string to byte array");

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

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

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

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

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

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

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

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

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

            mixedConversions = string.Join(",", mixedValues).TryParseByteArray();
            Assert.IsNotNull(mixedConversions, "Error converting string to byte array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultByteConversionValue())), "Error converting string to byte array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to byte array");
            #endregion
        }
Exemplo n.º 7
0
        public void TryParseByte_FromString()
        {
            var invalidValue1 = double.MaxValue.ToString(CultureInfo.InvariantCulture);
            var invalidValue2 = double.MinValue.ToString(CultureInfo.InvariantCulture);

            #region full method
            var success = ConvertibleStringValue.TryParseByte(DefaultValue,
                                                              BasePrimitivesExtensions.GetDefaultByteAllowDefaultConversion(),
                                                              BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                              BasePrimitivesExtensions.GetCurrentCulture());

            var invalid1 = invalidValue1.TryParseByte(DefaultValue,
                                                      BasePrimitivesExtensions.GetDefaultByteAllowDefaultConversion(),
                                                      BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                      BasePrimitivesExtensions.GetCurrentCulture());

            var invalid2 = invalidValue2.TryParseByte(DefaultValue,
                                                      BasePrimitivesExtensions.GetDefaultByteAllowDefaultConversion(),
                                                      BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                      BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseByte(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseByte(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

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

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

            invalid1 = invalidValue1.TryParseByte(BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

            invalid2 = invalidValue2.TryParseByte(BasePrimitivesExtensions.GetDefaultByteNumberStyle(),
                                                  BasePrimitivesExtensions.GetCurrentCulture());

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting string to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting string to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting string to byte");
            #endregion

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

            invalid1 = invalidValue1.TryParseByte(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultByteAllowDefaultConversion());

            invalid2 = invalidValue2.TryParseByte(DefaultValue,
                                                  BasePrimitivesExtensions.GetDefaultByteAllowDefaultConversion());

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

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

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

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

            Assert.AreEqual(success, ConvertibleByteValue, "Error converting string to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting string to byte");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultByteConversionValue(), "Error converting string to byte");
            #endregion
        }