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

Partial properties: misc compiler layer work #73527

Merged
Prev Previous commit
Next Next commit
Test doc comment cases for decl without impl
  • Loading branch information
RikkiGibson committed May 20, 2024
commit 5912984471f47e24d4d90b4b71403763aa09137e
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ partial class C
[Fact]
public void PartialMethod_NoImplementation()
{
// Whole document XML does not include the member, but single symbol XML does include it
var source = @"
partial class C
{
Expand All @@ -923,20 +924,25 @@ partial class C
";

var tree = SyntaxFactory.ParseSyntaxTree(source, options: TestOptions.RegularWithDocumentationComments);

var comp = CreateCompilation(tree, assemblyName: "Test");
var actual = GetDocumentationCommentText(comp);
var expected = @"
<?xml version=""1.0""?>
<doc>
<assembly>
<name>Test</name>
</assembly>
<members>
</members>
</doc>
".Trim();
Assert.Equal(expected, actual);
var method = comp.GlobalNamespace.GetMember<MethodSymbol>("C.M");

AssertEx.AssertLinesEqual(expected: """
<?xml version="1.0"?>
<doc>
<assembly>
<name>Test</name>
</assembly>
<members>
</members>
</doc>
""", actual: GetDocumentationCommentText(comp));

AssertEx.AssertLinesEqual("""
<member name="M:C.M">
<summary>Summary 2</summary>
</member>
""", DocumentationCommentCompiler.GetDocumentationCommentXml(method, processIncludes: true, cancellationToken: default));
}

[Fact]
Expand Down Expand Up @@ -1490,6 +1496,41 @@ void verify(CSharpTestSource source)
}
}

/// <summary>Counterpart to <see cref="PartialMethod_NoImplementation"/>.</summary>
[Fact]
public void PartialProperty_NoImplementation()
{
// Whole document XML does not include the member, but single symbol XML does include it
var source = @"
partial class C
{
/** <summary>Summary 2</summary>*/
public partial int P { get; set; }
}
";

var tree = SyntaxFactory.ParseSyntaxTree(source, options: TestOptions.RegularWithDocumentationComments);
var comp = CreateCompilation(tree, assemblyName: "Test");
var property = comp.GlobalNamespace.GetMember<PropertySymbol>("C.P");

AssertEx.AssertLinesEqual(expected: """
<?xml version="1.0"?>
<doc>
<assembly>
<name>Test</name>
</assembly>
<members>
</members>
</doc>
""", actual: GetDocumentationCommentText(comp));

AssertEx.AssertLinesEqual("""
<member name="P:C.P">
<summary>Summary 2</summary>
</member>
""", DocumentationCommentCompiler.GetDocumentationCommentXml(property, processIncludes: true, cancellationToken: default));
}

/// <summary>Counterpart to <see cref="ExtendedPartialMethods_MultipleFiles"/>.</summary>
[Fact]
public void PartialProperties_MultipleFiles()
Expand Down Expand Up @@ -1592,6 +1633,43 @@ public partial class C
Assert.Equal(expected, actualB);
}

/// <summary>Counterpart to <see cref="PartialMethod_NoImplementation"/>.</summary>
[Fact]
public void PartialIndexer_NoImplementation()
{
// Whole document XML does not include the member, but single symbol XML does include it
var source = """
partial class C
{
/// <summary>Summary 2</summary>
/// <param name="p">My param</param>
public partial int this[int p] { get; set; }
}
""";

var tree = SyntaxFactory.ParseSyntaxTree(source, options: TestOptions.RegularWithDocumentationComments);
var comp = CreateCompilation(tree, assemblyName: "Test");
var property = comp.GlobalNamespace.GetMember<NamedTypeSymbol>("C").Indexers.Single();

AssertEx.AssertLinesEqual(expected: """
<?xml version="1.0"?>
<doc>
<assembly>
<name>Test</name>
</assembly>
<members>
</members>
</doc>
""", actual: GetDocumentationCommentText(comp));

AssertEx.AssertLinesEqual("""
<member name="P:C.Item(System.Int32)">
<summary>Summary 2</summary>
<param name="p">My param</param>
</member>
""", DocumentationCommentCompiler.GetDocumentationCommentXml(property, processIncludes: true, cancellationToken: default));
}

/// <summary>Counterpart to <see cref="ExtendedPartialMethods_MultipleFiles_DefinitionComment"/>.</summary>
[Fact]
public void PartialProperties_MultipleFiles_DefinitionComment()
Expand Down
14 changes: 11 additions & 3 deletions src/Compilers/Test/Core/Assert/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,19 @@ private sealed class LineComparer : IEqualityComparer<string>
public int GetHashCode(string str) => str.Trim().GetHashCode();
}

public static void AssertLinesEqual(string expected, string actual, string message, string expectedValueSourcePath, int expectedValueSourceLine, bool escapeQuotes)
{
IEnumerable<string> GetLines(string str) =>
private static IEnumerable<string> GetLines(string str) =>
str.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

public static void AssertLinesEqual(string expected, string actual)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context. Something about my testing workflow was causing trailing whitespace on lines of the test to be lost, but our asserts were sensitive to that whitespace. I was finding it really frustrating to dig up that whitespace (basically had to go into the debugger and poke at the actual documentation string) so went ahead and started using a whitespace-insensitive comparison (though still line-sensitive) for a few of the tests.

{
AssertEx.Equal(
GetLines(expected),
GetLines(actual),
comparer: LineComparer.Instance);
}

public static void AssertLinesEqual(string expected, string actual, string message, string expectedValueSourcePath, int expectedValueSourceLine, bool escapeQuotes)
{
AssertEx.Equal(
GetLines(expected),
GetLines(actual),
Expand Down