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 AnalyzeExpectedResult(SyntaxNodeAnalysisContext context, ImmutableArray <AttributeArgumentSyntax> attributeNamedArguments, IMethodSymbol methodSymbol) { var expectedResultNamedArgument = attributeNamedArguments.SingleOrDefault( _ => _.DescendantTokens().Any(__ => __.Text == NunitFrameworkConstants.NameOfExpectedResult)); if (expectedResultNamedArgument != null) { var methodReturnValueType = methodSymbol.ReturnType; if (methodReturnValueType.SpecialType == SpecialType.System_Void) { context.ReportDiagnostic(Diagnostic.Create( TestMethodUsageAnalyzer.CreateDescriptor( TestMethodUsageAnalyzerConstants.SpecifiedExpectedResultForVoidMethodMessage), expectedResultNamedArgument.GetLocation())); } else { if (!expectedResultNamedArgument.CanAssignTo(methodReturnValueType, context.SemanticModel)) { context.ReportDiagnostic(Diagnostic.Create( TestMethodUsageAnalyzer.CreateDescriptor( string.Format(TestMethodUsageAnalyzerConstants.ExpectedResultTypeMismatchMessage, methodReturnValueType.MetadataName)), expectedResultNamedArgument.GetLocation())); } } } }
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)); } } } }