Skip to content

Conversation

@bkoelman
Copy link

@DustinCampbell Here the pull request for my comment earlier. I left out the FluentAssertions parts. But all my other features are in here, because I'm using a quite different API. Putting all features in separate pull requests at this moment would case much duplication and painful merges later. Based on discussions around this pull request, I can update it and remove parts that may be added later in another form. At a later time, I'd like to add some extensibility points to plug a custom assertion framework. Consider this pull request an expression to the direction I'd like to take this.

A key difference is that testing a CodeFixProvider can only be done from the output of its associated analyzer. This makes it possible to transfer analyzer state through Diagnostic.Properties, along with its reported span(s) and automatically test-apply all suggested fixes. Personally, as an analyzer developer, I am more interested in testing a working combination of Analyzer + FixProvider, rather than unittesting scenarios for a standalone FixProvider.

So I have one base class, AnalysisTestFixture, from which unittests derive. To run a test, an immutable context is passed in. It has Withers to supply additional information, such as extra references, filename etc.

Lastly, I changed reporting of builtin compiler errors, which were discarded before. I found that valuable, as it revealed some broken tests I had written weeks ago. The assertion method is virtual, so can be overridden from unittest classes. Callback could also be moved into context, if desired.

As an example, I rewrote the template tests for a new Analyzer/FixProvider project to use my API:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using NUnit.Framework;
using RoslynNUnitLight;

namespace UppercaseClass.Test
{
    [TestFixture]
    internal class UppercaseClassNameUnitTest: AnalysisTestFixture
    {
        protected override string DiagnosticId => UppercaseClassAnalyzer.DiagnosticId;

        [Test]
        public void TestMethod1()
        {
            // No diagnostics expected to show up

            const string source = @"";

            var context = new AnalyzerTestContext(source, LanguageNames.CSharp);
            AssertDiagnostics(context);
        }

        [Test]
        public void TestMethod2()
        {
            // Diagnostic and CodeFix both triggered and checked for

            const string source = @"
    using System;

    namespace ConsoleApplication1
    {
        class [|SomeTypeName|]
        {   
        }
    }";

            string expected = source.Replace("[|SomeTypeName|]", "SOMETYPENAME");

            var analyzerContext = new AnalyzerTestContext(source, LanguageNames.CSharp);
            var fixContext = new FixProviderTestContext(analyzerContext, new[] { expected }, reformatExpected: false);
            AssertDiagnosticsWithCodeFixes(fixContext);
        }

        protected override DiagnosticAnalyzer CreateAnalyzer()
        {
            return new UppercaseClassAnalyzer();
        }

        protected override CodeFixProvider CreateFixProvider()
        {
            return new UppercaseClassCodeFixProvider();
        }
    }
}

@cezarypiatek
Copy link

Hi @bkoelman

I know it's a very long time since this PR was reported. RoslynNUnitLight is in the process of deprecating and the recommended successor is https://github.com/cezarypiatek/RoslynTestKit - this is a newer version of RoslynNUnitLight. Your problem was solved in RoslynTestKit - if you need to test CodeFix that fixes an issue reported by given/provided DiagnosticAnalyzer you can do that by overriding CreateAdditionalAnalyzers() method. You can find an example here https://github.com/cezarypiatek/RoslynTestKit#example-test-code-fix-that-fixes-issue-reported-by-provided-diagnosticanalyzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants