From 2910db0e645b5367bce875bdae68e6b4797998d2 Mon Sep 17 00:00:00 2001 From: google-java-format Team Date: Tue, 24 Nov 2026 02:17:38 -0800 Subject: [PATCH] Add baseline tests for wrapping Markdown Javadoc before block markers. Add tests in `JavadocFormattingTest` documenting the current line-wrapping behavior when a Markdown Javadoc paragraph wraps right before CommonMark block markers (such as `-`, `+`, `*`, `>`, `#`, `1.`, `1)`, `=`, `---`, `~~~`, or HTML block openers) so the follow-up fix shows the change in test expectations. PiperOrigin-RevId: 1028869375 --- .../java/JavadocFormattingTest.java | 422 ++++++++++++++++++ 1 file changed, 422 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 a777c2baf..b965344cf 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -2890,6 +2890,428 @@ class Test {} doFormatTest(input, expected); } + @Test + public void markdownWrapBeforeBulletListMarkers() { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the number of elements in the given range of the array, which is always equal to `end - start`. + void a() {} + + /// Implementations can implement any variation of `#transform()` and must override only a single - depending on config. + void b() {} + + /// @param activeCount the 0-based index of the last active id (total active items is `activeCount + 1`) + void c(int activeCount) {} + + /// @return a result proto containing all elements for the corresponding namespace and the corpus + keyPrefix + void d() {} + + /// Computes the total area of the rectangle, which is always guaranteed to be equal to `widthVal * height`. + void e() {} + + /// - Returns the number of elements in the given range of an array, which is always equal to `end - start`. + void f() {} + } + """; + // TODO(b/587865985): Wrapping before -, +, or * turns the continuation line into a bullet list. + String expected = + """ + class T { + /// Returns the number of elements in the given range of the array, which is always equal to `end + /// - start`. + void a() {} + + /// Implementations can implement any variation of `#transform()` and must override only a single + /// - depending on config. + void b() {} + + /// @param activeCount the 0-based index of the last active id (total active items is `activeCount + /// + 1`) + void c(int activeCount) {} + + /// @return a result proto containing all elements for the corresponding namespace and the corpus + /// + keyPrefix + void d() {} + + /// Computes the total area of the rectangle, which is always guaranteed to be equal to `widthVal + /// * height`. + void e() {} + + /// - Returns the number of elements in the given range of an array, which is always equal to `end + /// - start`. + void f() {} + } + """; + doFormatTest(input, expected); + } + + @Test + public void markdownWrapBeforeBlockQuoteMarker() throws FormatterException { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start > end`. + void a() {} + + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start >= end`. + void b() {} + + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start >` end. + void c() {} + + /// @throws IndexOutOfBoundsException if `fromIndex < 0`, `toIndex > array.length`, or `fromIndex > toIndex` + void d() {} + + /// > Throws an exception if the requested indices are out of bounds for an array, i.e., if `start > end`. + void e() {} + } + """; + // TODO(b/587865985): Wrapping before > or >= turns the continuation line into a block quote + // (and formatting a second time corrupts >= into > = and >` into > `). + String expected = + """ + class T { + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start + /// > end`. + void a() {} + + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start + /// >= end`. + void b() {} + + /// Throws an exception if the requested indices are out of bounds for the array, i.e., if `start + /// >` end. + void c() {} + + /// @throws IndexOutOfBoundsException if `fromIndex < 0`, `toIndex > array.length`, or `fromIndex + /// > toIndex` + void d() {} + + /// > Throws an exception if the requested indices are out of bounds for an array, i.e., if `start + /// > > end`. + void e() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownWrapBeforeHeadingAndOrderedListMarkers() throws FormatterException { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the formatted identifier for the element, which is always prefixed by the section mark # 1 in output. + void a() {} + + /// Returns the formatted identifier for the element, which is always prefixed by the heading tag ### title here. + void b() {} + + /// When validating the configured options in order, the processor always checks the first option 1. first here. + void c() {} + + /// When validating the configured options in order, the processor always checks the first option 1) first here. + void d() {} + + /// When validating the configured options in order, the processor always checks the first item 01. first here. + void e() {} + } + """; + // TODO(b/587865985): Wrapping before #, ###, 1., 1), or 01. turns the continuation line into a + // heading or ordered list (and is not even idempotent for headings). + String expected = + """ + class T { + /// Returns the formatted identifier for the element, which is always prefixed by the section mark + /// # 1 in output. + void a() {} + + /// Returns the formatted identifier for the element, which is always prefixed by the heading tag + /// ### title here. + void b() {} + + /// When validating the configured options in order, the processor always checks the first option + /// 1. first here. + void c() {} + + /// When validating the configured options in order, the processor always checks the first option + /// 1) first here. + void d() {} + + /// When validating the configured options in order, the processor always checks the first item + /// 01. first here. + void e() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownWrapBeforeSetextHeadingAndThematicBreakMarkers() throws FormatterException { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns true if the computed value of the first argument equals the second argument when value = + void a() {} + + /// Returns true if the computed value of the first argument equals the second argument when val == veryLongIdentifierNameThatExceedsTheRemainingLineLengthAndWrapsOntoItsOwnLineForTestingCase12345 + void b() {} + + /// Separates the header section from the body section using a standard marker token sequence mark --- + void c() {} + + /// Separates the header section from the body section using a standard marker token sequence mark *** + void d() {} + + /// Separates the header section from the body section using a standard marker token sequence mark ___ + void e() {} + + /// Separates a header | column from a body section using a standard marker token sequence pattern |-| + void f() {} + } + """; + // TODO(b/587865985): Wrapping before =, ==, ---, ***, ___, or |-| turns the line into a setext + // heading underline, thematic break, or GFM table separator. + String expected = + """ + class T { + /// Returns true if the computed value of the first argument equals the second argument when value + /// = + void a() {} + + /// Returns true if the computed value of the first argument equals the second argument when val + /// == + /// veryLongIdentifierNameThatExceedsTheRemainingLineLengthAndWrapsOntoItsOwnLineForTestingCase12345 + void b() {} + + /// Separates the header section from the body section using a standard marker token sequence mark + /// --- + void c() {} + + /// Separates the header section from the body section using a standard marker token sequence mark + /// *** + void d() {} + + /// Separates the header section from the body section using a standard marker token sequence mark + /// ___ + void e() {} + + /// Separates a header | column from a body section using a standard marker token sequence pattern + /// |-| + void f() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownWrapBeforeFencedCodeAndHtmlBlockMarkers() throws FormatterException { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the delimiter string used for separating fenced sections in the output, such as `a val ~~~ b`. + void a() {} + + /// Returns the delimiter string used for separating fenced sections in the output, such as ````a ``` b````. + void b() {} + + /// Returns a formatted snippet showing the nested backtick syntax inside a code span: ```` `foo` ````. + void c() {} + + /// Returns an HTML fragment representing the rendered widget content, such as `foo bar baz value
bar
`. + void d() {} + + /// Returns an HTML fragment representing the rendered widget content, such as `foo bar baz value

bar

`. + void e() {} + + /// Returns an HTML fragment representing the rendered widget content, such as `foo bar baz value `. + void f() {} + + /// Use this helper method when rendering an inline element rather than a full standalone block
element. + void g() {} + } + """; + // TODO(b/587865985): Wrapping before ~~~, ```, ````,
,

, or `. + void f() {} + + /// Use this helper method when rendering an inline element rather than a full standalone block + ///

element. + void g() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownWrapBeforeBlockMarkerAfterHtmlCodeTag() { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the number of elements in the full range of an array, always equal to end - start. + void a() {} + } + """; + // TODO(b/587865985): Wrapping before - after turns the continuation line into a list. + String expected = + """ + class T { + /// Returns the number of elements in the full range of an array, always equal to end + /// - start. + void a() {} + } + """; + doFormatTest(input, expected); + } + + @Test + public void markdownWrapMultipleConsecutiveBlockMarkers() throws FormatterException { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the combined result of evaluating the sequence of operators in the given expression `a - + > b`. + void a() {} + } + """; + // TODO(b/587865985): None of the consecutive block markers should start a wrapped line (and + // formatting a second time splits - and + into nested lists). + String expected = + """ + class T { + /// Returns the combined result of evaluating the sequence of operators in the given expression `a + /// - + > b`. + void a() {} + } + """; + assertThat(formatter.formatSource(input)).isEqualTo(expected); + } + + @Test + public void markdownWrapAllowsNonBlockMarkers() { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = + """ + class T { + /// Returns the index of the first occurrence of the specified target element, or returns a value -1 if absent. + void a() {} + + /// Returns the index of the first occurrence of the specified target element, or adds an offset +1 if present. + void b() {} + + /// Returns the number of elements in the range when the maximum allowed capacity is capped up to 2. Next sentence. + void c() {} + + /// Returns the number of elements in the range when the maximum allowed capacity is set up to 100. Next line. + void d() {} + + /// Returns the formatted identifier for the element, which is described in detail at the link #method() below. + void e() {} + + /// Returns the value produced by applying the mapping function from the given input argument `x -> y` in the stream. + void f() {} + + /// Returns an inline formatted element rendered inside the enclosing paragraph using a simple tag here. + void g() {} + + /// Returns the formatted output containing an escaped hyphen marker written explicitly with char \\- in the prose. + void h() {} + } + """; + String expected = + """ + class T { + /// Returns the index of the first occurrence of the specified target element, or returns a value + /// -1 if absent. + void a() {} + + /// Returns the index of the first occurrence of the specified target element, or adds an offset + /// +1 if present. + void b() {} + + /// Returns the number of elements in the range when the maximum allowed capacity is capped up to + /// 2. Next sentence. + void c() {} + + /// Returns the number of elements in the range when the maximum allowed capacity is set up to + /// 100. Next line. + void d() {} + + /// Returns the formatted identifier for the element, which is described in detail at the link + /// #method() below. + void e() {} + + /// Returns the value produced by applying the mapping function from the given input argument `x + /// -> y` in the stream. + void f() {} + + /// Returns an inline formatted element rendered inside the enclosing paragraph using a simple + /// tag here. + void g() {} + + /// Returns the formatted output containing an escaped hyphen marker written explicitly with char + /// \\- in the prose. + void h() {} + } + """; + doFormatTest(input, expected); + } + + @Test + public void classicJavadocWrapBeforeMarkdownMarkersUnchanged() { + String input = + """ + class T { + /** + * Returns the number of elements in the given range of the array, which is equal to {@code endIdx - start}. + */ + void a() {} + } + """; + String expected = + """ + class T { + /** + * Returns the number of elements in the given range of the array, which is equal to {@code endIdx + * - start}. + */ + void a() {} + } + """; + doFormatTest(input, 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