Skip to content

Commit 0ddb78f

Browse files
committed
Merge pull request #1326 from Noryoko/ElementOrderCodeFix
2 parents 1c1d3e9 + 861c3d1 commit 0ddb78f

16 files changed

Lines changed: 1128 additions & 159 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ namespace StyleCop.Analyzers.Test.OrderingRules
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CodeFixes;
1011
using Microsoft.CodeAnalysis.Diagnostics;
1112
using StyleCop.Analyzers.OrderingRules;
1213
using TestHelper;
1314
using Xunit;
1415

15-
public class SA1201UnitTests : DiagnosticVerifier
16+
public class SA1201UnitTests : CodeFixVerifier
1617
{
1718
[Fact]
1819
public async Task TestOuterOrderCorrectOrderAsync()
@@ -131,8 +132,8 @@ public enum TestEnum { }
131132
public string TestProperty { get; set; }
132133
public struct TestStruct { }
133134
public void TestMethod () { }
134-
public string this[string arg] { get { return ""foo""; } set { } }
135135
public class TestClass { }
136+
public string this[string arg] { get { return ""foo""; } set { } }
136137
}
137138
";
138139
var expected = new[]
@@ -142,10 +143,32 @@ public class TestClass { }
142143
this.CSharpDiagnostic().WithLocation(11, 5).WithArguments("conversion", "operator"),
143144
this.CSharpDiagnostic().WithLocation(12, 19).WithArguments("property", "conversion"),
144145
this.CSharpDiagnostic().WithLocation(14, 17).WithArguments("method", "struct"),
145-
this.CSharpDiagnostic().WithLocation(15, 19).WithArguments("indexer", "method")
146+
this.CSharpDiagnostic().WithLocation(16, 19).WithArguments("indexer", "class")
146147
};
147148

148149
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
150+
151+
string fixedCode = @"public class OuterType
152+
{
153+
public string TestField;
154+
public OuterType() { }
155+
~OuterType() { }
156+
public delegate void TestDelegate();
157+
public event TestDelegate TestEvent { add { } remove { } }
158+
public enum TestEnum { }
159+
public interface ITest { }
160+
public string TestProperty { get; set; }
161+
public string this[string arg] { get { return ""foo""; } set { } }
162+
public static explicit operator bool(OuterType t1) { return t1.TestField != null; }
163+
public static OuterType operator +(OuterType t1, OuterType t2) { return t1; }
164+
public void TestMethod () { }
165+
public struct TestStruct { }
166+
public class TestClass { }
167+
}
168+
";
169+
170+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
171+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
149172
}
150173

151174
[Fact]
@@ -164,8 +187,8 @@ public enum TestEnum { }
164187
public string TestProperty { get; set; }
165188
public struct TestStruct { }
166189
public void TestMethod () { }
167-
public string this[string arg] { get { return ""foo""; } set { } }
168190
public class TestClass { }
191+
public string this[string arg] { get { return ""foo""; } set { } }
169192
}
170193
";
171194
var expected = new[]
@@ -174,29 +197,64 @@ public class TestClass { }
174197
this.CSharpDiagnostic().WithLocation(10, 5).WithArguments("conversion", "operator"),
175198
this.CSharpDiagnostic().WithLocation(11, 19).WithArguments("property", "conversion"),
176199
this.CSharpDiagnostic().WithLocation(13, 17).WithArguments("method", "struct"),
177-
this.CSharpDiagnostic().WithLocation(14, 19).WithArguments("indexer", "method")
200+
this.CSharpDiagnostic().WithLocation(15, 19).WithArguments("indexer", "class")
178201
};
179202

180203
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
204+
205+
string fixedCode = @"public struct OuterType
206+
{
207+
public string TestField;
208+
public OuterType(int argument) { TestField = ""foo""; TestProperty = ""bar""; }
209+
public delegate void TestDelegate();
210+
public event TestDelegate TestEvent { add { } remove { } }
211+
public enum TestEnum { }
212+
public interface ITest { }
213+
public string TestProperty { get; set; }
214+
public string this[string arg] { get { return ""foo""; } set { } }
215+
public static explicit operator bool(OuterType t1) { return t1.TestField != null; }
216+
public static OuterType operator +(OuterType t1, OuterType t2) { return t1; }
217+
public void TestMethod () { }
218+
public struct TestStruct { }
219+
public class TestClass { }
220+
}
221+
";
222+
223+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
224+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
181225
}
182226

183227
[Fact]
184228
public async Task TestTypeMemberOrderWrongOrderInterfaceAsync()
185229
{
186230
string testCode = @"public interface OuterType
187231
{
188-
event System.Action TestEvent;
189232
string TestProperty { get; set; }
233+
event System.Action TestEvent;
190234
void TestMethod ();
191235
string this[string arg] { get; set; }
192236
}
193237
";
194-
var expected = new[]
238+
239+
DiagnosticResult[] expected =
195240
{
241+
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("event", "property"),
196242
this.CSharpDiagnostic().WithLocation(6, 12).WithArguments("indexer", "method")
197243
};
198244

199245
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
246+
247+
string fixedCode = @"public interface OuterType
248+
{
249+
event System.Action TestEvent;
250+
string TestProperty { get; set; }
251+
string this[string arg] { get; set; }
252+
void TestMethod ();
253+
}
254+
";
255+
256+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
257+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
200258
}
201259

202260
[Fact]
@@ -248,9 +306,16 @@ public event System.Action TestEvent4 { add { } remove { } }
248306
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
249307
}
250308

309+
/// <inheritdoc/>
251310
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
252311
{
253312
yield return new SA1201ElementsMustAppearInTheCorrectOrder();
254313
}
314+
315+
/// <inheritdoc/>
316+
protected override CodeFixProvider GetCSharpCodeFixProvider()
317+
{
318+
return new ElementOrderCodeFixProvider();
319+
}
255320
}
256321
}

0 commit comments

Comments
 (0)