public void TryParseShort_FromObject() { var objectValid = (object)1; var objectInvalid = (object)"a"; Assert.AreEqual(objectValid.TryParseShort(), Int16.Parse(objectValid.ToString()), "Error converting object value to int16"); Assert.AreEqual(objectInvalid.TryParseShort(), BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting object value to int16"); Assert.AreEqual(objectValid.TryParseShort(2), Int16.Parse(objectValid.ToString()), "Error converting object value to int16"); Assert.AreEqual(objectInvalid.TryParseShort(2), 2, "Error converting object value to int16"); }
public void TryParseShort_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.TryParseShort(DefaultValue); var invalid1 = invalidValue1.TryParseShort(DefaultValue); var invalid2 = invalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting float to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting float to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting float to short"); #endregion #region float - without DefaultValue param success = ConvertibleFloatValue.TryParseShort(); invalid1 = invalidValue1.TryParseShort(); invalid2 = invalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting float to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting float to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting float to short"); #endregion #region float? - full method success = ConvertibleNullableFloatValue.TryParseShort(DefaultValue); invalid1 = nullableInvalidValue1.TryParseShort(DefaultValue); invalid2 = nullableInvalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting float? to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting float? to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting float? to short"); #endregion #region float? - without DefaultValue param success = ConvertibleNullableFloatValue.TryParseShort(); invalid1 = nullableInvalidValue1.TryParseShort(); invalid2 = nullableInvalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting float? to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting float? to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting float? to short"); #endregion }
public void TryParseShort_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.TryParseShort(DefaultValue); var invalid1 = invalidValue1.TryParseShort(DefaultValue); var invalid2 = invalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting double to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting double to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting double to short"); #endregion #region double - without DefaultValue param success = ConvertibleDoubleValue.TryParseShort(); invalid1 = invalidValue1.TryParseShort(); invalid2 = invalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting double to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting double to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting double to short"); #endregion #region double? - full method success = ConvertibleNullableDoubleValue.TryParseShort(DefaultValue); invalid1 = nullableInvalidValue1.TryParseShort(DefaultValue); invalid2 = nullableInvalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting double? to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting double? to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting double? to short"); #endregion #region double? - without DefaultValue param success = ConvertibleNullableDoubleValue.TryParseShort(); invalid1 = nullableInvalidValue1.TryParseShort(); invalid2 = nullableInvalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting double? to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting double? to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting double? to short"); #endregion }
public void TryParseShort_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.TryParseShort(DefaultValue); var invalid1 = invalidValue1.TryParseShort(DefaultValue); var invalid2 = invalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting long to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting long to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting long to short"); #endregion #region long - without DefaultValue param success = ConvertibleLongValue.TryParseShort(); invalid1 = invalidValue1.TryParseShort(); invalid2 = invalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting long to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting long to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting long to short"); #endregion #region long? - full method success = ConvertibleNullableLongValue.TryParseShort(DefaultValue); invalid1 = nullableInvalidValue1.TryParseShort(DefaultValue); invalid2 = nullableInvalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting long? to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting long? to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting long? to short"); #endregion #region long? - without DefaultValue param success = ConvertibleNullableLongValue.TryParseShort(); invalid1 = nullableInvalidValue1.TryParseShort(); invalid2 = nullableInvalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting long? to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting long? to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting long? to short"); #endregion }
public void TryParseShort_NullableShort() { #region full method var success = ConvertibleNullableShortValue.TryParseShort(DefaultValue); var invalid = ((short?)null).TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting nullable short to short"); Assert.AreEqual(invalid, DefaultValue, "Error converting nullable short to short"); #endregion #region simple method success = ConvertibleNullableShortValue.TryParseShort(); invalid = ((short?)null).TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting nullable short to short"); Assert.AreEqual(invalid, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting nullable short to short"); #endregion }
public void TryParseShort_FromByteAndNullableByte() { #region byte - full method var success = ConvertibleByteValue.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting byte to short"); #endregion #region byte? - full method success = ConvertibleNullableByteValue.TryParseShort(DefaultValue); var invalid1 = ((byte?)null).TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting byte? to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting byte? to short"); #endregion #region byte? - without DefaultValue param success = ConvertibleNullableByteValue.TryParseShort(); invalid1 = ((byte?)null).TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting byte? to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting byte? to short"); #endregion }
public void TryParseShortArray() { const string successValue1 = "10"; const string successValue2 = "20"; var errorValue1 = long.MaxValue.ToString(CultureInfo.InvariantCulture); var errorValue2 = long.MinValue.ToString(CultureInfo.InvariantCulture); var successByteValues = new[] { successValue1.TryParseShort(), successValue2.TryParseShort() }; 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).TryParseShortArray(null, false, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); var falseConversions = string.Join(",", errorValues).TryParseShortArray(null, false, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(), "Error converting string to short array"); var mixedConversions = string.Join(",", mixedValues).TryParseShortArray(null, false, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(mixedConversions.Count() == successByteValues.Count(), "Error converting string to short array"); #endregion #region partial method (with number format), without default successConversion = string.Join(",", successValues).TryParseShortArray(null, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); falseConversions = string.Join(",", errorValues).TryParseShortArray(null, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); mixedConversions = string.Join(",", mixedValues).TryParseShortArray(null, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to short array"); #endregion #region simple method (with number format) successConversion = string.Join(",", successValues).TryParseShortArray( BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); falseConversions = string.Join(",", errorValues).TryParseShortArray( BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); mixedConversions = string.Join(",", mixedValues).TryParseShortArray( BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to short array"); #endregion #region full method (without number format), without default, dont return defult conversion successConversion = string.Join(",", successValues).TryParseShortArray(null, false); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); falseConversions = string.Join(",", errorValues).TryParseShortArray(null, false); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); mixedConversions = string.Join(",", mixedValues).TryParseShortArray(null, false); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to short array"); #endregion #region partial method (without number format), without default successConversion = string.Join(",", successValues).TryParseShortArray(null); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); falseConversions = string.Join(",", errorValues).TryParseShortArray(null); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); mixedConversions = string.Join(",", mixedValues).TryParseShortArray(null); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to short array"); #endregion #region simple method (without number format) successConversion = string.Join(",", successValues).TryParseShortArray(); Assert.IsNotNull(successConversion, "Error converting string to short array"); Assert.IsTrue(successConversion.Any(), "Error converting string to short array"); Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to short array"); Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to short array"); falseConversions = string.Join(",", errorValues).TryParseShortArray(); Assert.IsNotNull(falseConversions, "Error converting string to short array"); Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); mixedConversions = string.Join(",", mixedValues).TryParseShortArray(); Assert.IsNotNull(mixedConversions, "Error converting string to short array"); Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultShortConversionValue())), "Error converting string to short array"); Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to short array"); #endregion }
public void TryParseShort_FromString() { var invalidValue1 = long.MaxValue.ToString(CultureInfo.InvariantCulture); var invalidValue2 = long.MinValue.ToString(CultureInfo.InvariantCulture); #region full method var success = ConvertibleStringValue.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(), BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); var invalid1 = invalidValue1.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(), BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); var invalid2 = invalidValue2.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion(), BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting string to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting string to short"); #endregion #region without allowZero param success = ConvertibleStringValue.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); invalid1 = invalidValue1.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); invalid2 = invalidValue2.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting string to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting string to short"); #endregion #region without allowZero and DefaultValue params success = ConvertibleStringValue.TryParseShort(BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); invalid1 = invalidValue1.TryParseShort(BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); invalid2 = invalidValue2.TryParseShort(BasePrimitivesExtensions.GetDefaultShortNumberStyle(), BasePrimitivesExtensions.GetCurrentCulture()); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short"); #endregion #region without number style and culture params success = ConvertibleStringValue.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion()); invalid1 = invalidValue1.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion()); invalid2 = invalidValue2.TryParseShort(DefaultValue, BasePrimitivesExtensions.GetDefaultShortAllowDefaultConversion()); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting string to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting string to short"); #endregion #region without number style, culture and allowZero params success = ConvertibleStringValue.TryParseShort(DefaultValue); invalid1 = invalidValue1.TryParseShort(DefaultValue); invalid2 = invalidValue2.TryParseShort(DefaultValue); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, DefaultValue, "Error converting string to short"); Assert.AreEqual(invalid2, DefaultValue, "Error converting string to short"); #endregion #region simple conversion success = ConvertibleStringValue.TryParseShort(); invalid1 = invalidValue1.TryParseShort(); invalid2 = invalidValue2.TryParseShort(); Assert.AreEqual(success, ConvertibleShortValue, "Error converting string to short"); Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short"); Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultShortConversionValue(), "Error converting string to short"); #endregion }