Kre4
Kre4

Reputation: 95

c# code analyzer test is not working, but test is correct

I'm writing a simple code analyzer and fixer using the Microsoft template. I want to fix nested if statements in else statement. Example:

using System;
using System.Runtime.InteropServices;

namespace ExamApp
{
    public class Program
    {

        static void Main()
        {

            if (Condition())
            {
                Action();
            }
            else
            {
                if (Condition())
                {
                    Action();
                }
            }
        }

        static bool Condition()
        {
            return false;
        }
        static void Action() { }
    }
}

Should be converted to

using System;
using System.Runtime.InteropServices;

namespace ExamApp
{
    public class Program
    {

        static void Main()
        {

            if (Condition())
            {
                Action();
            }
            else if (Condition())
            {
                Action();
            }
        }

        static bool Condition()
        {
            return false;
        }
        static void Action() { }
    }
}

I run Analyzer.Vsix that opens another VS window, opened some sandbox project, wrote code from 1-st example. VS showed me warining in the correct place, sugessted to fix it and fixed it correctly. I decided to write a test like this:

//Diagnostic and CodeFix both triggered and checked for
        [TestMethod]
        public async Task TestMethod2()
        {
            var test = @"
using System;
using System.Runtime.InteropServices;

namespace ExamApp
{
    public class Program
    {

        static void Main()
        {

            if (Condition())
            {
                Action();
            }
            else
            {
                if (Condition())
                {
                    Action();
                }
            }
        }

        static bool Condition()
        {
            return false;
        }
        static void Action() { }
    }
}";

            var fixtest = @"
using System;
using System.Runtime.InteropServices;

namespace ExamApp
{
    public class Program
    {

        static void Main()
        {

            if (Condition())
            {
                Action();
            }
            else if (Condition())
            {
                Action();
            }
        }

        static bool Condition()
        {
            return false;
        }
        static void Action() { }
    }
}";

            var expected = VerifyCS.Diagnostic("AnalyzerTemplate").WithSpan(17, 13, 17, 17).WithArguments("IfKeyword");
            await VerifyCS.VerifyCodeFixAsync(test,expected, fixtest);
            
        }

But something went wrong and this is output:

Assert.Fail failed. Context: Iterative code fix application
content of '/0/Test0.cs' did not match. Diff shown with expected as baseline:
 
 using System;
 using System.Runtime.InteropServices;
 
 namespace ExamApp
 {
     public class Program
     {
 
         static void Main()
         {
 
             if (Condition())
             {
                 Action();
             }
             else if (Condition())
             {
                 Action();
             }
         }
 
         static bool Condition()
         {
             return false;
         }
         static void Action() { }
     }
 }

   at Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier.Fail(String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.MSTest/MSTestVerifier.cs:line 78
   at Microsoft.CodeAnalysis.Testing.IVerifierExtensions.EqualOrDiff(IVerifier verifier, String expected, String actual, String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/Extensions/IVerifierExtensions.cs:line 56
   at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(String language, ImmutableArray`1 analyzers, ImmutableArray`1 codeFixProviders, SolutionState oldState, SolutionState newState, Int32 numberOfIterations, Func`10 getFixedProject, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 415
   at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(SolutionState testState, SolutionState fixedState, SolutionState batchFixedState, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 372
   at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.RunImplAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 248
   at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.RunAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 180
   at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult[] expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 58
   at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 46
   at AnalyzerTemplate.Test.AnalyzerTemplateUnitTest.TestMethod2() in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\AnalyzerTemplateUnitTests.cs:line 89
   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)

I can't understand, why there are no differences (usually it marks them with +/-) and how to make my test work correctly

Upvotes: 1

Views: 829

Answers (1)

Youssef13
Youssef13

Reputation: 4944

The difference is going to be an end of line (\r\n vs \n). The testing library was recently updated to show CRLF vs LF differences.

You'll need to use a newer package of the testing library that includes the fix from this PR.

Upvotes: 4

Related Questions