private static AttributeSyntax[] GetExpectedExceptionAttributes(NUnitFramework.Symbols nunit, AttributeWithSymbol[] attributesWithSymbols) { return(attributesWithSymbols.Where(x => nunit.ExpectedException.Equals(x.Symbol)) .Select(x => x.Attribute) .ToArray()); }
internal override INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit, Compilation compilation) { return(new[] { nunit.Suite, nunit.RequiredAddin }); }
private bool IsNUnitAttributeSymbol(AttributeSyntax attributeSyntax, NUnitFramework.Symbols nunit, SemanticModel semanticModel, Compilation compilation) { var attributeSymbol = semanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingSymbol; var analyzedAttributeSymbols = GetAnalyzedAttributeSymbols(nunit, compilation); return(analyzedAttributeSymbols.Any(analyzedAttr => analyzedAttr.Equals(attributeSymbol))); }
private static AttributeSyntax[] GetExceptionFreeTestCaseAttributeNodes(NUnitFramework.Symbols nunit, AttributeWithSymbol[] attributesWithSymbols, bool isExpectedException) { return(attributesWithSymbols .Where(x => IsTestCaseAttributeNotExpectingException(x.Attribute, x.Symbol, nunit, isExpectedException)) .Select(x => x.Attribute) .ToArray()); }
protected override INamedTypeSymbol[] GetMemberAccessContainingClassSymbolsEligibleForFix( NUnitFramework.Symbols nunit) { return(new[] { nunit.Text, nunit.Is }); }
internal override INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit, Compilation compilation) { return(new[] { nunit.TestCase }); }
internal override INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit, Compilation compilation) { var mtaThreadAttribute = compilation.GetTypeByMetadataName("System.MTAThreadAttribute"); var staThreadAttribute = compilation.GetTypeByMetadataName("System.STAThreadAttribute"); return(mtaThreadAttribute == null || staThreadAttribute == null ? new INamedTypeSymbol[] { } : new[] { mtaThreadAttribute, staThreadAttribute }); }
private static void AnalyzeMethod(SyntaxNodeAnalysisContext context, NUnitFramework.Symbols nunit) { var methodSyntax = (MethodDeclarationSyntax)context.Node; if (ExceptionExpectancyMethodModel.TryFindDiagnostic(methodSyntax, context.SemanticModel, nunit, out Diagnostic diagnostic)) { context.ReportDiagnostic(diagnostic); } }
public ExceptionExpectancyCodeAction(Document document, MethodDeclarationSyntax method, SemanticModel semanticModel, NUnitFramework.Symbols nunit) { _document = document; _method = method; _model = new ExceptionExpectancyMethodModel(method, semanticModel, nunit); _clusters = TestCaseExceptionEquivalenceCluster.CreateMany(_model); _methodLineSeparator = GetMethodLineSeparator(method); Debug.Assert(_clusters.Length > 0, "_clusters.Length > 0"); }
private static bool IsTestCaseAttributeNotExpectingException(AttributeSyntax attribute, ISymbol symbol, NUnitFramework.Symbols nunit, bool doesExpectedExceptionAttributeAlsoExist) { if (!nunit.TestCase.Equals(symbol) || doesExpectedExceptionAttributeAlsoExist) { return(false); } return(attribute.ArgumentList == null || attribute.ArgumentList.Arguments.All(arg => !DefinesExpectedException(arg))); }
internal override INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit, Compilation compilation) { return(new[] { nunit.RequiresMTA, nunit.RequiresSTA, nunit.TestFixtureSetUp, nunit.TestFixtureTearDown }); }
private static bool TryGetFirstExpectedExceptionAttribute(NUnitFramework.Symbols nunit, AttributeWithSymbol[] attributesWithSymbols, out ExpectedExceptionAttribute expectedException) { var expectedExceptionNode = GetExpectedExceptionAttributes(nunit, attributesWithSymbols)?.FirstOrDefault(); if (expectedExceptionNode != null) { expectedException = new ExpectedExceptionAttribute(expectedExceptionNode); return(true); } expectedException = null; return(false); }
private static bool IsTestCaseAttributeExpectingException(AttributeSyntax attribute, ISymbol symbol, NUnitFramework.Symbols nunit, bool doesExpectedExceptionAttributeAlsoExist) { if (!nunit.TestCase.Equals(symbol)) { return(false); } if (doesExpectedExceptionAttributeAlsoExist) { return(true); } return(attribute.ArgumentList != null && attribute.ArgumentList.Arguments.Any(DefinesExpectedException)); }
private bool DoesMemberAccessSymbolMatchNUnit( SyntaxNodeAnalysisContext context, NUnitFramework.Symbols nunit, MemberAccessExpressionSyntax memberAccess) { var containingClassSymbol = context.SemanticModel.GetSymbolInfo(memberAccess).Symbol?.ContainingSymbol; var allowedContainingClassSymbols = GetMemberAccessContainingClassSymbolsEligibleForFix(nunit); // since member access may be defined in client code too, we need to distinguish as we migrate nunit only if (containingClassSymbol == null || !allowedContainingClassSymbols.Any(classSymbol => classSymbol.Equals(containingClassSymbol))) { return(false); } return(true); }
public static bool TryFindDiagnostic(MethodDeclarationSyntax methodSyntax, SemanticModel semanticModel, NUnitFramework.Symbols nunit, out Diagnostic diagnostic) { var eligibleAttributes = GetEligibleAttributes(methodSyntax, semanticModel, nunit); if (eligibleAttributes.Length <= 0) { diagnostic = null; return(false); } var diagnosticLocation = eligibleAttributes.Length == 1 ? eligibleAttributes.First().GetLocation() : methodSyntax.Identifier.GetLocation(); var methodName = methodSyntax.Identifier.Text; diagnostic = Diagnostic.Create(Descriptors.ExceptionExpectancy, diagnosticLocation, methodName); return(true); }
private void Analyze(SyntaxNodeAnalysisContext context, NUnitFramework.Symbols nunit) { var containerNode = (TMemberAccessContainerNode)context.Node; if (!TryGetMemberAccess(containerNode, out MemberAccessExpressionSyntax memberAccess)) { return; } if (!FindOldApiAndProposedFix(context, nunit, memberAccess)) { return; } context.ReportDiagnostic(Diagnostic.Create( DiagnosticDescriptor, memberAccess.GetLocation(), $"{memberAccess}", $"{CreateReplaceWithTargetString(CreateFixedContainer(containerNode))}")); }
public ExceptionExpectancyMethodModel(MethodDeclarationSyntax method, SemanticModel semanticModel, NUnitFramework.Symbols nunit) { var attributesWithSymbols = GetAttributesWithSymbols(method, semanticModel); var attributes = new List <ExceptionExpectancyAtAttributeLevel>(); var isExpectedException = TryGetFirstExpectedExceptionAttribute(nunit, attributesWithSymbols, out ExpectedExceptionAttribute expectedException); if (isExpectedException) { attributes.Add(expectedException); } var exceptionRelatedTestCases = GetExceptionRelatedTestCases(nunit, attributesWithSymbols, isExpectedException, expectedException); attributes.AddRange(exceptionRelatedTestCases); ExceptionRelatedAttributes = attributes.ToArray(); ExceptionFreeTestCaseAttributeNodes = GetExceptionFreeTestCaseAttributeNodes(nunit, attributesWithSymbols, isExpectedException); }
private static TestCaseExpectingExceptionAttribute[] GetExceptionRelatedTestCases(NUnitFramework.Symbols nunit, AttributeWithSymbol[] attributesWithSymbols, bool isExpectedException, ExpectedExceptionAttribute expectedException) { return(attributesWithSymbols .Where(x => IsTestCaseAttributeExpectingException(x.Attribute, x.Symbol, nunit, isExpectedException)) .Select(x => new TestCaseExpectingExceptionAttribute(x.Attribute, expectedException)) .ToArray()); }
internal abstract INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit, Compilation compilation);
private void CheckAttributeSymbolsAndAnalyze(SyntaxNodeAnalysisContext context, NUnitFramework.Symbols nunit, Compilation compilation) { var attributeSyntax = (AttributeSyntax)context.Node; var semanticModel = context.SemanticModel; if (!IsNUnitAttributeSymbol(attributeSyntax, nunit, semanticModel, compilation)) { return; } Analyze(context, attributeSyntax); }
private static AttributeSyntax[] GetEligibleAttributes(BaseMethodDeclarationSyntax method, SemanticModel semanticModel, NUnitFramework.Symbols nunit) { var attributesWithSymbols = GetAttributesWithSymbols(method, semanticModel); var expectedExceptionAttributes = GetExpectedExceptionAttributes(nunit, attributesWithSymbols); var doesExpectedExceptionAttributeAlsoExist = expectedExceptionAttributes.Any(); var testCaseAttributes = attributesWithSymbols.Where(x => IsTestCaseAttributeExpectingException(x.Attribute, x.Symbol, nunit, doesExpectedExceptionAttributeAlsoExist)) .Select(x => x.Attribute); return(expectedExceptionAttributes.Union(testCaseAttributes).ToArray()); }
public void RegisterAnalysis(CompilationStartAnalysisContext context, NUnitFramework.Symbols nunit) { context.RegisterSyntaxNodeAction(syntaxNodeAnalysisContext => Analyze(syntaxNodeAnalysisContext, nunit), ContainerSyntaxKind); }
private bool FindOldApiAndProposedFix(SyntaxNodeAnalysisContext context, NUnitFramework.Symbols nunit, MemberAccessExpressionSyntax memberAccess) { return(MemberAccessMigrationTable.TryGetFixExpression(memberAccess, out ExpressionSyntax _) && DoesMemberAccessSymbolMatchNUnit(context, nunit, memberAccess)); }
protected abstract INamedTypeSymbol[] GetMemberAccessContainingClassSymbolsEligibleForFix( NUnitFramework.Symbols nunit);