public void Ctor_Converter() { ConvertUsingMapperDelegate converter = (ReadCellValueResult readResult, ref object value) => PropertyMapperResultType.Success; var item = new ConvertUsingMapper(converter); Assert.Same(converter, item.Converter); }
/// <summary> /// Specifies that the value of a cell should be mapped to a value using the given delegate. This is /// useful for specifying custom mapping behaviour for a property or field without having to write /// your own ICellValueMapper. /// </summary> /// <typeparam name="TPropertyMap">The type of the property map.</typeparam> /// <typeparam name="T">The type of the property or field that the property map represents.</typeparam> /// <param name="propertyMap">The property map to use.</param> /// <param name="converter">A delegate that is invoked to map the string value of a cell to the value of a property or field.</param> /// <returns>The property map on which this method was invoked.</returns> public static TPropertyMap WithConverter <TPropertyMap, T>(this TPropertyMap propertyMap, ConvertUsingSimpleMapperDelegate <T> converter) where TPropertyMap : ISinglePropertyMap <T> { if (converter == null) { throw new ArgumentNullException(nameof(converter)); } ConvertUsingMapperDelegate actualConverter = (ReadCellValueResult mapResult, ref object value) => { try { value = converter(mapResult.StringValue); return(PropertyMapperResultType.Success); } catch { return(PropertyMapperResultType.Invalid); } }; var item = new ConvertUsingMapper(actualConverter); propertyMap.AddCellValueMapper(item); return(propertyMap); }
public void GetProperty_ValidStringValue_ReturnsSuccess() { ConvertUsingMapperDelegate converter = (ReadCellValueResult readResult, ref object readValue) => { Assert.Equal(-1, readResult.ColumnIndex); Assert.Equal("string", readResult.StringValue); readValue = 10; return(PropertyMapperResultType.Success); }; var item = new ConvertUsingMapper(converter); object value = null; PropertyMapperResultType result = item.GetProperty(new ReadCellValueResult(-1, "string"), ref value); Assert.Equal(PropertyMapperResultType.Success, result); Assert.Equal(10, value); }
/// <summary> /// Constructs a mapper that tries to map the value of a cell to an object using a given conversion delegate. /// </summary> /// <param name="converter">The delegate used to map the value of a cell to an object</param> public ConvertUsingMapper(ConvertUsingMapperDelegate converter) { Converter = converter ?? throw new ArgumentNullException(nameof(converter)); }