Skip to content

Commit

Permalink
Add C# tests for trivia and string method
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossanlop committed Jun 29, 2023
1 parent 840db9e commit 79db421
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,18 @@ private static IEnumerable<object[]> DiagnosedAndFixedEqualityToEqualsData(Tuple
yield return new object[] { $"a {before} b.To{casing}()", $"{after}a.Equals(b, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"a {before} b.To{casing}Invariant()", $"{after}a.Equals(b, StringComparison.InvariantCultureIgnoreCase)" };

yield return new object[] { $"\"abc\" {before} b.To{casing}()", $"{after}\"abc\".Equals(b, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"\"abc\" {before} b.To{casing}Invariant()", $"{after}\"abc\".Equals(b, StringComparison.InvariantCultureIgnoreCase)" };
yield return new object[] { $"\"abc\" {before} b.To{casing}()", $"{after}\"abc\".Equals(b, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"\"abc\" {before} b.To{casing}Invariant()", $"{after}\"abc\".Equals(b, StringComparison.InvariantCultureIgnoreCase)" };
yield return new object[] { $"\"abc\".To{casing}() {before} b.To{casing}()", $"{after}\"abc\".Equals(b, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"\"abc\".To{casing}() {before} b.To{casing}Invariant()", $"{after}\"abc\".Equals(b, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"\"abc\".To{casing}Invariant() {before} b.To{casing}Invariant()", $"{after}\"abc\".Equals(b, StringComparison.InvariantCultureIgnoreCase)" };

yield return new object[] { $"GetString().To{casing}() {before} a.To{casing}()", $"{after}GetString().Equals(a, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"GetString().To{casing}() {before} a.To{casing}Invariant()", $"{after}GetString().Equals(a, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"GetString().To{casing}Invariant() {before} a.To{casing}()", $"{after}GetString().Equals(a, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"GetString().To{casing}() {before} a", $"{after}GetString().Equals(a, StringComparison.CurrentCultureIgnoreCase)" };
yield return new object[] { $"GetString().To{casing}Invariant() {before} a", $"{after}GetString().Equals(a, StringComparison.InvariantCultureIgnoreCase)" };
yield return new object[] { $"GetString().To{casing}Invariant() {before} \"abc\"", $"{after}GetString().Equals(\"abc\", StringComparison.InvariantCultureIgnoreCase)" };
}
}
#pragma warning restore format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public async Task Diagnostic_Equality_To_Equals(string diagnosedLine, string fix
string originalCode = $@"using System;
class C
{{
string GetString() => ""cde"";
bool M(string a, string b)
{{
bool result = [|{diagnosedLine}|];
Expand All @@ -228,6 +229,7 @@ bool M(string a, string b)
string fixedCode = $@"using System;
class C
{{
string GetString() => ""cde"";
bool M(string a, string b)
{{
bool result = {fixedLine};
Expand All @@ -238,6 +240,41 @@ bool M(string a, string b)
await VerifyFixCSharpAsync(originalCode, fixedCode);
}

[Fact]
public async Task Diagnostic_Equality_To_Equals_Trivia()
{
string originalCode = $@"using System;
class C
{{
bool M(string a, string b)
{{
// Trivia
bool /* Trivia */ result = /* Trivia */ [|a.ToLower() // Trivia
== /* Trivia */ b.ToLowerInvariant()|] /* Trivia */; // Trivia
if (/* Trivia */ [|a.ToLowerInvariant() /* Trivia */ != /* Trivia */ b.ToLower()|] /* Trivia */) // Trivia
return /* Trivia */ [|b /* Trivia */ != /* Trivia */ a.ToLowerInvariant()|] /* Trivia */; // Trivia
return // Trivia
[|""abc"" /* Trivia */ == /* Trivia */ a.ToUpperInvariant()|] /* Trivia */; // Trivia
// Trivia
}}
}}";
string fixedCode = $@"using System;
class C
{{
bool M(string a, string b)
{{
// Trivia
bool /* Trivia */ result = /* Trivia */ a.Equals(b, StringComparison.CurrentCultureIgnoreCase) /* Trivia */; // Trivia
if (/* Trivia */ !a.Equals(b, StringComparison.CurrentCultureIgnoreCase) /* Trivia */) // Trivia
return /* Trivia */ !b /* Trivia */ .Equals /* Trivia */ (a, StringComparison.InvariantCultureIgnoreCase) /* Trivia */; // Trivia
return // Trivia
""abc"" /* Trivia */ .Equals /* Trivia */ (a, StringComparison.InvariantCultureIgnoreCase) /* Trivia */; // Trivia
// Trivia
}}
}}";
await VerifyFixCSharpAsync(originalCode, fixedCode);
}

[Theory]
[MemberData(nameof(NoDiagnosticData))]
[InlineData("\"aBc\".CompareTo(null)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ public async Task Diagnostic_Equality_To_Equals(string diagnosedLine, string fix
{
string originalCode = $@"Imports System
Class C
Function GetString() As String
Return ""cde""
End Function
Function M(a As String, b As String) As Boolean
Dim result As Boolean = [|{diagnosedLine}|]
If [|{diagnosedLine}|] Then
Expand All @@ -234,6 +237,9 @@ End Function
End Class";
string fixedCode = $@"Imports System
Class C
Function GetString() As String
Return ""cde""
End Function
Function M(a As String, b As String) As Boolean
Dim result As Boolean = {fixedLine}
If {fixedLine} Then
Expand Down

0 comments on commit 79db421

Please sign in to comment.