示例#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);