private void AnalyzeSyntax(SyntaxNodeAnalysisContext context) { if (context.Node is MethodDeclarationSyntax methodSyntax && methodSyntax.AttributeLists.Any()) { if (methodSyntax.Parent is ClassDeclarationSyntax classDeclaration && ((classDeclaration.BaseList?.Types.Any() ?? false) || (classDeclaration.Members.Any(x => x.IsKind(SyntaxKind.ConstructorDeclaration))))) { return; } foreach (var attribute in methodSyntax.AttributeLists.SelectMany(x => x.Attributes)) { var attributeSymbol = context.SemanticModel.GetSymbolInfo(attribute).Symbol as IMethodSymbol; if (AttributesRecognizer.IsTestAttribute(attributeSymbol) || AttributesRecognizer.IsTestCaseAttribute(attributeSymbol) || AttributesRecognizer.IsTestCaseSourceAttribute(attributeSymbol)) { var diagnostic = Diagnostic.Create(Rule, methodSyntax.GetLocation(), methodSyntax.Identifier.ValueText); context.ReportDiagnostic(diagnostic); return; } } } }
public override void VisitAttribute(AttributeSyntax node) { base.VisitAttribute(node); var symbolInfo = _semanticModel.GetSymbolInfo(node).Symbol; if (AttributesRecognizer.IsTestCaseAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasTestCase = true; } if (AttributesRecognizer.IsTestAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasTestAttribute = true; } if (AttributesRecognizer.IsTestCaseSourceAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasTestCaseSourceAttribute = true; } if (AttributesRecognizer.IsSetUpAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasSetUp = true; return; } if (AttributesRecognizer.IsTearDownAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasTearDown = true; return; } if (AttributesRecognizer.IsOneTimeSetUpAttribute(symbolInfo)) { _methodDeclarationContext.Current.HasOneTimeSetUp = true; return; } }
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) { var newProperty = base.VisitPropertyDeclaration(node) as PropertyDeclarationSyntax; if (_classDeclarationContext.Current.TestCaseSources.Any(x => x.ValueText == newProperty.Identifier.ValueText)) { if (node.Type is GenericNameSyntax genericType && genericType.TypeArgumentList.Arguments.Count == 1 && genericType.TypeArgumentList.Arguments.First() is IdentifierNameSyntax closedType) // This is always IdentifierNameSyntax because it is IEnumerable<TestCaseData> type. { var genericSymbol = _semanticModel.GetSymbolInfo(closedType).Symbol; if (!AttributesRecognizer.IsTestCaseData(genericSymbol)) { return(newProperty); } newProperty = newProperty.WithType(genericType.WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.ArrayType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.ObjectKeyword) ), SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier( SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression()))) ) ) ) ) ); } } return(newProperty); }
public override SyntaxNode VisitUsingDirective(UsingDirectiveSyntax node) { var symbolInfo = _semanticModel.GetSymbolInfo(node.Name).Symbol; var inner = base.VisitUsingDirective(node); if (AttributesRecognizer.IsNUnitUsingDirective(symbolInfo)) { return(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Xunit"))); } return(inner); }
public override SyntaxNode VisitAttribute(AttributeSyntax node) { var symbolInfo = _semanticModel.GetSymbolInfo(node).Symbol; var newAttribute = base.VisitAttribute(node) as AttributeSyntax; if (AttributesRecognizer.IsTestAttribute(symbolInfo)) { var newAttributeName = _methodDeclarationContext.Current.HasTestCase || _methodDeclarationContext.Current.HasTestCaseSourceAttribute ? "Theory" : "Fact"; return(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(newAttributeName))); } if (AttributesRecognizer.IsTestCaseAttribute(symbolInfo)) { return(newAttribute.WithName(SyntaxFactory.IdentifierName("InlineData"))); } if (AttributesRecognizer.IsTestCaseSourceAttribute(symbolInfo) && newAttribute.ArgumentList.Arguments.Count == 1) { return(newAttribute.WithName(SyntaxFactory.IdentifierName("MemberData"))); } return(newAttribute); }
public override void VisitAttribute(AttributeSyntax node) { base.VisitAttribute(node); var symbolInfo = _semanticModel.GetSymbolInfo(node).Symbol; if (AttributesRecognizer.IsTestCaseSourceAttribute(symbolInfo)) { foreach (var arg in node.ArgumentList.Arguments.Select(x => x.Expression)) { if (arg is InvocationExpressionSyntax invocationExp && invocationExp.Expression is IdentifierNameSyntax identifierName && identifierName.Identifier.ValueText == "nameof") { var member = invocationExp.ArgumentList.Arguments.First().Expression as IdentifierNameSyntax; _classDeclarationContext.Current.TestCaseSources.Add(member.Identifier); } if (arg is LiteralExpressionSyntax literalExp && literalExp.IsKind(SyntaxKind.StringLiteralExpression)) { _classDeclarationContext.Current.TestCaseSources.Add(literalExp.Token); } } } if (AttributesRecognizer.IsTearDownAttribute(symbolInfo)) { _classDeclarationContext.Current.HasTearDown = true; } if (AttributesRecognizer.IsOneTimeSetUpAttribute(symbolInfo)) { _classDeclarationContext.Current.HasOneTimeSetup = true; } if (AttributesRecognizer.IsOneTimeSetUpAttribute(symbolInfo)) { _collectIndentifiers = true; } }
public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { var newObjectCreation = base.VisitObjectCreationExpression(node) as ObjectCreationExpressionSyntax; if (!AttributesRecognizer.IsTestCaseDataCtor(_semanticModel.GetSymbolInfo(newObjectCreation).Symbol)) { return(newObjectCreation); } var arrayExprssion = SyntaxFactory.ArrayType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.ObjectKeyword) ), SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier( SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression()))) ); IEnumerable <ExpressionSyntax> args = newObjectCreation.ArgumentList.Arguments.Select(x => x.Expression); var arrayInitializerExpression = SyntaxFactory.InitializerExpression(SyntaxKind.ArrayInitializerExpression, SyntaxFactory.SeparatedList(args)); var arrayCreationExpression = SyntaxFactory.ArrayCreationExpression(arrayExprssion, arrayInitializerExpression); return(arrayCreationExpression); }