Exemple #1
0
        public void WithTrim_Invoke_Success()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithTrim());

            ICellValueTransformer transformer = Assert.Single(propertyMap.CellValueTransformers);

            Assert.IsType <TrimCellValueTransformer>(transformer);
        }
Exemple #2
0
        public void WithColumnIndex_ValidColumnIndex_Success(int columnIndex)
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithColumnIndex(columnIndex));

            ColumnIndexValueReader reader = Assert.IsType <ColumnIndexValueReader>(propertyMap.CellReader);

            Assert.Equal(columnIndex, reader.ColumnIndex);
        }
Exemple #3
0
        public void RemoveCellValueMapper_Index_Success()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo);

            propertyMap.AddCellValueMapper(new BoolMapper());

            propertyMap.RemoveCellValueMapper(0);
            Assert.Empty(propertyMap.CellValueMappers);
        }
Exemple #4
0
        public void WithDateFormats_NullableAutoMappedIEnumerableString_Success(IEnumerable <string> formats)
        {
            SingleExcelPropertyMap <DateTime?> propertyMap = Map(t => t.NullableDateValue);

            Assert.Same(propertyMap, propertyMap.WithDateFormats(formats));

            DateTimeMapper item = propertyMap.CellValueMappers.OfType <DateTimeMapper>().Single();

            Assert.Equal(formats, item.Formats);
        }
Exemple #5
0
        public void WithEmptyFallback_Invoke_Success()
        {
            SingleExcelPropertyMap <string> mapping = Map(t => t.Value);

            Assert.Same(mapping, mapping.WithEmptyFallback("abc"));

            FixedValueFallback fallback = Assert.IsType <FixedValueFallback>(mapping.EmptyFallback);

            Assert.Equal("abc", fallback.Value);
        }
Exemple #6
0
        public void WithColumnName_ValidColumnName_Success()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithColumnName("ColumnName"));

            ColumnNameValueReader reader = Assert.IsType <ColumnNameValueReader>(propertyMap.CellReader);

            Assert.Equal("ColumnName", reader.ColumnName);
        }
Exemple #7
0
        public void WithInvalidFallback_Invoke_Success()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithInvalidFallback("abc"));

            FixedValueFallback fallback = Assert.IsType <FixedValueFallback>(propertyMap.InvalidFallback);

            Assert.Equal("abc", fallback.Value);
        }
Exemple #8
0
        public void AddCellValueTransformer_ValidTransformer_Success()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo);
            var        transformer1 = new TrimCellValueTransformer();
            var        transformer2 = new TrimCellValueTransformer();

            propertyMap.AddCellValueTransformer(transformer1);
            propertyMap.AddCellValueTransformer(transformer2);
            Assert.Equal(new ICellValueTransformer[] { transformer1, transformer2 }, propertyMap.CellValueTransformers);
        }
Exemple #9
0
        public void AddCellValueMapper_ValidItem_Success()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo);
            var        item1        = new BoolMapper();
            var        item2        = new BoolMapper();

            propertyMap.AddCellValueMapper(item1);
            propertyMap.AddCellValueMapper(item2);
            Assert.Equal(new ICellValueMapper[] { item1, item2 }, propertyMap.CellValueMappers);
        }
Exemple #10
0
        public void Ctor_Member_Type_EmptyValueStrategy()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));

            var propertyMap = new SingleExcelPropertyMap(propertyInfo);

            Assert.Same(propertyInfo, propertyMap.Member);

            Assert.Empty(propertyMap.CellValueMappers);
            Assert.Empty(propertyMap.CellValueTransformers);
        }
Exemple #11
0
        public void CellReader_SetValid_GetReturnsExpected()
        {
            var        cellReader   = new ColumnNameValueReader("ColumnName");
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo)
            {
                CellReader = cellReader
            };

            Assert.Same(cellReader, propertyMap.CellReader);
        }
Exemple #12
0
        public void MakeOptional_HasMapper_ReturnsExpected()
        {
            var innerReader = new ColumnIndexValueReader(1);
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value).WithReader(innerReader);

            Assert.Same(propertyMap, propertyMap.MakeOptional());

            OptionalCellValueReader reader = Assert.IsType <OptionalCellValueReader>(propertyMap.CellReader);

            Assert.Same(innerReader, reader.InnerReader);
        }
Exemple #13
0
        public void WithColumnIndex_OptionalColumn_Success()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value).MakeOptional();

            Assert.Same(propertyMap, propertyMap.WithColumnIndex(1));

            OptionalCellValueReader reader      = Assert.IsType <OptionalCellValueReader>(propertyMap.CellReader);
            ColumnIndexValueReader  innerReader = Assert.IsType <ColumnIndexValueReader>(reader.InnerReader);

            Assert.Equal(1, innerReader.ColumnIndex);
        }
Exemple #14
0
        public void WithReader_OptionalColumn_Success()
        {
            var innerReader = new ColumnNameValueReader("ColumnName");

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

            Assert.Same(propertyMap, propertyMap.WithReader(innerReader));

            OptionalCellValueReader reader = Assert.IsType <OptionalCellValueReader>(propertyMap.CellReader);

            Assert.Same(innerReader, reader.InnerReader);
        }
Exemple #15
0
        public void WithDateFormats_NullableAutoMappedStringArray_Success(IEnumerable <string> formats)
        {
            var formatsArray = formats.ToArray();

            SingleExcelPropertyMap <DateTime?> propertyMap = Map(t => t.NullableDateValue);

            Assert.Same(propertyMap, propertyMap.WithDateFormats(formatsArray));

            DateTimeMapper item = propertyMap.CellValueMappers.OfType <DateTimeMapper>().Single();

            Assert.Same(formatsArray, item.Formats);
        }
Exemple #16
0
        public void InvalidFallback_Set_GetReturnsExpected()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo);

            var fallback = new FixedValueFallback(10);

            propertyMap.InvalidFallback = fallback;
            Assert.Same(fallback, propertyMap.InvalidFallback);

            propertyMap.InvalidFallback = null;
            Assert.Null(propertyMap.InvalidFallback);
        }
Exemple #17
0
        public static bool TryMapPrimitive <T>(MemberInfo member, FallbackStrategy emptyValueStrategy, out SingleExcelPropertyMap <T> map)
        {
            if (!GetWellKnownMapper(typeof(T), emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback))
            {
                map = null;
                return(false);
            }

            map = new SingleExcelPropertyMap <T>(member)
                  .WithCellValueMappers(mapper)
                  .WithEmptyFallbackItem(emptyFallback)
                  .WithInvalidFallbackItem(invalidFallback);
            return(true);
        }
Exemple #18
0
        public static bool AutoMap <T>(this MemberInfo member, FallbackStrategy emptyValueStrategy, out SingleExcelPropertyMap <T> mapping)
        {
            if (!typeof(T).AutoMap(emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback))
            {
                mapping = null;
                return(false);
            }

            mapping = new SingleExcelPropertyMap <T>(member)
                      .WithCellValueMappers(mapper)
                      .WithEmptyFallbackItem(emptyFallback)
                      .WithInvalidFallbackItem(invalidFallback);
            return(true);
        }
Exemple #19
0
        public void WithElementMap_ValidMap_Success()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        elementMap   = new SingleExcelPropertyMap <string>(propertyInfo);

            var propertyMap = new SubPropertyMap(propertyInfo);

            Assert.Same(propertyMap, propertyMap.WithElementMap(e =>
            {
                Assert.Same(e, propertyMap.ElementMap);
                return(elementMap);
            }));
            Assert.Same(elementMap, propertyMap.ElementMap);
        }
Exemple #20
0
        public void WithMapping_ValidReader_Success()
        {
            var dictionaryMapping = new Dictionary <string, DateTime> {
                { "key", DateTime.MinValue }
            };
            StringComparer comparer = StringComparer.CurrentCultureIgnoreCase;

            SingleExcelPropertyMap <DateTime> propertyMap = Map(t => t.DateValue);

            Assert.Same(propertyMap, propertyMap.WithMapping(dictionaryMapping, comparer));

            DictionaryMapper <DateTime> item = propertyMap.CellValueMappers.OfType <DictionaryMapper <DateTime> >().Single();

            Assert.NotSame(dictionaryMapping, item.MappingDictionary);
            Assert.Equal(dictionaryMapping, item.MappingDictionary);

            Assert.Same(comparer, Assert.IsType <Dictionary <string, DateTime> >(item.MappingDictionary).Comparer);
        }
Exemple #21
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);
        }
Exemple #22
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);
        }
Exemple #23
0
        public void WithColumnIndex_NegativeColumnIndex_ThrowsArgumentOutOfRangeException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentOutOfRangeException>("columnIndex", () => propertyMap.WithColumnIndex(-1));
        }
Exemple #24
0
        public void WithInvalidFallbackItem_NullFallbackItem_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentNullException>("fallbackItem", () => propertyMap.WithInvalidFallbackItem(null));
        }
Exemple #25
0
        public void WithColumnName_EmptyColumnName_ThrowsArgumentException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentException>("columnName", () => propertyMap.WithColumnName(string.Empty));
        }
Exemple #26
0
        public void MakeOptional_AlreadyOptional_ThrowsExcelMappingException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value).WithColumnIndex(1).MakeOptional();

            Assert.Throws <ExcelMappingException>(() => propertyMap.MakeOptional());
        }
Exemple #27
0
        public void WithReader_NullReader_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentNullException>("reader", () => propertyMap.WithReader(null));
        }
Exemple #28
0
        public void WithMapping_NullMapping_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentNullException>("mappingDictionary", () => propertyMap.WithMapping((Dictionary <string, string>)null));
        }
Exemple #29
0
        public void WithColumnName_NullColumnName_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentNullException>("columnName", () => propertyMap.WithColumnName(null));
        }
Exemple #30
0
        public void WithCellValueMappers_NullMapperInMapperss_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);

            Assert.Throws <ArgumentNullException>("mappers", () => propertyMap.WithCellValueMappers(new ICellValueMapper[] { null }));
        }