Skip to content
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

Fix logic around out-of-order arguments for classic assertions (#712) #716

Merged
merged 17 commits into from
Apr 12, 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
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);
}

manfred-brands marked this conversation as resolved.
Show resolved Hide resolved
[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