Skip to content

Commit

Permalink
Fix code actions integration tests (#11051)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Oct 21, 2024
2 parents 019b7c8 + 6df5cfb commit a91bdcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Roslyn.Test.Utilities;
using Microsoft.VisualStudio.Language.Intellisense;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -23,8 +24,7 @@ public async Task CSharpCodeActionsTests_MakeExpressionBodiedMethod()
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
var codeActionSet = Assert.Single(codeActions);
var codeAction = Assert.Single(codeActionSet.Actions, a => a.DisplayText.Equals("Use expression body for method"));
var codeAction = VerifyAndGetFirst(codeActions, "Use expression body for method");

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand All @@ -50,11 +50,9 @@ await TestServices.Editor.SetTextAsync("""
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
Assert.Collection(codeActions,
a => AssertEx.EqualOrDiff("@using System.Data", a.Actions.Single().DisplayText),
a => AssertEx.EqualOrDiff("System.Data.ConflictOption", a.Actions.Single().DisplayText));

var codeAction = codeActions.ElementAt(1).Actions.First();
var codeAction = VerifyAndGetFirst(codeActions,
"System.Data.ConflictOption",
"@using System.Data");

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand All @@ -80,11 +78,9 @@ await TestServices.Editor.SetTextAsync("""
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
Assert.Collection(codeActions,
a => AssertEx.EqualOrDiff("@using System.Data", a.Actions.Single().DisplayText),
a => AssertEx.EqualOrDiff("System.Data.ConflictOption", a.Actions.Single().DisplayText));

var codeAction = codeActions.First().Actions.First();
var codeAction = VerifyAndGetFirst(codeActions,
"@using System.Data",
"System.Data.ConflictOption");

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand Down Expand Up @@ -117,10 +113,8 @@ await TestServices.Editor.SetTextAsync("""
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
Assert.Collection(codeActions,
a => Assert.Equal("ConflictOption - @using System.Data", a.Actions.Single().DisplayText));

var codeAction = codeActions.First().Actions.First();
var codeAction = VerifyAndGetFirst(codeActions,
"ConflictOption - @using System.Data");

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand Down Expand Up @@ -160,10 +154,7 @@ void M(string[] args)
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
var introduceLocal = codeActions.FirstOrDefault(a => a.Actions.Single().DisplayText.Equals("Introduce local"));
Assert.NotNull(introduceLocal);

var codeAction = introduceLocal.Actions.First();
var codeAction = VerifyAndGetFirst(codeActions, "Introduce local");

Assert.True(codeAction.HasActionSets);

Expand Down Expand Up @@ -214,10 +205,7 @@ void M(string[] args)
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
var introduceLocal = codeActions.FirstOrDefault(a => a.Actions.Single().DisplayText.Equals("Introduce local"));
Assert.NotNull(introduceLocal);

var codeAction = introduceLocal.Actions.First();
var codeAction = VerifyAndGetFirst(codeActions, "Introduce local");

Assert.True(codeAction.HasActionSets);

Expand All @@ -241,4 +229,14 @@ void M(string[] args)
}
""", ControlledHangMitigatingCancellationToken);
}

private ISuggestedAction VerifyAndGetFirst(IEnumerable<SuggestedActionSet> codeActions, params string[] expected)
{
foreach (var title in expected)
{
Assert.Contains(codeActions, a => a.Actions.Single().DisplayText == title);
}

return codeActions.First(a => a.Actions.Single().DisplayText == expected[0]).Actions.Single();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -21,8 +22,7 @@ public async Task HtmlCodeActionsTests_RemoveTag()
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
var codeActionSet = Assert.Single(codeActions);
var codeAction = Assert.Single(codeActionSet.Actions, a => a.DisplayText.Equals("Remove <h1> tag and leave contents"));
var codeAction = Assert.Single(codeActions.SelectMany(s => s.Actions), a => a.DisplayText.Equals("Remove <h1> tag and leave contents"));

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand All @@ -41,8 +41,7 @@ public async Task HtmlCodeActionsTests_RemoveTag_WithCSharpContent()
var codeActions = await TestServices.Editor.InvokeCodeActionListAsync(ControlledHangMitigatingCancellationToken);

// Assert
var codeActionSet = Assert.Single(codeActions);
var codeAction = Assert.Single(codeActionSet.Actions, a => a.DisplayText.Equals("Remove <body> tag and leave contents"));
var codeAction = Assert.Single(codeActions.SelectMany(s => s.Actions), a => a.DisplayText.Equals("Remove <body> tag and leave contents"));

await TestServices.Editor.InvokeCodeActionAsync(codeAction, ControlledHangMitigatingCancellationToken);

Expand Down

0 comments on commit a91bdcc

Please sign in to comment.