Пример #1
0
        public void GetTypeDifferenceWeightForZeroArgCtor()
        {
            int expectedWeight = 0;
            int actualWeight   = AutowireUtils.GetTypeDifferenceWeight(
                ReflectionUtils.GetParameterTypes(typeof(Fable).GetConstructor(Type.EmptyTypes).GetParameters()), new object[] { });

            Assert.AreEqual(expectedWeight, actualWeight, "AutowireUtils.GetTypeDifferenceWeight() was wrong, evidently.");
        }
Пример #2
0
        public void GetTypeDifferenceWeightWithSameTypes()
        {
            int expectedWeight = 0;
            int actualWeight   = AutowireUtils.GetTypeDifferenceWeight(
                ReflectionUtils.GetParameterTypes(typeof(Fool).GetConstructor(new Type[] { typeof(string) }).GetParameters()),
                new object[] { "Noob" });

            Assert.AreEqual(expectedWeight, actualWeight, "AutowireUtils.GetTypeDifferenceWeight() was wrong, evidently.");
        }
Пример #3
0
        public void GetTypeDifferenceWeightWhenPassingExactTypeMatch()
        {
            int expectedWeight = 0;
            int actualWeight   = AutowireUtils.GetTypeDifferenceWeight(
                ReflectionUtils.GetParameterTypes(typeof(Fable).GetConstructor(
                                                      new Type[] { typeof(EnglishCharacter) }).GetParameters()),
                new object[] { new EnglishCharacter() });

            Assert.AreEqual(expectedWeight, actualWeight, "AutowireUtils.GetTypeDifferenceWeight() was wrong, evidently.");
        }
Пример #4
0
        public void GetTypeDifferenceWeightWhenPassingDerivedTypeArgsToBaseTypeCtor()
        {
            int expectedWeight = 2;
            int actualWeight   = AutowireUtils.GetTypeDifferenceWeight(
                ReflectionUtils.GetParameterTypes(typeof(Fable).GetConstructor(
                                                      new Type[] { typeof(NurseryRhymeCharacter) }).GetParameters()),
                new object[] { new EnglishCharacter() });

            Assert.AreEqual(expectedWeight, actualWeight, "AutowireUtils.GetTypeDifferenceWeight() was wrong, evidently.");
        }
Пример #5
0
            public int GetTypeDifferenceWeight(Type[] paramTypes)
            {
                // If valid arguments found, determine type difference weight.
                // Try type difference weight on both the converted arguments and
                // the raw arguments. If the raw weight is better, use it.
                // Decrease raw weight by 1024 to prefer it over equal converted weight.
                int typeDiffWeight    = AutowireUtils.GetTypeDifferenceWeight(paramTypes, this.arguments);
                int rawTypeDiffWeight = AutowireUtils.GetTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;

                return(rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight);
            }
Пример #6
0
        private static ConstructorInfo AssertTypeWeightingPicksCorrectCtor(object[] arguments, Type ctorType)
        {
            ConstructorInfo pickedCtor = null;

            // we want to pick the ctor that has the lowest type difference weight...
            ConstructorInfo[] ctors = ctorType.GetConstructors();
            int weighting           = Int32.MaxValue;

            foreach (ConstructorInfo ctor in ctors)
            {
                ParameterInfo[] parameters = ctor.GetParameters();
                if (parameters.Length == arguments.Length)
                {
                    Type[] paramTypes = ReflectionUtils.GetParameterTypes(parameters);
                    int    weight     = AutowireUtils.GetTypeDifferenceWeight(paramTypes, arguments);
                    if (weight < weighting)
                    {
                        pickedCtor = ctor;
                        weighting  = weight;
                    }
                }
            }
            return(pickedCtor);
        }
Пример #7
0
 public void GetTypeDifferenceWeightWithAllNulls()
 {
     Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(null, null));
 }
Пример #8
0
 public void GetTypeDifferenceWeightWithNullArgumentTypes()
 {
     Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(ReflectionUtils.GetParameterTypes(typeof(Fable).GetConstructor(new Type[] { typeof(FableCharacter) }).GetParameters()), null));
 }
Пример #9
0
 public void GetTypeDifferenceWeightWithNullParameterTypes()
 {
     Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(null, new object[] {}));
 }
Пример #10
0
 public void GetTypeDifferenceWeightWithMismatchedLengths()
 {
     Assert.Throws <ArgumentException>(() => AutowireUtils.GetTypeDifferenceWeight(new Type[] {}, new object[] { 1 }), "Cannot calculate the type difference weight for argument types and arguments with differing lengths.");
 }