Skip to content

Commit b2fb695

Browse files
committed
added a test that exemplifies replacements chaning formatted files
1 parent 8f96dc6 commit b2fb695

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222

2323
import com.google.common.base.Joiner;
24+
import com.google.common.collect.Range;
2425
import com.google.common.io.CharStreams;
2526
import java.io.ByteArrayInputStream;
2627
import java.io.IOException;
@@ -31,6 +32,7 @@
3132
import java.nio.charset.StandardCharsets;
3233
import java.nio.file.Files;
3334
import java.nio.file.Path;
35+
import java.util.List;
3436
import org.junit.jupiter.api.Test;
3537
import org.junit.jupiter.api.io.TempDir;
3638
import org.junit.jupiter.api.parallel.Execution;
@@ -443,4 +445,29 @@ void canParse_java9_private_interface_methods() {
443445
+ "}"))
444446
.doesNotThrowAnyException();
445447
}
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+
}
446473
}

0 commit comments

Comments
 (0)