@@ -28,8 +28,8 @@ protected override CreateChangedDocument TryComputeFixCore(IInvocationOperation
28
28
"Assert" when invocation . TargetMethod . Name is "That" => TryComputeFixForNunitThat ( invocation , context , t ) ,
29
29
"Assert" when isNunit3 => TryComputeFixForNunitClassicAssert ( invocation , context , t ) ,
30
30
"ClassicAssert" when isNunit4 => TryComputeFixForNunitClassicAssert ( invocation , context , t ) ,
31
- //"StringAssert" => TryComputeFixForStringAssert(invocation, context, testContext ),
32
- // "CollectionAssert" => TryComputeFixForCollectionAssert(invocation, context, testContext ),
31
+ //"StringAssert" => TryComputeFixForStringAssert(invocation, context, t ),
32
+ "CollectionAssert" => TryComputeFixForCollectionAssert ( invocation , context , t ) ,
33
33
_ => null
34
34
} ;
35
35
}
@@ -52,17 +52,9 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
52
52
return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "NotBeNull" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
53
53
case "IsNaN" : // Assert.IsNaN(double actual)
54
54
return null ;
55
- case "IsEmpty" : // Assert.IsEmpty(IEnumerable collection) | Assert.IsEmpty(string s)
56
- if ( invocation . Arguments [ 0 ] . Value . UnwrapConversion ( ) . Type . SpecialType is SpecialType . System_Collections_IEnumerable )
57
- {
58
- return null ;
59
- }
55
+ case "IsEmpty" when ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) : // Assert.IsEmpty(IEnumerable collection) | Assert.IsEmpty(string s)
60
56
return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "BeEmpty" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
61
- case "IsNotEmpty" : // Assert.IsNotEmpty(IEnumerable collection) | Assert.IsNotEmpty(string s)
62
- if ( invocation . Arguments [ 0 ] . Value . UnwrapConversion ( ) . Type . SpecialType is SpecialType . System_Collections_IEnumerable )
63
- {
64
- return null ;
65
- }
57
+ case "IsNotEmpty" when ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) : // Assert.IsNotEmpty(IEnumerable collection) | Assert.IsNotEmpty(string s)
66
58
return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "NotBeEmpty" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
67
59
case "Zero" : // Assert.Zero(int anObject)
68
60
return DocumentEditorUtils . RewriteExpression ( invocation , [
@@ -231,13 +223,25 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
231
223
return null ;
232
224
}
233
225
226
+ private CreateChangedDocument TryComputeFixForCollectionAssert ( IInvocationOperation invocation , CodeFixContext context , NunitCodeFixContext t )
227
+ {
228
+ switch ( invocation . TargetMethod . Name )
229
+ {
230
+ case "IsEmpty" when ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) : // CollectionAssert.IsEmpty(IEnumerable collection)
231
+ return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "BeEmpty" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
232
+ case "IsNotEmpty" when ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) : // CollectionAssert.IsNotEmpty(IEnumerable collection)
233
+ return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "NotBeEmpty" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
234
+ }
235
+ return null ;
236
+ }
237
+
234
238
private CreateChangedDocument TryComputeFixForNunitThat ( IInvocationOperation invocation , CodeFixContext context , NunitCodeFixContext t )
235
239
{
236
240
// Assert.That(condition)
237
241
if ( invocation . Arguments [ 0 ] . Value . Type . EqualsSymbol ( t . Boolean )
238
- && ( invocation . Arguments . Length is 1
239
- || ( invocation . Arguments . Length >= 2
240
- && ( invocation . Arguments [ 1 ] . Value . Type . EqualsSymbol ( t . NUnitString )
242
+ && ( invocation . Arguments . Length is 1
243
+ || ( invocation . Arguments . Length >= 2
244
+ && ( invocation . Arguments [ 1 ] . Value . Type . EqualsSymbol ( t . NUnitString )
241
245
|| invocation . Arguments [ 1 ] . Value . Type . EqualsSymbol ( t . String ) )
242
246
)
243
247
)
@@ -259,7 +263,7 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
259
263
return RenameAssertThatAssertionToSubjectShouldAssertion ( "BeNull" ) ;
260
264
else if ( IsPropertyOfSymbol ( constraint , "Not" , "Null" , t . Is ) ) // Assert.That(subject, Is.Not.Null)
261
265
return RenameAssertThatAssertionToSubjectShouldAssertion ( "NotBeNull" ) ;
262
- else if ( subject . Type . SpecialType is not SpecialType . System_Collections_IEnumerable )
266
+ else if ( ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) )
263
267
{
264
268
if ( IsPropertyOfSymbol ( constraint , "Empty" , t . Is ) ) // Assert.That(subject, Is.Empty)
265
269
return RenameAssertThatAssertionToSubjectShouldAssertion ( "BeEmpty" ) ;
@@ -290,6 +294,9 @@ private static bool IsPropertyOfSymbol(IPropertyReferenceOperation propertyRefer
290
294
private static bool IsPropertyOfSymbol ( IOperation operation , string property , INamedTypeSymbol type )
291
295
=> operation is IPropertyReferenceOperation propertyReference && propertyReference . Property . Name == property && IsPropertyReferencedFromType ( propertyReference , type ) ;
292
296
297
+ private static bool IsArgumentTypeOfNonGenericEnumerable ( IInvocationOperation invocation , int argumentIndex ) => IsArgumentTypeOf ( invocation , argumentIndex , SpecialType . System_Collections_IEnumerable ) ;
298
+ private static bool IsArgumentTypeOf ( IInvocationOperation invocation , int argumentIndex , SpecialType specialType )
299
+ => invocation . Arguments [ argumentIndex ] . Value . UnwrapConversion ( ) . Type . SpecialType == specialType ;
293
300
public class NunitCodeFixContext ( Compilation compilation ) : TestingFrameworkCodeFixProvider . TestingFrameworkCodeFixContext ( compilation )
294
301
{
295
302
public INamedTypeSymbol Is { get ; } = compilation . GetTypeByMetadataName ( "NUnit.Framework.Is" ) ;
0 commit comments