Пример #1
0
        public static void VerifyCSharpAnalyzer(string snippet, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                IEnumerable <CSharpParseOptions> options             = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default,
                                                IEnumerable <MetadataReference> additionalReferences = null)
        {
            var solution = SolutionBuilder
                           .Create()
                           .AddProject(AnalyzerLanguage.CSharp)
                           .AddSnippet(snippet)
                           .AddReferences(additionalReferences)
                           .GetSolution();

            // ToDo: add [CallerLineNumber]int lineNumber = 0
            // then add ability to shift result reports with this line number
            foreach (var compilation in solution.Compile(options?.ToArray()))
            {
                DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, checkMode);
            }
        }
Пример #2
0
        public SnippetCompiler(string code, bool ignoreErrors, AnalyzerLanguage language, IEnumerable <MetadataReference> additionalReferences = null)
        {
            CompilationOptions compilationOptions = language == AnalyzerLanguage.CSharp
                ? (CompilationOptions) new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                : (CompilationOptions) new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            this.compilation = SolutionBuilder
                               .Create()
                               .AddProject(language, createExtraEmptyFile: false)
                               .AddSnippet(code)
                               .AddReferences(additionalReferences ?? Enumerable.Empty <MetadataReference>())
                               .GetCompilation(compilationOptions: compilationOptions);

            if (!ignoreErrors && HasCompilationErrors(compilation))
            {
                DumpCompilationErrors(compilation);
                throw new System.InvalidOperationException("Test setup error: test code snippet did not compile. See output window for details.");
            }

            this.SyntaxTree    = compilation.SyntaxTrees.First();
            this.SemanticModel = compilation.GetSemanticModel(this.SyntaxTree);
        }
Пример #3
0
        public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                 IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default,
                                                 IEnumerable <MetadataReference> additionalReferences = null)
        {
            var projectBuilder = SolutionBuilder.Create()
                                 .AddProject(AnalyzerLanguage.FromPath(path))
                                 .AddReferences(additionalReferences)
                                 .AddDocument(path);


            if (options == null)
            {
                var compilation = projectBuilder.GetCompilation(null);
                DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode);
            }
            else
            {
                foreach (var option in options)
                {
                    var compilation = projectBuilder.GetCompilation(option);
                    DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode);
                }
            }
        }
Пример #4
0
 private static Document CreateDocument(string path, IEnumerable <MetadataReference> additionalReferences) =>
 SolutionBuilder.Create()
 .AddProject(AnalyzerLanguage.FromPath(path))
 .AddReferences(additionalReferences)
 .AddDocument(path, true)
 .FindDocument(Path.GetFileName(path));