private static void AnalyzeExpectedResult(SyntaxNodeAnalysisContext context,
                                                  AttributeSyntax attributeNode, IMethodSymbol methodSymbol)
        {
            var(_, attributeNamedArguments) = attributeNode.GetArguments();

            var expectedResultNamedArgument = attributeNamedArguments.SingleOrDefault(
                _ => _.DescendantTokens().Any(_ => _.Text == NunitFrameworkConstants.NameOfExpectedResult));

            if (expectedResultNamedArgument != null)
            {
                ExpectedResultSupplied(context, methodSymbol, attributeNode, expectedResultNamedArgument);
            }
            else
            {
                NoExpectedResultSupplied(context, methodSymbol, attributeNode);
            }
        }
        private static int?GetOptionalEnumValue(SyntaxNodeAnalysisContext context, AttributeSyntax attributeNode)
        {
            var(attributePositionalArguments, _) = attributeNode.GetArguments();
            var noExplicitEnumArgument = attributePositionalArguments.Length == 0;

            if (noExplicitEnumArgument)
            {
                return(ParallelizableUsageAnalyzerConstants.ParallelScope.Self);
            }
            else
            {
                var arg           = attributePositionalArguments[0];
                var constantValue = context.SemanticModel.GetConstantValue(arg.Expression);
                if (constantValue.HasValue)
                {
                    return(constantValue.Value as int?);
                }
            }

            return(null);
        }
Beispiel #3
0
        public static SourceAttributeInformation?ExtractInfoFromAttribute(
            SyntaxNodeAnalysisContext context,
            AttributeSyntax attributeSyntax)
        {
            var(positionalArguments, _) = attributeSyntax.GetArguments();

            if (positionalArguments.Length < 1)
            {
                return(null);
            }

            var firstArgumentExpression = positionalArguments[0]?.Expression;

            if (firstArgumentExpression == null)
            {
                return(null);
            }

            // TestCaseSourceAttribute has the following constructors:
            // * TestCaseSourceAttribute(Type sourceType)
            // * TestCaseSourceAttribute(Type sourceType, string sourceName)
            // * TestCaseSourceAttribute(Type sourceType, string sourceName, object?[]? methodParams)
            // * TestCaseSourceAttribute(string sourceName)
            // * TestCaseSourceAttribute(string sourceName, object?[]? methodParams)
            // and ValueSource has:
            // * ValueSource(Type sourceType, string sourceName)
            // * ValueSource(string sourceName)
            if (firstArgumentExpression is TypeOfExpressionSyntax typeofSyntax)
            {
                var sourceType = context.SemanticModel.GetSymbolInfo(typeofSyntax.Type).Symbol as INamedTypeSymbol;
                return(ExtractElementsInAttribute(context, sourceType, positionalArguments, 1));
            }
            else
            {
                var sourceType = context.ContainingSymbol.ContainingType;
                return(ExtractElementsInAttribute(context, sourceType, positionalArguments, 0));
            }
        }
        private static void AnalyzeExpectedResult(SyntaxNodeAnalysisContext context,
                                                  AttributeSyntax attributeNode, IMethodSymbol methodSymbol)
        {
            var attributePositionalAndNamedArguments = attributeNode.GetArguments();
            var attributeNamedArguments = attributePositionalAndNamedArguments.Item2;

            var methodReturnValueType = methodSymbol.ReturnType;

            var expectedResultNamedArgument = attributeNamedArguments.SingleOrDefault(
                _ => _.DescendantTokens().Any(__ => __.Text == NunitFrameworkConstants.NameOfExpectedResult));

            if (expectedResultNamedArgument != null)
            {
                if (methodReturnValueType.SpecialType == SpecialType.System_Void)
                {
                    context.ReportDiagnostic(Diagnostic.Create(specifiedExpectedResultForVoid,
                                                               expectedResultNamedArgument.GetLocation()));
                }
                else
                {
                    if (!expectedResultNamedArgument.CanAssignTo(methodReturnValueType, context.SemanticModel))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(expectedResultTypeMismatch,
                                                                   expectedResultNamedArgument.GetLocation(), methodReturnValueType.MetadataName));
                    }
                }
            }
            else
            {
                if (methodReturnValueType.SpecialType != SpecialType.System_Void)
                {
                    context.ReportDiagnostic(Diagnostic.Create(noExpectedResultButNonVoidReturnType,
                                                               attributeNode.GetLocation()));
                }
            }
        }