@@ -59,6 +59,30 @@ public static IEnumerable<object[]> InvalidDeclarations
5959 }
6060 }
6161
62+ public static IEnumerable < object [ ] > ValidNestedDeclarations
63+ {
64+ get
65+ {
66+ yield return new object [ ] { "public" , "class" } ;
67+ yield return new object [ ] { "protected" , "class" } ;
68+ yield return new object [ ] { "internal" , "class" } ;
69+ yield return new object [ ] { "protected internal" , "class" } ;
70+ yield return new object [ ] { "private" , "class" } ;
71+
72+ yield return new object [ ] { "public" , "struct" } ;
73+ yield return new object [ ] { "protected" , "struct" } ;
74+ yield return new object [ ] { "internal" , "struct" } ;
75+ yield return new object [ ] { "protected internal" , "struct" } ;
76+ yield return new object [ ] { "private" , "struct" } ;
77+
78+ yield return new object [ ] { "public" , "interface" } ;
79+ yield return new object [ ] { "protected" , "interface" } ;
80+ yield return new object [ ] { "internal" , "interface" } ;
81+ yield return new object [ ] { "protected internal" , "interface" } ;
82+ yield return new object [ ] { "private" , "interface" } ;
83+ }
84+ }
85+
6286 /// <summary>
6387 /// Verifies that a valid declaration (with an access modifier or not a partial type) will not produce a diagnostic.
6488 /// </summary>
@@ -171,21 +195,7 @@ public partial class Foo
171195 /// <param name="typeKeyword">The type keyword to use.</param>
172196 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
173197 [ Theory ]
174- [ InlineData ( "public" , "class" ) ]
175- [ InlineData ( "protected" , "class" ) ]
176- [ InlineData ( "internal" , "class" ) ]
177- [ InlineData ( "protected internal" , "class" ) ]
178- [ InlineData ( "private" , "class" ) ]
179- [ InlineData ( "public" , "struct" ) ]
180- [ InlineData ( "protected" , "struct" ) ]
181- [ InlineData ( "internal" , "struct" ) ]
182- [ InlineData ( "protected internal" , "struct" ) ]
183- [ InlineData ( "private" , "struct" ) ]
184- [ InlineData ( "public" , "interface" ) ]
185- [ InlineData ( "protected" , "interface" ) ]
186- [ InlineData ( "internal" , "interface" ) ]
187- [ InlineData ( "protected internal" , "interface" ) ]
188- [ InlineData ( "private" , "interface" ) ]
198+ [ MemberData ( nameof ( ValidNestedDeclarations ) ) ]
189199 public async Task TestNestedTypeAccessModifiersAsync ( string accessModifier , string typeKeyword )
190200 {
191201 var testCode = $@ "
@@ -232,6 +242,47 @@ public class Foo
232242 await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
233243 }
234244
245+ /// <summary>
246+ /// Verifies that the code fix will properly copy over the access modifier defined in another fragment of the nested partial element.
247+ /// </summary>
248+ /// <param name="accessModifier">The access modifier to use for the nested type.</param>
249+ /// <param name="typeKeyword">The type keyword to use.</param>
250+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
251+ [ Theory ]
252+ [ MemberData ( nameof ( ValidNestedDeclarations ) ) ]
253+ public async Task TestProperNestedAccessModifierPropagationAsync ( string accessModifier , string typeKeyword )
254+ {
255+ var testCode = $@ "
256+ public class Foo
257+ {{
258+ { accessModifier } partial { typeKeyword } Bar
259+ {{
260+ }}
261+
262+ partial { typeKeyword } Bar
263+ {{
264+ }}
265+ }}
266+ " ;
267+
268+ var fixedTestCode = $@ "
269+ public class Foo
270+ {{
271+ { accessModifier } partial { typeKeyword } Bar
272+ {{
273+ }}
274+
275+ { accessModifier } partial { typeKeyword } Bar
276+ {{
277+ }}
278+ }}
279+ " ;
280+
281+ await this . VerifyCSharpDiagnosticAsync ( testCode , this . CSharpDiagnostic ( ) . WithLocation ( 8 , 14 + typeKeyword . Length ) , CancellationToken . None ) . ConfigureAwait ( false ) ;
282+ await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
283+ await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
284+ }
285+
235286 /// <inheritdoc/>
236287 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
237288 {
0 commit comments