Skip to content

Commit 5aaffa0

Browse files
cushongoogle-java-format Team
authored andcommitted
Add baseline test cases for comment fencing in google-java-format.
Add `FormatterTest` cases documenting the existing formatting of leading comments after opening delimiters in `assert` conditions with detail expressions, semicolons after `enum` constants, `for`-loop initializers, multi-resource `try` headers, multi-catch clauses, and explicit type argument lists. PiperOrigin-RevId: 1009526679
1 parent 441b262 commit 5aaffa0

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

‎core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,4 +1108,191 @@ void f(Map<String, String> map) {
11081108
""";
11091109
assertThat(new Formatter().formatSource(input)).isEqualTo(input);
11101110
}
1111+
1112+
@Test
1113+
public void fenceCommentsAssert() throws Exception {
1114+
String input =
1115+
"""
1116+
class T {
1117+
void f(int x) {
1118+
assert
1119+
// comment
1120+
x > 0 : "positive";
1121+
}
1122+
}
1123+
""";
1124+
String expected =
1125+
"""
1126+
class T {
1127+
void f(int x) {
1128+
assert
1129+
// comment
1130+
x > 0 : "positive";
1131+
}
1132+
}
1133+
""";
1134+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1135+
}
1136+
1137+
@Test
1138+
public void fenceCommentsEnumSemicolon() throws Exception {
1139+
String input =
1140+
"""
1141+
enum E1 {
1142+
A,
1143+
// comment
1144+
;
1145+
}
1146+
1147+
enum E2 {
1148+
A
1149+
// comment
1150+
;
1151+
}
1152+
""";
1153+
String expected =
1154+
"""
1155+
enum E1 {
1156+
A,
1157+
// comment
1158+
;
1159+
}
1160+
1161+
enum E2 {
1162+
A
1163+
// comment
1164+
;
1165+
}
1166+
""";
1167+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1168+
}
1169+
1170+
@Test
1171+
public void fenceCommentsForInitializer() throws Exception {
1172+
String input =
1173+
"""
1174+
class T {
1175+
void f(int n) {
1176+
for (
1177+
// comment
1178+
int i = 0; i < n; i++) {}
1179+
for (
1180+
/* comment */
1181+
int i = 0; i < n; i++) {}
1182+
}
1183+
}
1184+
""";
1185+
String expected =
1186+
"""
1187+
class T {
1188+
void f(int n) {
1189+
for (
1190+
// comment
1191+
int i = 0; i < n; i++) {}
1192+
for (
1193+
/* comment */
1194+
int i = 0; i < n; i++) {}
1195+
}
1196+
}
1197+
""";
1198+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1199+
}
1200+
1201+
@Test
1202+
public void fenceCommentsTryWithResources() throws Exception {
1203+
String input =
1204+
"""
1205+
class T {
1206+
void f(AutoCloseable a, AutoCloseable b) throws Exception {
1207+
try (
1208+
// comment
1209+
AutoCloseable r1 = a; AutoCloseable r2 = b) {}
1210+
}
1211+
}
1212+
""";
1213+
String expected =
1214+
"""
1215+
class T {
1216+
void f(AutoCloseable a, AutoCloseable b) throws Exception {
1217+
try (
1218+
// comment
1219+
AutoCloseable r1 = a;
1220+
AutoCloseable r2 = b) {}
1221+
}
1222+
}
1223+
""";
1224+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1225+
}
1226+
1227+
@Test
1228+
public void fenceCommentsMultiCatch() throws Exception {
1229+
String input =
1230+
"""
1231+
class T {
1232+
void f() {
1233+
try {
1234+
} catch (
1235+
// comment
1236+
IllegalArgumentException | IllegalStateException e) {
1237+
}
1238+
}
1239+
}
1240+
""";
1241+
String expected =
1242+
"""
1243+
class T {
1244+
void f() {
1245+
try {
1246+
} catch (
1247+
// comment
1248+
IllegalArgumentException | IllegalStateException e) {
1249+
}
1250+
}
1251+
}
1252+
""";
1253+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1254+
}
1255+
1256+
@Test
1257+
public void fenceCommentsTypeArguments() throws Exception {
1258+
String input =
1259+
"""
1260+
class T {
1261+
<U> T() {}
1262+
1263+
static <U> void g() {}
1264+
1265+
void f() {
1266+
new <
1267+
// comment
1268+
String>T();
1269+
Runnable r =
1270+
T
1271+
::<
1272+
// comment
1273+
String>g;
1274+
}
1275+
}
1276+
""";
1277+
String expected =
1278+
"""
1279+
class T {
1280+
<U> T() {}
1281+
1282+
static <U> void g() {}
1283+
1284+
void f() {
1285+
new <
1286+
// comment
1287+
String>T();
1288+
Runnable r =
1289+
T
1290+
::<
1291+
// comment
1292+
String>g;
1293+
}
1294+
}
1295+
""";
1296+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1297+
}
11111298
}

0 commit comments

Comments
 (0)