public void RegisterConverterThrowsArgumentNullExceptionIfTheConverterIsNull()
        {
            var target = new CustomConverterFactory();

            var exception = Assert.Catch<ArgumentNullException>(() => target.RegisterConverter(typeof(CustomConverterFactoryTests), null));

            Assert.AreEqual("converter", exception.ParamName);
        }
        public void RegisterConverterThrowsArgumentNullExceptionIfTheTypeIsNull()
        {
            var target = new CustomConverterFactory();

            Func<object, object> converter = x => null;

            var exception = Assert.Catch<ArgumentNullException>(() => target.RegisterConverter(null, converter));

            Assert.AreEqual("type", exception.ParamName);
        }
        public void RegisterConverterAddsNewConverter()
        {
            var target = new CustomConverterFactory();

            Func<object, object> converter = x => null;

            target.RegisterConverter(typeof(CustomConverterFactoryTests), converter);

            var result = target.GetConverter(typeof(CustomConverterFactoryTests));

            Assert.AreSame(converter, result);
        }
        public void RegisterConverterChangesExisitongConverter()
        {
            var target = new CustomConverterFactory();

            Func<object, object> converter1 = x => null;
            Func<object, object> converter2 = x => new object();

            target.RegisterConverter(typeof(CustomConverterFactoryTests), converter1);

            // act here
            target.RegisterConverter(typeof(CustomConverterFactoryTests), converter2);

            var result = target.GetConverter(typeof(CustomConverterFactoryTests));

            Assert.AreSame(converter2, result);
        }
        public void RemoveConverterRemovesExistingConverter()
        {
            var target = new CustomConverterFactory();

            Func<object, object> converter = x => null;

            target.RegisterConverter(typeof(CustomConverterFactoryTests), converter);

            var result1 = target.RemoveConverter(typeof (CustomConverterFactoryTests));

            var result2 = target.GetConverter(typeof(CustomConverterFactoryTests));

            Assert.IsNotNull(result1);
            Assert.AreSame(converter, result1);

            Assert.IsNull(result2);
        }
        public void StringConverterChangesTypeFromDateTimeOffset()
        {
            var time = new DateTimeOffset(2009, 10, 22, 14, 59, 32, TimeSpan.Zero);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(string));

            Assert.AreEqual("2009-10-22T14:59:32.000+0000", converter(time));
        }
        public void NullableDateTimeOffsetConverterChangesTypeFromByteArray()
        {
            var time = DateTimeOffset.Now;

            var array = BitConverter.GetBytes(time.DateTime.ToBinary());

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTimeOffset?));

            Assert.AreEqual(time, converter(array));
        }
        public void DateTimeConverterChangesTypeFromUtcString()
        {
            var time = new DateTime(2009, 10, 22, 14, 59, 32, 125, DateTimeKind.Utc);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTime));

            Assert.AreEqual(time, converter("2009-10-22T14:59:32.125Z"));
        }
        public void NullableDateTimeConverterChangesTypeFromJavaString()
        {
            var time = new DateTime(2009, 10, 22, 14, 59, 32, 125, DateTimeKind.Utc);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTime?));

            Assert.AreEqual(time, ((DateTime)(converter("2009-10-22T14:59:32.125+0000"))).ToUniversalTime());
        }
        public void NullableDateTimeOffsetConverterChangesTypeFromJavaString()
        {
            var time = new DateTimeOffset(2009, 10, 22, 14, 59, 32, 125, TimeSpan.Zero);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTimeOffset?));

            Assert.AreEqual(time, converter("2009-10-22T14:59:32.125+0000"));
        }
        public void ConverterThrowsInvalidCastExceptionWhenCannotChangeToNullableType(Type type, object badValue)
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(Nullable<>).MakeGenericType(type));

            Assert.Catch<InvalidCastException>(() => converter(badValue));
        }
        public void StringConverterChangesTypeFromLocalDateTime()
        {
            var time = new DateTime(2009, 10, 22, 14, 59, 32, 125, DateTimeKind.Local);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(string));

            var localOffset = TimeZoneInfo.Local.GetUtcOffset(time.ToUniversalTime());

            var offset = localOffset.ToString("hhmm");

            offset = localOffset >= TimeSpan.Zero ? "+" + offset : "-" + offset;

            Assert.AreEqual("2009-10-22T14:59:32.125" + offset, converter(time));
        }
        public void RemoveConverterRetunrsNullForNonRegisteredConverter()
        {
            var target = new CustomConverterFactory();

            var result = target.RemoveConverter(typeof(CustomConverterFactoryTests));

            Assert.IsNull(result);
        }
        public void ConverterThrowsFormatExceptionWhenInputValueIsEmptyString(Type type)
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(type);

            Assert.Catch<FormatException>(() => converter(string.Empty));
        }
        public void NullableDecimalConverterChangesTypeToExpectedValue(object testValue, string expectedValueStr)
        {
            var expectedValue = decimal.Parse(expectedValueStr);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(decimal?));

            Assert.AreEqual(expectedValue, converter(testValue));
        }
        public void NullableDecimalConverterChangesTypeFromDecimal()
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(decimal?));

            Assert.AreEqual(123.45M, converter(123.45M));
            Assert.AreEqual(-123.45M, converter(-123.45M));
        }
        public void StringConverterChangesTypeFromDecimal()
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(string));

            Assert.AreEqual("123.45", converter(123.45M));
            Assert.AreEqual("-123.45", converter(-123.45M));
        }
        public void ConverterThrowsFormatExceptionWhenCannotChangeTypeToNullableTypeFromBadlyFormattedString(Type type, object badValue)
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(Nullable<>).MakeGenericType(type));

            Assert.Catch<FormatException>(() => converter(badValue));
        }
        public void DateTimeOffsetConverterChangesTypeFromDateTimeOffset()
        {
            var time = new DateTimeOffset(2009, 10, 22, 14, 59, 32, TimeSpan.Zero);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTimeOffset));

            Assert.AreEqual(time, converter(time));
        }
        public void NullableTimeSpanConverterChangesTypeFromByteArray()
        {
            var timeSpan = TimeSpan.FromSeconds(2000);

            var array = BitConverter.GetBytes(timeSpan.Ticks);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(TimeSpan?));

            Assert.AreEqual(timeSpan, converter(array));
        }
        public void NullableDateTimeOffsetConverterChangesTypeFromIso8601String()
        {
            var time = new DateTimeOffset(2009, 10, 22, 14, 59, 32, TimeSpan.Zero);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTimeOffset?));

            Assert.AreEqual(time, converter(time.ToString(CustomConverterFactory.Iso8601DateTimePattern)));
        }
        public void NullableTimeSpanConverterChangesTypeFromTimeSpan()
        {
            var timeSpan = TimeSpan.FromSeconds(4000);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(TimeSpan?));

            Assert.AreEqual(timeSpan, converter(timeSpan));
        }
        public void NullableDateTimeOffsetConverterChangesTypeFromRoundtripString()
        {
            var time = DateTimeOffset.Now;

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTimeOffset?));

            Assert.AreEqual(time, converter(time.ToString("O", CultureInfo.InvariantCulture)));
        }
        public void ConverterReturnsDefaultValueWhenInputValueIsNull(Type type)
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(type);

            var result = converter(null);

            Assert.AreEqual(CustomConverter.GetDefaultValue(type), result);
        }
        public void StringConverterChangesTypeFromTimeSpan()
        {
            var timeSpan = new TimeSpan(3, 12, 53, 45, 765);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(string));

            Assert.AreEqual("3.12:53:45.7650000", converter(timeSpan));
        }
        public void NullableConverterReturnsNullWhenInputValueIsEmptyString(Type type)
        {
            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(Nullable<>).MakeGenericType(type));

            var result = converter(string.Empty);

            Assert.IsNull(result);
        }
        public void RemoveConverterThrowsArgumentNullExceptionIfTheTypeIsNull()
        {
            var target = new CustomConverterFactory();

            var exception = Assert.Catch<ArgumentNullException>(() => target.RemoveConverter(null));

            Assert.AreEqual("type", exception.ParamName);
        }
        public void NullableDateTimeConverterChangesTypeFromDateTime()
        {
            var time = new DateTime(2009, 10, 22, 14, 59, 32, 125, DateTimeKind.Local);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTime?));

            Assert.AreEqual(time, converter(time));
        }
        public void NullableTimeSpanConverterChangesTypeFromString()
        {
            var timeSpan = new TimeSpan(3, 12, 53, 45, 765);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(TimeSpan?));

            Assert.AreEqual(timeSpan, converter("3.12:53:45.765"));
        }
        public void DateTimeConverterChangesTypeFromIso8601String()
        {
            var time = new DateTime(2009, 10, 22, 14, 59, 32, 125, DateTimeKind.Local);

            var target = new CustomConverterFactory();

            var converter = target.GetConverter(typeof(DateTime));

            Assert.AreEqual(time, converter(time.ToString(CustomConverterFactory.Iso8601DateTimePattern)));
        }