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."); }
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."); }
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."); }
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."); }
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); }
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); }
public void GetTypeDifferenceWeightWithAllNulls() { Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(null, null)); }
public void GetTypeDifferenceWeightWithNullArgumentTypes() { Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(ReflectionUtils.GetParameterTypes(typeof(Fable).GetConstructor(new Type[] { typeof(FableCharacter) }).GetParameters()), null)); }
public void GetTypeDifferenceWeightWithNullParameterTypes() { Assert.Throws <NullReferenceException>(() => AutowireUtils.GetTypeDifferenceWeight(null, new object[] {})); }
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."); }