|
14 | 14 |
|
15 | 15 | package com.palantir.javaformat.java; |
16 | 16 |
|
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
17 | 18 | import static java.util.Objects.requireNonNull; |
18 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 20 |
|
| 21 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 24 | +import com.fasterxml.jackson.datatype.guava.GuavaModule; |
| 25 | +import com.google.common.collect.Range; |
20 | 26 | import com.palantir.javaformat.java.JavaFormatterOptions.Style; |
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.io.PrintWriter; |
| 29 | +import java.io.StringWriter; |
21 | 30 | import java.nio.file.Files; |
22 | 31 | import java.nio.file.Path; |
23 | 32 | import java.util.ArrayList; |
|
26 | 35 |
|
27 | 36 | /** |
28 | 37 | * One test for each {@link CaseFile} in the given folders on the test classpath: formatting the before block gives the |
29 | | - * after block, and formatting that again changes nothing. Adding a case is adding a file. The cases are in a folder of |
30 | | - * the JDK they need, and each JDK has a class that runs the folders it can parse, such as |
| 38 | + * after block, and formatting that again changes nothing. For the lines that formatting changes, taken as a range, the |
| 39 | + * service that formats in this JVM, as {@code formatDiff} does without the native binary, gives the replacements that |
| 40 | + * the command line prints, which the bootstrapping and native services run. Adding a case is adding a file. The cases |
| 41 | + * are in a folder of the JDK they need, and each JDK has a class that runs the folders it can parse, such as |
31 | 42 | * {@link FormatterCasesJava21Test}. |
32 | 43 | */ |
33 | 44 | final class FormatterCasesTest { |
34 | 45 | private static final Formatter FORMATTER = Formatter.createFormatter( |
35 | 46 | JavaFormatterOptions.builder().style(Style.OJF).build()); |
| 47 | + private static final FormatterService SERVICE = new FormatterServiceImpl(); |
| 48 | + private static final ObjectMapper MAPPER = |
| 49 | + JsonMapper.builder().addModule(new GuavaModule()).build(); |
36 | 50 |
|
37 | 51 | /** The tests of the case files under {@code folders} on the test classpath. */ |
38 | 52 | static List<DynamicTest> casesIn(String... folders) throws Exception { |
@@ -64,10 +78,74 @@ static List<DynamicTest> casesIn(String... folders) throws Exception { |
64 | 78 | assertThat(twice) |
65 | 79 | .as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice)) |
66 | 80 | .isEqualTo(once); |
| 81 | + |
| 82 | + var lines = changedLines(testCase); |
| 83 | + List<Replacement> replacements = MAPPER.readValue( |
| 84 | + commandLine( |
| 85 | + testCase.before(), |
| 86 | + "--character-ranges", |
| 87 | + lines.lowerEndpoint() + ":" + (lines.upperEndpoint() - 1), |
| 88 | + "--output-replacements", |
| 89 | + "-"), |
| 90 | + new TypeReference<>() {}); |
| 91 | + |
| 92 | + assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines))) |
| 93 | + .as(() -> folder + "/" + path + ", the in-process service on characters " + lines) |
| 94 | + .isEqualTo(replacements); |
67 | 95 | })); |
68 | 96 | } |
69 | 97 | } |
70 | 98 |
|
71 | 99 | return tests; |
72 | 100 | } |
| 101 | + |
| 102 | + /** |
| 103 | + * The lines of the before block that formatting changes, from the first that differs from the after block to the |
| 104 | + * last, as one range of characters without the last line break; the whole block when no line differs. |
| 105 | + */ |
| 106 | + private static Range<Integer> changedLines(CaseFile testCase) { |
| 107 | + var before = testCase.before(); |
| 108 | + var beforeLines = List.of(before.split("(?<=\n)")); |
| 109 | + var afterLines = List.of(testCase.after().split("(?<=\n)")); |
| 110 | + var first = 0; |
| 111 | + while (first < beforeLines.size() |
| 112 | + && first < afterLines.size() |
| 113 | + && beforeLines.get(first).equals(afterLines.get(first))) { |
| 114 | + first++; |
| 115 | + } |
| 116 | + var last = 0; |
| 117 | + while (last < beforeLines.size() - first |
| 118 | + && last < afterLines.size() - first |
| 119 | + && beforeLines |
| 120 | + .get(beforeLines.size() - 1 - last) |
| 121 | + .equals(afterLines.get(afterLines.size() - 1 - last))) { |
| 122 | + last++; |
| 123 | + } |
| 124 | + var start = 0; |
| 125 | + for (var i = 0; i < first; i++) { |
| 126 | + start += beforeLines.get(i).length(); |
| 127 | + } |
| 128 | + var end = start; |
| 129 | + for (var i = first; i < beforeLines.size() - last; i++) { |
| 130 | + end += beforeLines.get(i).length(); |
| 131 | + } |
| 132 | + while (end > start && (before.charAt(end - 1) == '\n' || before.charAt(end - 1) == '\r')) { |
| 133 | + end--; |
| 134 | + } |
| 135 | + return end > start ? Range.closedOpen(start, end) : Range.closedOpen(0, before.length()); |
| 136 | + } |
| 137 | + |
| 138 | + /** What {@code open-java-format} prints for the source on standard input. */ |
| 139 | + static String commandLine(String source, String... args) throws UsageException { |
| 140 | + var out = new StringWriter(); |
| 141 | + var err = new StringWriter(); |
| 142 | + var main = new Main( |
| 143 | + new PrintWriter(out, true), |
| 144 | + new PrintWriter(err, true), |
| 145 | + new ByteArrayInputStream(source.getBytes(UTF_8))); |
| 146 | + |
| 147 | + assertThat(main.format(args)).as(err::toString).isZero(); |
| 148 | + |
| 149 | + return out.toString(); |
| 150 | + } |
73 | 151 | } |
0 commit comments