Skip to content

Commit

Permalink
Fix logic around out-of-order arguments for classic assertions (#712) (
Browse files Browse the repository at this point in the history
…#716)

* Fix #712: AreEqualClassicModelAssertUsageCodeFix no longer assumes that the first argument is expected and the second argument is actual

* Add a new overload for UpdateArguments

* Make all code functional but not yet readable/maintainable

* Refactoring

* Handle out-of-order arguments in all ClassicModelAssertUsageCodeFixes

* Add out-of-order test cases to CollectionAssertUsageCodeFixTests

* Update all other CodeFixTests, fixing CodeFixes to account for bugs found

* Remove an extra whitespace

* Code review changes

* Omit named parameters for the constraint model

* Add test case: two arguments for params in all ClassicAssert tests

* Omit named parameters in StringAssertUsageCodeFix

* Consistently use WithNameColon

Update Tests to remove named parameters

* Code review changes

* We no longer need HasToleranceValue

* We no longer need ComparerParameterIndex

* Update param tags for GetInterpolatedMessageArgumentOrDefault

---------

Co-authored-by: Manfred Brands <manfred-brands@users.noreply.github.com>
  • Loading branch information
Bartleby2718 and manfred-brands authored Apr 12, 2024
1 parent ea11a27 commit d14cc57
Show file tree
Hide file tree
Showing 55 changed files with 1,931 additions and 445 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,43 @@ public void VerifyGetFixableDiagnosticIds()
[Test]
public void VerifyAreEqualFix()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreEqual(2d, 3d);
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(3d, Is.EqualTo(2d));
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWhenToleranceExistsWithNamedArgumentButInNonstandardOrder()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
↓ClassicAssert.AreEqual(delta: 0.0000001d, actual: 3d, expected: 2d);
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d));
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWithNamedParametersInNonstandardOrder()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
↓ClassicAssert.AreEqual(actual: 3d, expected: 2d);
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -43,11 +75,11 @@ public void TestMethod()
[Test]
public void VerifyAreEqualFixWithMessage()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreEqual(2d, 3d, ""message"");
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -73,13 +105,13 @@ public void TestMethod()
}

[Test]
public void VerifyAreEqualFixWithMessageAndParams()
public void VerifyAreEqualFixWithMessageAndOneArgumentForParams()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
↓ClassicAssert.AreEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid());
}}");
{
↓ClassicAssert.AreEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid());
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -88,14 +120,50 @@ public void TestMethod()
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWithMessageAnd2ParamsInNonstandardOrder()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
var actual = 3d;
↓ClassicAssert.AreEqual(delta: 0.0000001d, expected: 2d, actual: actual, args: new[] { ""Guid.NewGuid()"", Guid.NewGuid().ToString() }, message: ""message-id: {0}, {1}"");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
var actual = 3d;
Assert.That(actual, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString()}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWithMessageAnd2ParamsInStandardOrder()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
var actual = 3d;
↓ClassicAssert.AreEqual(expected: 2d, actual: actual, delta: 0.0000001d, ""message-id: {0}, {1}"", ""Guid.NewGuid()"", Guid.NewGuid());
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
var actual = 3d;
Assert.That(actual, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid()}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWhenToleranceExists()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d);
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -107,27 +175,27 @@ public void TestMethod()
[Test]
public void VerifyAreEqualFixWhenToleranceExistsWithNamedArgument()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreEqual(expected: 2d, actual: 3d, delta: 0.0000001d);
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(actual: 3d, Is.EqualTo(expected: 2d).Within(0.0000001d));
Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d));
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWhenToleranceExistsWithMessage()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message"");
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -153,13 +221,13 @@ public void TestMethod()
}

[Test]
public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndParams()
public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndOneArgumentForParams()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {{0}}"", Guid.NewGuid());
}}");
{
↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {0}"", Guid.NewGuid());
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -168,10 +236,26 @@ public void TestMethod()
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndTwoArgumentsForParams()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""{0}, {1}"", ""first"", ""second"");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d), $""{""first""}, {""second""}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void CodeFixPreservesLineBreakBeforeMessage()
{
var code = TestUtility.WrapInTestMethod($@"
var code = TestUtility.WrapInTestMethod(@"
ClassicAssert.AreEqual(2d, 3d, 0.0000001d,
""message"");");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds()
[Test]
public void VerifyAreNotEqualFix()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreNotEqual(2d, 3d);
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -43,11 +43,11 @@ public void TestMethod()
[Test]
public void VerifyAreNotEqualFixWithMessage()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
{
↓ClassicAssert.AreNotEqual(2d, 3d, ""message"");
}}");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Expand All @@ -57,19 +57,51 @@ public void TestMethod()
}

[Test]
public void VerifyAreNotEqualFixWithMessageAndParams()
public void VerifyAreNotEqualFixWithMessageAndOneArgumentForParams()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@"
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{{
↓ClassicAssert.AreNotEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid());
}}");
{
↓ClassicAssert.AreNotEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid());
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(3d, Is.Not.EqualTo(2d), $""message-id: {Guid.NewGuid()}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreNotEqualFixWithMessageAndTwoArgumentsForParams()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
↓ClassicAssert.AreNotEqual(2d, 3d, ""{0}, {1}"", ""first"", ""second"");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(3d, Is.Not.EqualTo(2d), $""{""first""}, {""second""}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}

[Test]
public void VerifyAreNotEqualFixWithMessageAndArrayParamsInNonstandardOrder()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
↓ClassicAssert.AreNotEqual(args: new[] { ""first"", ""second"" }, actual: ""actual"", message: ""{0}, {1}"", expected: ""expected"");
}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void TestMethod()
{
Assert.That(""actual"", Is.Not.EqualTo(""expected""), $""{""first""}, {""second""}"");
}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription);
}
}
}
Loading

0 comments on commit d14cc57

Please sign in to comment.