Пример #1
0
        private static TestClass GetTestClass(Type testClass, Type collectionType)
        {
            var testCollection = GetTestCollection(testClass, collectionType);
            var typeInfo       = new ReflectionTypeInfo(testClass);
            var retval         = new TestClass(testCollection, typeInfo);

            return(retval);
        }
Пример #2
0
        internal static TestMethod CreateTestMethod(Type @class, string name)
        {
            var assemblyInfo   = new ReflectionAssemblyInfo(@class.Assembly);
            var testAssembly   = new TestAssembly(assemblyInfo);
            var testCollection = new TestCollection(testAssembly, null, string.Empty);
            var classInfo      = new ReflectionTypeInfo(@class);
            var testClass      = new TestClass(testCollection, classInfo);
            var method         = testClass.Class.GetMethods(true).First(m => m.Name == name);
            var testMethod     = new TestMethod(testClass, method);

            return(testMethod);
        }
        List <MethodDefinition> AddPostInjectMethods(
            TypeDefinition typeDef, TypeReference genericTypeDef, ReflectionTypeInfo typeInfo)
        {
            var postInjectMethods = new List <MethodDefinition>();

            for (int i = 0; i < typeInfo.InjectMethods.Count; i++)
            {
                postInjectMethods.Add(
                    AddPostInjectMethod(
                        TypeAnalyzer.ReflectionBakingInjectMethodPrefix + i, typeInfo.InjectMethods[i], typeDef, genericTypeDef));
            }

            return(postInjectMethods);
        }
        List <MethodDefinition> AddFieldSetters(
            TypeDefinition typeDef, TypeReference genericTypeDef, ReflectionTypeInfo typeInfo)
        {
            var methodDefs = new List <MethodDefinition>();

            for (int i = 0; i < typeInfo.InjectFields.Count; i++)
            {
                methodDefs.Add(
                    AddSetterMethod(
                        TypeAnalyzer.ReflectionBakingFieldSetterPrefix + i,
                        typeInfo.InjectFields[i].FieldInfo, typeDef, genericTypeDef));
            }

            return(methodDefs);
        }
Пример #5
0
        List <MethodDefinition> AddPropertySetters(
            TypeDefinition typeDef, TypeReference genericTypeDef, ReflectionTypeInfo typeInfo)
        {
            var methodDefs = new List <MethodDefinition>();

            for (int i = 0; i < typeInfo.InjectProperties.Count; i++)
            {
                methodDefs.Add(
                    AddSetterMethod(
                        TypeAnalyzer.ReflectionBakingPropertySetterPrefix + i.ToString(),
                        typeInfo.InjectProperties[i].PropertyInfo, typeDef, genericTypeDef));
            }

            return(methodDefs);
        }
Пример #6
0
    public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions,
                                                 ITestMethod testMethod, IAttributeInfo factAttribute)
    {
        var result = new List <IXunitTestCase>();
        var types  = factAttribute.GetNamedArgument <Type[]>("Types");

        foreach (var type in types)
        {
            var typeInfo          = new ReflectionTypeInfo(type);
            var genericMethodInfo = testMethod.Method.MakeGenericMethod(typeInfo);
            var genericTestMethod = new GenericTestMethod(testMethod.TestClass, genericMethodInfo, typeInfo);
            result.Add(
                new XunitTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(),
                                  genericTestMethod));
        }
        return(result);
    }
Пример #7
0
        public void ReturnsCorrectCustomAttributes(Type classType, IEnumerable <string> expectedTraits)
        {
            var testAssembly   = new TestAssembly(new ReflectionAssemblyInfo(classType.Assembly));
            var testCollection = new TestCollection(testAssembly, null, "Trait inheritance tests");
            var @class         = new ReflectionTypeInfo(classType);
            var testClass      = new TestClass(testCollection, @class);
            var methodInfo     = new ReflectionMethodInfo(classType.GetMethod("TraitsTest") !);
            var testMethod     = new TestMethod(testClass, methodInfo);
            var testCase       = new TestableXunitTestCase(testMethod);

            var testTraits = testCase.Traits["Test"];

            Assert.NotNull(testTraits);
            foreach (var expectedTrait in expectedTraits)
            {
                Assert.Contains(expectedTrait, testTraits);
            }
        }
        void CreateGetInfoMethod(
            TypeDefinition typeDef, TypeReference genericTypeDef, ReflectionTypeInfo typeInfo,
            MethodDefinition factoryMethod, List <MethodDefinition> fieldSetMethods,
            List <MethodDefinition> propertySetMethods, List <MethodDefinition> postInjectMethods)
        {
            var getInfoMethodDef = new MethodDefinition(
                TypeAnalyzer.ReflectionBakingGetInjectInfoMethodName,
                MethodAttributes.Private | MethodAttributes.HideBySig |
                MethodAttributes.Static,
                _zenjectTypeInfoType);

            typeDef.Methods.Add(getInfoMethodDef);

            getInfoMethodDef.CustomAttributes.Add(
                new CustomAttribute(_preserveConstructor));

            var returnValueVar = new VariableDefinition(_module.TypeSystem.Object);

            var body = getInfoMethodDef.Body;

            body.Variables.Add(returnValueVar);
            body.InitLocals = true;

            var instructions = new List <Instruction>();

            instructions.Add(Instruction.Create(OpCodes.Ldtoken, genericTypeDef));
            instructions.Add(Instruction.Create(OpCodes.Call, _getTypeFromHandleMethod));

            if (factoryMethod == null)
            {
                instructions.Add(Instruction.Create(OpCodes.Ldnull));
            }
            else
            {
                instructions.Add(Instruction.Create(OpCodes.Ldnull));
                instructions.Add(Instruction.Create(OpCodes.Ldftn, factoryMethod.ChangeDeclaringType(genericTypeDef)));
                instructions.Add(Instruction.Create(OpCodes.Newobj, _funcConstructor));
            }

            instructions.Add(Instruction.Create(OpCodes.Ldc_I4, typeInfo.InjectConstructor.Parameters.Count));
            instructions.Add(Instruction.Create(OpCodes.Newarr, _injectableInfoType));

            for (int i = 0; i < typeInfo.InjectConstructor.Parameters.Count; i++)
            {
                var injectableInfo = typeInfo.InjectConstructor.Parameters[i].InjectableInfo;

                instructions.Add(Instruction.Create(OpCodes.Dup));
                instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i));

                EmitNewInjectableInfoInstructions(
                    instructions, injectableInfo, typeDef);

                instructions.Add(Instruction.Create(OpCodes.Stelem_Ref));
            }

            instructions.Add(Instruction.Create(OpCodes.Newobj, _constructorInfoConstructor));

            instructions.Add(Instruction.Create(OpCodes.Ldc_I4, typeInfo.InjectMethods.Count));
            instructions.Add(Instruction.Create(OpCodes.Newarr, _injectMethodInfoType));

            Assert.IsEqual(postInjectMethods.Count, typeInfo.InjectMethods.Count);

            for (int i = 0; i < typeInfo.InjectMethods.Count; i++)
            {
                var injectMethodInfo = typeInfo.InjectMethods[i];

                instructions.Add(Instruction.Create(OpCodes.Dup));
                instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i));

                AddInjectableMethodInstructions(
                    instructions, injectMethodInfo, typeDef, genericTypeDef, postInjectMethods[i]);

                instructions.Add(Instruction.Create(OpCodes.Stelem_Ref));
            }

            instructions.Add(Instruction.Create(OpCodes.Ldc_I4, fieldSetMethods.Count + propertySetMethods.Count));
            instructions.Add(Instruction.Create(OpCodes.Newarr, _injectMemberInfoType));

            for (int i = 0; i < fieldSetMethods.Count; i++)
            {
                var injectField = typeInfo.InjectFields[i];

                instructions.Add(Instruction.Create(OpCodes.Dup));
                instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i));

                AddInjectableMemberInstructions(
                    instructions,
                    injectField.InjectableInfo, injectField.FieldInfo.Name,
                    typeDef, genericTypeDef, fieldSetMethods[i]);

                instructions.Add(Instruction.Create(OpCodes.Stelem_Ref));
            }

            for (int i = 0; i < propertySetMethods.Count; i++)
            {
                var injectProperty = typeInfo.InjectProperties[i];

                instructions.Add(Instruction.Create(OpCodes.Dup));
                instructions.Add(Instruction.Create(OpCodes.Ldc_I4, fieldSetMethods.Count + i));

                AddInjectableMemberInstructions(
                    instructions,
                    injectProperty.InjectableInfo,
                    injectProperty.PropertyInfo.Name, typeDef, genericTypeDef,
                    propertySetMethods[i]);

                instructions.Add(Instruction.Create(OpCodes.Stelem_Ref));
            }

            instructions.Add(Instruction.Create(OpCodes.Newobj, _zenjectTypeInfoConstructor));

            instructions.Add(Instruction.Create(OpCodes.Stloc_0));
            instructions.Add(Instruction.Create(OpCodes.Ldloc_S, returnValueVar));
            instructions.Add(Instruction.Create(OpCodes.Ret));

            var processor = body.GetILProcessor();

            foreach (var instruction in instructions)
            {
                processor.Append(instruction);
            }
        }
        MethodDefinition TryAddFactoryMethod(
            TypeDefinition typeDef, ReflectionTypeInfo typeInfo)
        {
            if (typeInfo.Type.GetParentTypes().Any(x => x.FullName == "UnityEngine.Component"))
            {
                Assert.That(typeInfo.InjectConstructor.Parameters.IsEmpty());
                return(null);
            }

            if (typeInfo.InjectConstructor.ConstructorInfo == null)
            {
                // static classes, abstract types
                return(null);
            }

            var factoryMethod = new MethodDefinition(
                TypeAnalyzer.ReflectionBakingFactoryMethodName,
                MethodAttributes.Private | MethodAttributes.HideBySig |
                MethodAttributes.Static,
                _module.TypeSystem.Object);

            var p1 = new ParameterDefinition(_objectArrayType);

            p1.Name = "P_0";
            factoryMethod.Parameters.Add(p1);

            var body = factoryMethod.Body;

            body.InitLocals = true;

            var processor = body.GetILProcessor();

            var returnValueVar = new VariableDefinition(_module.TypeSystem.Object);

            body.Variables.Add(returnValueVar);

            processor.Emit(OpCodes.Nop);

            Assert.IsNotNull(typeInfo.InjectConstructor);

            var args = typeInfo.InjectConstructor.Parameters;

            for (int i = 0; i < args.Count; i++)
            {
                var arg = args[i];

                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Ldc_I4, i);
                processor.Emit(OpCodes.Ldelem_Ref);

                EmitCastOperation(
                    processor, arg.ParameterInfo.ParameterType, typeDef.GenericParameters);
            }

            processor.Emit(OpCodes.Newobj, _module.Import(typeInfo.InjectConstructor.ConstructorInfo));

            processor.Emit(OpCodes.Stloc_0);
            processor.Emit(OpCodes.Ldloc_S, returnValueVar);
            processor.Emit(OpCodes.Ret);

            typeDef.Methods.Add(factoryMethod);

            return(factoryMethod);
        }