Skip to content

Commit

Permalink
feat: Rewrite parameters order when handling constant values in asser…
Browse files Browse the repository at this point in the history
…tions (#370)

* feat: Rewrite parameters order when handling constant values in assertions
  • Loading branch information
Meir017 authored Jun 6, 2024
1 parent 59a92c6 commit e24e463
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 24 deletions.
30 changes: 28 additions & 2 deletions docs/MsTestAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
- [AssertIsInstanceOfType](#scenario-assertisinstanceoftype) - `obj.Should().BeOfType<List<object>>();`
- [AssertIsNotInstanceOfType](#scenario-assertisnotinstanceoftype) - `obj.Should().NotBeOfType<List<object>>();`
- [AssertObjectAreEqual](#scenario-assertobjectareequal) - `obj1.Should().Be(obj2);`
- [AssertObjectAreEqual_LiteralValue](#scenario-assertobjectareequal_literalvalue) - `obj1.Should().Be("foo");`
- [AssertOptionalIntegerAreEqual](#scenario-assertoptionalintegerareequal) - `number1.Should().Be(number2);`
- [AssertOptionalIntegerAndNullAreEqual](#scenario-assertoptionalintegerandnullareequal) - `number.Should().BeNull();`
- [AssertDoubleAreEqual](#scenario-assertdoubleareequal) - `number1.Should().BeApproximately(number2, delta);`
Expand Down Expand Up @@ -228,6 +229,31 @@ Assert.AreEqual(obj2, obj1); /* fail message: Assert.AreEqual failed. Expected:<
obj1.Should().Be(obj2); /* fail message: Expected obj1 to be 42, but found "foo". */
```

### scenario: AssertObjectAreEqual_LiteralValue

```cs
// arrange
object obj1 = "foo";

// old assertion:
Assert.AreEqual(obj1, "foo");

// new assertion:
obj1.Should().Be("foo");
```

#### Failure messages

```cs
object obj1 = "foo";

// old assertion:
Assert.AreEqual(obj1, "bar"); /* fail message: Assert.AreEqual failed. Expected:<foo>. Actual:<bar>. */

// new assertion:
obj1.Should().Be("bar"); /* fail message: Expected obj1 to be "bar", but found "foo". */
```

### scenario: AssertOptionalIntegerAreEqual

```cs
Expand Down Expand Up @@ -950,7 +976,7 @@ Action action = ThrowException;
// old assertion:
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
Exception Message: Operation is not valid due to the current state of the object.
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|106_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1265
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|109_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1298
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException[T](Action action, String message, Object[] parameters) */

// new assertion:
Expand Down Expand Up @@ -980,7 +1006,7 @@ Func<Task> action = ThrowExceptionAsync;
// old assertion:
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
Exception Message: Operation is not valid due to the current state of the object.
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|109_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1301
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|112_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1334
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync[T](Func`1 action, String message, Object[] parameters) */

// new assertion:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,39 @@ public void AssertObjectAreEqual_Failure_NewAssertion()
obj1.Should().Be(obj2);
}

[TestMethod]
public void AssertObjectAreEqual_LiteralValue()
{
// arrange
object obj1 = "foo";

// old assertion:
Assert.AreEqual(obj1, "foo");

// new assertion:
obj1.Should().Be("foo");
}

[TestMethod, ExpectedException(typeof(AssertFailedException))]
public void AssertObjectAreEqual_LiteralValue_Failure_OldAssertion()
{
// arrange
object obj1 = "foo";

// old assertion:
Assert.AreEqual(obj1, "bar");
}

[TestMethod, ExpectedException(typeof(AssertFailedException))]
public void AssertObjectAreEqual_LiteralValue_Failure_NewAssertion()
{
// arrange
object obj1 = "foo";

// new assertion:
obj1.Should().Be("bar");
}

[TestMethod]
public void AssertOptionalIntegerAreEqual()
{
Expand Down
83 changes: 71 additions & 12 deletions src/FluentAssertions.Analyzers.Tests/Tips/MsTestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,33 +322,26 @@ public void AssertOptionalIntegerAreEqual_TestCodeFix(string oldAssertion, strin

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(actual, null{0});")]
[AssertionDiagnostic("Assert.AreEqual(null, actual{0});")]
[Implemented]
public void AssertOptionalIntegerAndNullAreEqual1_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);
public void AssertOptionalIntegerAndNullAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, null{0});",
newAssertion: "actual.Should().BeNull({0});")]
[Implemented]
public void AssertOptionalIntegerAndNullAreEqual1_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(null, actual{0});")]
[Implemented]
public void AssertOptionalIntegerAndNullAreEqual2_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(null, actual{0});",
newAssertion: "actual.Should().BeNull({0});")]
[Implemented]
public void AssertOptionalIntegerAndNullAreEqual2_TestCodeFix(string oldAssertion, string newAssertion)
public void AssertOptionalIntegerAndNullAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, 0.6{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, 4.2d, 0.6{0});")]
[AssertionDiagnostic("Assert.AreEqual(4.2d, actual, 0.6{0});")]
[Implemented]
public void AssertDoubleAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("double actual, double expected, double delta", assertion);

Expand All @@ -359,13 +352,21 @@ public void AssertOptionalIntegerAndNullAreEqual2_TestCodeFix(string oldAssertio
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, 0.6{0});",
newAssertion: "actual.Should().BeApproximately(expected, 0.6{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, 4.2d, 0.6{0});",
newAssertion: "actual.Should().BeApproximately(4.2d, 0.6{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(4.2d, actual, 0.6{0});",
newAssertion: "actual.Should().BeApproximately(4.2d, 0.6{0});")]
[Implemented]
public void AssertDoubleAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("double actual, double expected, double delta", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, 0.6f{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, 4.2f, 0.6f{0});")]
[AssertionDiagnostic("Assert.AreEqual(4.2f, actual, 0.6f{0});")]
[Implemented]
public void AssertFloatAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("float actual, float expected, float delta", assertion);

Expand All @@ -376,35 +377,81 @@ public void AssertDoubleAreEqual_TestCodeFix(string oldAssertion, string newAsse
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, 0.6f{0});",
newAssertion: "actual.Should().BeApproximately(expected, 0.6f{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, 4.2f, 0.6f{0});",
newAssertion: "actual.Should().BeApproximately(4.2f, 0.6f{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(4.2f, actual, 0.6f{0});",
newAssertion: "actual.Should().BeApproximately(4.2f, 0.6f{0});")]
[Implemented]
public void AssertFloatAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("float actual, float expected, float delta", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(expected, actual{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\"{0});")]
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, false{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", false{0});")]
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, false{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, true{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", true{0});")]
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, true{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, false, System.Globalization.CultureInfo.CurrentCulture{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", false, System.Globalization.CultureInfo.CurrentCulture{0});")]
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, false, System.Globalization.CultureInfo.CurrentCulture{0});")]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, true, System.Globalization.CultureInfo.CurrentCulture{0});")]
[AssertionDiagnostic("Assert.AreEqual(actual, \"literal\", true, System.Globalization.CultureInfo.CurrentCulture{0});")]
[AssertionDiagnostic("Assert.AreEqual(\"literal\", actual, true, System.Globalization.CultureInfo.CurrentCulture{0});")]
[Implemented]
public void AssertStringAreEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("string actual, string expected", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual{0});",
newAssertion: "actual.Should().Be(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, \"literal\"{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(\"literal\", actual{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, false{0});",
newAssertion: "actual.Should().Be(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, \"literal\", false{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(\"literal\", actual, false{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, true{0});",
newAssertion: "actual.Should().BeEquivalentTo(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, \"literal\", true{0});",
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(\"literal\", actual, true{0});",
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, false, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().Be(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, \"literal\", false, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(\"literal\", actual, false, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().Be(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, true, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().BeEquivalentTo(expected{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(actual, \"literal\", true, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(\"literal\", actual, true, System.Globalization.CultureInfo.CurrentCulture{0});",
newAssertion: "actual.Should().BeEquivalentTo(\"literal\"{0});")]
[Implemented]
public void AssertStringAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("string actual, string expected", oldAssertion, newAssertion);
Expand Down Expand Up @@ -455,20 +502,26 @@ public void AssertOptionalIntAreNotEqual_TestCodeFix(string oldAssertion, string

[DataTestMethod]
[AssertionDiagnostic("Assert.AreNotEqual(actual, null{0});")]
[AssertionDiagnostic("Assert.AreNotEqual(null, actual{0});")]
[Implemented]
public void AssertOptionalIntAndNullAreNotEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("int? actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(actual, null{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(null, actual{0});",
newAssertion: "actual.Should().NotBeNull({0});")]
[Implemented]
public void AssertOptionalIntAndNullAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("int? actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreNotEqual(expected, actual, delta{0});")]
[AssertionDiagnostic("Assert.AreNotEqual(expected, actual, 0.6f{0});")]
[AssertionDiagnostic("Assert.AreNotEqual(actual, 4.2f, 0.6f{0});")]
[AssertionDiagnostic("Assert.AreNotEqual(4.2f, actual, 0.6f{0});")]
[Implemented]
public void AssertFloatAreNotEqual_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic("float actual, float expected, float delta", assertion);

Expand All @@ -479,6 +532,12 @@ public void AssertOptionalIntAndNullAreNotEqual_TestCodeFix(string oldAssertion,
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(expected, actual, 0.6f{0});",
newAssertion: "actual.Should().NotBeApproximately(expected, 0.6f{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(actual, 4.2f, 0.6f{0});",
newAssertion: "actual.Should().NotBeApproximately(4.2f, 0.6f{0});")]
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(4.2f, actual, 0.6f{0});",
newAssertion: "actual.Should().NotBeApproximately(4.2f, 0.6f{0});")]
[Implemented]
public void AssertFloatAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("float actual, float expected, float delta", oldAssertion, newAssertion);
Expand Down
Loading

0 comments on commit e24e463

Please sign in to comment.