forked from dotnet/roslynator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxLogicalInverterTests.cs
More file actions
70 lines (63 loc) · 2.76 KB
/
SyntaxLogicalInverterTests.cs
File metadata and controls
70 lines (63 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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());
}
}