private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) { var attributeInfo = SourceHelpers.GetSourceAttributeInformation( context, NUnitFrameworkConstants.FullNameOfTypeValueSourceAttribute, NUnitFrameworkConstants.NameOfValueSourceAttribute); if (attributeInfo is null) { return; } var stringConstant = attributeInfo.SourceName; var syntaxNode = attributeInfo.SyntaxNode; if (syntaxNode is null || stringConstant is null) { return; } var symbol = SourceHelpers.GetMember(attributeInfo); if (symbol is null) { context.ReportDiagnostic(Diagnostic.Create( missingSourceDescriptor, syntaxNode.GetLocation(), stringConstant)); } else { SourceHelpers.ReportToUseNameOfIfApplicable( context, syntaxNode, attributeInfo, symbol, stringConstant, considerNameOfDescriptor); if (!symbol.IsStatic) { context.ReportDiagnostic(Diagnostic.Create( sourceNotStaticDescriptor, syntaxNode.GetLocation(), stringConstant)); } ReportIfSymbolNotIEnumerable(context, symbol, syntaxNode); if (symbol is IMethodSymbol methodSymbol && methodSymbol.Parameters.Length != 0) { context.ReportDiagnostic(Diagnostic.Create( methodExpectParameters, syntaxNode.GetLocation(), methodSymbol.Parameters.Length)); } } }
private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) { var attributeInfo = SourceHelpers.GetSourceAttributeInformation( context, NUnitFrameworkConstants.FullNameOfTypeTestCaseSourceAttribute, NUnitFrameworkConstants.NameOfTestCaseSourceAttribute); if (attributeInfo is null) { return; } var attributeNode = (AttributeSyntax)context.Node; var stringConstant = attributeInfo.SourceName; if (stringConstant is null && attributeNode.ArgumentList?.Arguments.Count == 1) { // The Type argument in this form represents the class that provides test cases. // It must have a default constructor and implement IEnumerable. var sourceType = attributeInfo.SourceType; bool typeImplementsIEnumerable = sourceType.IsIEnumerable(out _); bool typeHasDefaultConstructor = sourceType.Constructors.Any(c => c.Parameters.IsEmpty); if (!typeImplementsIEnumerable) { context.ReportDiagnostic(Diagnostic.Create( sourceTypeNotIEnumerableDescriptor, attributeNode.ArgumentList.Arguments[0].GetLocation(), sourceType.Name)); } else if (!typeHasDefaultConstructor) { context.ReportDiagnostic(Diagnostic.Create( sourceTypeNoDefaultConstructorDescriptor, attributeNode.ArgumentList.Arguments[0].GetLocation(), sourceType.Name)); } return; } var syntaxNode = attributeInfo.SyntaxNode; if (syntaxNode is null || stringConstant is null) { return; } var symbol = SourceHelpers.GetMember(attributeInfo); if (symbol is null) { context.ReportDiagnostic(Diagnostic.Create( missingSourceDescriptor, syntaxNode.GetLocation(), stringConstant)); } else { SourceHelpers.ReportToUseNameOfIfApplicable( context, syntaxNode, attributeInfo, symbol, stringConstant, considerNameOfDescriptor); if (!symbol.IsStatic) { context.ReportDiagnostic(Diagnostic.Create( sourceNotStaticDescriptor, syntaxNode.GetLocation(), stringConstant)); } switch (symbol) { case IPropertySymbol property: ReportIfSymbolNotIEnumerable(context, syntaxNode, property.Type); ReportIfParametersSupplied(context, syntaxNode, attributeInfo.NumberOfMethodParameters, "properties"); break; case IFieldSymbol field: ReportIfSymbolNotIEnumerable(context, syntaxNode, field.Type); ReportIfParametersSupplied(context, syntaxNode, attributeInfo.NumberOfMethodParameters, "fields"); break; case IMethodSymbol method: ReportIfSymbolNotIEnumerable(context, syntaxNode, method.ReturnType); var methodParametersFromAttribute = attributeInfo.NumberOfMethodParameters ?? 0; if (method.Parameters.Length != methodParametersFromAttribute) { context.ReportDiagnostic(Diagnostic.Create( mismatchInNumberOfParameters, syntaxNode.GetLocation(), methodParametersFromAttribute, method.Parameters.Length)); } break; } } }