|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.palantir.javaformat.java; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import com.google.common.collect.Range; |
| 20 | +import com.palantir.javaformat.Utils; |
| 21 | +import com.palantir.javaformat.java.SnippetFormatter.SnippetKind; |
| 22 | +import java.util.List; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +final class SnippetFormatterTest { |
| 26 | + private static final String LONG_STRINGS = """ |
| 27 | + class S { |
| 28 | + String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed."; |
| 29 | +
|
| 30 | + String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna."; |
| 31 | + } |
| 32 | + """; |
| 33 | + |
| 34 | + // The editor's Format: a compilation unit with its comments. |
| 35 | + @Test |
| 36 | + void reflowsTheLongStringOfTheRangeOnly() throws FormatterException { |
| 37 | + int start = LONG_STRINGS.indexOf(" String changed"); |
| 38 | + Range<Integer> changed = Range.closedOpen(start, LONG_STRINGS.indexOf('\n', start)); |
| 39 | + |
| 40 | + List<Replacement> replacements = new SnippetFormatter() |
| 41 | + .format(SnippetKind.COMPILATION_UNIT, LONG_STRINGS, List.of(changed), 0, /* includeComments= */ true); |
| 42 | + |
| 43 | + assertThat(Utils.applyReplacements(LONG_STRINGS, replacements)).isEqualTo(""" |
| 44 | + class S { |
| 45 | + String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed."; |
| 46 | +
|
| 47 | + String changed = |
| 48 | + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et" |
| 49 | + + " dolore magna."; |
| 50 | + } |
| 51 | + """); |
| 52 | + } |
| 53 | + |
| 54 | + // Snippets, which Eclipse formats when it inserts code, are wrapped in a class and may only change whitespace. |
| 55 | + @Test |
| 56 | + void leavesTheLongStringOfAStatementSnippet() throws FormatterException { |
| 57 | + String literal = "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" |
| 58 | + + " ut labore et dolore magna.\""; |
| 59 | + String statement = "String s = " + literal + ";"; |
| 60 | + |
| 61 | + // Two levels: the statement sits in a block of the dummy class, as in a method's body. |
| 62 | + List<Replacement> replacements = new SnippetFormatter() |
| 63 | + .format( |
| 64 | + SnippetKind.STATEMENTS, |
| 65 | + statement, |
| 66 | + List.of(Range.closedOpen(0, statement.length())), |
| 67 | + 2, |
| 68 | + /* includeComments= */ false); |
| 69 | + |
| 70 | + assertThat(Utils.applyReplacements(statement, replacements)).contains(literal); |
| 71 | + } |
| 72 | +} |
0 commit comments