-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve SyntaxLogicalInverter to Handling Additional Cases and reduce…
… Unnecessary Parentheses (#1086) Co-authored-by: Josef Pihrt <josef@pihrt.net>
- Loading branch information
1 parent
6833900
commit ee6668f
Showing
3 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Tests/CSharp.Workspaces.Tests/SyntaxLogicalInverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Xunit; | ||
|
||
namespace Roslynator.CSharp.Workspaces.Tests; | ||
|
||
public class SyntaxLogicallyInvertTests | ||
{ | ||
private SyntaxLogicalInverter _inverter; | ||
|
||
public SyntaxLogicallyInvertTests() | ||
{ | ||
_inverter = SyntaxLogicalInverter.Default; | ||
} | ||
|
||
[Theory] | ||
[InlineData(@"x", @"!x")] | ||
[InlineData(@"!x", @"x")] | ||
[InlineData(@"x is ""abc""", @"x is not ""abc""")] | ||
[InlineData(@"x is 1", @"x is not 1")] | ||
[InlineData(@"x is null", @"x is not null")] | ||
[InlineData(@"x is true", @"x is false")] | ||
[InlineData(@"true", @"false")] | ||
[InlineData(@"false", @"true")] | ||
[InlineData(@"x >= 3", @"x < 3")] | ||
[InlineData(@"x > 3", @"x <= 3")] | ||
[InlineData(@"x <= 3", @"x > 3")] | ||
[InlineData(@"x < 3", @"x >= 3")] | ||
[InlineData(@"x == y", @"x != y")] | ||
[InlineData(@"x != y", @"x == y")] | ||
[InlineData(@"(bool)x || (bool)y", @"!((bool)x) && !((bool)y)")] | ||
[InlineData(@"(bool)x && (bool)y", @"!((bool)x) || !((bool)y)")] | ||
[InlineData(@"x ?? true", @"x == false")] | ||
[InlineData(@"x ?? false", @"x != true")] | ||
[InlineData(@"(bool)x ? y : z", @"(bool)x ? !y : !z")] | ||
[InlineData(@"x[0]", @"!x[0]")] | ||
[InlineData(@"default(bool)", @"!default(bool)")] | ||
[InlineData(@"checked(x + y)", @"!checked(x + y)")] | ||
[InlineData(@"unchecked(x + y)", @"!unchecked(x + y)")] | ||
[InlineData(@"(bool)x", @"!((bool)x)")] | ||
[InlineData(@"x & y", @"!x | !y")] | ||
[InlineData(@"x ^ y", @"!(x ^ y)")] | ||
[InlineData(@"x | y", @"!x & !y")] | ||
[InlineData(@"x = y", @"!(x = y)")] | ||
[InlineData(@"await x", @"!(await x)")] | ||
[InlineData(@"x ?? y", @"!(x ?? y)")] | ||
[InlineData(@"x.a", @"!x.a")] | ||
[InlineData(@"x.a()", @"!x.a()")] | ||
[InlineData(@"x?.a", @"!x?.a")] | ||
public async Task LogicallyInvert(string source, string expected) | ||
{ | ||
var sourceCode = $"class C {{ void M(dynamic x, dynamic y, dynamic z){{ if({source})return;}} }}"; | ||
var workspace = new AdhocWorkspace(); | ||
var newProject = workspace.AddProject("TestProject", LanguageNames.CSharp); | ||
var newDocument = workspace.AddDocument(newProject.Id, "TestDocument.cs", SourceText.From(sourceCode)); | ||
var syntaxTree = await newDocument.GetSyntaxTreeAsync(); | ||
var compilation = await newDocument.Project.GetCompilationAsync(); | ||
var semanticModel = compilation.GetSemanticModel(syntaxTree); | ||
|
||
var expression = syntaxTree.GetRoot().DescendantNodes().OfType<IfStatementSyntax>().Single().Condition; | ||
|
||
var result = _inverter.LogicallyInvert(expression, semanticModel, CancellationToken.None); | ||
Assert.Equal(expected, result.NormalizeWhitespace().ToFullString()); | ||
} | ||
|
||
} |