@@ -28,8 +28,8 @@ protected override CreateChangedDocument TryComputeFixCore(IInvocationOperation
2828 "Assert" when invocation . TargetMethod . Name is "That" => TryComputeFixForNunitThat ( invocation , context , t ) ,
2929 "Assert" when isNunit3 => TryComputeFixForNunitClassicAssert ( invocation , context , t ) ,
3030 "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 ) ,
3333 _ => null
3434 } ;
3535 }
@@ -52,17 +52,9 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
5252 return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "NotBeNull" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
5353 case "IsNaN" : // Assert.IsNaN(double actual)
5454 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)
6056 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)
6658 return DocumentEditorUtils . RenameMethodToSubjectShouldAssertion ( invocation , context , "NotBeEmpty" , subjectIndex : 0 , argumentsToRemove : [ ] ) ;
6759 case "Zero" : // Assert.Zero(int anObject)
6860 return DocumentEditorUtils . RewriteExpression ( invocation , [
@@ -231,13 +223,25 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
231223 return null ;
232224 }
233225
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+
234238 private CreateChangedDocument TryComputeFixForNunitThat ( IInvocationOperation invocation , CodeFixContext context , NunitCodeFixContext t )
235239 {
236240 // Assert.That(condition)
237241 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 )
241245 || invocation . Arguments [ 1 ] . Value . Type . EqualsSymbol ( t . String ) )
242246 )
243247 )
@@ -259,7 +263,7 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
259263 return RenameAssertThatAssertionToSubjectShouldAssertion ( "BeNull" ) ;
260264 else if ( IsPropertyOfSymbol ( constraint , "Not" , "Null" , t . Is ) ) // Assert.That(subject, Is.Not.Null)
261265 return RenameAssertThatAssertionToSubjectShouldAssertion ( "NotBeNull" ) ;
262- else if ( subject . Type . SpecialType is not SpecialType . System_Collections_IEnumerable )
266+ else if ( ! IsArgumentTypeOfNonGenericEnumerable ( invocation , argumentIndex : 0 ) )
263267 {
264268 if ( IsPropertyOfSymbol ( constraint , "Empty" , t . Is ) ) // Assert.That(subject, Is.Empty)
265269 return RenameAssertThatAssertionToSubjectShouldAssertion ( "BeEmpty" ) ;
@@ -290,6 +294,9 @@ private static bool IsPropertyOfSymbol(IPropertyReferenceOperation propertyRefer
290294 private static bool IsPropertyOfSymbol ( IOperation operation , string property , INamedTypeSymbol type )
291295 => operation is IPropertyReferenceOperation propertyReference && propertyReference . Property . Name == property && IsPropertyReferencedFromType ( propertyReference , type ) ;
292296
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 ;
293300 public class NunitCodeFixContext ( Compilation compilation ) : TestingFrameworkCodeFixProvider . TestingFrameworkCodeFixContext ( compilation )
294301 {
295302 public INamedTypeSymbol Is { get ; } = compilation . GetTypeByMetadataName ( "NUnit.Framework.Is" ) ;
0 commit comments