private void AssertValueTypeConverts <T>(params T[] values) where T : struct { // Common factory should detect and return nullable converter ArgumentValueConverter <T> converter = ArgumentValueConverter.CreateValueConverter <T>(); Assert.AreEqual(typeof(ArgumentValueConverter.ValueTypeConverter <T>), converter.GetType(), "Did not create a pass through converter"); // Loop over the sample values foreach (T value in values) { T result = converter.ConvertFrom(value); Assert.AreEqual(value, result, "Passthrough<" + typeof(T).Name + " failed to convert"); } // A null should convert to default(T) object valueFromNull = converter.ConvertFrom(null); Assert.IsNotNull(valueFromNull, "Value type conversion from null cannot be null"); Assert.AreEqual(default(T), valueFromNull, "Value type conversion from null should be default(T)"); ExceptionAssert.ThrowsInvalidOperation( "Attempting to convert a string to a value type should throw", () => converter.ConvertFrom("hello") ); }
private void AssertReferenceTypeConverts <T>(params T[] values) { // Common factory should detect and return nullable converter ArgumentValueConverter <T> converter = ArgumentValueConverter.CreateValueConverter <T>(); Assert.AreEqual(typeof(ArgumentValueConverter.ReferenceTypeConverter <T>), converter.GetType(), "Did not create a pass through converter"); T result; // Loop over the sample values foreach (T value in values) { result = converter.ConvertFrom(value); Assert.AreEqual(value, result, "Passthrough<" + typeof(T).Name + " failed to convert"); } // A reference type should be able to convert null result = converter.ConvertFrom(null); Assert.IsNull(result, "Converting reference type " + typeof(T).Name + " failed to convert null"); }