Beispiel #1
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"));
        }
Beispiel #2
0
        public static bool?GetBooleanOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            switch (rawValue)
            {
            case "0":
                return(false);

            case "1":
                return(true);
            }

            if (bool.TryParse(rawValue, out var boolValue))
            {
                return(boolValue);
            }

            throw ValueException.ForInvalidValue <bool>(new DataSheetCell(cell));
        }
Beispiel #3
0
        public object ConvertTo(IDataSheetCell cell, Type convertTo)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }
            if (convertTo == null)
            {
                throw new ArgumentNullException(nameof(convertTo));
            }

            var converters = _converterProvider.GetConverters();

            if (converters.Empty())
            {
                throw new NoConvertersConfiguredException();
            }

            if (converters.ContainsKey(convertTo) == false)
            {
                throw new CellValueConversionNotSupportedException(convertTo, cell);
            }

            var converter = converters[convertTo];

            try
            {
                return(converter(cell));
            }
            catch (Exception exception)
            {
                throw ValueException.ForInvalidValue(convertTo, cell, exception);
            }
        }
Beispiel #4
0
        public static Guid?GetGuidOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            if (Guid.TryParse(rawValue, out var guidValue))
            {
                return(guidValue);
            }

            throw ValueException.ForInvalidValue <Guid>(new DataSheetCell(cell));
        }
Beispiel #5
0
        public static long?GetLongOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            if (long.TryParse(rawValue, out var longValue))
            {
                return(longValue);
            }

            throw ValueException.ForInvalidValue <long>(new DataSheetCell(cell));
        }
Beispiel #6
0
        public static int?GetIntegerOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            if (int.TryParse(rawValue, out var integerValue))
            {
                return(integerValue);
            }

            throw ValueException.ForInvalidValue <int>(new DataSheetCell(cell));
        }
Beispiel #7
0
        public static DateTime?GetDateTimeOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            if (double.TryParse(rawValue, out var date))
            {
                return(DateTime.FromOADate(date));
            }

            if (DateTime.TryParse(rawValue, out var value))
            {
                return(value);
            }

            throw ValueException.ForInvalidValue <DateTime>(new DataSheetCell(cell));
        }
Beispiel #8
0
        public static decimal?GetDecimalOrDefault(this IXLCell cell)
        {
            var rawValue = cell.GetStringOrDefault();

            if (string.IsNullOrEmpty(rawValue))
            {
                return(null);
            }

            if (decimal.TryParse(rawValue, out var decimalValue))
            {
                return(decimalValue);
            }

            if (double.TryParse(rawValue, out var doubleValue))
            {
                return(Convert.ToDecimal(doubleValue));
            }

            throw ValueException.ForInvalidValue <decimal>(new DataSheetCell(cell));
        }