Пример #1
0
 private static AttributeSyntax[] GetExpectedExceptionAttributes(NUnitFramework.Symbols nunit,
                                                                 AttributeWithSymbol[] attributesWithSymbols)
 {
     return(attributesWithSymbols.Where(x => nunit.ExpectedException.Equals(x.Symbol))
            .Select(x => x.Attribute)
            .ToArray());
 }
Пример #2
0
 internal override INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit,
                                                                  Compilation compilation)
 {
     return(new[]
     {
         nunit.Suite, nunit.RequiredAddin
     });
 }
Пример #3
0
        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)));
        }
Пример #4
0
 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
     });
 }
Пример #6
0
 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 });
        }
Пример #8
0
        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);
            }
        }
Пример #9
0
        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");
        }
Пример #10
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
     });
 }
Пример #12
0
        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);
        }
Пример #13
0
        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));
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        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))}"));
        }
Пример #17
0
        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);
        }
Пример #18
0
 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());
 }
Пример #19
0
 internal abstract INamedTypeSymbol[] GetAnalyzedAttributeSymbols(NUnitFramework.Symbols nunit,
                                                                  Compilation compilation);
Пример #20
0
        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);
        }
Пример #21
0
        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());
        }
Пример #22
0
 public void RegisterAnalysis(CompilationStartAnalysisContext context, NUnitFramework.Symbols nunit)
 {
     context.RegisterSyntaxNodeAction(syntaxNodeAnalysisContext =>
                                      Analyze(syntaxNodeAnalysisContext, nunit), ContainerSyntaxKind);
 }
Пример #23
0
 private bool FindOldApiAndProposedFix(SyntaxNodeAnalysisContext context,
                                       NUnitFramework.Symbols nunit, MemberAccessExpressionSyntax memberAccess)
 {
     return(MemberAccessMigrationTable.TryGetFixExpression(memberAccess, out ExpressionSyntax _) &&
            DoesMemberAccessSymbolMatchNUnit(context, nunit, memberAccess));
 }
Пример #24
0
 protected abstract INamedTypeSymbol[] GetMemberAccessContainingClassSymbolsEligibleForFix(
     NUnitFramework.Symbols nunit);