Example #1
0
        public void InputWithIncident_MemberAccesses_SurfacesDiagnostic(string accessedInstance)
        {
            var test    = $@"using FakeNamespace;
namespace SampleTestProject.CsSamples 
{{
    public class SampleClass
    {{
        public void SampleMethod()
        {{
            var instance = new FakeClass();
            var a1 = {accessedInstance}.FakeMethod();
            var a2 = {accessedInstance}?.FakeMethod();

            var b1 = {accessedInstance}.FakeMethod().Substring(0);
            var b2 = {accessedInstance}.FakeMethod()?.Substring(0);
            var b3 = {accessedInstance}?.FakeMethod().Substring(0);
            var b4 = {accessedInstance}?.FakeMethod()?.Substring(0);
        }}
    }}
}}";
            var sources = new[] { test, _fakeClassSource };

            var documents   = ProjectCompilation.GetDocuments(sources, null, null);
            var analyzer    = CSharpDiagnosticAnalyzer;
            var diagnostics = AnalyzerExecution.GetSortedDiagnosticsFromDocuments(analyzer, documents);

            Assert.AreEqual(6, diagnostics.Length);

            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(9, 22), diagnostics.ElementAt(0));
            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(10, 22), diagnostics.ElementAt(1));
            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(12, 22), diagnostics.ElementAt(2));
            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(13, 22), diagnostics.ElementAt(3));
            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(14, 22), diagnostics.ElementAt(4));
            AssertBestEfforDiagnostic(analyzer, CreateDiagnosticResult(string.Empty).WithMessage(".FakeMethod()").WithLocation(15, 22), diagnostics.ElementAt(5));
        }
Example #2
0
        /// <summary>
        /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run,
        /// then verifies each of them.
        /// </summary>
        /// <param name="sources">An array of strings to create source documents from to run the analyzers on</param>
        /// <param name="analyzer">The analyzer to be run on the source code</param>
        /// <param name="references">An array of additional metadata references, source files are dependent on</param>
        /// <param name="fakeFileInfo">Fake file info generated files should have</param>
        /// <param name="expected">DiagnosticResults that should appear after the analyzer is run on the sources</param>
        private static void VerifyDiagnostics(string[] sources, DiagnosticAnalyzer analyzer, MetadataReference[] references, FakeFileInfo fakeFileInfo = null, params DiagnosticResult[] expected)
        {
            var documents   = ProjectCompilation.GetDocuments(sources, references, fakeFileInfo);
            var diagnostics = AnalyzerExecution.GetSortedDiagnosticsFromDocuments(analyzer, documents);

            VerifyDiagnosticResults(diagnostics, analyzer, expected);
        }