|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
|
5 | 5 | {
|
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
|
| 10 | + using Xunit; |
| 11 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 12 | + StyleCop.Analyzers.OrderingRules.SA1201ElementsMustAppearInTheCorrectOrder, |
| 13 | + StyleCop.Analyzers.OrderingRules.ElementOrderCodeFixProvider>; |
7 | 14 |
|
8 | 15 | public class SA1201CSharp9UnitTests : SA1201CSharp8UnitTests
|
9 | 16 | {
|
| 17 | + [Fact] |
| 18 | + [WorkItem(3236, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3236")] |
| 19 | + public async Task TestOuterOrderWithRecordCorrectOrderAsync() |
| 20 | + { |
| 21 | + string testCode = @"namespace Foo { } |
| 22 | +public delegate void bar(); |
| 23 | +public enum TestEnum { } |
| 24 | +public interface IFoo { } |
| 25 | +public struct FooStruct { } |
| 26 | +public record FooClass { } |
| 27 | +"; |
| 28 | + |
| 29 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 30 | + await VerifyCSharpDiagnosticAsync("namespace OuterNamespace { " + testCode + " }", DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + [WorkItem(3236, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3236")] |
| 35 | + public async Task TestOuterOrderWithRecordWrongOrderAsync() |
| 36 | + { |
| 37 | + string testCode = @" |
| 38 | +namespace Foo { } |
| 39 | +public enum TestEnum { } |
| 40 | +public delegate void {|#0:bar|}(); |
| 41 | +public interface IFoo { } |
| 42 | +public record FooClass { } |
| 43 | +public struct {|#1:FooStruct|} { } |
| 44 | +"; |
| 45 | + var expected = new[] |
| 46 | + { |
| 47 | + Diagnostic().WithLocation(0).WithArguments("delegate", "enum"), |
| 48 | + Diagnostic().WithLocation(1).WithArguments("struct", "record"), |
| 49 | + }; |
| 50 | + |
| 51 | + await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 52 | + await VerifyCSharpDiagnosticAsync("namespace OuterNamespace { " + testCode + " }", expected, CancellationToken.None).ConfigureAwait(false); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task TestTypeMemberOrderCorrectOrderRecordAsync() |
| 57 | + { |
| 58 | + string testCode = @"public record OuterType |
| 59 | +{ |
| 60 | + public string TestField; |
| 61 | + public OuterType(int argument) { TestField = ""foo""; TestProperty = """"; } |
| 62 | + public delegate void TestDelegate(); |
| 63 | + public event TestDelegate TestEvent { add { } remove { } } |
| 64 | + public enum TestEnum { } |
| 65 | + public interface ITest { } |
| 66 | + public string TestProperty { get; set; } |
| 67 | + public string this[string arg] { get { return ""foo""; } set { } } |
| 68 | + public static explicit operator bool(OuterType t1) { return t1.TestField != null; } |
| 69 | + public static OuterType operator +(OuterType t1, OuterType t2) { return t1; } |
| 70 | + public void TestMethod () { } |
| 71 | + public struct TestStruct { } |
| 72 | + public class TestClass1 { } |
| 73 | + public record TestRecord1 { } |
| 74 | + public class TestClass2 { } |
| 75 | + public record TestRecord2 { } |
| 76 | +} |
| 77 | +"; |
| 78 | + |
| 79 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task TestTypeMemberOrderWrongOrderRecordAsync() |
| 84 | + { |
| 85 | + string testCode = @"public record OuterType |
| 86 | +{ |
| 87 | + public string TestField; |
| 88 | + public OuterType(int argument) { TestField = ""foo""; TestProperty = ""bar""; } |
| 89 | + public interface ITest { } |
| 90 | + public delegate void TestDelegate(); |
| 91 | + public event TestDelegate TestEvent { add { } remove { } } |
| 92 | + public enum TestEnum { } |
| 93 | + public static OuterType operator +(OuterType t1, OuterType t2) { return t1; } |
| 94 | + public static explicit operator bool(OuterType t1) { return t1.TestField != null; } |
| 95 | + public string TestProperty { get; set; } |
| 96 | + public struct TestStruct { } |
| 97 | + public void TestMethod () { } |
| 98 | + public class TestClass { } |
| 99 | + public string this[string arg] { get { return ""foo""; } set { } } |
| 100 | +} |
| 101 | +"; |
| 102 | + var expected = new[] |
| 103 | + { |
| 104 | + Diagnostic().WithLocation(6, 26).WithArguments("delegate", "interface"), |
| 105 | + Diagnostic().WithLocation(10, 5).WithArguments("conversion", "operator"), |
| 106 | + Diagnostic().WithLocation(11, 19).WithArguments("property", "conversion"), |
| 107 | + Diagnostic().WithLocation(13, 17).WithArguments("method", "struct"), |
| 108 | + Diagnostic().WithLocation(15, 19).WithArguments("indexer", "class"), |
| 109 | + }; |
| 110 | + |
| 111 | + string fixedCode = @"public record OuterType |
| 112 | +{ |
| 113 | + public string TestField; |
| 114 | + public OuterType(int argument) { TestField = ""foo""; TestProperty = ""bar""; } |
| 115 | + public delegate void TestDelegate(); |
| 116 | + public event TestDelegate TestEvent { add { } remove { } } |
| 117 | + public enum TestEnum { } |
| 118 | + public interface ITest { } |
| 119 | + public string TestProperty { get; set; } |
| 120 | + public string this[string arg] { get { return ""foo""; } set { } } |
| 121 | + public static explicit operator bool(OuterType t1) { return t1.TestField != null; } |
| 122 | + public static OuterType operator +(OuterType t1, OuterType t2) { return t1; } |
| 123 | + public void TestMethod () { } |
| 124 | + public struct TestStruct { } |
| 125 | + public class TestClass { } |
| 126 | +} |
| 127 | +"; |
| 128 | + |
| 129 | + var test = new CSharpTest |
| 130 | + { |
| 131 | + TestCode = testCode, |
| 132 | + FixedCode = fixedCode, |
| 133 | + NumberOfIncrementalIterations = 7, |
| 134 | + NumberOfFixAllIterations = 3, |
| 135 | + }; |
| 136 | + |
| 137 | + test.ExpectedDiagnostics.AddRange(expected); |
| 138 | + await test.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 139 | + } |
10 | 140 | }
|
11 | 141 | }
|
0 commit comments