Esempio n. 1
0
        private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
        {
            var methodNode = context.Node.Ancestors().OfType <MethodDeclarationSyntax>().FirstOrDefault();

            if (methodNode != null)
            {
                if (!methodNode.ContainsDiagnostics)
                {
                    var testCaseType = context.SemanticModel.Compilation.GetTypeByMetadataName(NunitFrameworkConstants.FullNameOfTypeTestCaseAttribute);
                    var testType     = context.SemanticModel.Compilation.GetTypeByMetadataName(NunitFrameworkConstants.FullNameOfTypeTestAttribute);

                    if (testCaseType == null && testType == null)
                    {
                        return;
                    }

                    var attributeNode   = (AttributeSyntax)context.Node;
                    var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeNode).Symbol;

                    var isTestCaseAttribute = IsAttribute(testCaseType, NunitFrameworkConstants.NameOfTestCaseAttribute, attributeSymbol);
                    var isTestAttribute     = IsAttribute(testType, NunitFrameworkConstants.NameOfTestAttribute, attributeSymbol);

                    if (isTestCaseAttribute || isTestAttribute)
                    {
                        context.CancellationToken.ThrowIfCancellationRequested();

                        var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);

                        context.CancellationToken.ThrowIfCancellationRequested();
                        TestMethodUsageAnalyzer.AnalyzeExpectedResult(context, attributeNode, methodSymbol);
                    }
                }
            }
        }
        private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
        {
            var declarationNode = context.Node.Parent?.Parent;

            if (declarationNode is MethodDeclarationSyntax methodNode)
            {
                if (!methodNode.ContainsDiagnostics)
                {
                    var testCaseType = context.SemanticModel.Compilation.GetTypeByMetadataName(NunitFrameworkConstants.FullNameOfTypeTestCaseAttribute);
                    var testType     = context.SemanticModel.Compilation.GetTypeByMetadataName(NunitFrameworkConstants.FullNameOfTypeTestAttribute);

                    if (testCaseType == null || testType == null)
                    {
                        return;
                    }

                    var attributeNode   = (AttributeSyntax)context.Node;
                    var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeNode).Symbol;

                    var isTestCaseAttribute = IsAttribute(testCaseType, NunitFrameworkConstants.NameOfTestCaseAttribute, attributeSymbol);
                    var isTestAttribute     = IsAttribute(testType, NunitFrameworkConstants.NameOfTestAttribute, attributeSymbol);

                    if (isTestCaseAttribute || (isTestAttribute && !HasITestBuilderAttribute(context.SemanticModel, methodNode.AttributeLists)))
                    {
                        context.CancellationToken.ThrowIfCancellationRequested();

                        var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);

                        context.CancellationToken.ThrowIfCancellationRequested();
                        TestMethodUsageAnalyzer.AnalyzeExpectedResult(context, attributeNode, methodSymbol);
                    }

                    var parameters                     = methodNode.ParameterList.Parameters;
                    var testMethodParameters           = parameters.Count;
                    var hasISimpleTestBuilderAttribute = HasISimpleTestBuilderAttribute(context.SemanticModel, methodNode.AttributeLists);
                    var hasITestBuilderAttribute       = HasITestBuilderAttribute(context.SemanticModel, methodNode.AttributeLists);
                    var parametersMarkedWithIParameterDataSourceAttribute =
                        parameters.Count(p => HasIParameterDataSourceAttribute(context.SemanticModel, p.AttributeLists));

                    if (testMethodParameters > 0 &&
                        hasISimpleTestBuilderAttribute &&
                        !hasITestBuilderAttribute &&
                        parametersMarkedWithIParameterDataSourceAttribute < testMethodParameters)
                    {
                        context.ReportDiagnostic(
                            Diagnostic.Create(
                                simpleTestHasParameters,
                                attributeNode.GetLocation(),
                                testMethodParameters,
                                parametersMarkedWithIParameterDataSourceAttribute));
                    }
                }
            }
        }