Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs =================================================================== diff -u -r92dcf3ff33f93a75b92ac8fcd05441ce35332edb -r52d5e1c2609a6f84238d88e20aa135d28de24cb0 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs (.../XElementExtensionsTest.cs) (revision 92dcf3ff33f93a75b92ac8fcd05441ce35332edb) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs (.../XElementExtensionsTest.cs) (revision 52d5e1c2609a6f84238d88e20aa135d28de24cb0) @@ -294,6 +294,39 @@ } [Test] + public void GetConvertedValueFromDescendantStringElement_DescendantElementInvalidToConvert_ThrowException() + { + // Setup + const string descendantElementName = "value"; + const string elementValue = "three"; + + var element = new XElement("Root", new XElement(descendantElementName, elementValue)); + + // Call + TestDelegate call = () => element.GetConvertedValueFromDescendantStringElement(descendantElementName); + + // Assert + Exception exception = Assert.Throws(call); + Assert.AreEqual($"{elementValue} is not a valid value for Double.", exception.Message); + } + + [Test] + public void GetConvertedValueFromDescendantStringElement_DescendantElementNotSupported_ThrowNotSupportedException() + { + // Setup + const string descendantElementName = "value"; + const string elementValue = "3"; + + var element = new XElement("Root", new XElement(descendantElementName, elementValue)); + + // Call + TestDelegate call = () => element.GetConvertedValueFromDescendantStringElement(descendantElementName); + + // Assert + Assert.Throws(call); + } + + [Test] [TestCase(true)] [TestCase(false)] public void GetConvertedValueFromDescendantStringElement_ValidDescendantElement_ReturnValue(bool value) @@ -327,6 +360,20 @@ } [Test] + public void GetConvertedValueFromDescendantDoubleElement_ParentElementNull_ThrowArgumentNullException() + { + // Setup + XElement element = null; + + // Call + TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement("0"); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("parentElement", exception.ParamName); + } + + [Test] public void GetConvertedValueFromDescendantDoubleElement_DescendantElementNameNull_ThrowArgumentNullException() { // Setup @@ -341,6 +388,72 @@ } [Test] + public void GetConvertedValueFromDescendantDoubleElement_ConvertFromThrowsException_ThrowException() + { + // Setup + const string descendantElementName = "value"; + const double elementValue = 1; + + var element = new XElement("Root", new XElement(descendantElementName, elementValue)); + + // Call + TestDelegate call = () => element.GetConvertedValueFromDescendantDoubleElement( + descendantElementName); + + // Assert + Exception exception = Assert.Throws(call); + Assert.AreEqual($"{elementValue} is not a valid value for the target type.", exception.Message); + } + + [Test] + public void GetConvertedValueFromDescendantDoubleElement_DescendantElementInvalidFormat_ThrowFormatException() + { + // Setup + const string descendantElementName = "number"; + const string descendantElementValue = "drie"; + + var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue)); + + // Call + TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName); + + // Assert + Assert.Throws(test); + } + + [Test] + public void GetConvertedValueFromDescendantDoubleElement_DescendantElementOverflows_ThrowOverflowException() + { + // Setup + const string descendantElementName = "number"; + string descendantElementValue = string.Format(CultureInfo.InvariantCulture, "1{0}", double.MaxValue); + + var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue)); + + // Call + TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName); + + // Assert + Assert.Throws(test); + } + + [Test] + public void GetConvertedValueFromDescendantDoubleElement_DescendantElementNotSupportedByConverter_ThrowNotSupportedException() + { + // Setup + const string descendantElementName = "number"; + const string descendantElementValue = "4"; + + var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue)); + + // Call + TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName); + + // Assert + Assert.Throws(test); + } + + [Test] [TestCase(0, false)] [TestCase(-1, true)] [TestCase(1, true)] @@ -546,5 +659,13 @@ return base.ConvertFrom(context, culture, value); } } + + private class ConverterThrowsExceptionOnConvertFrom : TypeConverter + { + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + throw new Exception($"{value} is not a valid value for the target type."); + } + } } } \ No newline at end of file