|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 | +using Microsoft.CodeAnalysis.Text; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Roslynator.CSharp.Workspaces.Tests; |
| 10 | + |
| 11 | +public class SyntaxLogicallyInvertTests |
| 12 | +{ |
| 13 | + private SyntaxLogicalInverter _inverter; |
| 14 | + |
| 15 | + public SyntaxLogicallyInvertTests() |
| 16 | + { |
| 17 | + _inverter = SyntaxLogicalInverter.Default; |
| 18 | + } |
| 19 | + |
| 20 | + [Theory] |
| 21 | + [InlineData(@"x", @"!x")] |
| 22 | + [InlineData(@"!x", @"x")] |
| 23 | + [InlineData(@"x is ""abc""", @"x is not ""abc""")] |
| 24 | + [InlineData(@"x is 1", @"x is not 1")] |
| 25 | + [InlineData(@"x is null", @"x is not null")] |
| 26 | + [InlineData(@"x is true", @"x is false")] |
| 27 | + [InlineData(@"true", @"false")] |
| 28 | + [InlineData(@"false", @"true")] |
| 29 | + [InlineData(@"x >= 3", @"x < 3")] |
| 30 | + [InlineData(@"x > 3", @"x <= 3")] |
| 31 | + [InlineData(@"x <= 3", @"x > 3")] |
| 32 | + [InlineData(@"x < 3", @"x >= 3")] |
| 33 | + [InlineData(@"x == y", @"x != y")] |
| 34 | + [InlineData(@"x != y", @"x == y")] |
| 35 | + [InlineData(@"(bool)x || (bool)y", @"!((bool)x) && !((bool)y)")] |
| 36 | + [InlineData(@"(bool)x && (bool)y", @"!((bool)x) || !((bool)y)")] |
| 37 | + [InlineData(@"x ?? true", @"x == false")] |
| 38 | + [InlineData(@"x ?? false", @"x != true")] |
| 39 | + [InlineData(@"(bool)x ? y : z", @"(bool)x ? !y : !z")] |
| 40 | + [InlineData(@"x[0]", @"!x[0]")] |
| 41 | + [InlineData(@"default(bool)", @"!default(bool)")] |
| 42 | + [InlineData(@"checked(x + y)", @"!checked(x + y)")] |
| 43 | + [InlineData(@"unchecked(x + y)", @"!unchecked(x + y)")] |
| 44 | + [InlineData(@"(bool)x", @"!((bool)x)")] |
| 45 | + [InlineData(@"x & y", @"!x | !y")] |
| 46 | + [InlineData(@"x ^ y", @"!(x ^ y)")] |
| 47 | + [InlineData(@"x | y", @"!x & !y")] |
| 48 | + [InlineData(@"x = y", @"!(x = y)")] |
| 49 | + [InlineData(@"await x", @"!(await x)")] |
| 50 | + [InlineData(@"x ?? y", @"!(x ?? y)")] |
| 51 | + [InlineData(@"x.a", @"!x.a")] |
| 52 | + [InlineData(@"x.a()", @"!x.a()")] |
| 53 | + [InlineData(@"x?.a", @"!x?.a")] |
| 54 | + public async Task LogicallyInvert(string source, string expected) |
| 55 | + { |
| 56 | + var sourceCode = $"class C {{ void M(dynamic x, dynamic y, dynamic z){{ if({source})return;}} }}"; |
| 57 | + var workspace = new AdhocWorkspace(); |
| 58 | + var newProject = workspace.AddProject("TestProject", LanguageNames.CSharp); |
| 59 | + var newDocument = workspace.AddDocument(newProject.Id, "TestDocument.cs", SourceText.From(sourceCode)); |
| 60 | + var syntaxTree = await newDocument.GetSyntaxTreeAsync(); |
| 61 | + var compilation = await newDocument.Project.GetCompilationAsync(); |
| 62 | + var semanticModel = compilation.GetSemanticModel(syntaxTree); |
| 63 | + |
| 64 | + var expression = syntaxTree.GetRoot().DescendantNodes().OfType<IfStatementSyntax>().Single().Condition; |
| 65 | + |
| 66 | + var result = _inverter.LogicallyInvert(expression, semanticModel, CancellationToken.None); |
| 67 | + Assert.Equal(expected, result.NormalizeWhitespace().ToFullString()); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments