Пример #1
0
        public MetaEntityClass FindClass(string className)
        {
            if (MetaEntityTools.IsMultinodeExpressionPath(className))
            {
                return(Parent.SelectTargetByPath(className) as MetaEntityClass);
            }

            return(Declarations.FirstOrDefault(x => x.name.Equals(className)));
        }
Пример #2
0
        private Declaration GetGetter()
        {
            if (TargetDeclaration == null)
            {
                return(null);
            }

            if (TargetDeclaration.DeclarationType != DeclarationType.PropertyLet &&
                TargetDeclaration.DeclarationType != DeclarationType.PropertySet)
            {
                return(TargetDeclaration);
            }

            var getter = Declarations.FirstOrDefault(item => item.Scope == TargetDeclaration.Scope &&
                                                     item.IdentifierName == TargetDeclaration.IdentifierName &&
                                                     item.DeclarationType == DeclarationType.PropertyGet);

            return(getter ?? TargetDeclaration);
        }
Пример #3
0
        public override IMetaEntityExpressionTarget SelectTarget(string expressionPathNode)
        {
            if (MetaEntityTools.IsMultinodeExpressionPath(expressionPathNode))
            {
                return(Parent.SelectTargetByPath(expressionPathNode));
            }

            var head = Declarations.FirstOrDefault(x => x.name.Equals(expressionPathNode));

            if (head == null)
            {
                MetaEntityClass item = new MetaEntityClass();
                item.name   = expressionPathNode;
                item.Parent = this;

                Declarations.Add(item);
                head = item;
            }


            return(head);
        }
Пример #4
0
        public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
        {
            if (context.Node is ClassDeclarationSyntax cls)
            {
                ISymbol currentType = ModelExtensions.GetDeclaredSymbol(context.SemanticModel, cls);
                if (HasNamedAttribute(
                        currentType,
                        "Microsoft.DotNet.Internal.Testing.DependencyInjection.Abstractions.TestDependencyInjectionSetupAttribute"
                        ))
                {
                    bool error  = false;
                    var  nested = cls.Parent as ClassDeclarationSyntax;
                    if (nested == null)
                    {
                        Diagnostics.Add(Error(DiagnosticIds.NestedClassRequired, $"Class {cls.Identifier.Text} must be nested inside class to use [TestDependencyInjectionSetupAttribute]", cls));
                        error = true;
                    }

                    if (cls.Modifiers.All(m => m.Kind() != SyntaxKind.StaticKeyword))
                    {
                        Diagnostics.Add(Error(DiagnosticIds.StaticClassRequired, $"Class {cls.Identifier.Text} must be declared static to use [TestDependencyInjectionSetupAttribute]", nested));
                        error = true;
                    }

                    if (!HasDependencyInjectionReference(context))
                    {
                        Diagnostics.Add(
                            Error(
                                DiagnosticIds.DependencyInjectionRequired,
                                "Missing reference to Microsoft.Extensions.DependencyInjection to use [TestDependencyInjectionSetupAttribute]",
                                cls
                                )
                            );
                        error = true;
                    }

                    if (error)
                    {
                        return;
                    }

                    if (nested.Modifiers.All(m => m.Kind() != SyntaxKind.PartialKeyword))
                    {
                        Diagnostics.Add(Error(DiagnosticIds.PartialClassRequired, $"Class {nested.Identifier.Text} must be declared partial to use [TestDependencyInjectionSetupAttribute]", nested));
                        error = true;
                    }

                    if (!(nested.Parent is NamespaceDeclarationSyntax))
                    {
                        Diagnostics.Add(Error(DiagnosticIds.NamespaceRequired, $"Class {nested.Identifier.Text} must be inside a namespace to use [TestDependencyInjectionSetupAttribute]", nested));
                        error = true;
                    }

                    if (error)
                    {
                        return;
                    }

                    ISymbol parentType = ModelExtensions.GetDeclaredSymbol(context.SemanticModel, nested);
                    Declarations.Add(
                        new TestConfigDeclaration(
                            parentType.ContainingNamespace.FullName(),
                            nested.Identifier.Text,
                            currentType,
                            cls
                            )
                        );
                }
            }

            if (context.Node is MethodDeclarationSyntax method)
            {
                var sMethod = ModelExtensions.GetDeclaredSymbol(context.SemanticModel, method) as IMethodSymbol;
                TestConfigDeclaration decl = Declarations.FirstOrDefault(
                    d => d.DeclaringClassSymbol.Equals(sMethod.ContainingType, SymbolEqualityComparer.Default)
                    );

                if (decl == null)
                {
                    return;
                }

                if (sMethod.Parameters.Length == 0)
                {
                    return;
                }

                if (sMethod.Parameters[0].Type.FullName() !=
                    "Microsoft.Extensions.DependencyInjection.IServiceCollection")
                {
                    return;
                }

                string configType;
                bool   isConfigurationAsync;
                bool   hasFetch;
                bool   isFetchAsync;
                if (sMethod.ReturnsVoid)
                {
                    configType           = null;
                    isConfigurationAsync = false;
                    isFetchAsync         = false;
                    hasFetch             = false;
                }
                else if (!(sMethod.ReturnType is INamedTypeSymbol returnType))
                {
                    return;
                }
                else if (returnType.FullName() == "System.Threading.Tasks.Task")
                {
                    configType           = null;
                    isConfigurationAsync = true;
                    isFetchAsync         = false;
                    hasFetch             = false;
                }
                else if (returnType.IsGenericType)
                {
                    if (returnType.ConstructedFrom.FullName() == "System.Action<T>" && returnType.TypeArguments[0].FullName() == "System.IServiceProvider")
                    {
                        configType           = null;
                        hasFetch             = true;
                        isFetchAsync         = false;
                        isConfigurationAsync = false;
                    }
                    else if (returnType.ConstructedFrom.FullName() == "System.Func<T,TResult>")
                    {
                        isConfigurationAsync = false;
                        hasFetch             = true;

                        if (returnType.TypeArguments[0].FullName() != "System.IServiceProvider")
                        {
                            return;
                        }


                        ITypeSymbol funcReturn = returnType.TypeArguments[1];
                        if (funcReturn.FullName() == "System.Threading.Tasks.Task")
                        {
                            configType   = null;
                            isFetchAsync = true;
                        }
                        else if (funcReturn is INamedTypeSymbol {
                            IsGenericType : true
                        } namedFuncReturn&&
                                 namedFuncReturn.ConstructedFrom.FullName() == "System.Threading.Tasks.Task<TResult>")
                        {
                            configType   = namedFuncReturn.TypeArguments[0].FullName();
                            isFetchAsync = true;
                        }
                        else
                        {
                            configType   = funcReturn.FullName();
                            isFetchAsync = false;
                        }
                    }
                    else if (returnType.ConstructedFrom.FullName() == "System.Threading.Tasks.Task<TResult>")
                    {
                        isConfigurationAsync = true;

                        if (returnType.TypeArguments[0] is INamedTypeSymbol {
                            IsGenericType : true
                        } asyncReturn)
                        {
                            if (asyncReturn.ConstructedFrom.FullName() == "System.Action<T>" && asyncReturn.TypeArguments[0].FullName() == "System.IServiceProvider")
                            {
                                configType   = null;
                                hasFetch     = true;
                                isFetchAsync = false;
                            }
                            else if (asyncReturn.ConstructedFrom.FullName() == "System.Func<T,TResult>" && asyncReturn.TypeArguments[0].FullName() == "System.IServiceProvider")
                            {
                                hasFetch = true;
                                ITypeSymbol funcReturn = asyncReturn.TypeArguments[1];
                                if (funcReturn.FullName() == "System.Threading.Tasks.Task")
                                {
                                    configType   = null;
                                    isFetchAsync = true;
                                }
                                else if (funcReturn is INamedTypeSymbol {
                                    IsGenericType : true
                                } namedFuncReturn&&
                                         namedFuncReturn.ConstructedFrom.FullName() == "System.Threading.Tasks.Task<TResult>")
                                {
                                    configType   = namedFuncReturn.TypeArguments[0].FullName();
                                    isFetchAsync = true;
                                }
                                else
                                {
                                    configType   = funcReturn.FullName();
                                    isFetchAsync = false;
                                }
                            }
                            else
                            {
                                return;
                            }
                        }
Пример #5
0
        public MetaEntity CreateEntity(String EntityClassName, String entityName = "")
        {
            var metaClass = Declarations.FirstOrDefault(x => x.name == EntityClassName);

            return(metaClass.CreateEntity(this, entityName));
        }