Skip to content
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
64 changes: 64 additions & 0 deletions JavaToCSharp.Tests/CommentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,68 @@ public class Foo

Assert.Equal(expected.ReplaceLineEndings(), parsed.ReplaceLineEndings());
}

[Theory]
[InlineData("Child", "Child")]
[InlineData("Child extends Parent", "Child : Parent")]
[InlineData("Child implements Parent", "Child : Parent")]
[InlineData("Child extends Parent implements IParent", "Child : Parent, IParent")]

[InlineData("Parent<T>", "Parent<T>")]
[InlineData("Child<T extends BoundType<T>>", "Child<T>")] // issue #125, should add: where T : BoundType<T>
[InlineData("Child extends Parent<BoundType>", "Child : Parent<BoundType>")]
public void CommentsInsideClass_ShouldNotBeDuplicated_Fix_88(string javaClass, string csharpClass)
{
string javaCode = $$"""
//class comment
public class {{javaClass}} {
//before comment 1
public void method1() {
doSomething(); //after comment1
}
//before comment 2
public void method1() {
doSomething(); //after comment2
}
//before comment 3
public void method1() {
}
}
""";
var options = new JavaConversionOptions
{
IncludeUsings = false,
IncludeNamespace = false,
};

var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";

testOutputHelper.WriteLine(parsed);

string expected = $$"""
//class comment
public class {{csharpClass}}
{
//before comment 1
public virtual void Method1()
{
DoSomething(); //after comment1
}

//before comment 2
public virtual void Method1()
{
DoSomething(); //after comment2
}

//before comment 3
public virtual void Method1()
{
}
}

""";

Assert.Equal(expected.ReplaceLineEndings(), parsed.ReplaceLineEndings());
}
}
1 change: 1 addition & 0 deletions JavaToCSharp/CommentsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private static bool HasNextSibling(JavaAst.Node parentNode, JavaParser.Position
return parentNode.getChildNodes()
.OfType<JavaAst.Node>()
.Where(sibling => sibling is not JavaComments.Comment)
.OrderBy(sibling => sibling.getEnd().FromOptional<JavaParser.Position>()) // fix #88
.LastOrDefault(sibling =>
{
var siblingEnd = sibling.getEnd().FromOptional<JavaParser.Position>();
Expand Down