From 4536eac4bc51a36601ec77019c26fa1eac8867e4 Mon Sep 17 00:00:00 2001 From: google-java-format Team Date: Wed, 25 Nov 2026 03:06:31 -0800 Subject: [PATCH] Add baseline tests for Markdown Javadoc tokens spanning newlines. Add tests in `JavadocFormattingTest` and `MarkdownPositionsTest` documenting the current formatting behavior for empty Markdown list items, list items whose content begins on the next line, empty block quotes before fenced code blocks, unclosed HTML tags spanning newlines, and multiline `@param ` / MOE strip comments so the follow-up fix shows the change in test expectations. PiperOrigin-RevId: 1029772356 --- .../java/JavadocFormattingTest.java | 342 ++++++++++++++++++ .../java/javadoc/MarkdownPositionsTest.java | 44 +++ 2 files changed, 386 insertions(+) diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java index 6a5214d2a..338acc0ad 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -3551,6 +3551,348 @@ void a() {} doFormatTest(input, expected); } + @Test + public void markdownEmptyListItemBeforeFencedCodeBlock() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Items: + /// + /// - First item. + /// - + /// + /// ``` + /// Foo x = new Foo(); + /// ``` + void m() {} + } + """; + // Baseline: currently emits an uncommented ``` line (not valid Java, so idempotency fails). + String expected = + """ + class T { + /// Items: + /// + /// - First item. + /// - + ``` + /// Foo x = new Foo(); + /// ``` + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownEmptyListItemBeforeFencedCodeBlockNoBlankLine() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Items: + /// + /// - First item. + /// - + /// ``` + /// Foo x = new Foo(); + /// ``` + void m() {} + } + """; + String expected = + """ + class T { + /// Items: + /// + /// - First item. + /// - + ``` + /// Foo x = new Foo(); + /// ``` + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownEmptyListItemBeforeParagraphAndFooterTag() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// - First item. + /// - + /// + /// Paragraph after. + /// + /// @return value + int m() { + return 0; + } + } + """; + // Baseline: currently splits the comment with an uncommented blank line. + String expected = + """ + class T { + /// - First item. + /// - + + /// + /// Paragraph after. + /// + /// @return value + int m() { + return 0; + } + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownEmptyListItemWithTrailingSpacesAndHtmlBlock() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// - First item. + /// - \s + ///
After.
+ void m() {} + } + """; + // Baseline: merges the following HTML block onto the empty list item line. + String expected = + """ + class T { + /// - First item. + /// -
After.
+ void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownListItemContentOnNextLine() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// - + /// foo + /// - + /// ``` + /// bar(); + /// ``` + /// - + /// - nested + /// - + /// > quote + /// - + /// | a | b | + /// | - | - | + /// | 1 | 2 | + /// -\ttabbed + void m() {} + } + """; + // Baseline: emits uncommented lines when content starts on the line after `-`. + String expected = + """ + class T { + /// - + foo + /// - + ``` + /// bar(); + /// ``` + /// + /// - + + /// - nested + /// - + > quote + /// - + | a | b | + /// | - | - | + /// | 1 | 2 | + /// + /// -\ttabbed + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownFencedCodeBlockInFirstListItem() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// - ``` + /// foo(); + /// ``` + void m() {} + } + """; + // Baseline: breaks the fenced code block onto a new line with a blank line when on line 1 + // (and re-formatting splits the comment on the empty `-` item). + String expected = + """ + class T { + /// - + /// + /// ``` + /// foo(); + /// ``` + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownEmptyBlockQuoteBeforeFencedCodeBlock() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Summary. + /// + /// > + /// ``` + /// foo(); + /// ``` + void m() {} + } + """; + // Baseline: merges the fenced code block opening fence onto the empty `>` line. + String expected = + """ + class T { + /// Summary. + /// + /// > ``` + /// foo(); + /// ``` + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownUnclosedHtmlTagAcrossNewlines() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + ///
+          /// foo();
+          /// 
+ /// After. + void m() {} + } + """; + // Baseline: currently emits uncommented

outside the comment. + String expected = + """ + class T { + ///

+          /// foo();
+          /// 
+ /// + /// After. + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownUnclosedOpenTagAcrossNewlines() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Summary. + /// + ///

After. + void m() {} + } + """; + // Baseline: currently emits uncommented After. outside the comment. + String expected = + """ + class T { + /// Summary. + /// + ///

After. + void m() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownMultilineFooterTagAndMoeComment() throws Exception { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Summary. + /// + /// + /// Internal. + /// + /// + /// @param + /// description + void m() {} + } + """ + .replace("moe", "MOE"); + // Baseline: emits uncommented lines for multiline MOE comments and multiline `@param\n`. + String expected = + """ + class T { + /// Summary. + /// + /// + /// Internal. + /// + /// + /// @param + description + void m() {} + } + """ + .replace("moe", "MOE"); + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + // TODO: b/346668798 - Test the following Markdown constructs, and make the tests work as needed. // We can assume that the CommonMark parser correctly handles Markdown, so the question is whether // they are subsequently mishandled by our formatting logic. So for example the CommonMark parser diff --git a/core/src/test/java/com/google/googlejavaformat/java/javadoc/MarkdownPositionsTest.java b/core/src/test/java/com/google/googlejavaformat/java/javadoc/MarkdownPositionsTest.java index 22dd5a13b..3b9293469 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/javadoc/MarkdownPositionsTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/javadoc/MarkdownPositionsTest.java @@ -316,6 +316,50 @@ public void paragraphAfterHtmlCommentWithBlankLine() { assertThat(map).isEqualTo(expected); } + @Test + public void emptyListItemBeforeBlankLine() { + String text = + """ + - + + foo + """; + var positions = MarkdownPositions.parse(text); + ImmutableListMultimap map = positionToToken(positions, text); + int fooStart = text.indexOf("foo"); + int fooEnd = fooStart + "foo".length(); + ImmutableListMultimap expected = + ImmutableListMultimap.builder() + .put(0, new ListOpenTag("")) + .put(0, new ListItemOpenTag("-\n")) + .put(1, new ListItemCloseTag("")) + .put(1, new ListCloseTag("")) + .put(fooStart, new ParagraphOpenTag("")) + .put(fooEnd, new ParagraphCloseTag("")) + .build(); + assertThat(map).isEqualTo(expected); + } + + @Test + public void listItemContentOnNextLine() { + String text = + """ + - + foo + """; + var positions = MarkdownPositions.parse(text); + ImmutableListMultimap map = positionToToken(positions, text); + int end = text.length() - 1; + ImmutableListMultimap expected = + ImmutableListMultimap.builder() + .put(0, new ListOpenTag("")) + .put(0, new ListItemOpenTag("-\n")) + .put(end, new ListItemCloseTag("")) + .put(end, new ListCloseTag("")) + .build(); + assertThat(map).isEqualTo(expected); + } + private static ImmutableListMultimap positionToToken( MarkdownPositions positions, String input) { return IntStream.rangeClosed(0, input.length())