Skip to content

Commit 69cd0c0

Browse files
authored
feat: add assert that IsEmpty scenarios (#356)
* feat: add assert that IsEmpty scenarios
1 parent 0904d4f commit 69cd0c0

File tree

6 files changed

+238
-60
lines changed

6 files changed

+238
-60
lines changed

docs/Nunit3Analyzer.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ var collection = new List<int>();
189189
// old assertion:
190190
Assert.IsEmpty(collection);
191191
Assert.That(collection, Is.Empty);
192+
Assert.That(collection, Has.Count.EqualTo(0));
193+
Assert.That(collection, Has.Count.Zero);
192194
CollectionAssert.IsEmpty(collection);
193195

194196
// new assertion:
@@ -207,6 +209,12 @@ Assert.IsEmpty(collection); /* fail message: Expected: <empty>
207209
Assert.That(collection, Is.Empty); /* fail message: Expected: <empty>
208210
But was: < 1, 2, 3 >
209211
*/
212+
Assert.That(collection, Has.Count.EqualTo(0)); /* fail message: Expected: property Count equal to 0
213+
But was: 3
214+
*/
215+
Assert.That(collection, Has.Count.Zero); /* fail message: Expected: property Count equal to 0
216+
But was: 3
217+
*/
210218
CollectionAssert.IsEmpty(collection); /* fail message: Expected: <empty>
211219
But was: < 1, 2, 3 >
212220
*/
@@ -224,6 +232,8 @@ var collection = new List<int> { 1, 2, 3 };
224232
// old assertion:
225233
Assert.IsNotEmpty(collection);
226234
Assert.That(collection, Is.Not.Empty);
235+
Assert.That(collection, Has.Count.GreaterThan(0));
236+
Assert.That(collection, Has.Count.Not.Zero);
227237
CollectionAssert.IsNotEmpty(collection);
228238

229239
// new assertion:
@@ -242,6 +252,12 @@ Assert.IsNotEmpty(collection); /* fail message: Expected: not <empty>
242252
Assert.That(collection, Is.Not.Empty); /* fail message: Expected: not <empty>
243253
But was: <empty>
244254
*/
255+
Assert.That(collection, Has.Count.GreaterThan(0)); /* fail message: Expected: property Count greater than 0
256+
But was: 0
257+
*/
258+
Assert.That(collection, Has.Count.Not.Zero); /* fail message: Expected: property Count not equal to 0
259+
But was: 0
260+
*/
245261
CollectionAssert.IsNotEmpty(collection); /* fail message: Expected: not <empty>
246262
But was: <empty>
247263
*/

docs/Nunit4Analyzer.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ var collection = new List<int>();
204204
// old assertion:
205205
ClassicAssert.IsEmpty(collection);
206206
Assert.That(collection, Is.Empty);
207+
Assert.That(collection, Has.Count.EqualTo(0));
208+
Assert.That(collection, Has.Count.Zero);
207209
CollectionAssert.IsEmpty(collection);
208210

209211
// new assertion:
@@ -224,6 +226,14 @@ Assert.That(collection, Is.Empty); /* fail message: Assert.That(collection, Is
224226
Expected: <empty>
225227
But was: < 1, 2, 3 >
226228
*/
229+
Assert.That(collection, Has.Count.EqualTo(0)); /* fail message: Assert.That(collection, Has.Count.EqualTo(0))
230+
Expected: property Count equal to 0
231+
But was: 3
232+
*/
233+
Assert.That(collection, Has.Count.Zero); /* fail message: Assert.That(collection, Has.Count.Zero)
234+
Expected: property Count equal to 0
235+
But was: 3
236+
*/
227237
CollectionAssert.IsEmpty(collection); /* fail message: Assert.That(collection, new EmptyCollectionConstraint())
228238
Expected: <empty>
229239
But was: < 1, 2, 3 >
@@ -242,6 +252,8 @@ var collection = new List<int> { 1, 2, 3 };
242252
// old assertion:
243253
ClassicAssert.IsNotEmpty(collection);
244254
Assert.That(collection, Is.Not.Empty);
255+
Assert.That(collection, Has.Count.GreaterThan(0));
256+
Assert.That(collection, Has.Count.Not.Zero);
245257
CollectionAssert.IsNotEmpty(collection);
246258

247259
// new assertion:
@@ -262,6 +274,14 @@ Assert.That(collection, Is.Not.Empty); /* fail message: Assert.That(collection
262274
Expected: not <empty>
263275
But was: <empty>
264276
*/
277+
Assert.That(collection, Has.Count.GreaterThan(0)); /* fail message: Assert.That(collection, Has.Count.GreaterThan(0))
278+
Expected: property Count greater than 0
279+
But was: 0
280+
*/
281+
Assert.That(collection, Has.Count.Not.Zero); /* fail message: Assert.That(collection, Has.Count.Not.Zero)
282+
Expected: property Count not equal to 0
283+
But was: 0
284+
*/
265285
CollectionAssert.IsNotEmpty(collection); /* fail message: Assert.That(collection, new NotConstraint(new EmptyCollectionConstraint()))
266286
Expected: not <empty>
267287
But was: <empty>
@@ -384,17 +404,17 @@ obj1.Should().NotBeSameAs(obj2);
384404
#### Failure messages
385405

386406
```cs
387-
var obj1 = new object();
388-
var obj2 = obj1;
407+
object obj1 = "foo";
408+
object obj2 = "foo";
389409

390410
// old assertion:
391411
ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is.Not.SameAs(expected))
392-
Expected: not same as <System.Object>
393-
But was: <System.Object>
412+
Expected: not same as "foo"
413+
But was: "foo"
394414
*/
395415

396416
// new assertion:
397-
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=19989589). */
417+
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to "foo". */
398418
```
399419

400420
### scenario: CollectionAssertAreEqual

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,16 @@ public void AssertIsEmpty()
269269
// old assertion:
270270
ClassicAssert.IsEmpty(collection);
271271
Assert.That(collection, Is.Empty);
272+
Assert.That(collection, Has.Count.EqualTo(0));
273+
Assert.That(collection, Has.Count.Zero);
272274
CollectionAssert.IsEmpty(collection);
273275

274276
// new assertion:
275277
collection.Should().BeEmpty();
276278
}
277279

278280
[Test, ExpectedAssertionException]
281+
279282
public void AssertIsEmpty_Failure_OldAssertion_0()
280283
{
281284
// arrange
@@ -286,6 +289,7 @@ public void AssertIsEmpty_Failure_OldAssertion_0()
286289
}
287290

288291
[Test, ExpectedAssertionException]
292+
289293
public void AssertIsEmpty_Failure_OldAssertion_1()
290294
{
291295
// arrange
@@ -296,16 +300,40 @@ public void AssertIsEmpty_Failure_OldAssertion_1()
296300
}
297301

298302
[Test, ExpectedAssertionException]
303+
299304
public void AssertIsEmpty_Failure_OldAssertion_2()
300305
{
301306
// arrange
302307
var collection = new List<int> { 1, 2, 3 };
303308

309+
// old assertion:
310+
Assert.That(collection, Has.Count.EqualTo(0));
311+
}
312+
313+
[Test, ExpectedAssertionException]
314+
315+
public void AssertIsEmpty_Failure_OldAssertion_3()
316+
{
317+
// arrange
318+
var collection = new List<int> { 1, 2, 3 };
319+
320+
// old assertion:
321+
Assert.That(collection, Has.Count.Zero);
322+
}
323+
324+
[Test, ExpectedAssertionException]
325+
326+
public void AssertIsEmpty_Failure_OldAssertion_4()
327+
{
328+
// arrange
329+
var collection = new List<int> { 1, 2, 3 };
330+
304331
// old assertion:
305332
CollectionAssert.IsEmpty(collection);
306333
}
307334

308335
[Test, ExpectedAssertionException]
336+
309337
public void AssertIsEmpty_Failure_NewAssertion()
310338
{
311339
// arrange
@@ -324,13 +352,16 @@ public void AssertIsNotEmpty()
324352
// old assertion:
325353
ClassicAssert.IsNotEmpty(collection);
326354
Assert.That(collection, Is.Not.Empty);
355+
Assert.That(collection, Has.Count.GreaterThan(0));
356+
Assert.That(collection, Has.Count.Not.Zero);
327357
CollectionAssert.IsNotEmpty(collection);
328358

329359
// new assertion:
330360
collection.Should().NotBeEmpty();
331361
}
332362

333363
[Test, ExpectedAssertionException]
364+
334365
public void AssertIsNotEmpty_Failure_OldAssertion_0()
335366
{
336367
// arrange
@@ -341,6 +372,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_0()
341372
}
342373

343374
[Test, ExpectedAssertionException]
375+
344376
public void AssertIsNotEmpty_Failure_OldAssertion_1()
345377
{
346378
// arrange
@@ -351,11 +383,34 @@ public void AssertIsNotEmpty_Failure_OldAssertion_1()
351383
}
352384

353385
[Test, ExpectedAssertionException]
386+
354387
public void AssertIsNotEmpty_Failure_OldAssertion_2()
355388
{
356389
// arrange
357390
var collection = new List<int>();
358391

392+
// old assertion:
393+
Assert.That(collection, Has.Count.GreaterThan(0));
394+
}
395+
396+
[Test, ExpectedAssertionException]
397+
398+
public void AssertIsNotEmpty_Failure_OldAssertion_3()
399+
{
400+
// arrange
401+
var collection = new List<int>();
402+
403+
// old assertion:
404+
Assert.That(collection, Has.Count.Not.Zero);
405+
}
406+
407+
[Test, ExpectedAssertionException]
408+
409+
public void AssertIsNotEmpty_Failure_OldAssertion_4()
410+
{
411+
// arrange
412+
var collection = new List<int>();
413+
359414
// old assertion:
360415
CollectionAssert.IsNotEmpty(collection);
361416
}
@@ -512,8 +567,8 @@ public void AssertAreNotSame()
512567
public void AssertAreNotSame_Failure_OldAssertion()
513568
{
514569
// arrange
515-
var obj1 = new object();
516-
var obj2 = obj1;
570+
object obj1 = "foo";
571+
object obj2 = "foo";
517572

518573
// old assertion:
519574
ClassicAssert.AreNotSame(obj1, obj2);
@@ -523,8 +578,8 @@ public void AssertAreNotSame_Failure_OldAssertion()
523578
public void AssertAreNotSame_Failure_NewAssertion()
524579
{
525580
// arrange
526-
var obj1 = new object();
527-
var obj2 = obj1;
581+
object obj1 = "foo";
582+
object obj2 = "foo";
528583

529584
// new assertion:
530585
obj1.Should().NotBeSameAs(obj2);

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ public void AssertIsEmpty()
272272
// old assertion:
273273
Assert.IsEmpty(collection);
274274
Assert.That(collection, Is.Empty);
275+
Assert.That(collection, Has.Count.EqualTo(0));
276+
Assert.That(collection, Has.Count.Zero);
275277
CollectionAssert.IsEmpty(collection);
276278

277279
// new assertion:
@@ -304,6 +306,26 @@ public void AssertIsEmpty_Failure_OldAssertion_2()
304306
// arrange
305307
var collection = new List<int> { 1, 2, 3 };
306308

309+
// old assertion:
310+
Assert.That(collection, Has.Count.EqualTo(0));
311+
}
312+
313+
[TestMethod, ExpectedTestFrameworkException]
314+
public void AssertIsEmpty_Failure_OldAssertion_3()
315+
{
316+
// arrange
317+
var collection = new List<int> { 1, 2, 3 };
318+
319+
// old assertion:
320+
Assert.That(collection, Has.Count.Zero);
321+
}
322+
323+
[TestMethod, ExpectedTestFrameworkException]
324+
public void AssertIsEmpty_Failure_OldAssertion_4()
325+
{
326+
// arrange
327+
var collection = new List<int> { 1, 2, 3 };
328+
307329
// old assertion:
308330
CollectionAssert.IsEmpty(collection);
309331
}
@@ -327,6 +349,8 @@ public void AssertIsNotEmpty()
327349
// old assertion:
328350
Assert.IsNotEmpty(collection);
329351
Assert.That(collection, Is.Not.Empty);
352+
Assert.That(collection, Has.Count.GreaterThan(0));
353+
Assert.That(collection, Has.Count.Not.Zero);
330354
CollectionAssert.IsNotEmpty(collection);
331355

332356
// new assertion:
@@ -359,6 +383,26 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2()
359383
// arrange
360384
var collection = new List<int>();
361385

386+
// old assertion:
387+
Assert.That(collection, Has.Count.GreaterThan(0));
388+
}
389+
390+
[TestMethod, ExpectedTestFrameworkException]
391+
public void AssertIsNotEmpty_Failure_OldAssertion_3()
392+
{
393+
// arrange
394+
var collection = new List<int>();
395+
396+
// old assertion:
397+
Assert.That(collection, Has.Count.Not.Zero);
398+
}
399+
400+
[TestMethod, ExpectedTestFrameworkException]
401+
public void AssertIsNotEmpty_Failure_OldAssertion_4()
402+
{
403+
// arrange
404+
var collection = new List<int>();
405+
362406
// old assertion:
363407
CollectionAssert.IsNotEmpty(collection);
364408
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
333333
[AssertionCodeFix(
334334
oldAssertion: "Assert.That(actual, Is.Empty{0});",
335335
newAssertion: "actual.Should().BeEmpty({0});")]
336+
[AssertionCodeFix(
337+
oldAssertion: "Assert.That(actual, Has.Count.EqualTo(0){0});",
338+
newAssertion: "actual.Should().BeEmpty({0});")]
339+
[AssertionCodeFix(
340+
oldAssertion: "Assert.That(actual, Has.Count.Zero{0});",
341+
newAssertion: "actual.Should().BeEmpty({0});")]
336342
[AssertionCodeFix(
337343
oldAssertion: "CollectionAssert.IsEmpty(actual{0});",
338344
newAssertion: "actual.Should().BeEmpty({0});")]
@@ -352,6 +358,12 @@ public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAsse
352358
[AssertionCodeFix(
353359
oldAssertion: "Assert.That(actual, Is.Empty);",
354360
newAssertion: "actual.Should().BeEmpty();")]
361+
[AssertionCodeFix(
362+
oldAssertion: "Assert.That(actual, Has.Count.EqualTo(0));",
363+
newAssertion: "actual.Should().BeEmpty();")]
364+
[AssertionCodeFix(
365+
oldAssertion: "Assert.That(actual, Has.Count.Zero);",
366+
newAssertion: "actual.Should().BeEmpty();")]
355367
[AssertionCodeFix(
356368
oldAssertion: "CollectionAssert.IsEmpty(actual{0});",
357369
newAssertion: "actual.Should().BeEmpty({0});")]

0 commit comments

Comments
 (0)