Properly handle multi-line comments split with the newline character (#17)

* Properly handle multi-line comments split with the newline character
* Don't discard trailing newlines
* Add unit tests
dev
Pierce Thompson 2 years ago committed by GitHub
parent 26dfd4d6b5
commit ce87129094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -95,11 +95,13 @@ final class CommentNodeExtractor {
final Deque<String> elementNameStack
) {
if (element.isAnnotationPresent(Comment.class)) {
final var comments = element.getAnnotation(Comment.class).value();
final var comments = Arrays.stream(element.getAnnotation(Comment.class).value())
.flatMap(s -> Arrays.stream(s.split("\n", -1)))
.toList();
final var formattedName = nameFormatter.format(elementName);
final var elementNames = new ArrayList<>(elementNameStack);
elementNames.add(formattedName);
final var result = new CommentNode(Arrays.asList(comments), elementNames);
final var result = new CommentNode(comments, elementNames);
return Optional.of(result);
}
return Optional.empty();

@ -70,6 +70,88 @@ class CommentNodeExtractorTest {
assertTrue(nodes.isEmpty());
}
@Test
void extractSingleCommentMultipleLines() {
@Configuration
class A {
@Comment("""
Hello
World""")
int i;
}
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
assertEquals(cn(List.of("Hello", "World"), "i"), nodes.poll());
assertTrue(nodes.isEmpty());
}
@Test
void extractSingleCommentMultipleLinesTrailingNewlines() {
@Configuration
class A {
@Comment("""
Hello
World
""")
int i;
}
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
assertEquals(cn(List.of("", "", "Hello", "", "World", "", "", ""), "i"), nodes.poll());
assertTrue(nodes.isEmpty());
}
@Test
void extractSingleCommentMultipleLinesInArrayAndNewlineSplit() {
@Configuration
class A {
@Comment({ """
Hello
World""", """
Hi
Again""" })
int i;
}
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
assertEquals(cn(List.of("Hello", "World", "Hi", "Again"), "i"), nodes.poll());
assertTrue(nodes.isEmpty());
}
@Test
void extractSingleCommentMultipleLinesInArrayAndNewlineSplitWithTrailingNewlines() {
@Configuration
class A {
@Comment({ """
Hello
World
""", """
Hi
Again
""" })
int i;
}
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
assertEquals(cn(List.of("", "Hello", "World", "", "", "", "Hi", "Again", "", ""), "i"), nodes.poll());
assertTrue(nodes.isEmpty());
}
@Test
void extractMultipleComments() {
@Configuration

Loading…
Cancel
Save