public void ListTypeConverter_FromSource_Convert_ReturnsList() { var source = new[] { new User { Id = 1, Name = "Homer" }, new User { Id = 2, Name = "Marge" } }; var mapper = new Mock <IMapper <User, UserView> >(); mapper.Setup(m => m.Map(source[0])).Returns(new UserView { Id = 1, Name = "Homer" }); mapper.Setup(m => m.Map(source[1])).Returns(new UserView { Id = 2, Name = "Marge" }); var mappingFactory = new Mock <IMappingFactory>(); mappingFactory.Setup(f => f.Create <User, UserView>()).Returns(mapper.Object); var converter = new ListTypeConverter <User, UserView> { MappingFactory = mappingFactory.Object }; var target = converter.Convert(source.ToList()).ToList(); target[0].Id.ShouldBe(1); target[0].Name.ShouldBe("Homer"); target[1].Id.ShouldBe(2); target[1].Name.ShouldBe("Marge"); }
public void TestListTypeConverterConverter() { var converter = new ListTypeConverter(); Assert.IsTrue(converter.GetStandardValuesSupported(new TypeDescriptorContextMock())); Assert.IsTrue(converter.GetStandardValuesExclusive(new TypeDescriptorContextMock())); Assert.IsNotNull(converter.GetStandardValues(new TypeDescriptorContextMock())); }
public void ListTypeConverter_EmptySource_Convert_ReturnsNull() { var converter = new ListTypeConverter <User, UserView>(); var target = converter.Convert(new List <User>(0)); target.ShouldBeNull(); }
public void ListTypeConverter_UnknownTarget_Convert_ReturnsNull() { var mappingFactory = new Mock <IMappingFactory>(); mappingFactory.Setup(f => f.Create <User, UserView>()).Returns((IMapper <User, UserView>)null); var converter = new ListTypeConverter <User, UserView> { MappingFactory = mappingFactory.Object }; var target = converter.Convert(new[] { new User() }.ToList()); target.ShouldBeNull(); }
internal virtual TypeConverter CreateConverter() { TypeConverterAttribute converterAttr = TypeDescriptor.GetProperties(this, false)["Value"].Attributes[typeof(TypeConverterAttribute)] as TypeConverterAttribute; TypeConverter coreConverter; if (this is ICollectionSupport && converterAttr.ConverterTypeName == "") { //special handling for the list converter coreConverter = new ListTypeConverter(((ICollectionSupport)this).Domain, ((ICollectionSupport)this).EntryType, (bool) this.GetAttribute( CollectionOptionItem <object> .USE_ONLY_DOMAIN_ATTRIBUTE), null, "", null); } else if (converterAttr.ConverterTypeName != "") { //attribute set, get the converter that is set by the attribute coreConverter = TypeDescriptor.GetProperties(this, false)["Value"].Converter; } else { //no attribute, delegate to base coreConverter = TypeDescriptor.GetConverter(this.Type); } bool supportNull = (bool)this.GetAttribute(OptionItem.SUPPORT_NULL_VALUE_ATTRIBUTE); bool supportUndefined = (bool)this.GetAttribute(OptionItem.SUPPORT_UNDEFINED_VALUE_ATTRIBUTE); string nullValueRepresentation = this.GetAttribute(OptionItem.NULL_VALUE_STRING_ATTRIBUTE) as string; //test for overrides object converterOverride = this.GetAttribute(OptionItem.CUSTOM_CONVERTER_ATTRIBUTE); if (converterOverride != null) { if (converterOverride is Type) { try { TypeConverter o = System.Activator.CreateInstance((Type)converterOverride) as TypeConverter; if (o != null) { coreConverter = o; } } catch { Trace.WriteLine("Cannot create converter instance"); } } else if (converterOverride is string) { try { TypeConverter o = System.Activator.CreateInstance(Type.GetType((string)converterOverride)) as TypeConverter; if (o != null) { coreConverter = o; } } catch { Trace.WriteLine("Cannot create converter instance"); } } else { Trace.WriteLine("Invalid type for converter attribute"); } } if (supportNull || supportUndefined) { return(new NullableTypeConverter(coreConverter, supportNull, supportUndefined, nullValueRepresentation == null ? "" : nullValueRepresentation)); } return(coreConverter); }