示例#1
0
        public static void When_converting_a_boolean_to <TNumeric>(bool booleanValue, TNumeric expectedResult, IFormatProvider format, TNumeric defaultValue = default(TNumeric))
            where TNumeric : struct
        {
            var conversionObserver = new SimpleConversionObserver();
            var result             = booleanValue.ConvertTo <TNumeric>(
                defaultValue: defaultValue,
                format: format,
                conversionObserver: conversionObserver);

            Assert.AreEqual(expectedResult, result);
            Assert.IsFalse(conversionObserver.KnownFallbackToDefaultValueOccured);
            Assert.IsNull(conversionObserver.KnownFallbackToDefaultValueReason);
            Assert.IsFalse(conversionObserver.CaughtConversionExceptionOccured);
            Assert.IsNull(conversionObserver.CaughtConversionException);
        }
        public void When_converting_null_to_int_with_null_default_value()
        {
            var conversionObserver = new SimpleConversionObserver();
            var result             = Converter.Convert(
                value: null,
                targetType: typeof(int),
                targetDefaultValue: null,
                format: null,
                conversionObserver: conversionObserver);

            Assert.AreEqual(0, result);
            Assert.IsTrue(conversionObserver.KnownFallbackToDefaultValueOccured);
            Assert.IsInstanceOfType(conversionObserver.KnownFallbackToDefaultValueReason, typeof(ArgumentNullException));
            Assert.IsFalse(conversionObserver.CaughtConversionExceptionOccured);
            Assert.IsNull(conversionObserver.CaughtConversionException);
        }
示例#3
0
        public static void When_converting_an_empty_string_to <TNumeric>(IFormatProvider format, TNumeric defaultValue = default(TNumeric))
            where TNumeric : struct
        {
            var value = "";
            var conversionObserver = new SimpleConversionObserver();
            var result             = value.ConvertTo <TNumeric>(
                defaultValue: defaultValue,
                format: format,
                conversionObserver: conversionObserver);

            Assert.AreEqual(defaultValue, result);
            Assert.IsTrue(conversionObserver.KnownFallbackToDefaultValueOccured);
            Assert.IsInstanceOfType(conversionObserver.KnownFallbackToDefaultValueReason, typeof(ArgumentNullException));             // or maybe a FormatException??
            Assert.IsFalse(conversionObserver.CaughtConversionExceptionOccured);
            Assert.IsNull(conversionObserver.CaughtConversionException);
        }
示例#4
0
        public static void When_converting_an_object_with_exception_to <TNumeric, TException>(object value, IFormatProvider format, TNumeric defaultValue = default(TNumeric))
            where TNumeric : struct
            where TException : Exception
        {
            var conversionObserver = new SimpleConversionObserver();
            var result             = value.ConvertTo <TNumeric>(
                defaultValue: defaultValue,
                format: format,
                conversionObserver: conversionObserver);

            Assert.AreEqual(defaultValue, result);
            Assert.IsFalse(conversionObserver.KnownFallbackToDefaultValueOccured);
            Assert.IsNull(conversionObserver.KnownFallbackToDefaultValueReason);
            Assert.IsTrue(conversionObserver.CaughtConversionExceptionOccured);
            Assert.IsInstanceOfType(conversionObserver.CaughtConversionException, typeof(TException));
        }
示例#5
0
        public static void When_converting_a_nullable_boolean_to <TNumeric>(bool?booleanValue, TNumeric expectedResult, IFormatProvider format, TNumeric defaultValue = default(TNumeric))
            where TNumeric : struct
        {
            var conversionObserver = new SimpleConversionObserver();
            var result             = booleanValue.ConvertTo <TNumeric>(
                defaultValue: defaultValue,
                format: format,
                conversionObserver: conversionObserver);

            Assert.AreEqual(expectedResult, result);
            if (booleanValue.HasValue)
            {
                Assert.IsFalse(conversionObserver.KnownFallbackToDefaultValueOccured);
                Assert.IsNull(conversionObserver.KnownFallbackToDefaultValueReason);
            }
            else
            {
                Assert.IsTrue(conversionObserver.KnownFallbackToDefaultValueOccured);
                Assert.IsInstanceOfType(conversionObserver.KnownFallbackToDefaultValueReason, typeof(ArgumentNullException));
            }

            Assert.IsFalse(conversionObserver.CaughtConversionExceptionOccured);
            Assert.IsNull(conversionObserver.CaughtConversionException);
        }