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
10 changes: 10 additions & 0 deletions docs/Nunit3Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ var expected = new[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AreEqual(expected, collection);
Assert.That(collection, Is.EqualTo(expected));

// new assertion:
collection.Should().Equal(expected);
Expand All @@ -540,6 +541,11 @@ CollectionAssert.AreEqual(expected, collection); /* fail message: Expected and
Expected: 4
But was: 3
*/
Assert.That(collection, Is.EqualTo(expected)); /* fail message: Expected and actual are both <System.Int32[3]>
Values differ at index [2]
Expected: 4
But was: 3
*/

// new assertion:
collection.Should().Equal(expected); /* fail message: Expected collection to be equal to {1, 2, 4}, but {1, 2, 3} differs at index 2. */
Expand All @@ -554,6 +560,7 @@ var expected = new[] { 1, 2, 4 };

// old assertion:
CollectionAssert.AreNotEqual(expected, collection);
Assert.That(collection, Is.Not.EqualTo(expected));

// new assertion:
collection.Should().NotEqual(expected);
Expand All @@ -569,6 +576,9 @@ var expected = new[] { 1, 2, 3 };
CollectionAssert.AreNotEqual(expected, collection); /* fail message: Expected: not equal to < 1, 2, 3 >
But was: < 1, 2, 3 >
*/
Assert.That(collection, Is.Not.EqualTo(expected)); /* fail message: Expected: not equal to < 1, 2, 3 >
But was: < 1, 2, 3 >
*/

// new assertion:
collection.Should().NotEqual(expected); /* fail message: Did not expect collections {1, 2, 3} and {1, 2, 3} to be equal. */
Expand Down
12 changes: 12 additions & 0 deletions docs/Nunit4Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ var expected = new[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AreEqual(expected, collection);
Assert.That(collection, Is.EqualTo(expected));

// new assertion:
collection.Should().Equal(expected);
Expand All @@ -580,6 +581,12 @@ CollectionAssert.AreEqual(expected, collection); /* fail message: Assert.That(
Expected: 4
But was: 3
*/
Assert.That(collection, Is.EqualTo(expected)); /* fail message: Assert.That(collection, Is.EqualTo(expected))
Expected and actual are both <System.Int32[3]>
Values differ at index [2]
Expected: 4
But was: 3
*/

// new assertion:
collection.Should().Equal(expected); /* fail message: Expected collection to be equal to {1, 2, 4}, but {1, 2, 3} differs at index 2. */
Expand All @@ -594,6 +601,7 @@ var expected = new[] { 1, 2, 4 };

// old assertion:
CollectionAssert.AreNotEqual(expected, collection);
Assert.That(collection, Is.Not.EqualTo(expected));

// new assertion:
collection.Should().NotEqual(expected);
Expand All @@ -610,6 +618,10 @@ CollectionAssert.AreNotEqual(expected, collection); /* fail message: Assert.Th
Expected: not equal to < 1, 2, 3 >
But was: < 1, 2, 3 >
*/
Assert.That(collection, Is.Not.EqualTo(expected)); /* fail message: Assert.That(collection, Is.Not.EqualTo(expected))
Expected: not equal to < 1, 2, 3 >
But was: < 1, 2, 3 >
*/

// new assertion:
collection.Should().NotEqual(expected); /* fail message: Did not expect collections {1, 2, 3} and {1, 2, 3} to be equal. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,14 @@ public void CollectionAssertAreEqual()

// old assertion:
CollectionAssert.AreEqual(expected, collection);
Assert.That(collection, Is.EqualTo(expected));

// new assertion:
collection.Should().Equal(expected);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_OldAssertion()
public void CollectionAssertAreEqual_Failure_OldAssertion_0()
{
// arrange
var collection = new[] { 1, 2, 3 };
Expand All @@ -775,6 +776,17 @@ public void CollectionAssertAreEqual_Failure_OldAssertion()
CollectionAssert.AreEqual(expected, collection);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_OldAssertion_1()
{
// arrange
var collection = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 4 };

// old assertion:
Assert.That(collection, Is.EqualTo(expected));
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_NewAssertion()
{
Expand All @@ -795,13 +807,14 @@ public void CollectionAssertAreNotEqual()

// old assertion:
CollectionAssert.AreNotEqual(expected, collection);
Assert.That(collection, Is.Not.EqualTo(expected));

// new assertion:
collection.Should().NotEqual(expected);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_OldAssertion()
public void CollectionAssertAreNotEqual_Failure_OldAssertion_0()
{
// arrange
var collection = new[] { 1, 2, 3 };
Expand All @@ -811,6 +824,17 @@ public void CollectionAssertAreNotEqual_Failure_OldAssertion()
CollectionAssert.AreNotEqual(expected, collection);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_OldAssertion_1()
{
// arrange
var collection = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 3 };

// old assertion:
Assert.That(collection, Is.Not.EqualTo(expected));
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_NewAssertion()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,14 @@ public void CollectionAssertAreEqual()

// old assertion:
CollectionAssert.AreEqual(expected, collection);
Assert.That(collection, Is.EqualTo(expected));

// new assertion:
collection.Should().Equal(expected);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_OldAssertion()
public void CollectionAssertAreEqual_Failure_OldAssertion_0()
{
// arrange
var collection = new[] { 1, 2, 3 };
Expand All @@ -786,6 +787,17 @@ public void CollectionAssertAreEqual_Failure_OldAssertion()
CollectionAssert.AreEqual(expected, collection);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_OldAssertion_1()
{
// arrange
var collection = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 4 };

// old assertion:
Assert.That(collection, Is.EqualTo(expected));
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreEqual_Failure_NewAssertion()
{
Expand All @@ -806,13 +818,14 @@ public void CollectionAssertAreNotEqual()

// old assertion:
CollectionAssert.AreNotEqual(expected, collection);
Assert.That(collection, Is.Not.EqualTo(expected));

// new assertion:
collection.Should().NotEqual(expected);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_OldAssertion()
public void CollectionAssertAreNotEqual_Failure_OldAssertion_0()
{
// arrange
var collection = new[] { 1, 2, 3 };
Expand All @@ -822,6 +835,17 @@ public void CollectionAssertAreNotEqual_Failure_OldAssertion()
CollectionAssert.AreNotEqual(expected, collection);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_OldAssertion_1()
{
// arrange
var collection = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 3 };

// old assertion:
Assert.That(collection, Is.Not.EqualTo(expected));
}

[Test, ExpectedAssertionException]
public void CollectionAssertAreNotEqual_Failure_NewAssertion()
{
Expand Down
20 changes: 8 additions & 12 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ public void Nunit4_AssertContains_WithCasting_TestCodeFix(string methodArguments
[DataTestMethod]
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual{0});")]
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual, comparer{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected){0});", ignore: true)]
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected){0});")]
[Implemented]
public void Nunit3_CollectionAssertAreEqual_TestAnalyzer(string assertion)
{
Expand All @@ -1317,7 +1317,7 @@ public void Nunit3_CollectionAssertAreEqual_TestAnalyzer(string assertion)
[DataTestMethod]
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual{0});")]
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual, comparer{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected));", ignore: true)]
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected));")]
[Implemented]
public void Nunit4_CollectionAssertAreEqual_TestAnalyzer(string assertion)
{
Expand All @@ -1332,8 +1332,7 @@ public void Nunit4_CollectionAssertAreEqual_TestAnalyzer(string assertion)
newAssertion: "actual.Should().Equal(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.EqualTo(expected){0});",
newAssertion: "actual.Should().Equal(expected{0});",
ignore: true)]
newAssertion: "actual.Should().Equal(expected{0});")]
[Implemented]
public void Nunit3_CollectionAssertAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand Down Expand Up @@ -1364,8 +1363,7 @@ public void Nunit3_CollectionAssertAreEqual_NoFix_IComparer_TestCodeFix(string a
newAssertion: "actual.Should().Equal(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.EqualTo(expected));",
newAssertion: "actual.Should().Equal(expected);",
ignore: true)]
newAssertion: "actual.Should().Equal(expected);")]
[Implemented]
public void Nunit4_CollectionAssertAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand Down Expand Up @@ -1393,7 +1391,7 @@ public void Nunit4_CollectionAssertAreEqual_NoFix_IComparer_TestCodeFix(string a

[DataTestMethod]
[AssertionDiagnostic("CollectionAssert.AreNotEqual(expected, actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected){0});", ignore: true)]
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected){0});")]
[Implemented]
public void Nunit3_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
{
Expand All @@ -1404,7 +1402,7 @@ public void Nunit3_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)

[DataTestMethod]
[AssertionDiagnostic("CollectionAssert.AreNotEqual(expected, actual{0});")]
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected));", ignore: true)]
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected));")]
[Implemented]
public void Nunit4_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
{
Expand All @@ -1419,8 +1417,7 @@ public void Nunit4_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
newAssertion: "actual.Should().NotEqual(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.EqualTo(expected){0});",
newAssertion: "actual.Should().NotEqual(expected{0});",
ignore: true)]
newAssertion: "actual.Should().NotEqual(expected{0});")]
[Implemented]
public void Nunit3_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand All @@ -1435,8 +1432,7 @@ public void Nunit3_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion,
newAssertion: "actual.Should().NotEqual(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.That(actual, Is.Not.EqualTo(expected));",
newAssertion: "actual.Should().NotEqual(expected);",
ignore: true)]
newAssertion: "actual.Should().NotEqual(expected);")]
[Implemented]
public void Nunit4_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
{
Expand Down
5 changes: 5 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
else
return rewriter.Should("HaveCountGreaterThan", argument);
}
else if (matcher.Is(Method("EqualTo"), out argument))
return rewriter.Should("Equal", argument);
else if (matcher.Is("Not", Method("EqualTo"), out argument))
return rewriter.Should("NotEqual", argument);
}
if (matcher.Is("Zero"))
return rewriter.Should("Be", g => g.LiteralExpression(0));
Expand Down Expand Up @@ -557,6 +561,7 @@ public class NunitCodeFixContext(Compilation compilation) : TestingFrameworkCode
private class AssertThatMatcher(IOperation constraint, NunitCodeFixContext t)
{
public bool Is(MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Is, methodMatcher, out argument);
public bool Is(string propertyMatcher, MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Is, [Property(propertyMatcher)], methodMatcher, out argument);
public bool Is(params string[] matchers) => Matches(t.Is, matchers);
public bool Has(params string[] matchers) => Matches(t.Has, matchers);
public bool Has(IOperationMatcher[] matchers, MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Has, matchers, methodMatcher, out argument);
Expand Down