Skip to content

Commit 9a1cda4

Browse files
authored
feat: add nunit assert.that empty assertions (#339)
1 parent e675f6b commit 9a1cda4

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

docs/NunitAnalyzer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ var collection = new List<int>();
174174

175175
// old assertion:
176176
Assert.IsEmpty(collection);
177+
Assert.That(collection, Is.Empty);
177178

178179
// new assertion:
179180
collection.Should().BeEmpty();
@@ -188,6 +189,9 @@ var collection = new List<int> { 1, 2, 3 };
188189
Assert.IsEmpty(collection); /* fail message: Expected: <empty>
189190
But was: < 1, 2, 3 >
190191
*/
192+
Assert.That(collection, Is.Empty); /* fail message: Expected: <empty>
193+
But was: < 1, 2, 3 >
194+
*/
191195

192196
// new assertion:
193197
collection.Should().BeEmpty(); /* fail message: Expected collection to be empty, but found {1, 2, 3}. */
@@ -201,6 +205,7 @@ var collection = new List<int> { 1, 2, 3 };
201205

202206
// old assertion:
203207
Assert.IsNotEmpty(collection);
208+
Assert.That(collection, Is.Not.Empty);
204209

205210
// new assertion:
206211
collection.Should().NotBeEmpty();
@@ -215,6 +220,9 @@ var collection = new List<int>();
215220
Assert.IsNotEmpty(collection); /* fail message: Expected: not <empty>
216221
But was: <empty>
217222
*/
223+
Assert.That(collection, Is.Not.Empty); /* fail message: Expected: not <empty>
224+
But was: <empty>
225+
*/
218226

219227
// new assertion:
220228
collection.Should().NotBeEmpty(); /* fail message: Expected collection not to be empty. */

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,14 @@ public void AssertIsEmpty()
270270

271271
// old assertion:
272272
Assert.IsEmpty(collection);
273+
Assert.That(collection, Is.Empty);
273274

274275
// new assertion:
275276
collection.Should().BeEmpty();
276277
}
277278

278279
[TestMethod, ExpectedTestFrameworkException]
279-
public void AssertIsEmpty_Failure_OldAssertion()
280+
public void AssertIsEmpty_Failure_OldAssertion_0()
280281
{
281282
// arrange
282283
var collection = new List<int> { 1, 2, 3 };
@@ -285,6 +286,16 @@ public void AssertIsEmpty_Failure_OldAssertion()
285286
Assert.IsEmpty(collection);
286287
}
287288

289+
[TestMethod, ExpectedTestFrameworkException]
290+
public void AssertIsEmpty_Failure_OldAssertion_1()
291+
{
292+
// arrange
293+
var collection = new List<int> { 1, 2, 3 };
294+
295+
// old assertion:
296+
Assert.That(collection, Is.Empty);
297+
}
298+
288299
[TestMethod, ExpectedTestFrameworkException]
289300
public void AssertIsEmpty_Failure_NewAssertion()
290301
{
@@ -303,13 +314,14 @@ public void AssertIsNotEmpty()
303314

304315
// old assertion:
305316
Assert.IsNotEmpty(collection);
317+
Assert.That(collection, Is.Not.Empty);
306318

307319
// new assertion:
308320
collection.Should().NotBeEmpty();
309321
}
310322

311323
[TestMethod, ExpectedTestFrameworkException]
312-
public void AssertIsNotEmpty_Failure_OldAssertion()
324+
public void AssertIsNotEmpty_Failure_OldAssertion_0()
313325
{
314326
// arrange
315327
var collection = new List<int>();
@@ -318,6 +330,16 @@ public void AssertIsNotEmpty_Failure_OldAssertion()
318330
Assert.IsNotEmpty(collection);
319331
}
320332

333+
[TestMethod, ExpectedTestFrameworkException]
334+
public void AssertIsNotEmpty_Failure_OldAssertion_1()
335+
{
336+
// arrange
337+
var collection = new List<int>();
338+
339+
// old assertion:
340+
Assert.That(collection, Is.Not.Empty);
341+
}
342+
321343
[TestMethod, ExpectedTestFrameworkException]
322344
public void AssertIsNotEmpty_Failure_NewAssertion()
323345
{

src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public void Nunit4_AssertIsNaN_TestAnalyzer(string assertion)
243243
// IsEmpty
244244
[DataTestMethod]
245245
[AssertionDiagnostic("Assert.IsEmpty(actual{0});")]
246+
[AssertionDiagnostic("Assert.That(actual, Is.Empty{0});")]
246247
[Implemented]
247248
public void Nunit3_AssertIsEmpty_TestAnalyzer(string assertion)
248249
{
@@ -267,6 +268,9 @@ public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
267268
[AssertionCodeFix(
268269
oldAssertion: "Assert.IsEmpty(actual{0});",
269270
newAssertion: "actual.Should().BeEmpty({0});")]
271+
[AssertionCodeFix(
272+
oldAssertion: "Assert.That(actual, Is.Empty{0});",
273+
newAssertion: "actual.Should().BeEmpty({0});")]
270274
[Implemented]
271275
public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAssertion)
272276
{
@@ -292,6 +296,7 @@ public void Nunit4_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAsse
292296
// IsNotEmpty
293297
[DataTestMethod]
294298
[AssertionDiagnostic("Assert.IsNotEmpty(actual{0});")]
299+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Empty{0});")]
295300
[Implemented]
296301
public void Nunit3_AssertIsNotEmpty_TestAnalyzer(string assertion)
297302
{
@@ -316,6 +321,9 @@ public void Nunit4_AssertIsNotEmpty_TestAnalyzer(string assertion)
316321
[AssertionCodeFix(
317322
oldAssertion: "Assert.IsNotEmpty(actual{0});",
318323
newAssertion: "actual.Should().NotBeEmpty({0});")]
324+
[AssertionCodeFix(
325+
oldAssertion: "Assert.That(actual, Is.Not.Empty{0});",
326+
newAssertion: "actual.Should().NotBeEmpty({0});")]
319327
[Implemented]
320328
public void Nunit3_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newAssertion)
321329
{

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,38 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
240240
}
241241

242242
if (invocation.Arguments[1].Value.UnwrapConversion() is not IPropertyReferenceOperation constraint) return null;
243+
var subject = invocation.Arguments[0].Value;
243244

244-
switch (constraint.Property.Name)
245+
if (IsPropertyOfSymbol(constraint, "True", t.Is) // Assert.That(subject, Is.True)
246+
|| IsPropertyOfSymbol(constraint, "Not", "False", t.Is)) // Assert.That(subject, Is.False)
247+
return RenameAssertThatAssertionToSubjectShouldAssertion("BeTrue");
248+
else if (IsPropertyOfSymbol(constraint, "False", t.Is) // Assert.That(subject, Is.False)
249+
|| IsPropertyOfSymbol(constraint, "Not", "True", t.Is)) // Assert.That(subject, Is.Not.True)
250+
return RenameAssertThatAssertionToSubjectShouldAssertion("BeFalse");
251+
else if (IsPropertyOfSymbol(constraint, "Null", t.Is)) // Assert.That(subject, Is.Null)
252+
return RenameAssertThatAssertionToSubjectShouldAssertion("BeNull");
253+
else if (IsPropertyOfSymbol(constraint, "Not", "Null", t.Is)) // Assert.That(subject, Is.Not.Null)
254+
return RenameAssertThatAssertionToSubjectShouldAssertion("NotBeNull");
255+
else if (subject.Type.SpecialType is not SpecialType.System_Collections_IEnumerable)
245256
{
246-
case "True" when constraint.Property.ContainingType.EqualsSymbol(t.Is): // Assert.That(subject, Is.True)
247-
case "False" when constraint.Instance is IPropertyReferenceOperation { Property.Name: "Not" } chainedReference && PropertyReferencedFromType(chainedReference, t.Is): // Assert.That(subject, Is.Not.False)
248-
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeTrue", subjectIndex: 0, argumentsToRemove: [1]);
249-
case "True" when constraint.Instance is IPropertyReferenceOperation { Property.Name: "Not" } chainedReference && PropertyReferencedFromType(chainedReference, t.Is): // Assert.That(subject, Is.Not.True)
250-
case "False" when PropertyReferencedFromType(constraint, t.Is): // Assert.That(subject, Is.False)
251-
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeFalse", subjectIndex: 0, argumentsToRemove: [1]);
252-
case "Null" when PropertyReferencedFromType(constraint, t.Is): // Assert.That(subject, Is.Null)
253-
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeNull", subjectIndex: 0, argumentsToRemove: [1]);
254-
case "Null" when constraint.Instance is IPropertyReferenceOperation { Property.Name: "Not" } chainedReference && PropertyReferencedFromType(chainedReference, t.Is): // Assert.That(subject, Is.Not.Null)
255-
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeNull", subjectIndex: 0, argumentsToRemove: [1]);
256-
257-
default:
258-
return null;
257+
if (IsPropertyOfSymbol(constraint, "Empty", t.Is)) // Assert.That(subject, Is.Empty)
258+
return RenameAssertThatAssertionToSubjectShouldAssertion("BeEmpty");
259+
else if (IsPropertyOfSymbol(constraint, "Not", "Empty", t.Is)) // Assert.That(subject, Is.Not.Empty)
260+
return RenameAssertThatAssertionToSubjectShouldAssertion("NotBeEmpty");
259261
}
260262

263+
return null;
264+
265+
CreateChangedDocument RenameAssertThatAssertionToSubjectShouldAssertion(string assertionName)
266+
=> DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, assertionName, subjectIndex: 0, argumentsToRemove: [1]);
261267
}
262268

263-
private static bool PropertyReferencedFromType(IPropertyReferenceOperation propertyReference, INamedTypeSymbol type) => propertyReference.Property.ContainingType.EqualsSymbol(type);
269+
private static bool IsPropertyReferencedFromType(IPropertyReferenceOperation propertyReference, INamedTypeSymbol type)
270+
=> propertyReference.Property.ContainingType.EqualsSymbol(type);
271+
private static bool IsPropertyOfSymbol(IPropertyReferenceOperation propertyReference, string firstProperty, string secondProperty, INamedTypeSymbol type)
272+
=> propertyReference.Property.Name == secondProperty && IsPropertyOfSymbol(propertyReference.Instance, firstProperty, type);
273+
private static bool IsPropertyOfSymbol(IOperation operation, string property, INamedTypeSymbol type)
274+
=> operation is IPropertyReferenceOperation propertyReference && propertyReference.Property.Name == property && IsPropertyReferencedFromType(propertyReference, type);
264275

265276
public class NunitCodeFixContext(Compilation compilation) : TestingFrameworkCodeFixProvider.TestingFrameworkCodeFixContext(compilation)
266277
{

0 commit comments

Comments
 (0)