Example #1
0
        public void TestDefaultConstructor()
        {
            var ex = new ValueException();

            ex.ValueName.ShouldBe(null);
            ex.Message.ShouldBe(ValueException.DEFAULT_MESSAGE);
        }
Example #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));
        }
Example #3
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"));
        }
Example #4
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);
            }
        }
Example #5
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));
        }
Example #6
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));
        }
Example #7
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));
        }
Example #8
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));
        }
Example #9
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));
        }
Example #10
0
 protected string GenerateExceptionMessage(string title, ValueException valueException)
 {
     return(string.Format("{0} - '{1}' \r\nשגיאה בשורה: {2} עמודה: '{3}' מקט: {4}"
                          , title, valueException.Value, valueException.Line, valueException.ColumnName, valueException.SKU));
 }
        public void ValueException1()
        {
            var exception = new ValueException("The message");

            Assert.AreEqual("The message", exception.Message);
        }
Example #12
0
 private static void ThrowValueExpectedException <TExpectedValueType>(IXLCell cell)
 {
     throw ValueException.ForMissingValue <TExpectedValueType>(cell.Worksheet.Name, new DataSheetCell(cell));
 }
        /// <summary>
        /// Stores all settings at the service.
        /// </summary>
        /// <param name="parameter">An optional parameter for the command.</param>
        protected override void Execute(object parameter)
        {
            List <Dictionary <SettingKey, SettingItem> > settings = new List <Dictionary <SettingKey, SettingItem> >();
            int iFailedSettings = 0;

            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                foreach (GroupedSectionViewModel gsvm in this.GetParent().GetAllSections())
                {
                    SectionViewModel svm = gsvm.Section;
                    if (svm == null)
                    {
                        continue;
                    }

                    Dictionary <SettingKey, SettingItem> sectionSettings = new Dictionary <SettingKey, SettingItem>();
                    settings.Add(sectionSettings);

                    foreach (CategoryViewModel cvm in svm.CategoryItems)
                    {
                        foreach (SettingItemViewModel sivm in cvm.SettingItems)
                        {
                            object value = null;
                            try
                            {
                                value = sivm.TypeEditor.Value;

                                sivm.Setting.Value = value;
                                sectionSettings.Add(sivm.Info.CreateSettingKey(), sivm.Setting);
                            }
                            catch (Exception ex)
                            {
                                string exMessage = ex.Message;
                                string exHint = Properties.Resources.SettingSaveError_DefaultHints;

                                ValueException vex = ex as ValueException;
                                if (vex != null)
                                {
                                    exHint = vex.Hint;
                                }

                                Logger.Instance.LogException(this, ex);

                                string message = string.Format(Properties.Resources.SettingSaveError, sivm.DisplayText, svm.DisplayText, exMessage, exHint);
                                MessageBox.Show(message, Properties.Resources.SettingSaveError_Title, MessageBoxButton.OK, MessageBoxImage.Error);
                                iFailedSettings++;
                            }
                        }
                    }
                }
            }));

            iFailedSettings += SaveSettingItems(settings);

            string          boxMessage = null;
            MessageBoxImage boxImage   = MessageBoxImage.Information;

            if (iFailedSettings == 0)
            {
                boxMessage = Properties.Resources.SavingSettingsSuccess;
            }
            else
            {
                boxMessage = Properties.Resources.SavingSettingsWithErrors;
                boxImage   = MessageBoxImage.Warning;
            }
            MessageBox.Show(boxMessage, Properties.Resources.SettingSaveFinished_Title, MessageBoxButton.OK, boxImage);
        }