Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/NunitAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
- [AssertNotNull](#scenario-assertnotnull) - `obj.Should().NotBeNull();`
- [AssertIsEmpty](#scenario-assertisempty) - `collection.Should().BeEmpty();`
- [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();`
- [AssertZero](#scenario-assertzero) - `number.Should().Be(0);`
- [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);`


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

### scenario: AssertZero

```cs
// arrange
var number = 0;

// old assertion:
Assert.Zero(number);
Assert.That(number, Is.Zero);

// new assertion:
number.Should().Be(0);
```

#### Failure messages

```cs
var number = 1;

// old assertion:
Assert.Zero(number); /* fail message: Expected: 0
But was: 1
*/
Assert.That(number, Is.Zero); /* fail message: Expected: 0
But was: 1
*/

// new assertion:
number.Should().Be(0); /* fail message: Expected number to be 0, but found 1 (difference of 1). */
```

### scenario: AssertNotZero

```cs
// arrange
var number = 1;

// old assertion:
Assert.NotZero(number);
Assert.That(number, Is.Not.Zero);

// new assertion:
number.Should().NotBe(0);
```

#### Failure messages

```cs
var number = 0;

// old assertion:
Assert.NotZero(number); /* fail message: Expected: not equal to 0
But was: 0
*/
Assert.That(number, Is.Not.Zero); /* fail message: Expected: not equal to 0
But was: 0
*/

// new assertion:
number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */
```


Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,92 @@ public void AssertIsNotEmpty_Failure_NewAssertion()
// new assertion:
collection.Should().NotBeEmpty();
}

[TestMethod]
public void AssertZero()
{
// arrange
var number = 0;

// old assertion:
Assert.Zero(number);
Assert.That(number, Is.Zero);

// new assertion:
number.Should().Be(0);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertZero_Failure_OldAssertion_0()
{
// arrange
var number = 1;

// old assertion:
Assert.Zero(number);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertZero_Failure_OldAssertion_1()
{
// arrange
var number = 1;

// old assertion:
Assert.That(number, Is.Zero);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertZero_Failure_NewAssertion()
{
// arrange
var number = 1;

// new assertion:
number.Should().Be(0);
}

[TestMethod]
public void AssertNotZero()
{
// arrange
var number = 1;

// old assertion:
Assert.NotZero(number);
Assert.That(number, Is.Not.Zero);

// new assertion:
number.Should().NotBe(0);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertNotZero_Failure_OldAssertion_0()
{
// arrange
var number = 0;

// old assertion:
Assert.NotZero(number);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertNotZero_Failure_OldAssertion_1()
{
// arrange
var number = 0;

// old assertion:
Assert.That(number, Is.Not.Zero);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertNotZero_Failure_NewAssertion()
{
// arrange
var number = 0;

// new assertion:
number.Should().NotBe(0);
}
}
12 changes: 12 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/TestAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ public static class TestCasesInputUtils
private static readonly string FormattedBecause = "\"because message with {0} placeholders {1} at {2}\", 3, \"is awesome\", DateTime.Now.Add(2.Seconds())";
public static IEnumerable<string> GetTestCases(string assertion)
{
if (!assertion.Contains("{0}"))
{
yield return assertion;
yield break;
}

yield return SafeFormat(assertion, Empty);
yield return SafeFormat(assertion, Because);
yield return SafeFormat(assertion, FormattedBecause);
}
public static IEnumerable<(string oldAssertion, string newAssertion)> GetTestCases(string oldAssertion, string newAssertion)
{
if (!oldAssertion.Contains("{0}") && !newAssertion.Contains("{0}"))
{
yield return (oldAssertion, newAssertion);
yield break;
}

yield return (SafeFormat(oldAssertion, Empty), SafeFormat(newAssertion, Empty));
yield return (SafeFormat(oldAssertion, Because), SafeFormat(newAssertion, Because));
yield return (SafeFormat(oldAssertion, FormattedBecause), SafeFormat(newAssertion, FormattedBecause));
Expand Down
52 changes: 52 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class NunitTests
[AssertionDiagnostic("ClassicAssert.True(bool.Parse(\"true\"){0});")]
[AssertionDiagnostic("ClassicAssert.IsTrue(actual{0});")]
[AssertionDiagnostic("ClassicAssert.IsTrue(bool.Parse(\"true\"){0});")]
[AssertionDiagnostic("Assert.That(actual);")]
[AssertionDiagnostic("Assert.That(actual, Is.True);")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.False);")]
[Implemented]
public void Nunit4_AssertTrue_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("bool actual", assertion);

Expand Down Expand Up @@ -91,6 +94,15 @@ public class NunitTests
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsTrue(actual == false{0});",
newAssertion: "(actual == false).Should().BeTrue({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual);",
newAssertion: "actual.Should().BeTrue();")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.True);",
newAssertion: "actual.Should().BeTrue();")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.False);",
newAssertion: "actual.Should().BeTrue();")]
[Implemented]
public void Nunit4_AssertTrue_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);

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

Expand Down Expand Up @@ -147,6 +161,12 @@ public class NunitTests
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsFalse(bool.Parse(\"false\"){0});",
newAssertion: "bool.Parse(\"false\").Should().BeFalse({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.False);",
newAssertion: "actual.Should().BeFalse();")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.True);",
newAssertion: "actual.Should().BeFalse();")]
[Implemented]
public void Nunit4_AssertFalse_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("bool actual", oldAssertion, newAssertion);

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

Expand All @@ -183,6 +204,9 @@ public class NunitTests
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNull(actual{0});",
newAssertion: "actual.Should().BeNull({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Null);",
newAssertion: "actual.Should().BeNull();")]
[Implemented]
public void Nunit4_AssertNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

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

Expand All @@ -219,6 +244,9 @@ public class NunitTests
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNotNull(actual{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.Null);",
newAssertion: "actual.Should().NotBeNull();")]
[Implemented]
public void Nunit4_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

Expand Down Expand Up @@ -255,6 +283,7 @@ public void Nunit3_AssertIsEmpty_TestAnalyzer(string assertion)

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.IsEmpty(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Empty);")]
[Implemented]
public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
{
Expand Down Expand Up @@ -284,6 +313,9 @@ public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAsse
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsEmpty(actual{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Empty);",
newAssertion: "actual.Should().BeEmpty();")]
[Implemented]
public void Nunit4_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -308,6 +340,7 @@ public void Nunit3_AssertIsNotEmpty_TestAnalyzer(string assertion)

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.IsNotEmpty(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.Empty);")]
[Implemented]
public void Nunit4_AssertIsNotEmpty_TestAnalyzer(string assertion)
{
Expand Down Expand Up @@ -337,6 +370,9 @@ public void Nunit3_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newA
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNotEmpty(actual{0});",
newAssertion: "actual.Should().NotBeEmpty({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.Empty);",
newAssertion: "actual.Should().NotBeEmpty();")]
[Implemented]
public void Nunit4_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -348,6 +384,7 @@ public void Nunit4_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newA

[DataTestMethod]
[AssertionDiagnostic("Assert.Zero(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Zero{0});")]
[Implemented]
public void Nunit3_AssertZero_TestAnalyzer(string assertion)
{
Expand All @@ -362,6 +399,7 @@ public void Nunit3_AssertZero_TestAnalyzer(string assertion)

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.Zero(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Zero);")]
[Implemented]
public void Nunit4_AssertZero_TestAnalyzer(string assertion)
{
Expand All @@ -378,6 +416,9 @@ public void Nunit4_AssertZero_TestAnalyzer(string assertion)
[AssertionCodeFix(
oldAssertion: "Assert.Zero(actual{0});",
newAssertion: "actual.Should().Be(0{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Zero{0});",
newAssertion: "actual.Should().Be(0{0});")]
[Implemented]
public void Nunit3_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -394,6 +435,9 @@ public void Nunit3_AssertZero_TestCodeFix(string oldAssertion, string newAsserti
[AssertionCodeFix(
oldAssertion: "ClassicAssert.Zero(actual{0});",
newAssertion: "actual.Should().Be(0{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Zero);",
newAssertion: "actual.Should().Be(0);")]
[Implemented]
public void Nunit4_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -408,6 +452,7 @@ public void Nunit4_AssertZero_TestCodeFix(string oldAssertion, string newAsserti

[DataTestMethod]
[AssertionDiagnostic("Assert.NotZero(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.Zero{0});")]
[Implemented]
public void Nunit3_AssertNotZero_TestAnalyzer(string assertion)
{
Expand All @@ -422,6 +467,7 @@ public void Nunit3_AssertNotZero_TestAnalyzer(string assertion)

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.NotZero(actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.Zero);")]
[Implemented]
public void Nunit4_AssertNotZero_TestAnalyzer(string assertion)
{
Expand All @@ -438,6 +484,9 @@ public void Nunit4_AssertNotZero_TestAnalyzer(string assertion)
[AssertionCodeFix(
oldAssertion: "Assert.NotZero(actual{0});",
newAssertion: "actual.Should().NotBe(0{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.Zero{0});",
newAssertion: "actual.Should().NotBe(0{0});")]
[Implemented]
public void Nunit3_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -454,6 +503,9 @@ public void Nunit3_AssertNotZero_TestCodeFix(string oldAssertion, string newAsse
[AssertionCodeFix(
oldAssertion: "ClassicAssert.NotZero(actual{0});",
newAssertion: "actual.Should().NotBe(0{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.Zero);",
newAssertion: "actual.Should().NotBe(0);")]
[Implemented]
public void Nunit4_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand Down
Loading