private void NullableTestInternal <TFrom, TTo>(int count, bool assertNullConversion) { AdvancedConverter <TFrom, TTo> advancedConverter = AdvancedConverter <TFrom, TTo> .Default; Assert.IsNotNull(advancedConverter); Type fromType = typeof(TFrom); Type toType = typeof(TTo); bool fromIsNullable = fromType.IsNullable(); Type fromNonNullableType = fromIsNullable ? fromType.GetGenericArguments()[0] : typeof(TFrom); // Random test Action <object, int> nullableConverterTest = DelegateHelper.CreateDelegate <Action <object, int> >(this, GetType(), "NullableConverterTest", fromNonNullableType, fromType, toType); nullableConverterTest.Invoke(advancedConverter, count); // Null test if (fromIsNullable) { if (assertNullConversion) { AssertEx.Throws <ArgumentNullException>(delegate { advancedConverter.Convert((TFrom)(object)null); }); } else { advancedConverter.Convert((TFrom)(object)null); } } }
public void ConverterTest() { IAdvancedConverterProvider provider = AdvancedConverterProvider.Default; AdvancedConverter <int, string> intStringAdvancedConverter = provider.GetConverter <int, string>(); Assert.AreEqual("Int32AdvancedConverter", intStringAdvancedConverter.Implementation.GetType().Name); Assert.IsTrue(!(intStringAdvancedConverter.IsRough)); Assert.IsFalse(intStringAdvancedConverter.IsRough); int intValue = 1234; string intToStringValue = intStringAdvancedConverter.Convert(intValue); TestLog.Info("IntToString: {0} {1}", intValue, intToStringValue); Assert.AreEqual(intValue.ToString(), intToStringValue); }
private void EnumTestInternal <TFrom, TTo>() { AdvancedConverter <TFrom, TTo> advancedConverter = AdvancedConverter <TFrom, TTo> .Default; AdvancedConverter <TTo, TFrom> backwardAdvancedConverter = AdvancedConverter <TTo, TFrom> .Default; IInstanceGenerator <TFrom> instanceGenerator = InstanceGeneratorProvider.Default.GetInstanceGenerator <TFrom>(); Assert.IsNotNull(advancedConverter); Assert.IsNotNull(backwardAdvancedConverter); Assert.IsNotNull(instanceGenerator); foreach (TFrom from in instanceGenerator.GetInstances(random, enumTestCount)) { TTo result = advancedConverter.Convert(from); TFrom backwardResult = backwardAdvancedConverter.Convert(result); Assert.AreEqual(from, backwardResult); } }
private void NullableConverterTest <T, TFrom, TTo>(object converterObject, int count) { AdvancedConverter <TFrom, TTo> advancedConverter = (AdvancedConverter <TFrom, TTo>)converterObject; AdvancedConverter <TTo, TFrom> reverseAdvancedConverter = AdvancedConverter <TTo, TFrom> .Default; IInstanceGenerator <T> instanceGenerator = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>(); foreach (T instance in instanceGenerator.GetInstances(random, count)) { TTo result = advancedConverter.Convert((TFrom)(object)instance); if (reverseAdvancedConverter != null) { TFrom backwardResult = reverseAdvancedConverter.Convert(result); if (!advancedConverter.IsRough && !reverseAdvancedConverter.IsRough) { Assert.AreEqual(instance, backwardResult); } } } }
public void OneValueTest <TFrom, TTo>(TFrom value, int count) { AdvancedConverter <TFrom, TTo> advancedConverterTo = null; AdvancedConverter <TTo, TFrom> advancedConverterFrom = null; try { advancedConverterTo = AdvancedConverter <TFrom, TTo> .Default; advancedConverterFrom = AdvancedConverter <TTo, TFrom> .Default; } catch (InvalidOperationException e) { if (UseLog) { TestLog.Info("{0}", e.Message); } } for (int i = 0; i < count; i++) { bool innerLogFlag = UseLog && (i == 0); if (advancedConverterTo != null) { try { TTo convertedValue = advancedConverterTo.Convert(value); if (innerLogFlag) { TestLog.Info("Converting {0} of {1} type. Result: {2} of {3} type", value, typeof(TFrom).GetShortName(), convertedValue, typeof(TTo).GetShortName()); } if (advancedConverterFrom != null) { try { TFrom reconvertedValue = advancedConverterFrom.Convert(convertedValue); if (innerLogFlag) { TestLog.Info("Reconverting {0} of {1} type. Result: {2} of {3} type.", convertedValue, typeof(TTo).GetShortName(), reconvertedValue, typeof(TFrom).GetShortName()); } if ((advancedConverterTo.IsRough)) { if (innerLogFlag) { TestLog.Info("Conversion from {0} to {1} is rough.", typeof(TFrom).GetShortName(), typeof(TTo).GetShortName()); } } else { Assert.AreEqual(value, reconvertedValue, "reconvertedValue"); } } catch (OverflowException e) { if (innerLogFlag) { TestLog.Info(e, "Reconversion of {0} of {1} type to type {2} failed.", convertedValue, typeof(TTo).GetShortName(), typeof(TFrom).GetShortName()); } } catch (ArgumentOutOfRangeException e) { if (innerLogFlag) { TestLog.Info(e, "Reconversion of {0} of {1} type to type {2} failed.", convertedValue, typeof(TTo).GetShortName(), typeof(TFrom).GetShortName()); } } catch (FormatException e) { if (innerLogFlag) { TestLog.Info(e, "Reconversion of {0} of {1} type to type {2} failed.", convertedValue, typeof(TTo).GetShortName(), typeof(TFrom).GetShortName()); } } } } catch (OverflowException e) { if (innerLogFlag) { TestLog.Info(e, "Conversion of {0} of {1} type to type {2} failed.", value, typeof(TFrom).GetShortName(), typeof(TTo).GetShortName()); } } catch (ArgumentOutOfRangeException e) { if (innerLogFlag) { TestLog.Info(e, "Conversion of {0} of {1} type to type {2} failed.", value, typeof(TFrom).GetShortName(), typeof(TTo).GetShortName()); } } catch (FormatException e) { if (innerLogFlag) { TestLog.Info(e, "Conversion of {0} of {1} type to type {2} failed.", value, typeof(TFrom).GetShortName(), typeof(TTo).GetShortName()); } } } else if (innerLogFlag) { TestLog.Info("Conversion from {0} to {1} is not supported.", typeof(TFrom).GetShortName(), typeof(TTo).GetShortName()); } } }