Пример #1
0
        public void Default_Convention_Test()
        {
            var pairs = GooseTypePairs.Scan(options =>
            {
                options.FromAssemblyOf <Duck>().ToAssemblyOf <Duck>().WithDefaultConvention();
            });

            Assert.True(CheckPairs(pairs, new GooseTypePair[] { GooseTypePair.Create <Duck, IDuck>() }));
        }
Пример #2
0
        public void Convention_Test()
        {
            var pairs = GooseTypePairs.Scan(options =>
            {
                options.FromAssemblyOf <Duck>().ToAssemblyOf <Duck>().WithConvention((sourceType, targetType)
                                                                                     => targetType.Name == "IStandard" + sourceType.Name);
            });

            Assert.True(CheckPairs(pairs, new GooseTypePair[] { GooseTypePair.Create <Fish, IStandardFish>() }));
        }
Пример #3
0
        public void Not_Wrapped_If_Exception_Not_Registered()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(GooseTypePair.Create <Food, IFood>());

            Assert.Throws <FoodExpiredException>(() => personTarget.Eat(foodTarget));
        }
Пример #4
0
        public void Interchangable_For_System_Exception_class_Registered()
        {
            Person  person       = new Person();
            Poison  poison       = new Poison();
            IPoison poisonTarget = poison.As <IPoison>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Poison, IPoison>(),
                GooseTypePair.Create <Exception, IException>());

            var ex = Assert.Throws <WrappedException <IException> >(() => personTarget.Drink(poisonTarget));

            Assert.NotNull(ex.Exception);
            Assert.Equal(typeof(Exception), ex.Exception.GetSource <Exception>().GetType());
        }
Пример #5
0
        public void Different_Number_Of_Parameter_Overload_Test()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Food, IFood>(),
                GooseTypePair.Create <Remain, IRemain>());

            personTarget.Eat(foodTarget, 90);
        }
Пример #6
0
        public void Same_Number_of_Parameter_Overload_Test()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Fruit, IFruit>(),
                GooseTypePair.Create <Food, IFood>(),
                GooseTypePair.Create <Remain, IRemain>());

            personTarget.EatOne(foodTarget);
        }
Пример #7
0
        public void Ambiguous_If_Multiple_Register()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Food, IFood>(),
                GooseTypePair.Create <Fruit, IFood>(),
                GooseTypePair.Create <Remain, IRemain>());

            Assert.Throws <GooseAmbiguousMatchException>(() => personTarget.EatOne(foodTarget));
        }
Пример #8
0
        public void Interchangable_If_Registered()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Food, IFood>(),
                GooseTypePair.Create <Remain, IRemain>());

            IRemain remain = personTarget.Eat(foodTarget, 90, out var enough);

            Assert.Equal(food.FoodCalories, person.CaloriesTaken + remain.RemainCalories);
        }
Пример #9
0
        public void Not_Implemented_If_Not_Registered()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>();

            Assert.Throws <GooseNotImplementedException>(() => personTarget.Eat(foodTarget, 90, out var enough));

            // <Remain, IRemain> is not registered
            IPerson personTarget2 = person.As <IPerson>(GooseTypePair.Create <Food, IFood>());

            Assert.Throws <GooseNotImplementedException>(() => personTarget.Eat(foodTarget, 90, out var enough));
        }
Пример #10
0
        public void Interchangable_If_Exception_Registered()
        {
            Person person = new Person();
            Food   food   = new Food {
                FoodCalories = 100
            };

            IFood   foodTarget   = food.As <IFood>();
            IPerson personTarget = person.As <IPerson>(
                GooseTypePair.Create <Food, IFood>(),
                GooseTypePair.Create <FoodExpiredException, IFoodExpiredException>());

            var ex = Assert.Throws <WrappedException <IFoodExpiredException> >(() => personTarget.Eat(foodTarget));

            Assert.NotNull(ex.Exception);
            Assert.Same(food, ex.Exception.ExpiredFood.GetSource <Food>());
            Assert.Same(food, ex.Exception.GetSource <FoodExpiredException>().ExpiredFood);
        }
Пример #11
0
 void ISelector.Populate(List <GooseTypePair> pairs)
 {
     pairs.AddRange(_targets.SelectMany(target => _sources.Where(source =>
                                                                 _convention.Any(conv => conv.IsValidPair(source, target))
                                                                 ).Select(source => GooseTypePair.Create(source, target))
                                        ));
 }