예제 #1
0
        public void GetConversionCost_always_returns_zero_if_types_are_same()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            Assert.AreEqual(ConversionCost.Zero, converter.GetConversionCost(typeof(int), typeof(int)));
            Assert.AreEqual(ConversionCost.Invalid, converter.GetConversionCost(typeof(int), typeof(string)));
        }
예제 #2
0
        public void Nullable_types_are_equivalent_to_non_nullable_types_of_same_underlying_type()
        {
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), EmptyArray <IConversionRule> .Instance);

            Assert.AreEqual(ConversionCost.Zero, converter.GetConversionCost(typeof(int?), typeof(int)));
            Assert.AreEqual(ConversionCost.Zero, converter.GetConversionCost(typeof(int), typeof(int?)));
            Assert.AreEqual(ConversionCost.Zero, converter.GetConversionCost(typeof(int?), typeof(int?)));
        }
예제 #3
0
        public void Nullable_target_type_is_translated_before_being_passed_to_the_conversion_rule()
        {
            var mockRule  = MockRepository.GenerateMock <IConversionRule>();
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), new[] { mockRule });

            mockRule.Expect(x => x.GetConversionCost(typeof(int), typeof(double), converter)).Return(ConversionCost.Best);
            Assert.AreEqual(ConversionCost.Best, converter.GetConversionCost(typeof(int?), typeof(double)));
            Assert.AreEqual(ConversionCost.Best, converter.GetConversionCost(typeof(int), typeof(double?)));
            Assert.AreEqual(ConversionCost.Best, converter.GetConversionCost(typeof(int?), typeof(double?)));
            mockRule.VerifyAllExpectations();
        }
예제 #4
0
        public void Recursive_conversions_attempts_are_denied()
        {
            var mockRule  = MockRepository.GenerateMock <IConversionRule>();
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), new[] { mockRule });

            mockRule.Expect(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Do((GetConversionCostDelegate) delegate
            {
                Assert.AreEqual(ConversionCost.Invalid, converter.GetConversionCost(typeof(int), typeof(string)));
                return(ConversionCost.Best);
            });

            Assert.AreEqual(ConversionCost.Best, converter.GetConversionCost(typeof(int), typeof(string)));
            mockRule.VerifyAllExpectations();
        }
예제 #5
0
        public void GetConversionCost_chooses_least_costly_rule()
        {
            var mockRule0 = MockRepository.GenerateStub <IConversionRule>();
            var mockRule1 = MockRepository.GenerateStub <IConversionRule>();
            var mockRule2 = MockRepository.GenerateStub <IConversionRule>();
            var converter = new RuleBasedConverter(new DefaultExtensionPoints(), new[] { mockRule0, mockRule1, mockRule2 });;

            mockRule0.Stub(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Return(ConversionCost.Best);
            mockRule1.Stub(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Return(ConversionCost.Invalid);
            mockRule2.Stub(x => x.GetConversionCost(typeof(int), typeof(string), converter)).Return(ConversionCost.Default);
            Assert.AreEqual(ConversionCost.Best, converter.GetConversionCost(typeof(int), typeof(string)));
        }