Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - Testing codefix that also fixes issue #7531

Open
leus opened this issue Jan 14, 2025 · 0 comments
Open

Question - Testing codefix that also fixes issue #7531

leus opened this issue Jan 14, 2025 · 0 comments

Comments

@leus
Copy link

leus commented Jan 14, 2025

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?

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

No branches or pull requests

1 participant