|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -using System; |
6 | | -using System.Collections.Generic; |
7 | 5 | using System.Collections.Immutable; |
8 | 6 | using System.Threading.Tasks; |
9 | 7 | using Microsoft.CodeAnalysis.CSharp; |
10 | 8 | using Microsoft.CodeAnalysis.CSharp.ConvertToRecord; |
11 | 9 | using Microsoft.CodeAnalysis.Diagnostics; |
12 | 10 | using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; |
13 | 11 | using Microsoft.CodeAnalysis.Test.Utilities; |
14 | | -using Microsoft.CodeAnalysis.Testing; |
15 | | -using Roslyn.Test.Utilities; |
16 | 12 | using Xunit; |
17 | 13 |
|
18 | | -namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertToRecord |
| 14 | +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertToRecord; |
| 15 | + |
| 16 | +[Trait(Traits.Feature, Traits.Features.CodeActionsConvertToRecord)] |
| 17 | +public class ConvertToRecordCodeFixTests |
19 | 18 | { |
20 | | - [Trait(Traits.Feature, Traits.Features.CodeActionsConvertToRecord)] |
21 | | - public class ConvertToRecordCodeFixTests |
| 19 | + [Fact] |
| 20 | + public async Task TestMovePropertySimpleRecordInheritance_CodeFix() |
22 | 21 | { |
23 | | - [Fact] |
24 | | - public async Task TestMovePropertySimpleRecordInheritance_CodeFix() |
25 | | - { |
26 | | - var initialMarkup = """ |
27 | | - namespace N |
| 22 | + var initialMarkup = """ |
| 23 | + namespace N |
| 24 | + { |
| 25 | + public record B |
28 | 26 | { |
29 | | - public record B |
30 | | - { |
31 | | - public int Foo { get; init; } |
32 | | - } |
| 27 | + public int Foo { get; init; } |
| 28 | + } |
33 | 29 |
|
34 | | - public class C : [|B|] |
35 | | - { |
36 | | - public int P { get; init; } |
37 | | - } |
| 30 | + public class C : [|B|] |
| 31 | + { |
| 32 | + public int P { get; init; } |
38 | 33 | } |
39 | | - """; |
40 | | - var changedMarkup = """ |
41 | | - namespace N |
| 34 | + } |
| 35 | + """; |
| 36 | + var changedMarkup = """ |
| 37 | + namespace N |
| 38 | + { |
| 39 | + public record B |
42 | 40 | { |
43 | | - public record B |
44 | | - { |
45 | | - public int Foo { get; init; } |
46 | | - } |
47 | | -
|
48 | | - public record C(int P) : B; |
| 41 | + public int Foo { get; init; } |
49 | 42 | } |
50 | | - """; |
51 | | - await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
52 | | - } |
53 | 43 |
|
54 | | - [Fact] |
55 | | - public async Task TestMovePropertyPositionalParameterRecordInheritance_CodeFix() |
56 | | - { |
57 | | - var initialMarkup = """ |
58 | | - namespace N |
59 | | - { |
60 | | - public record B(int Foo, int Bar); |
| 44 | + public record C(int P) : B; |
| 45 | + } |
| 46 | + """; |
| 47 | + await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
| 48 | + } |
61 | 49 |
|
62 | | - public class {|CS1729:C|} : [|B|] |
63 | | - { |
64 | | - public int P { get; init; } |
65 | | - } |
66 | | - } |
67 | | - """; |
68 | | - var changedMarkup = """ |
69 | | - namespace N |
70 | | - { |
71 | | - public record B(int Foo, int Bar); |
| 50 | + [Fact] |
| 51 | + public async Task TestMovePropertyPositionalParameterRecordInheritance_CodeFix() |
| 52 | + { |
| 53 | + var initialMarkup = """ |
| 54 | + namespace N |
| 55 | + { |
| 56 | + public record B(int Foo, int Bar); |
72 | 57 |
|
73 | | - public record C(int Foo, int Bar, int P) : B(Foo, Bar); |
| 58 | + public class {|CS1729:C|} : [|B|] |
| 59 | + { |
| 60 | + public int P { get; init; } |
74 | 61 | } |
75 | | - """; |
76 | | - await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
77 | | - } |
| 62 | + } |
| 63 | + """; |
| 64 | + var changedMarkup = """ |
| 65 | + namespace N |
| 66 | + { |
| 67 | + public record B(int Foo, int Bar); |
78 | 68 |
|
79 | | - [Fact] |
80 | | - public async Task TestMovePropertyPositionalParameterRecordInheritanceWithComments_CodeFix() |
81 | | - { |
82 | | - var initialMarkup = """ |
83 | | - namespace N |
84 | | - { |
85 | | - /// <summary> B </summary> |
86 | | - /// <param name="Foo"> Foo is an int </param> |
87 | | - /// <param name="Bar"> Bar is an int as well </param> |
88 | | - public record B(int Foo, int Bar); |
| 69 | + public record C(int Foo, int Bar, int P) : B(Foo, Bar); |
| 70 | + } |
| 71 | + """; |
| 72 | + await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
| 73 | + } |
89 | 74 |
|
90 | | - /// <summary> C inherits from B </summary> |
91 | | - public class {|CS1729:C|} : [|B|] |
92 | | - { |
93 | | - /// <summary> P can be initialized </summary> |
94 | | - public int P { get; init; } |
95 | | - } |
96 | | - } |
97 | | - """; |
98 | | - var changedMarkup = """ |
99 | | - namespace N |
| 75 | + [Fact] |
| 76 | + public async Task TestMovePropertyPositionalParameterRecordInheritanceWithComments_CodeFix() |
| 77 | + { |
| 78 | + var initialMarkup = """ |
| 79 | + namespace N |
| 80 | + { |
| 81 | + /// <summary> B </summary> |
| 82 | + /// <param name="Foo"> Foo is an int </param> |
| 83 | + /// <param name="Bar"> Bar is an int as well </param> |
| 84 | + public record B(int Foo, int Bar); |
| 85 | +
|
| 86 | + /// <summary> C inherits from B </summary> |
| 87 | + public class {|CS1729:C|} : [|B|] |
100 | 88 | { |
101 | | - /// <summary> B </summary> |
102 | | - /// <param name="Foo"> Foo is an int </param> |
103 | | - /// <param name="Bar"> Bar is an int as well </param> |
104 | | - public record B(int Foo, int Bar); |
105 | | -
|
106 | | - /// <summary> C inherits from B </summary> |
107 | | - /// <param name="Foo"><inheritdoc/></param> |
108 | | - /// <param name="Bar"><inheritdoc/></param> |
109 | | - /// <param name="P"> P can be initialized </param> |
110 | | - public record C(int Foo, int Bar, int P) : B(Foo, Bar); |
| 89 | + /// <summary> P can be initialized </summary> |
| 90 | + public int P { get; init; } |
111 | 91 | } |
112 | | - """; |
113 | | - await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
114 | | - } |
| 92 | + } |
| 93 | + """; |
| 94 | + var changedMarkup = """ |
| 95 | + namespace N |
| 96 | + { |
| 97 | + /// <summary> B </summary> |
| 98 | + /// <param name="Foo"> Foo is an int </param> |
| 99 | + /// <param name="Bar"> Bar is an int as well </param> |
| 100 | + public record B(int Foo, int Bar); |
| 101 | +
|
| 102 | + /// <summary> C inherits from B </summary> |
| 103 | + /// <param name="Foo"><inheritdoc/></param> |
| 104 | + /// <param name="Bar"><inheritdoc/></param> |
| 105 | + /// <param name="P"> P can be initialized </param> |
| 106 | + public record C(int Foo, int Bar, int P) : B(Foo, Bar); |
| 107 | + } |
| 108 | + """; |
| 109 | + await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
| 110 | + } |
115 | 111 |
|
116 | | - [Fact] |
117 | | - public async Task TestMovePropertyAndReorderWithPositionalParameterRecordInheritance_CodeFix() |
118 | | - { |
119 | | - var initialMarkup = """ |
120 | | - namespace N |
| 112 | + [Fact] |
| 113 | + public async Task TestMovePropertyAndReorderWithPositionalParameterRecordInheritance_CodeFix() |
| 114 | + { |
| 115 | + var initialMarkup = """ |
| 116 | + namespace N |
| 117 | + { |
| 118 | + public record B(int Foo, int Bar); |
| 119 | +
|
| 120 | + public class C : [|B|] |
121 | 121 | { |
122 | | - public record B(int Foo, int Bar); |
| 122 | + public int P { get; init; } |
123 | 123 |
|
124 | | - public class C : [|B|] |
| 124 | + public {|CS1729:C|}(int p, int bar, int foo) |
125 | 125 | { |
126 | | - public int P { get; init; } |
127 | | -
|
128 | | - public {|CS1729:C|}(int p, int bar, int foo) |
129 | | - { |
130 | | - P = p; |
131 | | - Bar = bar; |
132 | | - Foo = foo; |
133 | | - } |
| 126 | + P = p; |
| 127 | + Bar = bar; |
| 128 | + Foo = foo; |
134 | 129 | } |
135 | 130 | } |
136 | | - """; |
137 | | - var changedMarkup = """ |
138 | | - namespace N |
139 | | - { |
140 | | - public record B(int Foo, int Bar); |
| 131 | + } |
| 132 | + """; |
| 133 | + var changedMarkup = """ |
| 134 | + namespace N |
| 135 | + { |
| 136 | + public record B(int Foo, int Bar); |
141 | 137 |
|
142 | | - public record C(int P, int Bar, int Foo) : B(Foo, Bar); |
143 | | - } |
144 | | - """; |
145 | | - await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
146 | | - } |
| 138 | + public record C(int P, int Bar, int Foo) : B(Foo, Bar); |
| 139 | + } |
| 140 | + """; |
| 141 | + await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false); |
| 142 | + } |
147 | 143 |
|
148 | | - private class CodeFixTest : CSharpCodeFixVerifier<TestAnalyzer, CSharpConvertToRecordCodeFixProvider>.Test |
149 | | - { |
150 | | - } |
| 144 | + private class CodeFixTest : CSharpCodeFixVerifier<TestAnalyzer, CSharpConvertToRecordCodeFixProvider>.Test |
| 145 | + { |
| 146 | + } |
151 | 147 |
|
152 | | - private static async Task TestCodeFixAsync(string initialMarkup, string fixedMarkup) |
| 148 | + private static async Task TestCodeFixAsync(string initialMarkup, string fixedMarkup) |
| 149 | + { |
| 150 | + var test = new CodeFixTest() |
153 | 151 | { |
154 | | - var test = new CodeFixTest() |
155 | | - { |
156 | | - TestCode = initialMarkup, |
157 | | - FixedCode = fixedMarkup, |
158 | | - LanguageVersion = LanguageVersion.CSharp10, |
159 | | - ReferenceAssemblies = Testing.ReferenceAssemblies.Net.Net60, |
160 | | - }; |
161 | | - await test.RunAsync().ConfigureAwait(false); |
162 | | - } |
| 152 | + TestCode = initialMarkup, |
| 153 | + FixedCode = fixedMarkup, |
| 154 | + LanguageVersion = LanguageVersion.CSharp10, |
| 155 | + ReferenceAssemblies = Testing.ReferenceAssemblies.Net.Net60, |
| 156 | + }; |
| 157 | + await test.RunAsync().ConfigureAwait(false); |
| 158 | + } |
163 | 159 |
|
164 | | - private class TestAnalyzer : DiagnosticAnalyzer |
165 | | - { |
166 | | - public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 160 | + private class TestAnalyzer : DiagnosticAnalyzer |
| 161 | + { |
| 162 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
167 | 163 | #pragma warning disable RS0030 // Do not used banned APIs |
168 | | - => ImmutableArray.Create(new DiagnosticDescriptor( |
169 | | - "CS8865", |
170 | | - "Only records may inherit from records.", |
171 | | - "Only records may inherit from records.", |
172 | | - "Compiler error", |
173 | | - DiagnosticSeverity.Error, |
174 | | - isEnabledByDefault: true)); |
| 164 | + => ImmutableArray.Create(new DiagnosticDescriptor( |
| 165 | + "CS8865", |
| 166 | + "Only records may inherit from records.", |
| 167 | + "Only records may inherit from records.", |
| 168 | + "Compiler error", |
| 169 | + DiagnosticSeverity.Error, |
| 170 | + isEnabledByDefault: true)); |
175 | 171 | #pragma warning restore RS0030 // Do not used banned APIs |
176 | 172 |
|
177 | | - public override void Initialize(AnalysisContext context) |
178 | | - { |
179 | | - } |
| 173 | + public override void Initialize(AnalysisContext context) |
| 174 | + { |
180 | 175 | } |
181 | 176 | } |
182 | 177 | } |
0 commit comments