public void GetProperty_InvalidStringValue_ReturnsInvalid(Type type, string stringValue) { var item = new ChangeTypeMapper(type); object value = 1; PropertyMapperResultType result = item.MapCellValue(new ReadCellValueResult(-1, stringValue), ref value); Assert.Equal(PropertyMapperResultType.Invalid, result); Assert.Equal(1, value); }
public void GetProperty_ValidStringValue_ReturnsSuccess(Type type, string stringValue, object expected) { var item = new ChangeTypeMapper(type); object value = 0; PropertyMapperResultType result = item.MapCellValue(new ReadCellValueResult(-1, stringValue), ref value); Assert.Equal(PropertyMapperResultType.Success, result); Assert.Equal(expected, value); }
public void Ctor_Type(Type type) { var item = new ChangeTypeMapper(type); Assert.Equal(type, item.Type); }
private static bool AutoMap(this Type memberType, FallbackStrategy emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback) { Type type = memberType.GetNullableTypeOrThis(out bool isNullable); Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray(); IFallbackItem ReconcileFallback(FallbackStrategy strategyToPursue, bool empty) { // Empty nullable values should be set to null. if (empty && isNullable) { return(new FixedValueFallback(null)); } else if (strategyToPursue == FallbackStrategy.SetToDefaultValue || emptyValueStrategy == FallbackStrategy.SetToDefaultValue) { return(new FixedValueFallback(type.DefaultValue())); } else { Debug.Assert(emptyValueStrategy == FallbackStrategy.ThrowIfPrimitive); // The user specified that we should set to the default value if it was empty. return(new ThrowFallback()); } } if (type == typeof(DateTime)) { mapper = new DateTimeMapper(); emptyFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false); } else if (type == typeof(bool)) { mapper = new BoolMapper(); emptyFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false); } else if (type.GetTypeInfo().IsEnum) { mapper = new EnumMapper(type); emptyFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false); } else if (type == typeof(string) || type == typeof(object) || type == typeof(IConvertible)) { mapper = new StringMapper(); emptyFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: false); } else if (type == typeof(Uri)) { mapper = new UriMapper(); emptyFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false); } else if (interfaces.Any(t => t == typeof(IConvertible))) { mapper = new ChangeTypeMapper(type); emptyFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true); invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false); } else { mapper = null; emptyFallback = null; invalidFallback = null; return(false); } return(true); }