Skip to content

Commit

Permalink
Move more rude edit analysis to semantics (#54576)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Jul 11, 2021
1 parent 7b4b65c commit 0f4736a
Show file tree
Hide file tree
Showing 29 changed files with 1,840 additions and 1,198 deletions.
44 changes: 20 additions & 24 deletions src/Compilers/Test/Core/Assert/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,7 @@ public static void Equal<T>(
return;
}

string assertMessage = GetAssertMessage(expected, actual, comparer, itemInspector, itemSeparator, expectedValueSourcePath, expectedValueSourceLine);

if (message != null)
{
assertMessage = message + "\r\n" + assertMessage;
}

Assert.True(false, assertMessage);
Assert.True(false, GetAssertMessage(expected, actual, comparer, message, itemInspector, itemSeparator, expectedValueSourcePath, expectedValueSourceLine));
}

/// <summary>
Expand Down Expand Up @@ -434,14 +427,10 @@ public static void SetEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, I
var result = expected.Count() == actual.Count() && expectedSet.SetEquals(actual);
if (!result)
{
if (string.IsNullOrEmpty(message))
{
message = GetAssertMessage(
ToString(expected, itemSeparator, itemInspector),
ToString(actual, itemSeparator, itemInspector));
}

Assert.True(result, message);
Assert.True(result, GetAssertMessage(
ToString(expected, itemSeparator, itemInspector),
ToString(actual, itemSeparator, itemInspector),
prefix: message));
}
}

Expand Down Expand Up @@ -523,6 +512,7 @@ public static void NotNull<T>(T @object, string message = null)
public static void AssertEqualToleratingWhitespaceDifferences(
string expected,
string actual,
string message = null,
bool escapeQuotes = true,
[CallerFilePath] string expectedValueSourcePath = null,
[CallerLineNumber] int expectedValueSourceLine = 0)
Expand All @@ -532,7 +522,7 @@ public static void AssertEqualToleratingWhitespaceDifferences(

if (normalizedExpected != normalizedActual)
{
Assert.True(false, GetAssertMessage(expected, actual, escapeQuotes, expectedValueSourcePath, expectedValueSourceLine));
Assert.True(false, GetAssertMessage(expected, actual, message, escapeQuotes, expectedValueSourcePath, expectedValueSourceLine));
}
}

Expand Down Expand Up @@ -597,15 +587,13 @@ internal static string NormalizeWhitespace(string input)
return output.ToString();
}

public static string GetAssertMessage(string expected, string actual, bool escapeQuotes = false, string expectedValueSourcePath = null, int expectedValueSourceLine = 0)
{
return GetAssertMessage(DiffUtil.Lines(expected), DiffUtil.Lines(actual), escapeQuotes, expectedValueSourcePath, expectedValueSourceLine);
}
public static string GetAssertMessage(string expected, string actual, string prefix = null, bool escapeQuotes = false, string expectedValueSourcePath = null, int expectedValueSourceLine = 0)
=> GetAssertMessage(DiffUtil.Lines(expected), DiffUtil.Lines(actual), prefix, escapeQuotes, expectedValueSourcePath, expectedValueSourceLine);

public static string GetAssertMessage<T>(IEnumerable<T> expected, IEnumerable<T> actual, bool escapeQuotes, string expectedValueSourcePath = null, int expectedValueSourceLine = 0)
public static string GetAssertMessage<T>(IEnumerable<T> expected, IEnumerable<T> actual, string prefix = null, bool escapeQuotes = false, string expectedValueSourcePath = null, int expectedValueSourceLine = 0)
{
Func<T, string> itemInspector = escapeQuotes ? new Func<T, string>(t => t.ToString().Replace("\"", "\"\"")) : null;
return GetAssertMessage(expected, actual, itemInspector: itemInspector, itemSeparator: "\r\n", expectedValueSourcePath: expectedValueSourcePath, expectedValueSourceLine: expectedValueSourceLine);
return GetAssertMessage(expected, actual, prefix: prefix, itemInspector: itemInspector, itemSeparator: "\r\n", expectedValueSourcePath: expectedValueSourcePath, expectedValueSourceLine: expectedValueSourceLine);
}

private static readonly string s_diffToolPath = Environment.GetEnvironmentVariable("ROSLYN_DIFFTOOL");
Expand All @@ -614,6 +602,7 @@ public static string GetAssertMessage<T>(
IEnumerable<T> expected,
IEnumerable<T> actual,
IEqualityComparer<T> comparer = null,
string prefix = null,
Func<T, string> itemInspector = null,
string itemSeparator = null,
string expectedValueSourcePath = null,
Expand Down Expand Up @@ -647,13 +636,20 @@ public static string GetAssertMessage<T>(
var actualString = string.Join(itemSeparator, actual.Select(itemInspector));

var message = new StringBuilder();
message.AppendLine();

if (!string.IsNullOrEmpty(prefix))
{
message.AppendLine(prefix);
message.AppendLine();
}

message.AppendLine("Expected:");
message.AppendLine(expectedString);
if (expected.Count() > 10)
{
message.AppendLine("... truncated ...");
}

message.AppendLine("Actual:");
message.AppendLine(actualString);
message.AppendLine("Differences:");
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Test/Core/CompilationVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private CompilationVerifier VerifyILImpl(
string source = null)
{
string actualIL = VisualizeIL(qualifiedMethodName, realIL, sequencePoints, source);
AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL, escapeQuotes, callerPath, callerLine);
AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL, message: null, escapeQuotes, callerPath, callerLine);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void Main(string[] args)
{
DocumentResults(
active,
diagnostics: new[] { Diagnostic(RudeEditKind.Delete, "class C", DeletedSymbolDisplay(FeaturesResources.method, "Goo(int)")) })
diagnostics: new[] { Diagnostic(RudeEditKind.Delete, "class C", DeletedSymbolDisplay(FeaturesResources.method, "Goo(int a)")) })
});
}

Expand Down Expand Up @@ -661,7 +661,7 @@ public void Indexer_BlockBodyToExpressionBody2()
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.Delete, "int this[int a]", DeletedSymbolDisplay(CSharpFeaturesResources.indexer_setter, "this[int].set")));
Diagnostic(RudeEditKind.Delete, "int this[int a]", DeletedSymbolDisplay(CSharpFeaturesResources.indexer_setter, "this[int a].set")));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7059,7 +7059,8 @@ public void LocalFunction_ReadOnlyRef_ReturnType_Update()
edits.VerifyEdits(
"Update [void M() { int local() { throw null; } }]@13 -> [void M() { ref readonly int local() { throw null; } }]@13");

edits.VerifyRudeDiagnostics();
edits.VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.ChangingLambdaReturnType, "local", CSharpFeaturesResources.local_function));
}

[WorkItem(37128, "https://github.com/dotnet/roslyn/issues/37128")]
Expand Down Expand Up @@ -7436,7 +7437,7 @@ public void LocalFunctions_TypeParameter_Insert1()
"Insert [A]@9");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Insert, "<A>", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7451,7 +7452,7 @@ public void LocalFunctions_TypeParameter_Insert2()
"Insert [B]@11");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Insert, "B", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7466,7 +7467,7 @@ public void LocalFunctions_TypeParameter_Delete1()
"Delete [A]@9");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Delete, "L", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7481,7 +7482,7 @@ public void LocalFunctions_TypeParameter_Delete2()
"Delete [A]@9");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Delete, "L", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7495,7 +7496,61 @@ public void LocalFunctions_TypeParameter_Update()
"Update [A]@9 -> [B]@9");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Renamed, "B", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Theory]
[InlineData("Enum", "Delegate")]
[InlineData("IDisposable", "IDisposable, new()")]
public void LocalFunctions_TypeParameter_Constraint_Clause_Update(string oldConstraint, string newConstraint)
{
var src1 = "void L<A>() where A : " + oldConstraint + " {}";
var src2 = "void L<A>() where A : " + newConstraint + " {}";

var edits = GetMethodEdits(src1, src2);

edits.VerifyEdits(
"Update [where A : " + oldConstraint + "]@14 -> [where A : " + newConstraint + "]@14");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Theory]
[InlineData("nonnull")]
[InlineData("struct")]
[InlineData("class")]
[InlineData("new()")]
[InlineData("unmanaged")]
[InlineData("System.IDisposable")]
[InlineData("System.Delegate")]
public void LocalFunctions_TypeParameter_Constraint_Clause_Delete(string oldConstraint)
{
var src1 = "void L<A>() where A : " + oldConstraint + " {}";
var src2 = "void L<A>() {}";

var edits = GetMethodEdits(src1, src2);

edits.VerifyEdits(
"Delete [where A : " + oldConstraint + "]@14");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
public void LocalFunctions_TypeParameter_Constraint_Clause_Add()
{
var src1 = "void L<A,B>() where A : new() {}";
var src2 = "void L<A,B>() where A : new() where B : System.IDisposable {}";

var edits = GetMethodEdits(src1, src2);

edits.VerifyEdits(
"Insert [where B : System.IDisposable]@32");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7509,7 +7564,8 @@ public void LocalFunctions_TypeParameter_Reorder()
"Reorder [B]@11 -> @9");

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Move, "B", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.Move, "B", FeaturesResources.type_parameter),
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

[Fact]
Expand All @@ -7525,7 +7581,7 @@ public void LocalFunctions_TypeParameter_ReorderAndUpdate()

GetTopEdits(edits).VerifyRudeDiagnostics(
Diagnostic(RudeEditKind.Move, "B", FeaturesResources.type_parameter),
Diagnostic(RudeEditKind.Renamed, "C", FeaturesResources.type_parameter));
Diagnostic(RudeEditKind.ChangingTypeParameters, "L", FeaturesResources.local_function));
}

#endregion
Expand Down
Loading

0 comments on commit 0f4736a

Please sign in to comment.