Example #1
0
        public bool Apply(MethodDefinition method)
        {
            if (!this.DoesApply(method))
            {
                return(false);
            }

            var wrapper = this.WrapTestMethod(ref method);

            foreach (var attr in method.CustomAttributes)
            {
                if (attr.AttributeType.FullName.Contains("DebuggerStepThrough") || attr.AttributeType.FullName.Contains("AsyncStateMachine"))
                {
                    continue;
                }

                wrapper.CustomAttributes.Add(attr);
            }

            foreach (var attr in wrapper.CustomAttributes)
            {
                method.CustomAttributes.Remove(attr);
            }

            method.DeclaringType.Methods.Add(wrapper);

            XUnitTransformation.MarkAsRewritten(method);
            XUnitTransformation.MarkAsRewritten(wrapper);

            return(true);
        }
Example #2
0
        private static MethodDefinition WrapTestMethodStatic(MethodDefinition method, ModuleDefinition module, string wrapperName)
        {
            var wrapper = new MethodDefinition(
                wrapperName,
                MethodAttributes.Static | MethodAttributes.Public,
                module.ImportReference(typeof(void)));

            var constructors = typeof(XUnitTestWrapper).GetConstructors();

            var instanceConstructor = XUnitTransformation.GetInstanceConstructor(constructors);

            var staticConstructor = constructors.First(c => !object.ReferenceEquals(c, instanceConstructor));

            var testWrapperConstructor = module.ImportReference(staticConstructor);

            var typeofReference = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle"));

            var ilProcessor = wrapper.Body.GetILProcessor();

            ilProcessor.Body.Instructions.Clear();
            wrapper.Body.Variables.Clear();

            var localVariable = new VariableDefinition(module.ImportReference(typeof(XUnitTestWrapper)));

            wrapper.Body.Variables.Add(localVariable);
            wrapper.Body.InitLocals = true;

            var argsLoadInstruction = ilProcessor.Create(OpCodes.Ldnull);

            if (method.HasParameters)
            {
                argsLoadInstruction = ilProcessor.Create(OpCodes.Ldarg_0);

                var argsParameter = new ParameterDefinition("args", ParameterAttributes.None, method.Module.ImportReference(typeof(object[])));

                var paramsAttributeCtor = module.ImportReference(typeof(ParamArrayAttribute).GetConstructor(Type.EmptyTypes));
                argsParameter.CustomAttributes.Add(new CustomAttribute(paramsAttributeCtor));
                wrapper.Parameters.Add(argsParameter);
            }

            // We are going to write the following:

            /* using XUnitTestWrapper xunitTestWrapper = new XUnitTestWrapper(typeof(method.DeclaringType), method.Name, method.HasParameters ? args : null)
             * {
             *     XUnitTestTemplates.RunTestInCoyote(new Func<Task>(xunitTestWrapper.Invoke);
             * }
             */

            ilProcessor.Append(ilProcessor.Create(OpCodes.Ldtoken, method.DeclaringType));
            ilProcessor.Append(ilProcessor.Create(OpCodes.Call, typeofReference));
            ilProcessor.Append(ilProcessor.Create(OpCodes.Ldstr, method.Name));
            ilProcessor.Append(argsLoadInstruction);
            return(WrapperBody(wrapper, testWrapperConstructor, ilProcessor, module));
        }
Example #3
0
        private bool DoesApply(MethodDefinition method)
        {
            var rewrittenAttributeTypeFullName = typeof(XunitCoyoteRewrittenAttribute).FullName;

            if (!method.HasCustomAttributes || method.CustomAttributes.Any(a => a.AttributeType.FullName == rewrittenAttributeTypeFullName))
            {
                return(false);
            }

            return(method.CustomAttributes.Any(a =>
            {
                var inheritedTypes = XUnitTransformation.InheritanceChain(a.AttributeType.Resolve());
                return inheritedTypes.Contains(XUnitTransformation.FactAttributeName) || inheritedTypes.Contains(XUnitTransformation.TheoryAttributeName);
            }));
        }