예제 #1
0
        public void WithConverter_NullConverter_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            ConvertUsingSimpleMapperDelegate <string> converter = null;

            Assert.Throws <ArgumentNullException>("converter", () => propertyMap.WithConverter(converter));
        }
예제 #2
0
        public void WithConverter_InvalidConverter_ReturnsExpected()
        {
            ConvertUsingSimpleMapperDelegate <string> converter = stringValue =>
            {
                Assert.Equal("stringValue", stringValue);
                throw new NotSupportedException();
            };

            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithConverter(converter));
            ConvertUsingMapper item = propertyMap.CellValueMappers.OfType <ConvertUsingMapper>().Single();

            object value = 1;
            PropertyMapperResultType result = item.Converter(new ReadCellValueResult(-1, "stringValue"), ref value);

            Assert.Equal(PropertyMapperResultType.Invalid, result);
            Assert.Equal(1, value);
        }
예제 #3
0
        public void WithConverter_SuccessConverter_ReturnsExpected()
        {
            ConvertUsingSimpleMapperDelegate <string> converter = stringValue =>
            {
                Assert.Equal("stringValue", stringValue);
                return("abc");
            };

            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithConverter(converter));
            ConvertUsingMapper item = propertyMap.CellValueMappers.OfType <ConvertUsingMapper>().Single();

            object value = null;
            PropertyMapperResultType result = item.Converter(new ReadCellValueResult(-1, "stringValue"), ref value);

            Assert.Equal(PropertyMapperResultType.Success, result);
            Assert.Equal("abc", value);
        }
예제 #4
0
        /// <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);
        }