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 for Assert.That(str1, Is.EqualTo(str2).IgnoreCase) and Not.EqualTo #30

Merged
merged 3 commits into from
Dec 30, 2023
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 @@ -286,6 +286,11 @@ public ProjectBuilder ShouldFixCodeWith(string codeFix)
return ShouldFixCodeWith(index: null, codeFix);
}

public ProjectBuilder ShouldFixCodeWithOriginalCode()
{
return ShouldFixCodeWith(index: null, SourceCode);
}

public ProjectBuilder ShouldFixCodeWith(int? index, string codeFix)
{
ExpectedFixedCode = codeFix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ private static ProjectBuilder CreateProjectBuilder()

private static Task Assert(string sourceCode, string fix = null)
{
return CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ShouldFixCodeWith(fix ?? sourceCode)
.ValidateAsync();
var projectBuilder = CreateProjectBuilder()
.WithSourceCode(sourceCode);

if (fix is null)
{
projectBuilder.ShouldFixCodeWithOriginalCode();
}
else
{
projectBuilder.ShouldFixCodeWith(fix);
}

return projectBuilder.ValidateAsync();
}

[Fact]
Expand Down Expand Up @@ -51,7 +60,7 @@ public void MyTest()
}
""");
}

[Fact]
public Task Assert_Inconclusive_NoReport()
{
Expand All @@ -69,7 +78,7 @@ public void MyTest()
}
""");
}

[Fact]
public Task Assert_Ignore_NoReport()
{
Expand All @@ -87,7 +96,7 @@ public void MyTest()
}
""");
}

[Fact]
public Task Assert_Fail_ExcludedFromOptions()
{
Expand All @@ -107,7 +116,7 @@ public void MyTest()
.AddAnalyzerConfiguration("mfa_excluded_methods", "M:NUnit.Framework.Assert.Fail")
.ValidateAsync();
}

[Fact]
public Task Assert_Fail_String_ExcludedFromOptions()
{
Expand All @@ -127,7 +136,7 @@ public void MyTest()
.AddAnalyzerConfiguration("mfa_excluded_methods", "M:NUnit.Framework.Assert.Fail(System.String)")
.ValidateAsync();
}

[Fact]
public Task Assert_MultiMethods_ExcludedFromOptions()
{
Expand Down Expand Up @@ -317,7 +326,7 @@ public void MyTest()
[InlineData(@"Assert.AreEqual(0d, (double?)null, delta: 2d)", @"((double?)null).Should().BeApproximately(0d, 2d)")]
[InlineData(@"Assert.AreEqual(0d, (double?)null, delta: 2d, ""because"")", @"((double?)null).Should().BeApproximately(0d, 2d, ""because"")")]
[InlineData(@"Assert.AreEqual(0d, (double?)null, delta: 2d, ""because"", 1, 2)", @"((double?)null).Should().BeApproximately(0d, 2d, ""because"", 1, 2)")]

[InlineData(@"Assert.AreEqual(0f, (float?)null, delta: 0.1f)", @"((float?)null).Should().BeApproximately(0f, 0.1f)")]
[InlineData(@"Assert.AreEqual(0f, 1f, delta: 0.1f)", @"1f.Should().BeApproximately(0f, 0.1f)")]

Expand Down Expand Up @@ -537,7 +546,6 @@ public void MyTest()
[InlineData(@"Assert.Less(0d, 1, ""because"")", @"0d.Should().BeLessThan(1, ""because"")")]
[InlineData(@"Assert.Less(0d, 1, ""because"", 2, 3)", @"0d.Should().BeLessThan(1, ""because"", 2, 3)")]


[InlineData(@"Assert.LessOrEqual(0, 1)", @"0.Should().BeLessThanOrEqualTo(1)")]
[InlineData(@"Assert.LessOrEqual(0, 1, ""because"")", @"0.Should().BeLessThanOrEqualTo(1, ""because"")")]
[InlineData(@"Assert.LessOrEqual(0, 1, ""because"", 2, 3)", @"0.Should().BeLessThanOrEqualTo(1, ""because"", 2, 3)")]
Expand Down Expand Up @@ -778,7 +786,9 @@ public void MyTest()
[InlineData(@"Assert.That(""aa"", Has.Length.EqualTo(2))", @"""aa"".Should().HaveLength(2)")]

[InlineData(@"Assert.That("""", Is.EqualTo(""expected""))", @""""".Should().Be(""expected"")")]
[InlineData(@"Assert.That("""", Is.EqualTo(""expected"").IgnoreCase)", @""""".Should().BeEquivalentTo(""expected"")")]
[InlineData(@"Assert.That("""", Is.Not.EqualTo(""expected""))", @""""".Should().NotBe(""expected"")")]
[InlineData(@"Assert.That("""", Is.Not.EqualTo(""expected"").IgnoreCase)", @""""".Should().NotBeEquivalentTo(""expected"")")]
[InlineData(@"Assert.That(collection, Is.EqualTo(new int[0]))", @"collection.Should().Equal(new int[0])")]
[InlineData(@"Assert.That(collection, Is.Not.EqualTo(new int[0]))", @"collection.Should().NotEqual(new int[0])")]
[InlineData(@"Assert.That((IEnumerable<int>)collection, Is.EqualTo(new int[0]))", @"((IEnumerable<int>)collection).Should().Equal(new int[0])")]
Expand Down Expand Up @@ -844,4 +854,27 @@ public void MyTest()
}
""");
}

[Theory]
[InlineData(@"Assert.That(collection, Is.EqualTo(otherCollection).IgnoreCase)")]
[InlineData(@"Assert.That(collection, Is.EquivalentTo(otherCollection).IgnoreCase)")]
[InlineData(@"Assert.That(collection, Is.Not.EqualTo(otherCollection).IgnoreCase)")]
[InlineData(@"Assert.That(collection, Is.Not.EquivalentTo(otherCollection).IgnoreCase)")]
public Task AssertThatStringCollectionIsEqualToIgnoreCase_ExpectReportButNoFix(string code)
{
return Assert($$"""
using System.Collections.Generic;
using NUnit.Framework;

class Test
{
public void MyTest()
{
var collection = new string[1];
var otherCollection = new string[1];
[|{{code}}|];
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
}
else
{
var useBeApproximately = leftType.SpecialType is SpecialType.System_Double or SpecialType.System_Single
var useBeApproximately = leftType.SpecialType is SpecialType.System_Double or SpecialType.System_Single
&& arguments.FirstOrDefault(x => x.NameColon?.Name.Identifier.ValueText is "delta") is not null;

result = rewrite.UsingShould(right, useBeApproximately ? "BeApproximately" : "Be", ArgumentList(left, arguments.Skip(2)));
}
}
Expand Down Expand Up @@ -500,6 +500,13 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
result = rewrite.UsingShould(arguments[0], replacementMethodName, ArgumentList(expected, arguments.Skip(2)));
}
}
else if (IsMethod(out expected, isSymbol, "EqualTo", "IgnoreCase"))
{
// Don't provide a fix if the assertion is applied to a string collection, as there is no straight conversion.
var isString = method.Parameters[0].Type.SpecialType == SpecialType.System_String;
if (isString)
result = rewrite.UsingShould(arguments[0], "BeEquivalentTo", ArgumentList(expected, arguments.Skip(2)));
}
else if (IsMethod(out expected, isSymbol, "Not", "EqualTo"))
{
var (isCollection, isMigrationSupported) = IsCollection(arguments[0]);
Expand All @@ -509,6 +516,13 @@ private static async Task<Document> Rewrite(Document document, SyntaxNode nodeTo
result = rewrite.UsingShould(arguments[0], replacementMethodName, ArgumentList(expected, arguments.Skip(2)));
}
}
else if (IsMethod(out expected, isSymbol, "Not", "EqualTo", "IgnoreCase"))
{
// Don't provide a fix if the assertion is applied to a string collection, as there is no straight conversion.
var isString = method.Parameters[0].Type.SpecialType == SpecialType.System_String;
if (isString)
result = rewrite.UsingShould(arguments[0], "NotBeEquivalentTo", ArgumentList(expected, arguments.Skip(2)));
}
else if (IsMethod(out expected, isSymbol, "SameAs"))
{
result = rewrite.UsingShould(arguments[0], "BeSameAs", ArgumentList(expected, arguments.Skip(2)));
Expand Down Expand Up @@ -882,13 +896,13 @@ private static (ArgumentSyntax left, ArgumentSyntax right) GetLeftRight(Separate
var right = arguments[1];
var leftValue = semanticModel.GetConstantValue(left.Expression, cancellationToken);
var rightValue = semanticModel.GetConstantValue(right.Expression, cancellationToken);

// Don't invert if both are constant
if (leftValue.HasValue && rightValue.HasValue)
{
return (left, right);
}

// Invert if right is constant
if (rightValue.HasValue)
{
Expand Down