Skip to content

Commit 9433848

Browse files
authored
feat: add nunit assert.that zero assertions (#340)
* feat: add nunit assert.that zero assertions
1 parent 9a1cda4 commit 9433848

File tree

6 files changed

+244
-2
lines changed

6 files changed

+244
-2
lines changed

docs/NunitAnalyzer.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1010
- [AssertNotNull](#scenario-assertnotnull) - `obj.Should().NotBeNull();`
1111
- [AssertIsEmpty](#scenario-assertisempty) - `collection.Should().BeEmpty();`
1212
- [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();`
13+
- [AssertZero](#scenario-assertzero) - `number.Should().Be(0);`
14+
- [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);`
1315

1416

1517
## Scenarios
@@ -228,4 +230,66 @@ Assert.That(collection, Is.Not.Empty); /* fail message: Expected: not <empty>
228230
collection.Should().NotBeEmpty(); /* fail message: Expected collection not to be empty. */
229231
```
230232

233+
### scenario: AssertZero
234+
235+
```cs
236+
// arrange
237+
var number = 0;
238+
239+
// old assertion:
240+
Assert.Zero(number);
241+
Assert.That(number, Is.Zero);
242+
243+
// new assertion:
244+
number.Should().Be(0);
245+
```
246+
247+
#### Failure messages
248+
249+
```cs
250+
var number = 1;
251+
252+
// old assertion:
253+
Assert.Zero(number); /* fail message: Expected: 0
254+
But was: 1
255+
*/
256+
Assert.That(number, Is.Zero); /* fail message: Expected: 0
257+
But was: 1
258+
*/
259+
260+
// new assertion:
261+
number.Should().Be(0); /* fail message: Expected number to be 0, but found 1 (difference of 1). */
262+
```
263+
264+
### scenario: AssertNotZero
265+
266+
```cs
267+
// arrange
268+
var number = 1;
269+
270+
// old assertion:
271+
Assert.NotZero(number);
272+
Assert.That(number, Is.Not.Zero);
273+
274+
// new assertion:
275+
number.Should().NotBe(0);
276+
```
277+
278+
#### Failure messages
279+
280+
```cs
281+
var number = 0;
282+
283+
// old assertion:
284+
Assert.NotZero(number); /* fail message: Expected: not equal to 0
285+
But was: 0
286+
*/
287+
Assert.That(number, Is.Not.Zero); /* fail message: Expected: not equal to 0
288+
But was: 0
289+
*/
290+
291+
// new assertion:
292+
number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */
293+
```
294+
231295

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,92 @@ public void AssertIsNotEmpty_Failure_NewAssertion()
349349
// new assertion:
350350
collection.Should().NotBeEmpty();
351351
}
352+
353+
[TestMethod]
354+
public void AssertZero()
355+
{
356+
// arrange
357+
var number = 0;
358+
359+
// old assertion:
360+
Assert.Zero(number);
361+
Assert.That(number, Is.Zero);
362+
363+
// new assertion:
364+
number.Should().Be(0);
365+
}
366+
367+
[TestMethod, ExpectedTestFrameworkException]
368+
public void AssertZero_Failure_OldAssertion_0()
369+
{
370+
// arrange
371+
var number = 1;
372+
373+
// old assertion:
374+
Assert.Zero(number);
375+
}
376+
377+
[TestMethod, ExpectedTestFrameworkException]
378+
public void AssertZero_Failure_OldAssertion_1()
379+
{
380+
// arrange
381+
var number = 1;
382+
383+
// old assertion:
384+
Assert.That(number, Is.Zero);
385+
}
386+
387+
[TestMethod, ExpectedTestFrameworkException]
388+
public void AssertZero_Failure_NewAssertion()
389+
{
390+
// arrange
391+
var number = 1;
392+
393+
// new assertion:
394+
number.Should().Be(0);
395+
}
396+
397+
[TestMethod]
398+
public void AssertNotZero()
399+
{
400+
// arrange
401+
var number = 1;
402+
403+
// old assertion:
404+
Assert.NotZero(number);
405+
Assert.That(number, Is.Not.Zero);
406+
407+
// new assertion:
408+
number.Should().NotBe(0);
409+
}
410+
411+
[TestMethod, ExpectedTestFrameworkException]
412+
public void AssertNotZero_Failure_OldAssertion_0()
413+
{
414+
// arrange
415+
var number = 0;
416+
417+
// old assertion:
418+
Assert.NotZero(number);
419+
}
420+
421+
[TestMethod, ExpectedTestFrameworkException]
422+
public void AssertNotZero_Failure_OldAssertion_1()
423+
{
424+
// arrange
425+
var number = 0;
426+
427+
// old assertion:
428+
Assert.That(number, Is.Not.Zero);
429+
}
430+
431+
[TestMethod, ExpectedTestFrameworkException]
432+
public void AssertNotZero_Failure_NewAssertion()
433+
{
434+
// arrange
435+
var number = 0;
436+
437+
// new assertion:
438+
number.Should().NotBe(0);
439+
}
352440
}

src/FluentAssertions.Analyzers.Tests/TestAttributes.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,24 @@ public static class TestCasesInputUtils
7373
private static readonly string FormattedBecause = "\"because message with {0} placeholders {1} at {2}\", 3, \"is awesome\", DateTime.Now.Add(2.Seconds())";
7474
public static IEnumerable<string> GetTestCases(string assertion)
7575
{
76+
if (!assertion.Contains("{0}"))
77+
{
78+
yield return assertion;
79+
yield break;
80+
}
81+
7682
yield return SafeFormat(assertion, Empty);
7783
yield return SafeFormat(assertion, Because);
7884
yield return SafeFormat(assertion, FormattedBecause);
7985
}
8086
public static IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases(string oldAssertion, string newAssertion)
8187
{
88+
if (!oldAssertion.Contains("{0}") && !newAssertion.Contains("{0}"))
89+
{
90+
yield return (oldAssertion, newAssertion);
91+
yield break;
92+
}
93+
8294
yield return (SafeFormat(oldAssertion, Empty), SafeFormat(newAssertion, Empty));
8395
yield return (SafeFormat(oldAssertion, Because), SafeFormat(newAssertion, Because));
8496
yield return (SafeFormat(oldAssertion, FormattedBecause), SafeFormat(newAssertion, FormattedBecause));

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class NunitTests
2626
[AssertionDiagnostic("ClassicAssert.True(bool.Parse(\"true\"){0});")]
2727
[AssertionDiagnostic("ClassicAssert.IsTrue(actual{0});")]
2828
[AssertionDiagnostic("ClassicAssert.IsTrue(bool.Parse(\"true\"){0});")]
29+
[AssertionDiagnostic("Assert.That(actual);")]
30+
[AssertionDiagnostic("Assert.That(actual, Is.True);")]
31+
[AssertionDiagnostic("Assert.That(actual, Is.Not.False);")]
2932
[Implemented]
3033
public void Nunit4_AssertTrue_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("bool actual", assertion);
3134

@@ -91,6 +94,15 @@ public class NunitTests
9194
[AssertionCodeFix(
9295
oldAssertion: "ClassicAssert.IsTrue(actual == false{0});",
9396
newAssertion: "(actual == false).Should().BeTrue({0});")]
97+
[AssertionCodeFix(
98+
oldAssertion: "Assert.That(actual);",
99+
newAssertion: "actual.Should().BeTrue();")]
100+
[AssertionCodeFix(
101+
oldAssertion: "Assert.That(actual, Is.True);",
102+
newAssertion: "actual.Should().BeTrue();")]
103+
[AssertionCodeFix(
104+
oldAssertion: "Assert.That(actual, Is.Not.False);",
105+
newAssertion: "actual.Should().BeTrue();")]
94106
[Implemented]
95107
public void Nunit4_AssertTrue_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);
96108

@@ -109,6 +121,8 @@ public class NunitTests
109121
[AssertionDiagnostic("ClassicAssert.False(bool.Parse(\"false\"){0});")]
110122
[AssertionDiagnostic("ClassicAssert.IsFalse(actual{0});")]
111123
[AssertionDiagnostic("ClassicAssert.IsFalse(bool.Parse(\"false\"){0});")]
124+
[AssertionDiagnostic("Assert.That(actual, Is.False);")]
125+
[AssertionDiagnostic("Assert.That(actual, Is.Not.True);")]
112126
[Implemented]
113127
public void Nunit4_AssertFalse_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("bool actual", assertion);
114128

@@ -147,6 +161,12 @@ public class NunitTests
147161
[AssertionCodeFix(
148162
oldAssertion: "ClassicAssert.IsFalse(bool.Parse(\"false\"){0});",
149163
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")]
164+
[AssertionCodeFix(
165+
oldAssertion: "Assert.That(actual, Is.False);",
166+
newAssertion: "actual.Should().BeFalse();")]
167+
[AssertionCodeFix(
168+
oldAssertion: "Assert.That(actual, Is.Not.True);",
169+
newAssertion: "actual.Should().BeFalse();")]
150170
[Implemented]
151171
public void Nunit4_AssertFalse_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);
152172

@@ -160,6 +180,7 @@ public class NunitTests
160180
[DataTestMethod]
161181
[AssertionDiagnostic("ClassicAssert.Null(actual{0});")]
162182
[AssertionDiagnostic("ClassicAssert.IsNull(actual{0});")]
183+
[AssertionDiagnostic("Assert.That(actual, Is.Null);")]
163184
[Implemented]
164185
public void Nunit4_AssertNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object actual", assertion);
165186

@@ -183,6 +204,9 @@ public class NunitTests
183204
[AssertionCodeFix(
184205
oldAssertion: "ClassicAssert.IsNull(actual{0});",
185206
newAssertion: "actual.Should().BeNull({0});")]
207+
[AssertionCodeFix(
208+
oldAssertion: "Assert.That(actual, Is.Null);",
209+
newAssertion: "actual.Should().BeNull();")]
186210
[Implemented]
187211
public void Nunit4_AssertNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);
188212

@@ -196,6 +220,7 @@ public class NunitTests
196220
[DataTestMethod]
197221
[AssertionDiagnostic("ClassicAssert.NotNull(actual{0});")]
198222
[AssertionDiagnostic("ClassicAssert.IsNotNull(actual{0});")]
223+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Null);")]
199224
[Implemented]
200225
public void Nunit4_AssertNotNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object actual", assertion);
201226

@@ -219,6 +244,9 @@ public class NunitTests
219244
[AssertionCodeFix(
220245
oldAssertion: "ClassicAssert.IsNotNull(actual{0});",
221246
newAssertion: "actual.Should().NotBeNull({0});")]
247+
[AssertionCodeFix(
248+
oldAssertion: "Assert.That(actual, Is.Not.Null);",
249+
newAssertion: "actual.Should().NotBeNull();")]
222250
[Implemented]
223251
public void Nunit4_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);
224252

@@ -255,6 +283,7 @@ public void Nunit3_AssertIsEmpty_TestAnalyzer(string assertion)
255283

256284
[DataTestMethod]
257285
[AssertionDiagnostic("ClassicAssert.IsEmpty(actual{0});")]
286+
[AssertionDiagnostic("Assert.That(actual, Is.Empty);")]
258287
[Implemented]
259288
public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
260289
{
@@ -284,6 +313,9 @@ public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAsse
284313
[AssertionCodeFix(
285314
oldAssertion: "ClassicAssert.IsEmpty(actual{0});",
286315
newAssertion: "actual.Should().BeEmpty({0});")]
316+
[AssertionCodeFix(
317+
oldAssertion: "Assert.That(actual, Is.Empty);",
318+
newAssertion: "actual.Should().BeEmpty();")]
287319
[Implemented]
288320
public void Nunit4_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAssertion)
289321
{
@@ -308,6 +340,7 @@ public void Nunit3_AssertIsNotEmpty_TestAnalyzer(string assertion)
308340

309341
[DataTestMethod]
310342
[AssertionDiagnostic("ClassicAssert.IsNotEmpty(actual{0});")]
343+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Empty);")]
311344
[Implemented]
312345
public void Nunit4_AssertIsNotEmpty_TestAnalyzer(string assertion)
313346
{
@@ -337,6 +370,9 @@ public void Nunit3_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newA
337370
[AssertionCodeFix(
338371
oldAssertion: "ClassicAssert.IsNotEmpty(actual{0});",
339372
newAssertion: "actual.Should().NotBeEmpty({0});")]
373+
[AssertionCodeFix(
374+
oldAssertion: "Assert.That(actual, Is.Not.Empty);",
375+
newAssertion: "actual.Should().NotBeEmpty();")]
340376
[Implemented]
341377
public void Nunit4_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newAssertion)
342378
{
@@ -348,6 +384,7 @@ public void Nunit4_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newA
348384

349385
[DataTestMethod]
350386
[AssertionDiagnostic("Assert.Zero(actual{0});")]
387+
[AssertionDiagnostic("Assert.That(actual, Is.Zero{0});")]
351388
[Implemented]
352389
public void Nunit3_AssertZero_TestAnalyzer(string assertion)
353390
{
@@ -362,6 +399,7 @@ public void Nunit3_AssertZero_TestAnalyzer(string assertion)
362399

363400
[DataTestMethod]
364401
[AssertionDiagnostic("ClassicAssert.Zero(actual{0});")]
402+
[AssertionDiagnostic("Assert.That(actual, Is.Zero);")]
365403
[Implemented]
366404
public void Nunit4_AssertZero_TestAnalyzer(string assertion)
367405
{
@@ -378,6 +416,9 @@ public void Nunit4_AssertZero_TestAnalyzer(string assertion)
378416
[AssertionCodeFix(
379417
oldAssertion: "Assert.Zero(actual{0});",
380418
newAssertion: "actual.Should().Be(0{0});")]
419+
[AssertionCodeFix(
420+
oldAssertion: "Assert.That(actual, Is.Zero{0});",
421+
newAssertion: "actual.Should().Be(0{0});")]
381422
[Implemented]
382423
public void Nunit3_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
383424
{
@@ -394,6 +435,9 @@ public void Nunit3_AssertZero_TestCodeFix(string oldAssertion, string newAsserti
394435
[AssertionCodeFix(
395436
oldAssertion: "ClassicAssert.Zero(actual{0});",
396437
newAssertion: "actual.Should().Be(0{0});")]
438+
[AssertionCodeFix(
439+
oldAssertion: "Assert.That(actual, Is.Zero);",
440+
newAssertion: "actual.Should().Be(0);")]
397441
[Implemented]
398442
public void Nunit4_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
399443
{
@@ -408,6 +452,7 @@ public void Nunit4_AssertZero_TestCodeFix(string oldAssertion, string newAsserti
408452

409453
[DataTestMethod]
410454
[AssertionDiagnostic("Assert.NotZero(actual{0});")]
455+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Zero{0});")]
411456
[Implemented]
412457
public void Nunit3_AssertNotZero_TestAnalyzer(string assertion)
413458
{
@@ -422,6 +467,7 @@ public void Nunit3_AssertNotZero_TestAnalyzer(string assertion)
422467

423468
[DataTestMethod]
424469
[AssertionDiagnostic("ClassicAssert.NotZero(actual{0});")]
470+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Zero);")]
425471
[Implemented]
426472
public void Nunit4_AssertNotZero_TestAnalyzer(string assertion)
427473
{
@@ -438,6 +484,9 @@ public void Nunit4_AssertNotZero_TestAnalyzer(string assertion)
438484
[AssertionCodeFix(
439485
oldAssertion: "Assert.NotZero(actual{0});",
440486
newAssertion: "actual.Should().NotBe(0{0});")]
487+
[AssertionCodeFix(
488+
oldAssertion: "Assert.That(actual, Is.Not.Zero{0});",
489+
newAssertion: "actual.Should().NotBe(0{0});")]
441490
[Implemented]
442491
public void Nunit3_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
443492
{
@@ -454,6 +503,9 @@ public void Nunit3_AssertNotZero_TestCodeFix(string oldAssertion, string newAsse
454503
[AssertionCodeFix(
455504
oldAssertion: "ClassicAssert.NotZero(actual{0});",
456505
newAssertion: "actual.Should().NotBe(0{0});")]
506+
[AssertionCodeFix(
507+
oldAssertion: "Assert.That(actual, Is.Not.Zero);",
508+
newAssertion: "actual.Should().NotBe(0);")]
457509
[Implemented]
458510
public void Nunit4_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
459511
{

0 commit comments

Comments
 (0)