Skip to content

Question - Testing codefix that also fixes issue #7531

Open
@leus

Description

Trying to implement a code fix to help writing manual mapping code.

This is my test case:

var test = @"
    using System;
    using System.Xml.Serialization;
    namespace ConsoleApplication1
    {
    #pragma warning disable CS1003
    #pragma warning disable CS1002
        interface IMessage { }
        class LeftClass : IMessage { }
        [System.Xml.Serialization.XmlTypeAttribute(Namespace=""http://www.mismo.org/residential/2009/schemas"")]
        class RightClass { }
        class Something { public LeftClass Prop { get; set; } }

        static class Test {
            public static void TestMethod()
            {
                var a = new RightClass();
                var b = new Something {
                    Prop = a
                };
            }
        }
    #pragma warning restore CS1003
    #pragma warning restore CS1002
    }";

The line Prop = a fails because the right side is of a different type. However, since it has a known annotation, I want to propose the creation of an extension method to transform to the left type:

            var fixtest = @"using System;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
#pragma warning disable CS1003
#pragma warning disable CS1002
    interface IMessage
    {
    }

    class LeftClass : IMessage
    {
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute(""xsd"", ""4.8.3928.0"")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute(""code"")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = ""http://www.mismo.org/residential/2009/schemas"")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = ""http://www.mismo.org/residential/2009/schemas"", IsNullable = false)]
    class RightClass
    {
    }

    class Something
    {
        public LeftClass Prop { get; set; }
    }

    static class Test
    {
        public static void TestMethod()
        {
            var a = new RightClass();
            var b = new Something
            {
                Prop = a.ToDomain()
            };
        }

        private static LeftClass ToDomain(this RightClass source) => throw new NotImplementedException();
    }
#pragma warning restore CS1003
#pragma warning restore CS1002
}";

Now, I have the code and it works. However, testing is proving difficult, since the code fix actually eliminates both diagnostics.

            var expected = new[]
            {
                // /0/Test0.cs(24,33): warning WILQO001: Class initialization assignment to IMessage detected
                VerifyCS.Diagnostic().WithSpan(24, 33, 24, 41),
                // /0/Test0.cs(24,40): error CS0029: Cannot implicitly convert type 'ConsoleApplication1.RightClass' to 'ConsoleApplication1.LeftClass'
                DiagnosticResult.CompilerError("CS0029").WithSpan(24, 40, 24, 41).WithArguments("ConsoleApplication1.RightClass", "ConsoleApplication1.LeftClass"),
            };

            var analyzerTest = new CSharpAnalyzerTest<MessageAssignmentAnalyzer, DefaultVerifier>
            {
                TestState = { Sources = { test } }
            };
            analyzerTest.TestState.ExpectedDiagnostics.AddRange(expected);
            await analyzerTest.RunAsync(); // works

            var codefixTest = new CSharpCodeFixTest<MessageAssignmentAnalyzer, MessageAssignmentCodeFixProvider, DefaultVerifier>
            {
                TestState = { Sources = { test } },
                FixedState = { Sources = { fixtest } },
            };
            codefixTest.TestState.ExpectedDiagnostics.AddRange(expected);
            await codefixTest.RunAsync();

If I add diagnostics to the FixedState member it fails because it actually is returning no diagnostics; but if I leave the FixedState.Diagnostics collection empty, the test code "helpfully" calculates 1 diagnostic expected.

Any setting that I need to change to allow for the diagnostics after the fix to be empty?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions