public void Facade_By_IType__List_IType_and_Func_IType() { Assert.AreEqual("success", ((IConverter)BuildRoutine.Converter().By(() => type.of <string>(), (_, _) => "success")).Convert(0, type.of <int>(), type.of <string>())); Assert.AreEqual("success", ((IConverter)BuildRoutine.Converter().By(type.of <string>(), (_, _) => "success")).Convert(0, type.of <int>(), type.of <string>())); Assert.AreEqual("success", ((IConverter)BuildRoutine.Converter().By(new List <IType> { type.of <string>() }, (_, _) => "success")).Convert(0, type.of <int>(), type.of <string>())); }
public void Does_not_cover_void__non_value_types_and_generic_types() { IConverter converter = BuildRoutine.Converter().ToNullable(); Assert.IsEmpty(converter.GetTargetTypes(type.ofvoid())); Assert.IsEmpty(converter.GetTargetTypes(type.of <string>())); Assert.IsEmpty(converter.GetTargetTypes(type.of <int?>())); }
public void Throws_CannotConvertException_when_given_type_cannot_be_converted_to_target_type() { var typeMock = new Mock <IType>(); typeMock.Setup(t => t.AssignableTypes).Returns(new List <IType>()); typeMock.Setup(t => t.Cast(It.IsAny <object>(), type.of <string>())).Throws <InvalidCastException>(); IConverter converter = BuildRoutine.Converter().ByCasting(); Assert.Throws <CannotConvertException>(() => converter.Convert(0, typeMock.Object, type.of <string>())); }
public void Converts_value_types_to_their_nullable_types() { IConverter converter = BuildRoutine.Converter().ToNullable(); Assert.AreEqual(type.of <int?>(), converter.GetTargetTypes(type.of <int>())[0]); var actual = converter.Convert(3, type.of <int>(), type.of <int?>()); int?expected = 3; Assert.AreEqual(expected, actual); }
public void Converts_object_to_target_type_using_cast_method_of_IType() { var typeMock = new Mock <IType>(); typeMock.Setup(t => t.Cast(It.IsAny <object>(), type.of <string>())).Returns("success"); typeMock.Setup(t => t.AssignableTypes).Returns(new List <IType> { type.of <string>() }); IConverter converter = BuildRoutine.Converter().ByCasting(); Assert.AreEqual("success", converter.Convert(0, typeMock.Object, type.of <string>())); }
public void Converts_object_to_target_type_using_given_delegate() { IConverter converter = BuildRoutine.Converter().By(() => new List <IType> { type.of <string>() }, (o, t) => { Assert.AreEqual(0, o); Assert.AreEqual(type.of <string>(), t); return("success"); }); Assert.AreEqual("success", converter.Convert(0, type.of <int>(), type.of <string>())); }
public void Facade_ToConstant_when_null_is_given_type_is_defined_as_null() { IConverter converter = BuildRoutine.Converter().ToConstant(null); Assert.IsNull(converter.GetTargetTypes(type.of <string>())[0]); }
public void Facade_ToConstant() { Assert.AreEqual("success", ((IConverter)BuildRoutine.Converter().ToConstant("success")).Convert(0, type.of <int>(), type.of <string>())); }
public void Throws_ArgumentNullException_when_any_of_the_given_delegates_is_null() { Assert.Throws <ArgumentNullException>(() => BuildRoutine.Converter().By(() => new List <IType>(), null)); Assert.Throws <ArgumentNullException>(() => BuildRoutine.Converter().By((Func <IType>)null, (_, _) => new object())); }
public void Throws_ArgumentNullException_when_given_view_type_predicate_is_null() { Assert.Throws <ArgumentNullException>(() => BuildRoutine.Converter().ByCasting(null)); }
public void View_types_can_be_filtered_so_that_only_appropriate_types_will_be_used() { IConverter converter = BuildRoutine.Converter().ByCasting(t => !t.CanBe <object>()); Assert.Throws <CannotConvertException>(() => converter.Convert("test", type.of <string>(), type.of <object>())); }
public static ConventionBase <TFrom, List <IConverter> > Convert <TFrom>( this ConventionBuilder <TFrom, List <IConverter> > source, Func <ConverterBuilder, IConverter> converterDelegate ) => source.Constant(converterDelegate(BuildRoutine.Converter()));