public void Convert_caches_the_conversion_so_GetConversionCost_is_only_called_once_for_a_pair_of_types()
        {
            var mockRule0 = MockRepository.GenerateMock <IConversionRule>();
            var mockRule1 = MockRepository.GenerateMock <IConversionRule>();
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), new[] { mockRule0, mockRule1 });

            mockRule0.Expect(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Return(ConversionCost.Invalid);
            mockRule1.Expect(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Return(ConversionCost.Default);
            mockRule1.Expect(x => x.Convert(42, typeof(string), converter, true)).Return("42");
            mockRule1.Expect(x => x.Convert(53, typeof(string), converter, true)).Return("53");
            Assert.AreEqual("42", converter.Convert(42, typeof(string)));
            Assert.AreEqual("53", converter.Convert(53, typeof(string)));
            mockRule0.VerifyAllExpectations();
            mockRule1.VerifyAllExpectations();
        }
        public void Custom_conversion()
        {
            var extensionPoints = new DefaultExtensionPoints();

            extensionPoints.CustomConverters.Register <string, Foo>(x => new Foo(Int32.Parse(x)));
            var converter = new RuleBasedConverter(extensionPoints, EmptyArray <IConversionRule> .Instance);
            var actual    = (Foo)converter.Convert("123", typeof(Foo));

            Assert.AreEqual(123, actual.Value);
        }
        public void Nulls_cannot_be_converted_to_non_nullable_ValueTypes()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            converter.Convert(null, typeof(int));
        }
        public void Nulls_remain_rull_during_conversions_to_nullable_ValueTypes()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            Assert.IsNull(converter.Convert(null, typeof(int?)));
        }
        public void Convert_throws_if_conversion_not_supported()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            converter.Convert("42", typeof(int));
        }
        public void Convert_returns_same_value_if_types_are_same()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            Assert.AreEqual(42, converter.Convert(42, typeof(int)));
        }