Skip to content

Commit 6e28b71

Browse files
committed
Leave long strings outside the changed lines in the service tests
FormatterServicesTest and FormatDiffTest give each of the in-process, bootstrapping and native formatters a file with a long string on a changed line and another on a line that is not: the first is reflowed, the second stays.
1 parent 95b0540 commit 6e28b71

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ class LongString {
118118
""");
119119
}
120120

121+
@ParameterizedTest
122+
@MethodSource("getFormatters")
123+
void reflows_the_long_strings_of_changed_lines_only(FormatterService formatterService)
124+
throws IOException, InterruptedException {
125+
initRepo();
126+
Path longStrings = repo.resolve("LongStrings.java");
127+
Files.writeString(longStrings, """
128+
class LongStrings {
129+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
130+
}
131+
""");
132+
runCommandInRepo("git", "add", ".");
133+
runCommandInRepo("git", "commit", "-m", "A long string");
134+
135+
Files.writeString(longStrings, """
136+
class LongStrings {
137+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
138+
139+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
140+
}
141+
""");
142+
143+
FormatDiff.formatDiff(repo, formatterService);
144+
145+
assertThat(longStrings).hasContent("""
146+
class LongStrings {
147+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
148+
149+
String changed =
150+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
151+
+ " dolore magna.";
152+
}
153+
""");
154+
}
155+
121156
private void initRepo() throws IOException, InterruptedException {
122157
runCommandInRepo("git", "init");
123158
runCommandInRepo("git", "config", "user.name", "Test User");

‎open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ class S {
121121
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
122122
}
123123

124+
@ParameterizedTest
125+
@MethodSource("getFormattersAndTheInProcessOne")
126+
void reflows_the_long_strings_of_the_range_only(FormatterService formatterService) throws FormatterException {
127+
String input = """
128+
class S {
129+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
130+
131+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
132+
}
133+
""";
134+
String expectedOutput = """
135+
class S {
136+
String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
137+
138+
String changed =
139+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
140+
+ " dolore magna.";
141+
}
142+
""";
143+
int start = input.indexOf(" String changed");
144+
int end = input.indexOf('\n', start);
145+
146+
List<Replacement> replacements =
147+
formatterService.getFormatReplacements(input, List.of(Range.closedOpen(start, end)));
148+
149+
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
150+
}
151+
124152
private static Stream<FormatterService> getFormatters() {
125153
return Stream.of(
126154
new BootstrappingFormatterService(

0 commit comments

Comments
 (0)