Ejemplo n.º 1
0
        private static void HandlePublicSetter(bool isMake, bool hasEvents, PropertyInfo baseProperty, List <string> propertyImplementations)
        {
            var setMethod     = baseProperty.SetMethod;
            var setVisibility = setMethod.IsPublic ? string.Empty : CodeTemplates.GetVisibility(setMethod.IsFamily, setMethod.IsFamilyOrAssembly);

            if (isMake)
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertySetForMake(setVisibility));
            }
            else if (setMethod.GetParameters()[0].ParameterType.IsSpanLike())
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertySetForSpanLike(setVisibility));
            }
            else
            {
                var setArgumentNameList = setMethod.GetArgumentNameList();
                var setDelegateCast     = setMethod.GetDelegateCast();

                if (setMethod.GetParameters().Length > 0)
                {
                    var setExpectationChecks           = setMethod.GetExpectationChecks();
                    var setExpectationExceptionMessage = setMethod.GetExpectationExceptionMessage();
                    propertyImplementations.Add(PropertyTemplates.GetPropertySet(
                                                    setMethod.MetadataToken, setArgumentNameList, setExpectationChecks, setDelegateCast,
                                                    setExpectationExceptionMessage, setVisibility, hasEvents));
                }
                else
                {
                    propertyImplementations.Add(PropertyTemplates.GetPropertySetAndNoIndexers(
                                                    setMethod.MetadataToken, setArgumentNameList, setDelegateCast, setVisibility, hasEvents));
                }
            }
        }
Ejemplo n.º 2
0
        private static bool HandleNonPrivateAbstractProperty(SortedSet <string> namespaces, bool requiresObsoleteSuppression,
                                                             List <string> generatedProperties, PropertyMockableResult property, PropertyInfo baseProperty, ParameterInfo[] indexers, MethodInfo propertyMethod)
        {
            var propertyImplementations = new List <string>();
            var visibility = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                             string.Empty : CodeTemplates.GetVisibility(propertyMethod.IsFamily, propertyMethod.IsFamilyOrAssembly);

            if (property.Accessors == PropertyAccessors.Get || property.Accessors == PropertyAccessors.GetAndSet)
            {
                var getVisibility = CodeTemplates.GetVisibility(baseProperty.GetMethod.IsFamily, baseProperty.GetMethod.IsFamilyOrAssembly);

                if (getVisibility == visibility)
                {
                    getVisibility = string.Empty;
                }

                propertyImplementations.Add(PropertyTemplates.GetNonPublicPropertyGet(getVisibility));
            }

            if (property.Accessors == PropertyAccessors.Set || property.Accessors == PropertyAccessors.GetAndSet)
            {
                var setVisibility = CodeTemplates.GetVisibility(baseProperty.SetMethod.IsFamily, baseProperty.SetMethod.IsFamilyOrAssembly);

                if (setVisibility == visibility)
                {
                    setVisibility = string.Empty;
                }

                propertyImplementations.Add(PropertyTemplates.GetNonPublicPropertySet(setVisibility));
            }

            var explicitInterfaceName = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                                        $"{property.Value.DeclaringType.GetFullName(namespaces)}." : string.Empty;

            if (indexers.Length > 0)
            {
                var parameters = string.Join(", ",
                                             from indexer in indexers
                                             let _ = namespaces.Add(indexer.ParameterType.Namespace)
                                                     select $"{indexer.ParameterType.Name} {indexer.Name}");

                // Indexer
                generatedProperties.Add(PropertyTemplates.GetNonPublicPropertyIndexer(visibility,
                                                                                      $"{baseProperty.PropertyType.GetFullName(namespaces)}", parameters,
                                                                                      string.Join(Environment.NewLine, propertyImplementations), explicitInterfaceName));
            }
            else
            {
                // Normal
                generatedProperties.Add(PropertyTemplates.GetNonPublicProperty(visibility,
                                                                               $"{baseProperty.PropertyType.GetFullName(namespaces)}", baseProperty.Name,
                                                                               string.Join(Environment.NewLine, propertyImplementations), explicitInterfaceName));
            }

            requiresObsoleteSuppression |= baseProperty.GetCustomAttribute <ObsoleteAttribute>() != null;
            return(requiresObsoleteSuppression);
        }
Ejemplo n.º 3
0
        private static void HandlePublicGetter(SortedSet <string> namespaces, bool isMake, bool hasEvents,
                                               PropertyInfo baseProperty, List <string> propertyImplementations)
        {
            var getMethod     = baseProperty.GetMethod;
            var getVisibility = getMethod.IsPublic ? string.Empty : CodeTemplates.GetVisibility(getMethod.IsFamily, getMethod.IsFamilyOrAssembly);
            var returnType    = $"{getMethod.ReturnType.GetFullName(namespaces, getMethod.ReturnParameter)}";

            if (isMake)
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertyGetForMake(getVisibility));
            }
            else if (getMethod.ReturnType.IsSpanLike())
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertyGetForSpanLike(getVisibility));
            }
            else
            {
                var getArgumentNameList = getMethod.GetArgumentNameList();
                var getDelegateCast     = getMethod.GetDelegateCast();

                if (getMethod.GetParameters().Length > 0)
                {
                    var getExpectationChecks           = getMethod.GetExpectationChecks();
                    var getExpectationExceptionMessage = getMethod.GetExpectationExceptionMessage();
                    propertyImplementations.Add(getMethod.ReturnType.RequiresExplicitCast() ?
                                                PropertyTemplates.GetPropertyGetWithValueTypeReturnValue(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType,
                                                    getExpectationChecks, getDelegateCast, getExpectationExceptionMessage, getVisibility, hasEvents) :
                                                PropertyTemplates.GetPropertyGetWithReferenceTypeReturnValue(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType,
                                                    getExpectationChecks, getDelegateCast, getExpectationExceptionMessage, getVisibility, hasEvents));
                }
                else
                {
                    propertyImplementations.Add(getMethod.ReturnType.RequiresExplicitCast() ?
                                                PropertyTemplates.GetPropertyGetWithValueTypeReturnValueAndNoIndexers(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType, getDelegateCast, getVisibility, hasEvents) :
                                                PropertyTemplates.GetPropertyGetWithReferenceTypeReturnValueAndNoIndexers(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType, getDelegateCast, getVisibility, hasEvents));
                }
            }
        }
Ejemplo n.º 4
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, MethodInformationBuilder builder)
        {
            var requiresObsoleteSuppression = false;
            var generatedEvents             = new List <string>();

            foreach (var @event in baseType.GetMockableEvents(generator))
            {
                var eventHandlerType     = @event.EventHandlerType;
                var eventHandlerTypeInfo = eventHandlerType;

                namespaces.Add(eventHandlerType.Namespace);

                var eventMethod = @event.AddMethod;

                var methodInformation = builder.Build(new MockableResult <MethodInfo>(
                                                          eventMethod, RequiresExplicitInterfaceImplementation.No));
                var @override = methodInformation.DescriptionWithOverride.Contains("override", StringComparison.Ordinal) ? "override " : string.Empty;

                if (eventMethod.IsPublic)
                {
                    generatedEvents.Add(EventTemplates.GetEvent(@override,
                                                                eventHandlerType.GetFullName(namespaces), @event.Name));
                }
                else if (!eventMethod.IsPrivate && eventMethod.IsAbstract)
                {
                    var visibility = CodeTemplates.GetVisibility(eventMethod.IsFamily, eventMethod.IsFamilyOrAssembly);

                    generatedEvents.Add(EventTemplates.GetNonPublicEvent(visibility,
                                                                         eventHandlerType.GetFullName(namespaces), @event.Name));
                }

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

            var result = generatedEvents.Count > 0 ? EventTemplates.GetEvents(generatedEvents.AsReadOnly()) : string.Empty;

            return(new GenerateResults(result, requiresObsoleteSuppression, false));
        }
Ejemplo n.º 5
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));
        }