Skip to content

Commit 8955931

Browse files
committed
Initial Tests
1 parent a4b7233 commit 8955931

File tree

1 file changed

+202
-22
lines changed

1 file changed

+202
-22
lines changed

src/test/java/org/openrewrite/staticanalysis/NeedBracesTest.java

Lines changed: 202 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,31 @@ class Test {
6969
static void addToWhile() {
7070
while (true) ;
7171
}
72-
72+
7373
static void addToWhileWithBody() {
7474
while (true) return;
7575
}
76-
76+
7777
static void addToIf(int n) {
7878
if (n == 1) return;
7979
// foo
8080
}
81-
81+
8282
static void addToIfElse(int n) {
8383
if (n == 1) return;
8484
else return;
8585
}
86-
86+
8787
static void addToIfElseIfElse(int n) {
8888
if (n == 1) return;
8989
else if (n == 2) return;
9090
else return;
9191
}
92-
92+
9393
static void addToDoWhile(Object obj) {
9494
do obj.notify(); while (true);
9595
}
96-
96+
9797
static void addToIterativeFor(Object obj) {
9898
for (int i = 0; ; ) obj.notify();
9999
}
@@ -105,28 +105,28 @@ static void addToWhile() {
105105
while (true) {
106106
}
107107
}
108-
108+
109109
static void addToWhileWithBody() {
110110
while (true) {
111111
return;
112112
}
113113
}
114-
114+
115115
static void addToIf(int n) {
116116
if (n == 1) {
117117
return;
118118
}
119119
// foo
120120
}
121-
121+
122122
static void addToIfElse(int n) {
123123
if (n == 1) {
124124
return;
125125
} else {
126126
return;
127127
}
128128
}
129-
129+
130130
static void addToIfElseIfElse(int n) {
131131
if (n == 1) {
132132
return;
@@ -136,13 +136,13 @@ static void addToIfElseIfElse(int n) {
136136
return;
137137
}
138138
}
139-
139+
140140
static void addToDoWhile(Object obj) {
141141
do {
142142
obj.notify();
143143
} while (true);
144144
}
145-
145+
146146
static void addToIterativeFor(Object obj) {
147147
for (int i = 0; ; ) {
148148
obj.notify();
@@ -165,7 +165,7 @@ class Test {
165165
static void emptyWhile() {
166166
while (true) ;
167167
}
168-
168+
169169
static void emptyForIterative() {
170170
for (int i = 0; i < 10; i++) ;
171171
}
@@ -186,26 +186,26 @@ class Test {
186186
static void allowIf(int n) {
187187
if (n == 1) return;
188188
}
189-
189+
190190
static void allowIfElse(int n) {
191191
if (n == 1) return;
192192
else return;
193193
}
194-
194+
195195
static void allowIfElseIfElse(int n) {
196196
if (n == 1) return;
197197
else if (n == 2) return;
198198
else return;
199199
}
200-
200+
201201
static void allowWhileWithBody() {
202202
while (true) return;
203203
}
204-
204+
205205
static void allowDoWhileWithBody(Object obj) {
206206
do obj.notify(); while (true);
207207
}
208-
208+
209209
static void allowForIterativeWithBody(Object obj) {
210210
for (int i = 0; ; ) obj.notify();
211211
}
@@ -226,11 +226,11 @@ class Test {
226226
static void doNotAllowWhileWithEmptyBody() {
227227
while (true) ;
228228
}
229-
229+
230230
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
231231
do ; while (true);
232232
}
233-
233+
234234
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
235235
for (int i = 0; ; ) ;
236236
}
@@ -242,12 +242,12 @@ static void doNotAllowWhileWithEmptyBody() {
242242
while (true) {
243243
}
244244
}
245-
245+
246246
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
247247
do {
248248
} while (true);
249249
}
250-
250+
251251
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
252252
for (int i = 0; ; ) {
253253
}
@@ -311,6 +311,15 @@ static void method() {
311311
if (true) return; // comment 2
312312
return;
313313
}
314+
static void commentWhile(Object obj) {
315+
while (true); // while comment
316+
while (true) return; // while comment with body
317+
do obj.notify(); while (true); // do while comment
318+
}
319+
static void commentIterative(Object obj) {
320+
for (int i = 0; ; ); // iterative comment
321+
for (int i = 0; ; ) obj.notify(); // iterative with body comment
322+
}
314323
}
315324
""",
316325
"""
@@ -324,6 +333,177 @@ static void method() {
324333
}
325334
return;
326335
}
336+
static void commentWhile(Object obj) {
337+
while (true) { // while comment
338+
}
339+
while (true) {
340+
return; // while comment with body
341+
}
342+
do {
343+
obj.notify(); // do while comment
344+
} while (true);
345+
}
346+
static void commentIterative(Object obj) {
347+
for (int i = 0; ; ) { // iterative comment
348+
}
349+
for (int i = 0; ; ) {
350+
obj.notify(); // iterative with body comment
351+
}
352+
}
353+
}
354+
"""
355+
)
356+
);
357+
}
358+
359+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/315")
360+
@Test
361+
void commentsBeforeElseBlockWithNoBraces() {
362+
rewriteRun(
363+
//language=java
364+
java(
365+
"""
366+
class Test {
367+
static void method() {
368+
if (true) {
369+
return;
370+
}
371+
/*
372+
* comment should be in else block
373+
*/
374+
else return;
375+
if(true) {
376+
return;
377+
} // comment on if
378+
else return;
379+
}
380+
}
381+
""",
382+
"""
383+
class Test {
384+
static void method() {
385+
if (true) {
386+
return;
387+
} else {
388+
/*
389+
* comment should be in else block
390+
*/
391+
return;
392+
}
393+
if(true) {
394+
return;
395+
} // comment on if
396+
else {
397+
return;
398+
}
399+
}
400+
}
401+
"""
402+
)
403+
);
404+
}
405+
406+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/315")
407+
@Test
408+
void commentsBeforeElseBlockWithBraces() {
409+
rewriteRun(
410+
//language=java
411+
java(
412+
"""
413+
class Test {
414+
static void method() {
415+
if (true)
416+
return;
417+
// if comment
418+
else{
419+
return;
420+
}
421+
}
422+
}
423+
""",
424+
"""
425+
class Test {
426+
static void method() {
427+
if (true) {
428+
return;
429+
// if comment
430+
} else {
431+
return;
432+
}
433+
}
434+
}
435+
"""
436+
)
437+
);
438+
}
439+
440+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/315")
441+
@Test
442+
void trailingCommentsElseBlock() {
443+
rewriteRun(
444+
//language=java
445+
java(
446+
"""
447+
class Test {
448+
static void method() {
449+
if (true) return; // if comment
450+
else return; // else comment
451+
if (true) return; // if comment 2
452+
else if (true) return; // else if comment 2
453+
else if (true) return; // second else if comment 2
454+
else return; // else comment 2
455+
}
456+
static void methodTwo(){
457+
if (true) return; // if comment
458+
else return; // else comment
459+
return; // return comment
460+
}
461+
static void methodThreeNested(){
462+
if (true){
463+
if (true) return; // nested if comment
464+
else return; // nested else comment
465+
}
466+
else return; // else comment
467+
}
468+
}
469+
""",
470+
"""
471+
class Test {
472+
static void method() {
473+
if (true) {
474+
return; // if comment
475+
} else {
476+
return; // else comment
477+
}
478+
if (true) {
479+
return; // if comment 2
480+
} else if (true) {
481+
return; // else if comment 2
482+
} else if (true) {
483+
return; // second else if comment 2
484+
} else {
485+
return; // else comment 2
486+
}
487+
}
488+
static void methodTwo(){
489+
if (true) {
490+
return; // if comment
491+
} else {
492+
return; // else comment
493+
}
494+
return; // return comment
495+
}
496+
static void methodThreeNested(){
497+
if (true) {
498+
if (true) {
499+
return; // nested if comment
500+
} else {
501+
return; // nested else comment
502+
}
503+
} else {
504+
return; // else comment
505+
}
506+
}
327507
}
328508
"""
329509
)

0 commit comments

Comments
 (0)