Ejemplo n.º 1
0
        public void ShouldThrowIfCellIsNotSupplied()
        {
            // Arrange
            // Act
            Action action = () => _sut.ConvertTo <string>(null);

            // Assert
            Assert.Throws <ArgumentNullException>(action);
        }
Ejemplo n.º 2
0
        public void ShouldThrowAggregatedExceptionContainingAllMappingErrors()
        {
            // Arrange
            var cell = Substitute.For <IDataSheetCell>();

            cell.GetString().Returns("value1", "value2");
            cell.ColumnLetter.Returns("A", "B");
            cell.RowNumber.Returns(2, 3);


            _typeConverter.ConvertTo(Arg.Any <IDataSheetCell>(), Arg.Any <Type>())
            .Returns(x => throw ValueException.ForInvalidValue(typeof(string), cell, new Exception()));

            // Act
            Action mapAction = () => _sut.Map <Person>(SheetName);

            // Assert
            var exception = Assert.Throws <WorksheetMappingFailedException>(mapAction);

            Assert.Equal(2, exception.Exceptions.Count);

            var first  = exception.Exceptions.First();
            var second = exception.Exceptions.Skip(1).First();

            Assert.True(first.Message.Contains("A2") && first.Message.Contains("value1"));
            Assert.True(second.Message.Contains("B3") && second.Message.Contains("value2"));
        }
Ejemplo n.º 3
0
        private TModel HydrateDestinationObject <TModel>(string fileName, string sheetName,
                                                         IDictionary <string, CellToPropertyMap> mappings, IDataSheetRow rowToMap)
            where TModel : class
        {
            var destinationObject = _modelBuilder.Build <TModel>();

            foreach (var cellToPropertyMapping in mappings)
            {
                var mapping        = cellToPropertyMapping.Value;
                var cell           = rowToMap.Cell(mapping.CellColumn);
                var convertedValue = _valueConverter.ConvertTo(cell, mapping.Property.PropertyType);
                mapping.Property.SetValue(destinationObject, convertedValue);
                destinationObject = AddPropertyMetadataIfPossible(mapping.Property, fileName, sheetName, cell,
                                                                  destinationObject);
            }

            return(destinationObject);
        }