|
| 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 java.nio.charset.StandardCharsets.UTF_8; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 23 | +import com.fasterxml.jackson.datatype.guava.GuavaModule; |
| 24 | +import com.palantir.javaformat.Utils; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.PrintWriter; |
| 27 | +import java.io.StringWriter; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import org.junit.jupiter.api.DynamicTest; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.parallel.Execution; |
| 34 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 35 | + |
| 36 | +/** |
| 37 | + * The command line with {@code --lines}, {@code --offset} or {@code --character-ranges} prints what applying its |
| 38 | + * {@code --output-replacements} for the same ranges gives: a part of the file formatted, the imports and the rest of the |
| 39 | + * file as they are. Each JDK has a class that runs the case folders it can parse, such as |
| 40 | + * {@link CommandLineRangesJava21Test}. |
| 41 | + */ |
| 42 | +@Execution(ExecutionMode.CONCURRENT) |
| 43 | +final class CommandLineRangesTest { |
| 44 | + private static final ObjectMapper MAPPER = |
| 45 | + JsonMapper.builder().addModule(new GuavaModule()).build(); |
| 46 | + |
| 47 | + /** The tests of the case files under {@code folders} on the test classpath. */ |
| 48 | + static List<DynamicTest> changedLinesIn(String... folders) throws Exception { |
| 49 | + var tests = new ArrayList<DynamicTest>(); |
| 50 | + |
| 51 | + for (var folder : folders) { |
| 52 | + var root = CaseFile.folder(folder); |
| 53 | + |
| 54 | + for (var path : CaseFile.pathsIn(root)) { |
| 55 | + tests.add(DynamicTest.dynamicTest(path, () -> { |
| 56 | + var testCase = CaseFile.parse(path, Files.readString(root.resolve(path))); |
| 57 | + var source = testCase.before(); |
| 58 | + var lines = testCase.changedLines(); |
| 59 | + var ranges = lines.lowerEndpoint() + ":" + (lines.upperEndpoint() - 1); |
| 60 | + List<Replacement> replacements = MAPPER.readValue( |
| 61 | + commandLine(source, "--character-ranges", ranges, "--output-replacements", "-"), |
| 62 | + new TypeReference<>() {}); |
| 63 | + |
| 64 | + assertThat(commandLine(source, "--character-ranges", ranges, "-")) |
| 65 | + .as(() -> folder + "/" + path + ", characters " + lines) |
| 66 | + .isEqualTo(Utils.applyReplacements(source, replacements)); |
| 67 | + })); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return tests; |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void linesLeaveTheImportsAndTheStringsElsewhere() throws Exception { |
| 76 | + String input = """ |
| 77 | + import java.util.List; |
| 78 | + import java.io.File; |
| 79 | +
|
| 80 | + class S { |
| 81 | + String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed."; |
| 82 | +
|
| 83 | + String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna."; |
| 84 | + } |
| 85 | + """; |
| 86 | + |
| 87 | + assertThat(commandLine(input, "--lines", "7", "-")).isEqualTo(""" |
| 88 | + import java.util.List; |
| 89 | + import java.io.File; |
| 90 | +
|
| 91 | + class S { |
| 92 | + String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed."; |
| 93 | +
|
| 94 | + String changed = |
| 95 | + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et" |
| 96 | + + " dolore magna."; |
| 97 | + } |
| 98 | + """); |
| 99 | + } |
| 100 | + |
| 101 | + /** What {@code open-java-format} prints for the source on standard input. */ |
| 102 | + private static String commandLine(String source, String... args) throws UsageException { |
| 103 | + var out = new StringWriter(); |
| 104 | + var err = new StringWriter(); |
| 105 | + var main = new Main( |
| 106 | + new PrintWriter(out, true), |
| 107 | + new PrintWriter(err, true), |
| 108 | + new ByteArrayInputStream(source.getBytes(UTF_8))); |
| 109 | + |
| 110 | + assertThat(main.format(args)).as(err::toString).isZero(); |
| 111 | + |
| 112 | + return out.toString(); |
| 113 | + } |
| 114 | +} |
0 commit comments