Пример #1
0
        public static void GetNonPublicFunctionImplementation() =>
        Assert.That(MethodTemplates.GetNonPublicFunctionImplementation("a", "b", "c", typeof(int), "e", "f"), Is.EqualTo(
                        @"fa e override b
{
	c	
	
	return default!;
}"));
Пример #2
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, MethodInformationBuilder informationBuilder, bool isMake,
                                                 Action <MethodInfo, MethodInformation> handleRefOutMethod, bool hasEvents)
        {
            var requiresObsoleteSuppression = false;
            var methods          = baseType.GetMockableMethods(generator);
            var generatedMethods = new List <string>(methods.Count);

            foreach (var method in methods)
            {
                var methodInformation = informationBuilder.Build(method);
                var baseMethod        = method.Value;
                var argumentNameList  = baseMethod.GetArgumentNameList();
                var outInitializers   = !methodInformation.ContainsDelegateConditions ? string.Empty : baseMethod.GetOutInitializers();

                if (baseMethod.IsPublic)
                {
                    var visibility = method.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                                     string.Empty : CodeTemplates.Public;

                    // Either the base method contains no Span<T> or ReadOnlySpan<T>
                    // and it doesn't have refs /outs, or the user specified a delegate
                    // to use to handle that method (remember, types with methods with refs/outs are gen'd
                    // each time, and that's the only reason the handlers are passed in).
                    if (!methodInformation.IsSpanLike)
                    {
                        if (isMake || !methodInformation.ContainsDelegateConditions || !string.IsNullOrWhiteSpace(methodInformation.DelegateCast))
                        {
                            if (!methodInformation.ContainsDelegateConditions && baseMethod.GetParameters().Length > 0)
                            {
                                generatedMethods.Add(MethodsGenerator.GenerateMethodWithNoRefOutParameters(
                                                         baseMethod, methodInformation.DelegateCast, argumentNameList, outInitializers,
                                                         methodInformation.DescriptionWithOverride, visibility,
                                                         method.RequiresNewImplementation, namespaces, isMake, hasEvents));
                            }
                            else
                            {
                                generatedMethods.Add(MethodsGenerator.GenerateMethodWithRefOutOrNoParameters(
                                                         baseMethod, methodInformation.DelegateCast, argumentNameList, outInitializers, methodInformation.DescriptionWithOverride,
                                                         visibility, method.RequiresNewImplementation,
                                                         namespaces, isMake, hasEvents));

                                if (methodInformation.ContainsDelegateConditions)
                                {
                                    handleRefOutMethod(baseMethod, methodInformation);
                                }
                            }
                        }
                        else
                        {
                            generatedMethods.Add(MethodTemplates.GetNotImplementedMethod(methodInformation.DescriptionWithOverride));
                        }
                    }
                    else
                    {
                        generatedMethods.Add(MethodTemplates.GetNotImplementedMethod(methodInformation.DescriptionWithOverride));
                    }

                    requiresObsoleteSuppression |= baseMethod.GetCustomAttribute <ObsoleteAttribute>() != null;
                }
                else if (!baseMethod.IsPrivate && baseMethod.IsAbstract)
                {
                    var visibility = CodeTemplates.GetVisibility(baseMethod.IsFamily, baseMethod.IsFamilyOrAssembly);

                    generatedMethods.Add(baseMethod.ReturnType != typeof(void) ?
                                         MethodTemplates.GetNonPublicFunctionImplementation(visibility, methodInformation.Description,
                                                                                            outInitializers, baseMethod.ReturnType,
                                                                                            method.RequiresNewImplementation == RequiresIsNewImplementation.Yes ? "new" : string.Empty,
                                                                                            baseMethod.ReturnParameter.GetAttributes(true, namespaces)) :
                                         MethodTemplates.GetNonPublicActionImplementation(visibility, methodInformation.Description,
                                                                                          outInitializers, method.RequiresNewImplementation == RequiresIsNewImplementation.Yes ? "new" : string.Empty));

                    requiresObsoleteSuppression |= baseMethod.GetCustomAttribute <ObsoleteAttribute>() != null;
                }
            }

            return(new GenerateResults(string.Join(Environment.NewLine, generatedMethods),
                                       requiresObsoleteSuppression, false));
        }