Skip to content

Commit 8878a3b

Browse files
Tenno641bradwilson
authored andcommitted
Fix preserve arguments (#198)
1 parent f7e403b commit 8878a3b

File tree

2 files changed

+107
-22
lines changed

2 files changed

+107
-22
lines changed

src/xunit.analyzers.fixes/X2000/BooleanAssertsShouldNotBeNegatedFixer.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Composition;
23
using System.Linq;
34
using System.Threading;
@@ -57,13 +58,24 @@ static async Task<Document> UseSuggestedAssert(
5758
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
5859

5960
if (invocation.Expression is MemberAccessExpressionSyntax memberAccess)
61+
{
6062
if (invocation.ArgumentList.Arguments[0].Expression is PrefixUnaryExpressionSyntax prefixUnaryExpression)
63+
{
64+
var originalArguments = invocation.ArgumentList.Arguments;
65+
var newFirstArgument = Argument(prefixUnaryExpression.Operand);
66+
67+
var newArguments = new List<ArgumentSyntax> { newFirstArgument };
68+
if (originalArguments.Count > 1)
69+
newArguments.AddRange(originalArguments.Skip(1));
70+
6171
editor.ReplaceNode(
6272
invocation,
6373
invocation
64-
.WithArgumentList(ArgumentList(SeparatedList([Argument(prefixUnaryExpression.Operand)])))
74+
.WithArgumentList(ArgumentList(SeparatedList(newArguments)))
6575
.WithExpression(memberAccess.WithName(IdentifierName(replacement)))
6676
);
77+
}
78+
}
6779

6880
return editor.GetChangedDocument();
6981
}

src/xunit.analyzers.tests/Fixes/X2000/BooleanAssertsShouldNotBeNegatedFixerTests.cs

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,101 @@
55

66
public class BooleanAssertsShouldNotBeNegatedFixerTests
77
{
8-
const string template = /* lang=c#-test */ """
9-
using Xunit;
10-
11-
public class TestClass {{
12-
[Fact]
13-
public void TestMethod() {{
14-
bool condition = true;
15-
16-
{0};
17-
}}
18-
}}
19-
""";
20-
21-
[Theory]
22-
[InlineData("False", "True")]
23-
[InlineData("True", "False")]
24-
public async Task ReplacesBooleanAssert(
25-
string assertion,
26-
string replacement)
8+
[Fact]
9+
public async Task AcceptanceTest()
2710
{
28-
var before = string.Format(template, $"[|Assert.{assertion}(!condition)|]");
29-
var after = string.Format(template, $"Assert.{replacement}(condition)");
11+
var before = /* lang=c#-test */ """
12+
using Xunit;
13+
14+
public class TestClass {
15+
[Fact]
16+
public void TestMethod() {
17+
bool condition = true;
18+
19+
// Not negated
20+
Assert.True(false);
21+
Assert.False(false);
22+
Assert.True(condition);
23+
Assert.False(condition);
24+
25+
// Negated
26+
[|Assert.True(!false)|];
27+
[|Assert.False(!false)|];
28+
[|Assert.True(!condition)|];
29+
[|Assert.False(!condition)|];
30+
31+
// Not negated, with message
32+
Assert.True(false, "test message");
33+
Assert.False(false, "test message");
34+
Assert.True(condition, "test message");
35+
Assert.False(condition, "test message");
36+
37+
// Negated, with message
38+
[|Assert.True(!false, "test message")|];
39+
[|Assert.False(!false, "test message")|];
40+
[|Assert.True(!condition, "test message")|];
41+
[|Assert.False(!condition, "test message")|];
42+
43+
// Not negated, with named parameter message
44+
Assert.True(false, userMessage: "test message");
45+
Assert.False(false, userMessage: "test message");
46+
Assert.True(condition, userMessage: "test message");
47+
Assert.False(condition, userMessage: "test message");
48+
49+
// Negated, with named parameter message
50+
[|Assert.True(!false, userMessage: "test message")|];
51+
[|Assert.False(!false, userMessage: "test message")|];
52+
[|Assert.True(!condition, userMessage: "test message")|];
53+
[|Assert.False(!condition, userMessage: "test message")|];
54+
}
55+
}
56+
""";
57+
var after = /* lang=c#-test */ """
58+
using Xunit;
59+
60+
public class TestClass {
61+
[Fact]
62+
public void TestMethod() {
63+
bool condition = true;
64+
65+
// Not negated
66+
Assert.True(false);
67+
Assert.False(false);
68+
Assert.True(condition);
69+
Assert.False(condition);
70+
71+
// Negated
72+
Assert.False(false);
73+
Assert.True(false);
74+
Assert.False(condition);
75+
Assert.True(condition);
76+
77+
// Not negated, with message
78+
Assert.True(false, "test message");
79+
Assert.False(false, "test message");
80+
Assert.True(condition, "test message");
81+
Assert.False(condition, "test message");
82+
83+
// Negated, with message
84+
Assert.False(false, "test message");
85+
Assert.True(false, "test message");
86+
Assert.False(condition, "test message");
87+
Assert.True(condition, "test message");
88+
89+
// Not negated, with named parameter message
90+
Assert.True(false, userMessage: "test message");
91+
Assert.False(false, userMessage: "test message");
92+
Assert.True(condition, userMessage: "test message");
93+
Assert.False(condition, userMessage: "test message");
94+
95+
// Negated, with named parameter message
96+
Assert.False(false, userMessage: "test message");
97+
Assert.True(false, userMessage: "test message");
98+
Assert.False(condition, userMessage: "test message");
99+
Assert.True(condition, userMessage: "test message");
100+
}
101+
}
102+
""";
30103

31104
await Verify.VerifyCodeFix(before, after, BooleanAssertsShouldNotBeNegatedFixer.Key_UseSuggestedAssert);
32105
}

0 commit comments

Comments
 (0)