Skip to content

Commit

Permalink
Merge pull request #3753 from bjornhellander/feature/code-action-vali…
Browse files Browse the repository at this point in the history
…dation-mode

Change default value of test property CodeActionValidationMode to SemanticStructure instead of None and update so that tests still pass
  • Loading branch information
sharwell committed Dec 19, 2023
2 parents 497c50a + e004d41 commit 0fc865d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ private static bool IsEnumWithDefaultMember(INamedTypeSymbol namedTypeSymbol, ou
/// <returns>A new member access expression.</returns>
private static SyntaxNode ConstructMemberAccessSyntax(TypeSyntax typeSyntax, string memberName)
{
// NOTE: This creates the correct source code when applied, but these are not necessarily the syntax
// nodes that the compiler would create from that source code. For example, the type syntax can
// contain QualifiedName nodes, whereas the compiler would have created SimpleMemberAccessExpression instead.
// This means that the validation that happens in the tests need to be turned off for some tests.
// We could have transformed the nodes to match, but we keep the code simple instead.
return SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
typeSyntax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,48 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen

private static BinaryExpressionSyntax TransformExpression(BinaryExpressionSyntax binaryExpression)
{
// NOTE: This code also changes the syntax node kind, besides the operator token. The modified source code would
// have been the same without this, but we do this to make tests pass with the default CodeActionValidationMode.
var newLeft = binaryExpression.Right.WithTriviaFrom(binaryExpression.Left);
var newRight = binaryExpression.Left.WithTriviaFrom(binaryExpression.Right);
return binaryExpression.WithLeft(newLeft).WithRight(newRight).WithOperatorToken(GetCorrectOperatorToken(binaryExpression.OperatorToken));
GetReplacementInfo(binaryExpression.OperatorToken, out var newOperatorToken, out var newNodeKind);
return SyntaxFactory.BinaryExpression(newNodeKind, newLeft, newOperatorToken, newRight);
}

private static SyntaxToken GetCorrectOperatorToken(SyntaxToken operatorToken)
private static void GetReplacementInfo(SyntaxToken operatorToken, out SyntaxToken newToken, out SyntaxKind newNodeKind)
{
switch (operatorToken.Kind())
{
case SyntaxKind.EqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
return operatorToken;
newToken = operatorToken;
newNodeKind = operatorToken.Parent.Kind();
break;

case SyntaxKind.GreaterThanToken:
return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanToken, operatorToken.TrailingTrivia);
newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanToken, operatorToken.TrailingTrivia);
newNodeKind = SyntaxKind.LessThanExpression;
break;

case SyntaxKind.GreaterThanEqualsToken:
return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanEqualsToken, operatorToken.TrailingTrivia);
newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanEqualsToken, operatorToken.TrailingTrivia);
newNodeKind = SyntaxKind.LessThanOrEqualExpression;
break;

case SyntaxKind.LessThanToken:
return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanToken, operatorToken.TrailingTrivia);
newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanToken, operatorToken.TrailingTrivia);
newNodeKind = SyntaxKind.GreaterThanExpression;
break;

case SyntaxKind.LessThanEqualsToken:
return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanEqualsToken, operatorToken.TrailingTrivia);
newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanEqualsToken, operatorToken.TrailingTrivia);
newNodeKind = SyntaxKind.GreaterThanOrEqualExpression;
break;

default:
return SyntaxFactory.Token(SyntaxKind.None);
newToken = SyntaxFactory.Token(SyntaxKind.None);
newNodeKind = (SyntaxKind)operatorToken.Parent.RawKind;
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ public void TestMethod()
}}
";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
await new CSharpTest
{
TestCode = testCode,
FixedCode = fixedTestCode,
CodeActionValidationMode = CodeActionValidationMode.None, // Differences in syntax nodes (SimpleMemberAccessExpression vs QualifiedName)
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public CSharpTest(LanguageVersion? languageVersion)
.WithChangedOption(FormattingOptions.UseTabs, this.Language, this.UseTabs));

this.TestState.AdditionalFilesFactories.Add(GenerateSettingsFile);
this.CodeActionValidationMode = CodeActionValidationMode.None;
this.CodeActionValidationMode = CodeActionValidationMode.SemanticStructure;

this.SolutionTransforms.Add((solution, projectId) =>
{
Expand Down

0 comments on commit 0fc865d

Please sign in to comment.