Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 0c6d48d

Browse files
committed
Verify Assert Argument Order
Test assert methods assume that the "actual" value is a variable and the "expected" value is a constant for the most part. Inverting these arguments causes misleading test failure messages that hinder the ability to quickly diagnose test failures. Detecting obvious mistakes (where "actual" is constant, and "expected" is variable) can be done and the arguments swapped. Fix #105
1 parent dc7409c commit 0c6d48d

File tree

7 files changed

+459
-1
lines changed

7 files changed

+459
-1
lines changed

src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal static class LocalSemanticRuleOrder
2727
public const int ExplicitVisibilityRule = 2;
2828
public const int IsFormattedFormattingRule = 3;
2929
public const int RemoveExplicitThisRule = 4;
30+
public const int AssertArgumentOrderRule = 5;
3031
}
3132

3233
// Please keep these values sorted by number, not rule name.
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
using Microsoft.CodeAnalysis;
8+
9+
using Xunit;
10+
11+
namespace XUnitConverter.Tests
12+
{
13+
public class AssertArgumentOrderTest : ConverterTestBase
14+
{
15+
protected override XUnitConverter.ConverterBase CreateConverter()
16+
{
17+
return new XUnitConverter.AssertArgumentOrderConverter();
18+
}
19+
20+
[Fact]
21+
public async Task TestSwapInvertedEqual()
22+
{
23+
string source = @"
24+
public class Tests
25+
{
26+
public void TestA()
27+
{
28+
int actual = 1;
29+
Xunit.Assert.Equal(actual, 1);
30+
}
31+
}
32+
";
33+
string expected = @"
34+
public class Tests
35+
{
36+
public void TestA()
37+
{
38+
int actual = 1;
39+
Xunit.Assert.Equal(1, actual);
40+
}
41+
}
42+
";
43+
44+
await Verify(source, expected);
45+
}
46+
47+
[Fact]
48+
public async Task TestSwapInvertedEqualEnum()
49+
{
50+
string source = @"
51+
public class Tests
52+
{
53+
private enum E
54+
{
55+
A,
56+
B,
57+
}
58+
59+
public void TestA()
60+
{
61+
E actual = E.A;
62+
Xunit.Assert.Equal(actual, E.A);
63+
}
64+
}
65+
";
66+
string expected = @"
67+
public class Tests
68+
{
69+
private enum E
70+
{
71+
A,
72+
B,
73+
}
74+
75+
public void TestA()
76+
{
77+
E actual = E.A;
78+
Xunit.Assert.Equal(E.A, actual);
79+
}
80+
}
81+
";
82+
await Verify(source, expected);
83+
}
84+
85+
[Fact]
86+
public async Task TestSwapInvertedEqualConstField()
87+
{
88+
string source = @"
89+
public class Tests
90+
{
91+
private const int A;
92+
93+
public void TestA()
94+
{
95+
int actual = A;
96+
Xunit.Assert.Equal(actual, A);
97+
}
98+
}
99+
";
100+
string expected = @"
101+
public class Tests
102+
{
103+
private const int A;
104+
105+
public void TestA()
106+
{
107+
int actual = A;
108+
Xunit.Assert.Equal(A, actual);
109+
}
110+
}
111+
";
112+
await Verify(source, expected);
113+
}
114+
115+
[Fact]
116+
public async Task TestSwapInvertedNotEqual()
117+
{
118+
string source = @"
119+
public class Tests
120+
{
121+
public void TestA()
122+
{
123+
int actual = 1;
124+
Xunit.Assert.NotEqual(actual, 1);
125+
}
126+
}
127+
";
128+
string expected = @"
129+
public class Tests
130+
{
131+
public void TestA()
132+
{
133+
int actual = 1;
134+
Xunit.Assert.NotEqual(1, actual);
135+
}
136+
}
137+
";
138+
await Verify(source, expected);
139+
}
140+
141+
[Fact]
142+
public async Task TestSwapInvertedEqualFromUsing()
143+
{
144+
string source = @"
145+
using Xunit;
146+
147+
public class Tests
148+
{
149+
public void TestA()
150+
{
151+
int actual = 1;
152+
Assert.Equal(actual, 1);
153+
}
154+
}
155+
";
156+
string expected = @"
157+
using Xunit;
158+
159+
public class Tests
160+
{
161+
public void TestA()
162+
{
163+
int actual = 1;
164+
Assert.Equal(1, actual);
165+
}
166+
}
167+
";
168+
await Verify(source, expected);
169+
}
170+
171+
[Fact]
172+
public async Task TestIgnoredCorrectEqual()
173+
{
174+
string text = @"
175+
public class Tests
176+
{
177+
public void TestA()
178+
{
179+
int actual = 1;
180+
Xunit.Assert.Equal(1, actual);
181+
}
182+
}
183+
";
184+
await Verify(text, text);
185+
}
186+
187+
[Fact]
188+
public async Task TestIgnoredDoubleConstEqual()
189+
{
190+
string text = @"
191+
public class Tests
192+
{
193+
public void TestA()
194+
{
195+
Xunit.Assert.Equal(1, 2);
196+
}
197+
}
198+
";
199+
await Verify(text, text);
200+
}
201+
202+
[Fact]
203+
public async Task TestIgnoredDoubleVariableEqual()
204+
{
205+
string text = @"
206+
public class Tests
207+
{
208+
public void TestA()
209+
{
210+
int actual = 1;
211+
int expected = 1;
212+
Xunit.Assert.Equal(actual, expected);
213+
}
214+
}
215+
";
216+
await Verify(text, text);
217+
}
218+
219+
[Fact]
220+
public async Task TestIgnoredCorrectNotEqual()
221+
{
222+
string text = @"
223+
public class Tests
224+
{
225+
public void TestA()
226+
{
227+
int actual = 1;
228+
Xunit.Assert.NotEqual(1, actual);
229+
}
230+
}
231+
";
232+
await Verify(text, text);
233+
}
234+
235+
[Fact]
236+
public async Task TestIgnoreOtherAssert()
237+
{
238+
string text = @"
239+
public class Assert
240+
{
241+
public void Equal(int expected, int actual)
242+
{
243+
}
244+
}
245+
246+
public class Tests
247+
{
248+
public void TestA()
249+
{
250+
int actual = 1;
251+
Assert.NotEqual(1, actual);
252+
}
253+
}
254+
";
255+
await Verify(text, text);
256+
}
257+
}
258+
}

src/XUnitConverter.Tests/XUnitConverter.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
</Reference>
104104
</ItemGroup>
105105
<ItemGroup>
106+
<Compile Include="AssertArgumentOrderTest.cs" />
106107
<Compile Include="ConverterTestBase.cs" />
107108
<Compile Include="Properties\AssemblyInfo.cs" />
108109
<Compile Include="TestAssertTrueOrFalseConverterTests.cs" />

0 commit comments

Comments
 (0)