Exemple #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>() }));
        }
Exemple #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>() }));
        }
        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));
        }
        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());
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #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))
                                        ));
 }