Skip to content

feat: add assert that IsEmpty scenarios #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2024
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
16 changes: 16 additions & 0 deletions docs/Nunit3Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ var collection = new List<int>();
// old assertion:
Assert.IsEmpty(collection);
Assert.That(collection, Is.Empty);
Assert.That(collection, Has.Count.EqualTo(0));
Assert.That(collection, Has.Count.Zero);
CollectionAssert.IsEmpty(collection);

// new assertion:
Expand All @@ -207,6 +209,12 @@ Assert.IsEmpty(collection); /* fail message: Expected: <empty>
Assert.That(collection, Is.Empty); /* fail message: Expected: <empty>
But was: < 1, 2, 3 >
*/
Assert.That(collection, Has.Count.EqualTo(0)); /* fail message: Expected: property Count equal to 0
But was: 3
*/
Assert.That(collection, Has.Count.Zero); /* fail message: Expected: property Count equal to 0
But was: 3
*/
CollectionAssert.IsEmpty(collection); /* fail message: Expected: <empty>
But was: < 1, 2, 3 >
*/
Expand All @@ -224,6 +232,8 @@ var collection = new List<int> { 1, 2, 3 };
// old assertion:
Assert.IsNotEmpty(collection);
Assert.That(collection, Is.Not.Empty);
Assert.That(collection, Has.Count.GreaterThan(0));
Assert.That(collection, Has.Count.Not.Zero);
CollectionAssert.IsNotEmpty(collection);

// new assertion:
Expand All @@ -242,6 +252,12 @@ Assert.IsNotEmpty(collection); /* fail message: Expected: not <empty>
Assert.That(collection, Is.Not.Empty); /* fail message: Expected: not <empty>
But was: <empty>
*/
Assert.That(collection, Has.Count.GreaterThan(0)); /* fail message: Expected: property Count greater than 0
But was: 0
*/
Assert.That(collection, Has.Count.Not.Zero); /* fail message: Expected: property Count not equal to 0
But was: 0
*/
CollectionAssert.IsNotEmpty(collection); /* fail message: Expected: not <empty>
But was: <empty>
*/
Expand Down
30 changes: 25 additions & 5 deletions docs/Nunit4Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ var collection = new List<int>();
// old assertion:
ClassicAssert.IsEmpty(collection);
Assert.That(collection, Is.Empty);
Assert.That(collection, Has.Count.EqualTo(0));
Assert.That(collection, Has.Count.Zero);
CollectionAssert.IsEmpty(collection);

// new assertion:
Expand All @@ -224,6 +226,14 @@ Assert.That(collection, Is.Empty); /* fail message: Assert.That(collection, Is
Expected: <empty>
But was: < 1, 2, 3 >
*/
Assert.That(collection, Has.Count.EqualTo(0)); /* fail message: Assert.That(collection, Has.Count.EqualTo(0))
Expected: property Count equal to 0
But was: 3
*/
Assert.That(collection, Has.Count.Zero); /* fail message: Assert.That(collection, Has.Count.Zero)
Expected: property Count equal to 0
But was: 3
*/
CollectionAssert.IsEmpty(collection); /* fail message: Assert.That(collection, new EmptyCollectionConstraint())
Expected: <empty>
But was: < 1, 2, 3 >
Expand All @@ -242,6 +252,8 @@ var collection = new List<int> { 1, 2, 3 };
// old assertion:
ClassicAssert.IsNotEmpty(collection);
Assert.That(collection, Is.Not.Empty);
Assert.That(collection, Has.Count.GreaterThan(0));
Assert.That(collection, Has.Count.Not.Zero);
CollectionAssert.IsNotEmpty(collection);

// new assertion:
Expand All @@ -262,6 +274,14 @@ Assert.That(collection, Is.Not.Empty); /* fail message: Assert.That(collection
Expected: not <empty>
But was: <empty>
*/
Assert.That(collection, Has.Count.GreaterThan(0)); /* fail message: Assert.That(collection, Has.Count.GreaterThan(0))
Expected: property Count greater than 0
But was: 0
*/
Assert.That(collection, Has.Count.Not.Zero); /* fail message: Assert.That(collection, Has.Count.Not.Zero)
Expected: property Count not equal to 0
But was: 0
*/
CollectionAssert.IsNotEmpty(collection); /* fail message: Assert.That(collection, new NotConstraint(new EmptyCollectionConstraint()))
Expected: not <empty>
But was: <empty>
Expand Down Expand Up @@ -384,17 +404,17 @@ obj1.Should().NotBeSameAs(obj2);
#### Failure messages

```cs
var obj1 = new object();
var obj2 = obj1;
object obj1 = "foo";
object obj2 = "foo";

// old assertion:
ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is.Not.SameAs(expected))
Expected: not same as <System.Object>
But was: <System.Object>
Expected: not same as "foo"
But was: "foo"
*/

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

### scenario: CollectionAssertAreEqual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,16 @@ public void AssertIsEmpty()
// old assertion:
ClassicAssert.IsEmpty(collection);
Assert.That(collection, Is.Empty);
Assert.That(collection, Has.Count.EqualTo(0));
Assert.That(collection, Has.Count.Zero);
CollectionAssert.IsEmpty(collection);

// new assertion:
collection.Should().BeEmpty();
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_OldAssertion_0()
{
// arrange
Expand All @@ -286,6 +289,7 @@ public void AssertIsEmpty_Failure_OldAssertion_0()
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_OldAssertion_1()
{
// arrange
Expand All @@ -296,16 +300,40 @@ public void AssertIsEmpty_Failure_OldAssertion_1()
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_OldAssertion_2()
{
// arrange
var collection = new List<int> { 1, 2, 3 };

// old assertion:
Assert.That(collection, Has.Count.EqualTo(0));
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_OldAssertion_3()
{
// arrange
var collection = new List<int> { 1, 2, 3 };

// old assertion:
Assert.That(collection, Has.Count.Zero);
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_OldAssertion_4()
{
// arrange
var collection = new List<int> { 1, 2, 3 };

// old assertion:
CollectionAssert.IsEmpty(collection);
}

[Test, ExpectedAssertionException]

public void AssertIsEmpty_Failure_NewAssertion()
{
// arrange
Expand All @@ -324,13 +352,16 @@ public void AssertIsNotEmpty()
// old assertion:
ClassicAssert.IsNotEmpty(collection);
Assert.That(collection, Is.Not.Empty);
Assert.That(collection, Has.Count.GreaterThan(0));
Assert.That(collection, Has.Count.Not.Zero);
CollectionAssert.IsNotEmpty(collection);

// new assertion:
collection.Should().NotBeEmpty();
}

[Test, ExpectedAssertionException]

public void AssertIsNotEmpty_Failure_OldAssertion_0()
{
// arrange
Expand All @@ -341,6 +372,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_0()
}

[Test, ExpectedAssertionException]

public void AssertIsNotEmpty_Failure_OldAssertion_1()
{
// arrange
Expand All @@ -351,11 +383,34 @@ public void AssertIsNotEmpty_Failure_OldAssertion_1()
}

[Test, ExpectedAssertionException]

public void AssertIsNotEmpty_Failure_OldAssertion_2()
{
// arrange
var collection = new List<int>();

// old assertion:
Assert.That(collection, Has.Count.GreaterThan(0));
}

[Test, ExpectedAssertionException]

public void AssertIsNotEmpty_Failure_OldAssertion_3()
{
// arrange
var collection = new List<int>();

// old assertion:
Assert.That(collection, Has.Count.Not.Zero);
}

[Test, ExpectedAssertionException]

public void AssertIsNotEmpty_Failure_OldAssertion_4()
{
// arrange
var collection = new List<int>();

// old assertion:
CollectionAssert.IsNotEmpty(collection);
}
Expand Down Expand Up @@ -512,8 +567,8 @@ public void AssertAreNotSame()
public void AssertAreNotSame_Failure_OldAssertion()
{
// arrange
var obj1 = new object();
var obj2 = obj1;
object obj1 = "foo";
object obj2 = "foo";

// old assertion:
ClassicAssert.AreNotSame(obj1, obj2);
Expand All @@ -523,8 +578,8 @@ public void AssertAreNotSame_Failure_OldAssertion()
public void AssertAreNotSame_Failure_NewAssertion()
{
// arrange
var obj1 = new object();
var obj2 = obj1;
object obj1 = "foo";
object obj2 = "foo";

// new assertion:
obj1.Should().NotBeSameAs(obj2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ public void AssertIsEmpty()
// old assertion:
Assert.IsEmpty(collection);
Assert.That(collection, Is.Empty);
Assert.That(collection, Has.Count.EqualTo(0));
Assert.That(collection, Has.Count.Zero);
CollectionAssert.IsEmpty(collection);

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

// old assertion:
Assert.That(collection, Has.Count.EqualTo(0));
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertIsEmpty_Failure_OldAssertion_3()
{
// arrange
var collection = new List<int> { 1, 2, 3 };

// old assertion:
Assert.That(collection, Has.Count.Zero);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertIsEmpty_Failure_OldAssertion_4()
{
// arrange
var collection = new List<int> { 1, 2, 3 };

// old assertion:
CollectionAssert.IsEmpty(collection);
}
Expand All @@ -327,6 +349,8 @@ public void AssertIsNotEmpty()
// old assertion:
Assert.IsNotEmpty(collection);
Assert.That(collection, Is.Not.Empty);
Assert.That(collection, Has.Count.GreaterThan(0));
Assert.That(collection, Has.Count.Not.Zero);
CollectionAssert.IsNotEmpty(collection);

// new assertion:
Expand Down Expand Up @@ -359,6 +383,26 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2()
// arrange
var collection = new List<int>();

// old assertion:
Assert.That(collection, Has.Count.GreaterThan(0));
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertIsNotEmpty_Failure_OldAssertion_3()
{
// arrange
var collection = new List<int>();

// old assertion:
Assert.That(collection, Has.Count.Not.Zero);
}

[TestMethod, ExpectedTestFrameworkException]
public void AssertIsNotEmpty_Failure_OldAssertion_4()
{
// arrange
var collection = new List<int>();

// old assertion:
CollectionAssert.IsNotEmpty(collection);
}
Expand Down
12 changes: 12 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Empty{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Has.Count.EqualTo(0){0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Has.Count.Zero{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[AssertionCodeFix(
oldAssertion: "CollectionAssert.IsEmpty(actual{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
Expand All @@ -352,6 +358,12 @@ public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAsse
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Empty);",
newAssertion: "actual.Should().BeEmpty();")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Has.Count.EqualTo(0));",
newAssertion: "actual.Should().BeEmpty();")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Has.Count.Zero);",
newAssertion: "actual.Should().BeEmpty();")]
[AssertionCodeFix(
oldAssertion: "CollectionAssert.IsEmpty(actual{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
Expand Down
Loading