|
| 1 | +/* |
| 2 | + * (c) Copyright 2026 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.javaformat.java; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import com.palantir.javaformat.java.JavaFormatterOptions.Style; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests comment rewriting against the comment toks javac produces, without needing the JDK that produces them. From |
| 26 | + * JDK 23 on, a run of {@code ///} markdown lines (JEP 467) arrives as a single comment tok whose lines after the first |
| 27 | + * carry their source indentation; before 23 each {@code //} line is its own tok. Building the tok directly covers the |
| 28 | + * multi-line shape on any JDK. |
| 29 | + */ |
| 30 | +final class JavaCommentsHelperTest { |
| 31 | + |
| 32 | + private static final int COLUMN = 4; |
| 33 | + |
| 34 | + /** Rewrites a comment tok that sits at column {@link #COLUMN}, as javac 23 or later would report it. */ |
| 35 | + private static String rewrite(String commentText, Style style) throws FormatterException { |
| 36 | + JavaFormatterOptions options = |
| 37 | + JavaFormatterOptions.builder().style(style).build(); |
| 38 | + JavaInput.Tok tok = new JavaInput.Tok(0, commentText, commentText, 0, COLUMN, false, null); |
| 39 | + // The helper reads the line separator and the index of the first code token from the input. Any input whose |
| 40 | + // first token has index 0 will do: the tok above then counts as a comment inside the code, which is wrapped. |
| 41 | + JavaInput input = new JavaInput("class T {}\n"); |
| 42 | + return new JavaCommentsHelper(input, options).rewrite(tok, options.maxLineLength(), COLUMN); |
| 43 | + } |
| 44 | + |
| 45 | + /** A run of {@code ///} lines, indented as javac reports it, with {@code body} as the second line's text. */ |
| 46 | + private static String markdownRun(String body) { |
| 47 | + return "/// Summary line.\n" + " ".repeat(COLUMN) + "/// " + body; |
| 48 | + } |
| 49 | + |
| 50 | + private static String wordsOfLength(int length) { |
| 51 | + StringBuilder sb = new StringBuilder(); |
| 52 | + while (sb.length() < length) { |
| 53 | + sb.append(sb.length() == 0 ? "" : " ").append("word"); |
| 54 | + } |
| 55 | + return sb.substring(0, length); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void everyWrappedLineKeepsTheSlashPrefix() throws Exception { |
| 60 | + // Before the fix the prefix was read from the untrimmed continuation line, came back empty, and the |
| 61 | + // overflow was emitted as bare code -- which stops being a comment at all. |
| 62 | + String rewritten = rewrite(markdownRun(wordsOfLength(140) + " @Deprecated"), Style.OJF); |
| 63 | + |
| 64 | + assertThat(rewritten).contains("\n"); |
| 65 | + assertThat(rewritten.lines()).allSatisfy(line -> assertThat(line.trim()).startsWith("///")); |
| 66 | + assertThat(rewritten).doesNotContain("\n @Deprecated"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void aContinuationLineAtTheLimitIsNotWrapped() throws Exception { |
| 71 | + // The line's visual width is COLUMN + its trimmed length, so the budget is maxLineLength - COLUMN. |
| 72 | + int budget = Style.OJF.maxLineLength() - COLUMN; |
| 73 | + String body = wordsOfLength(budget - "/// ".length()); |
| 74 | + |
| 75 | + assertThat(rewrite(markdownRun(body), Style.OJF).lines()).hasSize(2); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void aContinuationLineOverTheLimitIsWrapped() throws Exception { |
| 80 | + int budget = Style.OJF.maxLineLength() - COLUMN; |
| 81 | + String body = wordsOfLength(budget - "/// ".length() + 1); |
| 82 | + |
| 83 | + assertThat(rewrite(markdownRun(body), Style.OJF).lines()).hasSize(3); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void anUnbreakableTokenIsLeftLong() throws Exception { |
| 88 | + // There is nowhere to break, so the line stays over the limit rather than becoming an empty `///` |
| 89 | + // followed by a bare URL. |
| 90 | + String url = "https://example.com/" + "a".repeat(Style.OJF.maxLineLength()); |
| 91 | + |
| 92 | + String rewritten = rewrite(markdownRun(url), Style.OJF); |
| 93 | + |
| 94 | + assertThat(rewritten.lines()).hasSize(2); |
| 95 | + assertThat(rewritten).contains("/// " + url); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void theMissingSpaceRuleAppliesToEveryLineOfTheRun() throws Exception { |
| 100 | + // Otherwise the same source formats differently depending on whether the running JDK hands the run |
| 101 | + // over as one tok (23 and later) or as one tok per line. |
| 102 | + String rewritten = rewrite("///Summary line.\n" + " ".repeat(COLUMN) + "///More text.", Style.OJF); |
| 103 | + |
| 104 | + assertThat(rewritten).isEqualTo("/// Summary line.\n" + " ".repeat(COLUMN) + "/// More text."); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void anOrdinaryLineCommentStillWraps() throws Exception { |
| 109 | + String rewritten = rewrite("// " + wordsOfLength(140), Style.OJF); |
| 110 | + |
| 111 | + assertThat(rewritten.lines()).hasSize(2); |
| 112 | + assertThat(rewritten.lines()).allSatisfy(line -> assertThat(line.trim()).startsWith("// ")); |
| 113 | + } |
| 114 | +} |
0 commit comments