public static MsTestAttributeDefinitions FromCompilation(Compilation compilation)
        {
            MsTestAttributeDefinitions definitions = new MsTestAttributeDefinitions()
            {
                TestMethodSymbol         = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.TestMethodAttribute.FullName),
                DataTestMethodSymbol     = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.DataTestMethodAttribute.FullName),
                TestClassSymbol          = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.TestClassAttribute.FullName),
                ClassInitializeSymbol    = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.ClassInitializeAttribute.FullName),
                ClassCleanupSymbol       = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.ClassCleanupAttribute.FullName),
                AssemblyInitializeSymbol = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.AssemblyInitializeAttribute.FullName),
                AssemblyCleanupSymbol    = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.AssemblyCleanupAttribute.FullName),
                TestInitializeSymbol     = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.TestInitializeAttribute.FullName),
                TestCleanupSymbol        = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.TestCleanupAttribute.FullName),
                DataRowSymbol            = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.DataRowAttribute.FullName),
                DynamicDataSymbol        = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.DynamicDataAttribute.FullName),
                TestCategorySymbol       = compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.TestCategoryAttribute.FullName),
                ITestSourceSymbol        = compilation.GetTypeByMetadataName("Microsoft.VisualStudio.TestTools.UnitTesting.ITestDataSource"),
            };

            definitions.NonTestMethods = ImmutableHashSet.Create <INamedTypeSymbol>(SymbolEqualityComparer.Default,
                                                                                    definitions.TestClassSymbol,
                                                                                    definitions.ClassInitializeSymbol,
                                                                                    definitions.ClassCleanupSymbol,
                                                                                    definitions.AssemblyCleanupSymbol,
                                                                                    definitions.AssemblyInitializeSymbol,
                                                                                    definitions.TestInitializeSymbol,
                                                                                    definitions.TestCleanupSymbol,
                                                                                    definitions.DataRowSymbol,
                                                                                    definitions.DynamicDataSymbol,
                                                                                    definitions.TestCategorySymbol
                                                                                    );

            return(definitions);
        }
        private void Analyze(MsTestAttributeDefinitions definitions, SyntaxNodeAnalysisContext context, Implementation implementation)
        {
            MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.Node;

            ISymbol symbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration);

            if (symbol is null || !(symbol is IMethodSymbol methodSymbol))
            {
                return;
            }

            HashSet <INamedTypeSymbol> presentAttributes = new HashSet <INamedTypeSymbol>();

            foreach (AttributeData attribute in methodSymbol.GetAttributes())
            {
                if (definitions.NonTestMethods.Contains(attribute.AttributeClass))
                {
                    presentAttributes.Add(attribute.AttributeClass);
                }

                if (attribute.AttributeClass.IsDerivedFrom(definitions.TestMethodSymbol))
                {
                    presentAttributes.Add(attribute.AttributeClass);
                }
            }

            if (presentAttributes.Count > 0)
            {
                implementation.OnTestAttributeMethod(context, methodDeclaration, methodSymbol, presentAttributes);
            }
        }
        private void Analyze(MsTestAttributeDefinitions definitions, SyntaxNodeAnalysisContext context)
        {
            MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.Node;

            if (!methodDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword))
            {
                return;
            }

            if (!(methodDeclaration.Parent is ClassDeclarationSyntax classDeclaration))
            {
                return;
            }

            if (!Helper.IsTestClass(classDeclaration, context))
            {
                return;
            }

            ISymbol symbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration);

            if (symbol is null)
            {
                return;
            }

            bool isAllowedToBePublic = false;

            foreach (AttributeData attribute in symbol.GetAttributes())
            {
                if (definitions.NonTestMethods.Contains(attribute.AttributeClass))
                {
                    isAllowedToBePublic = true;
                    break;
                }

                if (attribute.AttributeClass.IsDerivedFrom(definitions.TestMethodSymbol))
                {
                    isAllowedToBePublic = true;
                    break;
                }
            }

            if (!isAllowedToBePublic)
            {
                Diagnostic diagnostic = Diagnostic.Create(Rule, methodDeclaration.GetLocation());
                context.ReportDiagnostic(diagnostic);
            }
        }
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterCompilationStartAction(startContext =>
            {
                if (startContext.Compilation.GetTypeByMetadataName(MsTestFrameworkDefinitions.AssemblyCleanupAttribute.FullName) == null)
                {
                    return;
                }

                MsTestAttributeDefinitions definitions = MsTestAttributeDefinitions.FromCompilation(startContext.Compilation);

                startContext.RegisterSyntaxNodeAction((x) => Analyze(definitions, x), SyntaxKind.MethodDeclaration);
            });
        }
        public sealed override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterCompilationStartAction(startContext =>
            {
                if (startContext.Compilation.GetTypeByMetadataName("Microsoft.VisualStudio.TestTools.UnitTesting.Assert") == null)
                {
                    return;
                }

                MsTestAttributeDefinitions definitions = MsTestAttributeDefinitions.FromCompilation(startContext.Compilation);

                Implementation implementation = OnInitializeAnalyzer(startContext.Options, startContext.Compilation, definitions);

                if (implementation is null)
                {
                    return;
                }

                startContext.RegisterSyntaxNodeAction((x) => Analyze(definitions, x, implementation), SyntaxKind.MethodDeclaration);
            });
        }
 public TestMethodsMustBePublic(MsTestAttributeDefinitions definitions) : base(definitions)
 {
 }
        protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions)
        {
            AdditionalFilesHelper helper = new AdditionalFilesHelper(options, compilation);

            var exceptions        = helper.LoadExceptions(FileName);
            var allowedCategories = helper.GetValuesFromEditorConfig(Rule.Id, @"allowed_test_categories").ToImmutableHashSet();

            return(new TestHasCategoryAttribute(exceptions, allowedCategories, definitions));
        }
Exemplo n.º 8
0
 protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions) => new TestHasDescription(definitions);
 protected abstract TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions);
 public TestMethodsShouldHaveValidReturnTypes(Compilation compilation, MsTestAttributeDefinitions definitions) : base(definitions)
 {
     _taskSymbol = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
 }
 protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions) => new TestMethodsShouldHaveValidReturnTypes(compilation, definitions);
 public TestHasTimeout(MsTestAttributeDefinitions definitions, AdditionalFilesHelper additionalFilesHelper) : base(definitions)
 {
     _additionalFilesHelper = additionalFilesHelper;
 }
        protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions)
        {
            var additionalFilesHelper = new AdditionalFilesHelper(options, compilation);

            return(new TestHasTimeout(definitions, additionalFilesHelper));
        }
 public TestMethodsMustHaveTheCorrectNumberOfArguments(MsTestAttributeDefinitions definitions) : base(definitions)
 {
 }
 protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions) => new TestMethodsMustHaveTheCorrectNumberOfArguments(definitions);
 protected sealed override Implementation OnInitializeAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions)
 {
     return(OnInitializeTestMethodAnalyzer(options, compilation, definitions));
 }
 protected override Implementation OnInitializeAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions) => new TestMethodsMustBeInTestClass();
 public TestMethodImplementation(MsTestAttributeDefinitions definitions)
 {
     Definitions = definitions;
 }
 protected override Implementation OnInitializeAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions) => new NoEmptyTestMethodsImplementation();
Exemplo n.º 20
0
 public TestHasDescription(MsTestAttributeDefinitions definitions) : base(definitions)
 {
 }
Exemplo n.º 21
0
 protected override TestMethodImplementation OnInitializeTestMethodAnalyzer(AnalyzerOptions options, Compilation compilation, MsTestAttributeDefinitions definitions)
 {
     return(new DataTestMethodsHaveDataRowsImplementation(definitions));
 }
 public TestHasCategoryAttribute(HashSet <string> exceptions, ImmutableHashSet <string> allowedCategories, MsTestAttributeDefinitions definitions) : base(definitions)
 {
     _exceptions        = exceptions;
     _allowedCategories = allowedCategories;
 }
Exemplo n.º 23
0
 public DataTestMethodsHaveDataRowsImplementation(MsTestAttributeDefinitions definitions) : base(definitions)
 {
 }