Skip to content

Update 'UseExpressionBody' tests to the new test library#51623

Merged
CyrusNajmabadi merged 18 commits intodotnet:mainfrom
Youssef1313:update-tests
Mar 9, 2021
Merged

Update 'UseExpressionBody' tests to the new test library#51623
CyrusNajmabadi merged 18 commits intodotnet:mainfrom
Youssef1313:update-tests

Conversation

@Youssef1313
Copy link
Member

No description provided.

@ghost ghost added the Area-Analyzers label Mar 3, 2021
@Youssef1313 Youssef1313 changed the title Update UseExpressionBodyForPropertiesAnalyzerTests to the new test library Update some 'UseExpressionBody' tests to the new test library Mar 3, 2021
@Youssef1313
Copy link
Member Author

Converting to a draft until I fix test failures. Meanwhile, I'd like to get an initial feedback on this and whether you want to do this.

@Youssef1313 Youssef1313 marked this pull request as draft March 3, 2021 16:09
Comment on lines +18 to +19
UseExpressionBodyDiagnosticAnalyzer,
UseExpressionBodyCodeFixProvider>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 It may be possible to remove the MEF suppressions on the constructor in each of these types

@sharwell
Copy link
Contributor

sharwell commented Mar 3, 2021

@Youssef1313 Definitely the direction we want to take the tests. I recommend converting to the syntax of the new tests first (which is exactly what you did here so far), and then make any corrections to the test (compiler errors, etc.) as separate commits in the PR so we can review those changes separately from the simple API transformation.

Base automatically changed from master to main March 3, 2021 23:53
@Youssef1313
Copy link
Member Author

@sharwell Should we get UseFirstDescriptor removed (in a later PR)?

public Test()
{
MarkupOptions = Testing.MarkupOptions.UseFirstDescriptor;

@Youssef1313
Copy link
Member Author

[xUnit.net 00:00:11.98]     Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseExpressionBody.UseExpressionBodyForPropertiesAnalyzerTests.TestUseBlockBodyForAccessorEventWhenAccessorWantExpress
ion1 [FAIL]
  Failed Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseExpressionBody.UseExpressionBodyForPropertiesAnalyzerTests.TestUseBlockBodyForAccessorEventWhenAccessorWantExpression1 [31 ms]
  Error Message:
   Microsoft.CodeAnalysis.Testing.Verifiers.EqualWithMessageException : Context: Diagnostics of fixed state
Mismatch between number of diagnostics returned, expected "0" actual "1"

Diagnostics:
// /0/Test0.cs(9,9): hidden IDE0027: Use expression body for accessors
VerifyCS.Diagnostic(UseExpressionBodyDiagnosticAnalyzer.IDE0027).WithSpan(9, 9, 12, 10).WithSpan(9, 9, 12, 10),

@sharwell I'm not sure why it expects "0" for fixed state? Here is the test:

        [WorkItem(20363, "https://github.com/dotnet/roslyn/issues/20363")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExpressionBody)]
        public async Task TestUseBlockBodyForAccessorEventWhenAccessorWantExpression1()
        {
            var code = @"
class C
{
    int Bar() { return 0; }

    {|IDE0025:int Goo => Bar();|}
}";
            var fixedCode = @"
class C
{
    int Bar() { return 0; }

    int Goo
    {
        // TODO: Likely a bug? It should have been expression body.
        {|IDE0027:get
        {
            return Bar();
        }|}
    }
}";
            await TestWithUseBlockBodyExceptAccessor(code, fixedCode);
        }

        private static async Task TestWithUseBlockBodyExceptAccessor(string code, string fixedCode)
        {
            await new VerifyCS.Test
            {
                TestCode = code,
                FixedCode = fixedCode,
                Options =
                {
                    { CSharpCodeStyleOptions.PreferExpressionBodiedProperties, ExpressionBodyPreference.Never },
                    { CSharpCodeStyleOptions.PreferExpressionBodiedAccessors, ExpressionBodyPreference.WhenPossible },
                },
                MarkupOptions = MarkupOptions.None,
            }.RunAsync();
        }

@sharwell
Copy link
Contributor

sharwell commented Mar 4, 2021

Should we get UseFirstDescriptor removed (in a later PR)?

It wasn't viable prior to #46666. Now it probably is, but I'm also not sure it's hurting anything.

I'm not sure why it expects "0" for fixed state?

You need to set FixedState.MarkupMode to MarkupMode.Allow. The property and all the enumeration values have a bunch of documentation, so read through them and let me know if you still have questions.

@Youssef1313 Youssef1313 changed the title Update some 'UseExpressionBody' tests to the new test library Update 'UseExpressionBody' tests to the new test library Mar 4, 2021
@Youssef1313
Copy link
Member Author

public async Task TestUseBlockBody5()
{
var whenOnSingleLineWithNoneEnforcement = new CodeStyleOption2<ExpressionBodyPreference>(ExpressionBodyPreference.WhenOnSingleLine, NotificationOption2.None);
var options = new OptionsCollection(GetLanguage())
{
{ CSharpCodeStyleOptions.PreferExpressionBodiedAccessors, whenOnSingleLineWithNoneEnforcement },
{ CSharpCodeStyleOptions.PreferExpressionBodiedProperties, whenOnSingleLineWithNoneEnforcement },
{ CSharpCodeStyleOptions.PreferExpressionBodiedIndexers, whenOnSingleLineWithNoneEnforcement },
};
await TestMissingInRegularAndScriptAsync(
@"class C
{
C this[int index]
{
get [|=>|] default;
}
}", new TestParameters(options: options));
}

@sharwell Does "WithNoneEnforcement" in the original test the same as "Never"?

The test is failing with "ExpressionBodyPreference.WhenOnSingleLine" as it expects expression body for indexer, while the original test doesn't.

@Youssef1313
Copy link
Member Author

There is also TestAccessorListFormatting_FixAll which is failing locally:

        public async Task TestAccessorListFormatting_FixAll()
        {
            var code = @"
class C
{
    int Bar() { return 0; }

    int Goo { {|IDE0027:get => Bar();|} {|IDE0027:set => Bar();|} }
}";
            var fixedCode = @"
class C
{
    int Bar() { return 0; }

    int Goo
    {
        get
        {
            return Bar();
        }

        set
        {
            Bar();
        }
    }
}";
            await TestWithUseBlockBodyIncludingPropertiesAndIndexers(code, fixedCode);
        }
content of '/0/Test0.cs' did not match. Diff shown with expected as baseline:

 class C
 {
     int Bar() { return 0; }

     int Goo
     {
-        get
+        get { return Bar(); }
-        {
-            return Bar();
-        }
-
         set
         {
             Bar();
         }
     }
 }

I can't figure out how it's passing in main. The original test is:

        public async Task TestAccessorListFormatting_FixAll()
        {
            await TestInRegularAndScriptAsync(
@"class C
{
    int Goo { get {|FixAllInDocument:=>|} Bar(); set => Bar(); }
}",
@"class C
{
    int Goo
    {
        get
        {
            return Bar();
        }
        set
        {
            Bar();
        }
    }
}", options: UseBlockBodyIncludingPropertiesAndIndexers);
        }

@Youssef1313 Youssef1313 marked this pull request as ready for review March 4, 2021 19:33
@sharwell
Copy link
Contributor

sharwell commented Mar 4, 2021

Does "WithNoneEnforcement" in the original test the same as "Never"?

No, you'll need to also pass the third parameter as NotifactionOption2.None.

@Youssef1313
Copy link
Member Author

Youssef1313 commented Mar 5, 2021

@sharwell That worked. The only failing test now is the one I stated in #51623 (comment)

I couldn't figure out what's wrong. It's failing with the following diff.

 class C
 {
     int Bar() { return 0; }

     int Goo
     {
-        get { return Bar(); }
+        get
+        {
+            return Bar();
+        }
+
         set
         {
             Bar();
         }
     }
 }

However, when I update it, it fails with the exact opposite diff.

 class C
 {
     int Bar() { return 0; }

     int Goo
     {
-        get
+        get { return Bar(); }
-        {
-            return Bar();
-        }
-
         set
         {
             Bar();
         }
     }
 }

Do you know what's going on? I've tried to set NumberOfIncrementalIterations and NumberOfFixAllIterations but no luck.

@sharwell
Copy link
Contributor

sharwell commented Mar 5, 2021

I'm guessing the iterative code fix and the Fix All are producing different results. You can specify both by setting FixedCode for the iterative result and BatchFixedCode for the Fix All result.

@Youssef1313
Copy link
Member Author

Thanks @sharwell. I think CI should pass now.

@CyrusNajmabadi
Copy link
Contributor

Thanks!

@CyrusNajmabadi CyrusNajmabadi merged commit b0f9a5a into dotnet:main Mar 9, 2021
@Youssef1313 Youssef1313 deleted the update-tests branch March 9, 2021 14:41
@allisonchou allisonchou added this to the Next milestone Mar 12, 2021
@allisonchou allisonchou modified the milestones: Next, 16.10.P2 Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants