|
21 | 21 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
22 | 22 |
|
23 | 23 | import com.google.common.base.Joiner; |
| 24 | +import com.google.common.collect.Range; |
24 | 25 | import com.google.common.io.CharStreams; |
25 | 26 | import java.io.ByteArrayInputStream; |
26 | 27 | import java.io.IOException; |
|
31 | 32 | import java.nio.charset.StandardCharsets; |
32 | 33 | import java.nio.file.Files; |
33 | 34 | import java.nio.file.Path; |
| 35 | +import java.util.List; |
34 | 36 | import org.junit.jupiter.api.Test; |
35 | 37 | import org.junit.jupiter.api.io.TempDir; |
36 | 38 | import org.junit.jupiter.api.parallel.Execution; |
@@ -443,4 +445,29 @@ void canParse_java9_private_interface_methods() { |
443 | 445 | + "}")) |
444 | 446 | .doesNotThrowAnyException(); |
445 | 447 | } |
| 448 | + |
| 449 | + @Test |
| 450 | + void doesntProduceFormattingChangesOnFormattedFiles() throws FormatterException { |
| 451 | + Formatter formatter = Formatter.create(); |
| 452 | + String simpleClass = "package com.palantir;\n" |
| 453 | + + "\n" |
| 454 | + + "public final class Formatted {\n" |
| 455 | + + " public static int count(int number) {\n" |
| 456 | + + " return number;\n" |
| 457 | + + " }\n" |
| 458 | + + "}"; |
| 459 | + // This is kinda overkill but the point of the test is to show that it |
| 460 | + // is for sure formatted and still producing a replacement. |
| 461 | + String formattedClass = formatter.formatSourceAndFixImports(simpleClass); |
| 462 | + String reformattedClass = formatter.formatSourceAndFixImports(formattedClass); |
| 463 | + |
| 464 | + // The formatSourceAndFixImports code path flows through getFormatReplacements and |
| 465 | + // this shows that despite the replacements happening they are superfluous |
| 466 | + assertThat(formattedClass).isEqualTo(reformattedClass); |
| 467 | + |
| 468 | + // Proof that the replacements are produced when fully formatted |
| 469 | + assertThat(formatter.getFormatReplacements( |
| 470 | + formattedClass, List.of(Range.closedOpen(0, formattedClass.length())))) |
| 471 | + .isEmpty(); |
| 472 | + } |
446 | 473 | } |
0 commit comments